Skip to content

BridgeJS: Move intrinsic declarations to JavaScriptKit under SPI #389

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
Jul 23, 2025
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
14 changes: 1 addition & 13 deletions Benchmarks/Sources/Generated/BridgeJS.ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

@_spi(JSObject_id) import JavaScriptKit

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "return_string")
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
@_extern(wasm, module: "bjs", name: "init_memory")
private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?)

@_extern(wasm, module: "bjs", name: "swift_js_retain")
private func _swift_js_retain(_ ptr: Int32) -> Int32
@_extern(wasm, module: "bjs", name: "swift_js_throw")
private func _swift_js_throw(_ id: Int32)
#endif
@_spi(BridgeJS) import JavaScriptKit

@_expose(wasm, "bjs_run")
@_cdecl("bjs_run")
Expand Down
22 changes: 2 additions & 20 deletions Benchmarks/Sources/Generated/BridgeJS.ImportTS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

@_spi(JSObject_id) import JavaScriptKit

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "make_jsstring")
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
#else
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32 {
fatalError("Only available on WebAssembly")
}
#endif

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "init_memory_with_result")
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
#else
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
fatalError("Only available on WebAssembly")
}
#endif
@_spi(BridgeJS) import JavaScriptKit

func benchmarkHelperNoop() -> Void {
#if arch(wasm32)
Expand Down Expand Up @@ -59,7 +41,7 @@ func benchmarkRunner(_ name: String, _ body: JSObject) -> Void {
#endif
var name = name
let nameId = name.withUTF8 { b in
_make_jsstring(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
_swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
}
bjs_benchmarkRunner(nameId, Int32(bitPattern: body.id))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,65 @@
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

@_spi(JSObject_id) import JavaScriptKit
@_spi(BridgeJS) import JavaScriptKit

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "return_string")
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
@_extern(wasm, module: "bjs", name: "init_memory")
private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?)
@_expose(wasm, "bjs_PlayBridgeJS_init")
@_cdecl("bjs_PlayBridgeJS_init")
public func _bjs_PlayBridgeJS_init() -> UnsafeMutableRawPointer {
#if arch(wasm32)
let ret = PlayBridgeJS()
return Unmanaged.passRetained(ret).toOpaque()
#else
fatalError("Only available on WebAssembly")
#endif
}

@_extern(wasm, module: "bjs", name: "swift_js_retain")
private func _swift_js_retain(_ ptr: Int32) -> Int32
@_extern(wasm, module: "bjs", name: "swift_js_throw")
private func _swift_js_throw(_ id: Int32)
#endif
@_expose(wasm, "bjs_PlayBridgeJS_update")
@_cdecl("bjs_PlayBridgeJS_update")
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLen: Int32, dtsSourceBytes: Int32, dtsSourceLen: Int32) -> UnsafeMutableRawPointer {
#if arch(wasm32)
do {
let swiftSource = String(unsafeUninitializedCapacity: Int(swiftSourceLen)) { b in
_swift_js_init_memory(swiftSourceBytes, b.baseAddress.unsafelyUnwrapped)
return Int(swiftSourceLen)
}
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
_swift_js_init_memory(dtsSourceBytes, b.baseAddress.unsafelyUnwrapped)
return Int(dtsSourceLen)
}
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
return Unmanaged.passRetained(ret).toOpaque()
} catch let error {
if let error = error.thrownValue.object {
withExtendedLifetime(error) {
_swift_js_throw(Int32(bitPattern: $0.id))
}
} else {
let jsError = JSError(message: String(describing: error))
withExtendedLifetime(jsError.jsObject) {
_swift_js_throw(Int32(bitPattern: $0.id))
}
}
return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped
}
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_PlayBridgeJS_deinit")
@_cdecl("bjs_PlayBridgeJS_deinit")
public func _bjs_PlayBridgeJS_deinit(pointer: UnsafeMutableRawPointer) {
Unmanaged<PlayBridgeJS>.fromOpaque(pointer).release()
}

@_expose(wasm, "bjs_PlayBridgeJSOutput_outputJs")
@_cdecl("bjs_PlayBridgeJSOutput_outputJs")
public func _bjs_PlayBridgeJSOutput_outputJs(_self: UnsafeMutableRawPointer) -> Void {
#if arch(wasm32)
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().outputJs()
return ret.withUTF8 { ptr in
_return_string(ptr.baseAddress, Int32(ptr.count))
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
}
#else
fatalError("Only available on WebAssembly")
Expand All @@ -37,7 +75,7 @@ public func _bjs_PlayBridgeJSOutput_outputDts(_self: UnsafeMutableRawPointer) ->
#if arch(wasm32)
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().outputDts()
return ret.withUTF8 { ptr in
_return_string(ptr.baseAddress, Int32(ptr.count))
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
}
#else
fatalError("Only available on WebAssembly")
Expand All @@ -50,7 +88,7 @@ public func _bjs_PlayBridgeJSOutput_importSwiftGlue(_self: UnsafeMutableRawPoint
#if arch(wasm32)
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().importSwiftGlue()
return ret.withUTF8 { ptr in
_return_string(ptr.baseAddress, Int32(ptr.count))
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
}
#else
fatalError("Only available on WebAssembly")
Expand All @@ -63,7 +101,7 @@ public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_self: UnsafeMutableRawPoint
#if arch(wasm32)
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().exportSwiftGlue()
return ret.withUTF8 { ptr in
_return_string(ptr.baseAddress, Int32(ptr.count))
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
}
#else
fatalError("Only available on WebAssembly")
Expand All @@ -74,54 +112,4 @@ public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_self: UnsafeMutableRawPoint
@_cdecl("bjs_PlayBridgeJSOutput_deinit")
public func _bjs_PlayBridgeJSOutput_deinit(pointer: UnsafeMutableRawPointer) {
Unmanaged<PlayBridgeJSOutput>.fromOpaque(pointer).release()
}

@_expose(wasm, "bjs_PlayBridgeJS_init")
@_cdecl("bjs_PlayBridgeJS_init")
public func _bjs_PlayBridgeJS_init() -> UnsafeMutableRawPointer {
#if arch(wasm32)
let ret = PlayBridgeJS()
return Unmanaged.passRetained(ret).toOpaque()
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_PlayBridgeJS_update")
@_cdecl("bjs_PlayBridgeJS_update")
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLen: Int32, dtsSourceBytes: Int32, dtsSourceLen: Int32) -> UnsafeMutableRawPointer {
#if arch(wasm32)
do {
let swiftSource = String(unsafeUninitializedCapacity: Int(swiftSourceLen)) { b in
_init_memory(swiftSourceBytes, b.baseAddress.unsafelyUnwrapped)
return Int(swiftSourceLen)
}
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
_init_memory(dtsSourceBytes, b.baseAddress.unsafelyUnwrapped)
return Int(dtsSourceLen)
}
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
return Unmanaged.passRetained(ret).toOpaque()
} catch let error {
if let error = error.thrownValue.object {
withExtendedLifetime(error) {
_swift_js_throw(Int32(bitPattern: $0.id))
}
} else {
let jsError = JSError(message: String(describing: error))
withExtendedLifetime(jsError.jsObject) {
_swift_js_throw(Int32(bitPattern: $0.id))
}
}
return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped
}
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_PlayBridgeJS_deinit")
@_cdecl("bjs_PlayBridgeJS_deinit")
public func _bjs_PlayBridgeJS_deinit(pointer: UnsafeMutableRawPointer) {
Unmanaged<PlayBridgeJS>.fromOpaque(pointer).release()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

@_spi(JSObject_id) import JavaScriptKit

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "make_jsstring")
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
#else
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32 {
fatalError("Only available on WebAssembly")
}
#endif

#if arch(wasm32)
@_extern(wasm, module: "bjs", name: "init_memory_with_result")
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
#else
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
fatalError("Only available on WebAssembly")
}
#endif
@_spi(BridgeJS) import JavaScriptKit

func createTS2Skeleton() -> TS2Skeleton {
#if arch(wasm32)
Expand Down Expand Up @@ -59,11 +41,11 @@ struct TS2Skeleton {
#endif
var ts = ts
let tsId = ts.withUTF8 { b in
_make_jsstring(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
_swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
}
let ret = bjs_TS2Skeleton_convert(Int32(bitPattern: self.this.id), tsId)
return String(unsafeUninitializedCapacity: Int(ret)) { b in
_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
_swift_js_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
return Int(ret)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
{
"classes" : [
{
"constructor" : {
"abiName" : "bjs_PlayBridgeJS_init",
"effects" : {
"isAsync" : false,
"isThrows" : false
},
"parameters" : [

]
},
"methods" : [
{
"abiName" : "bjs_PlayBridgeJS_update",
"effects" : {
"isAsync" : false,
"isThrows" : true
},
"name" : "update",
"parameters" : [
{
"label" : "swiftSource",
"name" : "swiftSource",
"type" : {
"string" : {

}
}
},
{
"label" : "dtsSource",
"name" : "dtsSource",
"type" : {
"string" : {

}
}
}
],
"returnType" : {
"swiftHeapObject" : {
"_0" : "PlayBridgeJSOutput"
}
}
}
],
"name" : "PlayBridgeJS"
},
{
"methods" : [
{
Expand Down Expand Up @@ -68,54 +116,6 @@
}
],
"name" : "PlayBridgeJSOutput"
},
{
"constructor" : {
"abiName" : "bjs_PlayBridgeJS_init",
"effects" : {
"isAsync" : false,
"isThrows" : false
},
"parameters" : [

]
},
"methods" : [
{
"abiName" : "bjs_PlayBridgeJS_update",
"effects" : {
"isAsync" : false,
"isThrows" : true
},
"name" : "update",
"parameters" : [
{
"label" : "swiftSource",
"name" : "swiftSource",
"type" : {
"string" : {

}
}
},
{
"label" : "dtsSource",
"name" : "dtsSource",
"type" : {
"string" : {

}
}
}
],
"returnType" : {
"swiftHeapObject" : {
"_0" : "PlayBridgeJSOutput"
}
}
}
],
"name" : "PlayBridgeJS"
}
],
"functions" : [
Expand Down
14 changes: 8 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ let package = Package(
? [
.unsafeFlags(["-fdeclspec"])
] : nil,
swiftSettings: shouldBuildForEmbedded
? [
.enableExperimentalFeature("Embedded"),
.enableExperimentalFeature("Extern"),
.unsafeFlags(["-Xfrontend", "-emit-empty-object-file"]),
] : nil
swiftSettings: [
.enableExperimentalFeature("Extern")
]
+ (shouldBuildForEmbedded
? [
.enableExperimentalFeature("Embedded"),
.unsafeFlags(["-Xfrontend", "-emit-empty-object-file"]),
] : [])
),
.target(name: "_CJavaScriptKit"),
.testTarget(
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy