Skip to content

[embedded] Add swift_initStaticObject to the embedded runtime #68820

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 1 commit into from
Sep 29, 2023
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
7 changes: 7 additions & 0 deletions stdlib/public/core/EmbeddedRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public func swift_deallocClassInstance(object: UnsafeMutablePointer<HeapObject>,
free(object)
}

@_silgen_name("swift_initStaticObject")
public func swift_initStaticObject(metadata: UnsafeMutablePointer<ClassMetadata>, object: UnsafeMutablePointer<HeapObject>) -> UnsafeMutablePointer<HeapObject> {
object.pointee.metadata = metadata
object.pointee.refcount = HeapObject.immortalRefCount
return object
}

@_silgen_name("swift_initStackObject")
public func swift_initStackObject(metadata: UnsafeMutablePointer<ClassMetadata>, object: UnsafeMutablePointer<HeapObject>) -> UnsafeMutablePointer<HeapObject> {
object.pointee.metadata = metadata
Expand Down
27 changes: 27 additions & 0 deletions test/embedded/static-object.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-swift-frontend -O -emit-irgen %s %S/Inputs/print.swift -module-name main -parse-as-library -enable-experimental-feature Embedded | %FileCheck %s --check-prefix CHECK-IR
// RUN: %target-run-simple-swift(-O %S/Inputs/print.swift -enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: VENDOR=apple
// REQUIRES: OS=macosx

public func stringArray() -> [StaticString] {
return ["a", "b", "c", "d"]
}
// CHECK-IR: define {{.*}}@"$s4main11stringArraySays12StaticStringVGyF"
// CHECK-IR-NEXT: entry:
// CHECK-IR-NEXT: call {{.*}}@swift_initStaticObject

@main
struct Main {
static func main() {
for c in stringArray() {
print(c)
// CHECK: a
// CHECK: b
// CHECK: c
// CHECK: d
}
}
}