Skip to content

Commit 1e4389d

Browse files
Runtime tests: cover imported JS closures
Add runtime import fixtures and tests for passing Swift closures into JS and receiving JS functions back as Swift closures.
1 parent f822943 commit 1e4389d

File tree

6 files changed

+404
-2
lines changed

6 files changed

+404
-2
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
@JSFunction func applyInt(_ value: Int, _ transform: (Int) -> Int) throws(JSException) -> Int
22

33
@JSFunction func makeAdder(_ base: Int) throws(JSException) -> (Int) -> Int
4-

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,52 @@ public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_
537537
#endif
538538
}
539539

540+
#if arch(wasm32)
541+
@_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y")
542+
fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y(_ callback: Int32, _ param0: Int32) -> Void
543+
#else
544+
fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y(_ callback: Int32, _ param0: Int32) -> Void {
545+
fatalError("Only available on WebAssembly")
546+
}
547+
#endif
548+
549+
private final class _BJS_ClosureBox_20BridgeJSRuntimeTestsSi_y: _BridgedSwiftClosureBox {
550+
let closure: (Int) -> Void
551+
init(_ closure: @escaping (Int) -> Void) {
552+
self.closure = closure
553+
}
554+
}
555+
556+
private enum _BJS_Closure_20BridgeJSRuntimeTestsSi_y {
557+
static func bridgeJSLower(_ closure: @escaping (Int) -> Void) -> UnsafeMutableRawPointer {
558+
let box = _BJS_ClosureBox_20BridgeJSRuntimeTestsSi_y(closure)
559+
return Unmanaged.passRetained(box).toOpaque()
560+
}
561+
static func bridgeJSLift(_ callbackId: Int32) -> (Int) -> Void {
562+
let callback = JSObject.bridgeJSLiftParameter(callbackId)
563+
return { [callback] param0 in
564+
#if arch(wasm32)
565+
let callbackValue = callback.bridgeJSLowerParameter()
566+
let param0Value = param0.bridgeJSLowerParameter()
567+
invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y(callbackValue, param0Value)
568+
#else
569+
fatalError("Only available on WebAssembly")
570+
#endif
571+
}
572+
}
573+
}
574+
575+
@_expose(wasm, "invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y")
576+
@_cdecl("invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y")
577+
public func _invoke_swift_closure_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSi_y(_ boxPtr: UnsafeMutableRawPointer, _ param0: Int32) -> Void {
578+
#if arch(wasm32)
579+
let box = Unmanaged<_BJS_ClosureBox_20BridgeJSRuntimeTestsSi_y>.fromOpaque(boxPtr).takeUnretainedValue()
580+
box.closure(Int.bridgeJSLiftParameter(param0))
581+
#else
582+
fatalError("Only available on WebAssembly")
583+
#endif
584+
}
585+
540586
#if arch(wasm32)
541587
@_extern(wasm, module: "bjs", name: "invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS")
542588
fileprivate func invoke_js_callback_BridgeJSRuntimeTests_20BridgeJSRuntimeTestsSq5ThemeO_SS(_ callback: Int32, _ param0IsSome: Int32, _ param0Value: Int32) -> Int32
@@ -6431,4 +6477,97 @@ func _$JsGreeter_changeName(_ self: JSObject, _ name: String) throws(JSException
64316477
if let error = _swift_js_take_exception() {
64326478
throw error
64336479
}
6480+
}
6481+
6482+
#if arch(wasm32)
6483+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsApplyInt")
6484+
fileprivate func bjs_jsApplyInt(_ value: Int32, _ transform: UnsafeMutableRawPointer) -> Int32
6485+
#else
6486+
fileprivate func bjs_jsApplyInt(_ value: Int32, _ transform: UnsafeMutableRawPointer) -> Int32 {
6487+
fatalError("Only available on WebAssembly")
6488+
}
6489+
#endif
6490+
6491+
func _$jsApplyInt(_ value: Int, _ transform: @escaping (Int) -> Int) throws(JSException) -> Int {
6492+
let valueValue = value.bridgeJSLowerParameter()
6493+
let transformPointer = _BJS_Closure_20BridgeJSRuntimeTestsSi_Si.bridgeJSLower(transform)
6494+
let ret = bjs_jsApplyInt(valueValue, transformPointer)
6495+
if let error = _swift_js_take_exception() {
6496+
throw error
6497+
}
6498+
return Int.bridgeJSLiftReturn(ret)
6499+
}
6500+
6501+
#if arch(wasm32)
6502+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsMakeAdder")
6503+
fileprivate func bjs_jsMakeAdder(_ base: Int32) -> Int32
6504+
#else
6505+
fileprivate func bjs_jsMakeAdder(_ base: Int32) -> Int32 {
6506+
fatalError("Only available on WebAssembly")
6507+
}
6508+
#endif
6509+
6510+
func _$jsMakeAdder(_ base: Int) throws(JSException) -> (Int) -> Int {
6511+
let baseValue = base.bridgeJSLowerParameter()
6512+
let ret = bjs_jsMakeAdder(baseValue)
6513+
if let error = _swift_js_take_exception() {
6514+
throw error
6515+
}
6516+
return _BJS_Closure_20BridgeJSRuntimeTestsSi_Si.bridgeJSLift(ret)
6517+
}
6518+
6519+
#if arch(wasm32)
6520+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsMapString")
6521+
fileprivate func bjs_jsMapString(_ value: Int32, _ transform: UnsafeMutableRawPointer) -> Int32
6522+
#else
6523+
fileprivate func bjs_jsMapString(_ value: Int32, _ transform: UnsafeMutableRawPointer) -> Int32 {
6524+
fatalError("Only available on WebAssembly")
6525+
}
6526+
#endif
6527+
6528+
func _$jsMapString(_ value: String, _ transform: @escaping (String) -> String) throws(JSException) -> String {
6529+
let valueValue = value.bridgeJSLowerParameter()
6530+
let transformPointer = _BJS_Closure_20BridgeJSRuntimeTestsSS_SS.bridgeJSLower(transform)
6531+
let ret = bjs_jsMapString(valueValue, transformPointer)
6532+
if let error = _swift_js_take_exception() {
6533+
throw error
6534+
}
6535+
return String.bridgeJSLiftReturn(ret)
6536+
}
6537+
6538+
#if arch(wasm32)
6539+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsMakePrefixer")
6540+
fileprivate func bjs_jsMakePrefixer(_ prefix: Int32) -> Int32
6541+
#else
6542+
fileprivate func bjs_jsMakePrefixer(_ prefix: Int32) -> Int32 {
6543+
fatalError("Only available on WebAssembly")
6544+
}
6545+
#endif
6546+
6547+
func _$jsMakePrefixer(_ prefix: String) throws(JSException) -> (String) -> String {
6548+
let prefixValue = prefix.bridgeJSLowerParameter()
6549+
let ret = bjs_jsMakePrefixer(prefixValue)
6550+
if let error = _swift_js_take_exception() {
6551+
throw error
6552+
}
6553+
return _BJS_Closure_20BridgeJSRuntimeTestsSS_SS.bridgeJSLift(ret)
6554+
}
6555+
6556+
#if arch(wasm32)
6557+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsCallTwice")
6558+
fileprivate func bjs_jsCallTwice(_ value: Int32, _ callback: UnsafeMutableRawPointer) -> Int32
6559+
#else
6560+
fileprivate func bjs_jsCallTwice(_ value: Int32, _ callback: UnsafeMutableRawPointer) -> Int32 {
6561+
fatalError("Only available on WebAssembly")
6562+
}
6563+
#endif
6564+
6565+
func _$jsCallTwice(_ value: Int, _ callback: @escaping (Int) -> Void) throws(JSException) -> Int {
6566+
let valueValue = value.bridgeJSLowerParameter()
6567+
let callbackPointer = _BJS_Closure_20BridgeJSRuntimeTestsSi_y.bridgeJSLower(callback)
6568+
let ret = bjs_jsCallTwice(valueValue, callbackPointer)
6569+
if let error = _swift_js_take_exception() {
6570+
throw error
6571+
}
6572+
return Int.bridgeJSLiftReturn(ret)
64346573
}

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9174,6 +9174,212 @@
91749174
],
91759175
"types" : [
91769176

9177+
]
9178+
},
9179+
{
9180+
"functions" : [
9181+
{
9182+
"name" : "jsApplyInt",
9183+
"parameters" : [
9184+
{
9185+
"name" : "value",
9186+
"type" : {
9187+
"int" : {
9188+
9189+
}
9190+
}
9191+
},
9192+
{
9193+
"name" : "transform",
9194+
"type" : {
9195+
"closure" : {
9196+
"_0" : {
9197+
"isAsync" : false,
9198+
"isThrows" : false,
9199+
"mangleName" : "20BridgeJSRuntimeTestsSi_Si",
9200+
"moduleName" : "BridgeJSRuntimeTests",
9201+
"parameters" : [
9202+
{
9203+
"int" : {
9204+
9205+
}
9206+
}
9207+
],
9208+
"returnType" : {
9209+
"int" : {
9210+
9211+
}
9212+
}
9213+
}
9214+
}
9215+
}
9216+
}
9217+
],
9218+
"returnType" : {
9219+
"int" : {
9220+
9221+
}
9222+
}
9223+
},
9224+
{
9225+
"name" : "jsMakeAdder",
9226+
"parameters" : [
9227+
{
9228+
"name" : "base",
9229+
"type" : {
9230+
"int" : {
9231+
9232+
}
9233+
}
9234+
}
9235+
],
9236+
"returnType" : {
9237+
"closure" : {
9238+
"_0" : {
9239+
"isAsync" : false,
9240+
"isThrows" : false,
9241+
"mangleName" : "20BridgeJSRuntimeTestsSi_Si",
9242+
"moduleName" : "BridgeJSRuntimeTests",
9243+
"parameters" : [
9244+
{
9245+
"int" : {
9246+
9247+
}
9248+
}
9249+
],
9250+
"returnType" : {
9251+
"int" : {
9252+
9253+
}
9254+
}
9255+
}
9256+
}
9257+
}
9258+
},
9259+
{
9260+
"name" : "jsMapString",
9261+
"parameters" : [
9262+
{
9263+
"name" : "value",
9264+
"type" : {
9265+
"string" : {
9266+
9267+
}
9268+
}
9269+
},
9270+
{
9271+
"name" : "transform",
9272+
"type" : {
9273+
"closure" : {
9274+
"_0" : {
9275+
"isAsync" : false,
9276+
"isThrows" : false,
9277+
"mangleName" : "20BridgeJSRuntimeTestsSS_SS",
9278+
"moduleName" : "BridgeJSRuntimeTests",
9279+
"parameters" : [
9280+
{
9281+
"string" : {
9282+
9283+
}
9284+
}
9285+
],
9286+
"returnType" : {
9287+
"string" : {
9288+
9289+
}
9290+
}
9291+
}
9292+
}
9293+
}
9294+
}
9295+
],
9296+
"returnType" : {
9297+
"string" : {
9298+
9299+
}
9300+
}
9301+
},
9302+
{
9303+
"name" : "jsMakePrefixer",
9304+
"parameters" : [
9305+
{
9306+
"name" : "prefix",
9307+
"type" : {
9308+
"string" : {
9309+
9310+
}
9311+
}
9312+
}
9313+
],
9314+
"returnType" : {
9315+
"closure" : {
9316+
"_0" : {
9317+
"isAsync" : false,
9318+
"isThrows" : false,
9319+
"mangleName" : "20BridgeJSRuntimeTestsSS_SS",
9320+
"moduleName" : "BridgeJSRuntimeTests",
9321+
"parameters" : [
9322+
{
9323+
"string" : {
9324+
9325+
}
9326+
}
9327+
],
9328+
"returnType" : {
9329+
"string" : {
9330+
9331+
}
9332+
}
9333+
}
9334+
}
9335+
}
9336+
},
9337+
{
9338+
"name" : "jsCallTwice",
9339+
"parameters" : [
9340+
{
9341+
"name" : "value",
9342+
"type" : {
9343+
"int" : {
9344+
9345+
}
9346+
}
9347+
},
9348+
{
9349+
"name" : "callback",
9350+
"type" : {
9351+
"closure" : {
9352+
"_0" : {
9353+
"isAsync" : false,
9354+
"isThrows" : false,
9355+
"mangleName" : "20BridgeJSRuntimeTestsSi_y",
9356+
"moduleName" : "BridgeJSRuntimeTests",
9357+
"parameters" : [
9358+
{
9359+
"int" : {
9360+
9361+
}
9362+
}
9363+
],
9364+
"returnType" : {
9365+
"void" : {
9366+
9367+
}
9368+
}
9369+
}
9370+
}
9371+
}
9372+
}
9373+
],
9374+
"returnType" : {
9375+
"int" : {
9376+
9377+
}
9378+
}
9379+
}
9380+
],
9381+
"types" : [
9382+
91779383
]
91789384
}
91799385
]

0 commit comments

Comments
 (0)