Skip to content

BridgeJS: Add runtime tests for importing TypeScript functions #368

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
Jun 12, 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
2 changes: 1 addition & 1 deletion Plugins/PackageToJS/Templates/instantiate.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { /* #if USE_SHARED_MEMORY */SwiftRuntimeThreadChannel, /* #endif */SwiftRuntime } from "./runtime.js";

/* #if HAS_BRIDGE */
// @ts-ignore
export type { Imports, Exports } from "./bridge.js";
import type { Imports, Exports } from "./bridge.js";
/* #else */
export type Imports = {}
export type Exports = {}
Expand Down
50 changes: 50 additions & 0 deletions Tests/BridgeJSRuntimeTests/Generated/ImportTS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
// DO NOT EDIT.
//
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

@_spi(JSObject_id) import JavaScriptKit

@_extern(wasm, module: "bjs", name: "make_jsstring")
private func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32

@_extern(wasm, module: "bjs", name: "init_memory_with_result")
private func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)

@_extern(wasm, module: "bjs", name: "free_jsobject")
private func _free_jsobject(_ ptr: Int32) -> Void

func jsRoundTripVoid() -> Void {
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripVoid")
func bjs_jsRoundTripVoid() -> Void
bjs_jsRoundTripVoid()
}

func jsRoundTripNumber(_ v: Double) -> Double {
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripNumber")
func bjs_jsRoundTripNumber(_ v: Float64) -> Float64
let ret = bjs_jsRoundTripNumber(v)
return Double(ret)
}

func jsRoundTripBool(_ v: Bool) -> Bool {
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripBool")
func bjs_jsRoundTripBool(_ v: Int32) -> Int32
let ret = bjs_jsRoundTripBool(Int32(v ? 1 : 0))
return ret == 1
}

func jsRoundTripString(_ v: String) -> String {
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripString")
func bjs_jsRoundTripString(_ v: Int32) -> Int32
var v = v
let vId = v.withUTF8 { b in
_make_jsstring(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
}
let ret = bjs_jsRoundTripString(vId)
return String(unsafeUninitializedCapacity: Int(ret)) { b in
_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
return Int(ret)
}
}
77 changes: 77 additions & 0 deletions Tests/BridgeJSRuntimeTests/Generated/JavaScript/ImportTS.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"children" : [
{
"functions" : [
{
"name" : "jsRoundTripVoid",
"parameters" : [

],
"returnType" : {
"void" : {

}
}
},
{
"name" : "jsRoundTripNumber",
"parameters" : [
{
"name" : "v",
"type" : {
"double" : {

}
}
}
],
"returnType" : {
"double" : {

}
}
},
{
"name" : "jsRoundTripBool",
"parameters" : [
{
"name" : "v",
"type" : {
"bool" : {

}
}
}
],
"returnType" : {
"bool" : {

}
}
},
{
"name" : "jsRoundTripString",
"parameters" : [
{
"name" : "v",
"type" : {
"string" : {

}
}
}
],
"returnType" : {
"string" : {

}
}
}
],
"types" : [

]
}
],
"moduleName" : "BridgeJSRuntimeTests"
}
37 changes: 37 additions & 0 deletions Tests/BridgeJSRuntimeTests/ImportAPITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import XCTest
import JavaScriptKit

class ImportAPITests: XCTestCase {
func testRoundTripVoid() {
jsRoundTripVoid()
}

func testRoundTripNumber() {
for v in [
0, 1, -1,
Double(Int32.max), Double(Int32.min),
Double(Int64.max), Double(Int64.min),
Double(UInt32.max), Double(UInt32.min),
Double(UInt64.max), Double(UInt64.min),
Double.greatestFiniteMagnitude, Double.leastNonzeroMagnitude,
Double.infinity,
Double.pi,
] {
XCTAssertEqual(jsRoundTripNumber(v), v)
}

XCTAssert(jsRoundTripNumber(Double.nan).isNaN)
}

func testRoundTripBool() {
for v in [true, false] {
XCTAssertEqual(jsRoundTripBool(v), v)
}
}

func testRoundTripString() {
for v in ["", "Hello, world!", "🧑‍🧑‍🧒"] {
XCTAssertEqual(jsRoundTripString(v), v)
}
}
}
4 changes: 4 additions & 0 deletions Tests/BridgeJSRuntimeTests/bridge.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function jsRoundTripVoid(): void
export function jsRoundTripNumber(v: number): number
export function jsRoundTripBool(v: boolean): boolean
export function jsRoundTripString(v: string): string
24 changes: 19 additions & 5 deletions Tests/prelude.mjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
/** @type {import('./../.build/plugins/PackageToJS/outputs/PackageTests/test.d.ts').Prelude["setupOptions"]} */
/** @type {import('../.build/plugins/PackageToJS/outputs/PackageTests/test.d.ts').SetupOptions} */
export function setupOptions(options, context) {
Error.stackTraceLimit = 100;
setupTestGlobals(globalThis);
return {
...options,
imports: {
"jsRoundTripVoid": () => {
return;
},
"jsRoundTripNumber": (v) => {
return v;
},
"jsRoundTripBool": (v) => {
return v;
},
"jsRoundTripString": (v) => {
return v;
},
},
addToCoreImports(importObject, importsContext) {
const { getInstance, getExports } = importsContext;
options.addToCoreImports?.(importObject, importsContext);
importObject["JavaScriptEventLoopTestSupportTests"] = {
"isMainThread": () => context.isMainThread,
}
importObject["BridgeJSRuntimeTests"] = {
"runJsWorks": () => {
return BridgeJSRuntimeTests_runJsWorks(getInstance(), getExports());
},
const bridgeJSRuntimeTests = importObject["BridgeJSRuntimeTests"] || {};
bridgeJSRuntimeTests["runJsWorks"] = () => {
return BridgeJSRuntimeTests_runJsWorks(getInstance(), getExports());
}
importObject["BridgeJSRuntimeTests"] = bridgeJSRuntimeTests;
}
}
}
Expand Down