Skip to content

Runtime: Implement wrapper function to call metadata access functions #30024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/SwiftShims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(sources
KeyPath.h
LibcOverlayShims.h
LibcShims.h
Metadata.h
Random.h
RefCount.h
RuntimeShims.h
Expand Down
46 changes: 46 additions & 0 deletions stdlib/public/SwiftShims/Metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===--- Metadata.h -------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_STDLIB_SHIMS_METADATA_H
#define SWIFT_STDLIB_SHIMS_METADATA_H

#include "SwiftStddef.h"
#include "Visibility.h"

#ifndef __swift__

#include "swift/ABI/Metadata.h"

#else // ifndef __swift__

typedef struct {
const void *type;
__swift_size_t state;
} MetadataResponse;

#endif

#ifdef __cplusplus
namespace swift { extern "C" {
#endif

// Shims to call a metadata accessor in Swift.
SWIFT_RUNTIME_STDLIB_API
MetadataResponse _swift_metadataAccessorCall(void *accessor,
__swift_size_t request,
const void * const *args,
__swift_size_t size);
#ifdef __cplusplus
}} // extern "C", namespace swift
#endif

#endif
1 change: 1 addition & 0 deletions stdlib/public/SwiftShims/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module SwiftShims {
header "HeapObject.h"
header "KeyPath.h"
header "LibcShims.h"
header "Metadata.h"
header "Random.h"
header "RefCount.h"
header "RuntimeShims.h"
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(swift_stubs_sources
CommandLine.cpp
GlobalObjects.cpp
LibcShims.cpp
Metadata.cpp
Random.cpp
Stubs.cpp
ThreadLocalStorage.cpp
Expand Down
27 changes: 27 additions & 0 deletions stdlib/public/stubs/Metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===--- Metadata.cpp -----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "../SwiftShims/Metadata.h"
#include "swift/ABI/Metadata.h"

// Shims to call a metadata accessor in Swift.
SWIFT_RUNTIME_STDLIB_API
swift::MetadataResponse
swift::_swift_metadataAccessorCall(void *accessor,
__swift_size_t request,
const void * const *args,
__swift_size_t size) {
using Fn = MetadataResponse (...);
auto func = reinterpret_cast<Fn *>(accessor);
auto array = llvm::ArrayRef<const void *>(args, size);
return MetadataAccessFunction(func)(MetadataRequest(request), array);
}
54 changes: 54 additions & 0 deletions test/Interpreter/metadata_access.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

import SwiftShims

struct MetadataAccessFunction {
let ptr: UnsafeMutableRawPointer

func callAsFunction(request: Int, args: [Any.Type]) -> MetadataResponse {
args.withUnsafeBufferPointer {
$0.baseAddress!.withMemoryRebound(
to: UnsafeRawPointer?.self,
capacity: args.count
) {
_swift_metadataAccessorCall(ptr, request, $0, args.count)
}
}
}
}

func callStructAccessor(
for type: Any.Type,
with generics: Any.Type...
) -> MetadataResponse {
let metadata = unsafeBitCast(type, to: UnsafeRawPointer.self)
let descriptor = metadata.advanced(by: MemoryLayout<Int>.size)
.load(as: UnsafeMutableRawPointer.self)
let accessorLoc = descriptor.advanced(by: MemoryLayout<Int32>.size * 3)
let accessor = accessorLoc.advanced(by: Int(accessorLoc.load(as: Int32.self)))

let accessFn = MetadataAccessFunction(ptr: accessor)
return accessFn(request: 0, args: generics)
}

let int = callStructAccessor(for: Int.self)
// CHECK: Int
print(unsafeBitCast(int.type!, to: Any.Type.self))
// CHECK: 0
print(int.state)

let doubleArray = callStructAccessor(for: [Int].self, with: Double.self)
// CHECK: Array<Double>
print(unsafeBitCast(doubleArray.type!, to: Any.Type.self))
// CHECK: 0
print(doubleArray.state)

let dictOfIntAndDoubleArray = callStructAccessor(
for: [String: [Int]].self,
with: Int.self, [Double].self
)
// CHECK: Dictionary<Int, Array<Double>>
print(unsafeBitCast(dictOfIntAndDoubleArray.type!, to: Any.Type.self))
// CHECK: 0
print(dictOfIntAndDoubleArray.state)