Skip to content

Commit 7eb770e

Browse files
Merge pull request #368 from swiftwasm/yt/add-import-ts-runtime-tests
BridgeJS: Add runtime tests for importing TypeScript functions
2 parents 8563ff7 + a69aa7e commit 7eb770e

File tree

6 files changed

+188
-6
lines changed

6 files changed

+188
-6
lines changed

Plugins/PackageToJS/Templates/instantiate.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { /* #if USE_SHARED_MEMORY */SwiftRuntimeThreadChannel, /* #endif */SwiftRuntime } from "./runtime.js";
22

33
/* #if HAS_BRIDGE */
4-
// @ts-ignore
54
export type { Imports, Exports } from "./bridge.js";
5+
import type { Imports, Exports } from "./bridge.js";
66
/* #else */
77
export type Imports = {}
88
export type Exports = {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
@_spi(JSObject_id) import JavaScriptKit
8+
9+
@_extern(wasm, module: "bjs", name: "make_jsstring")
10+
private func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
11+
12+
@_extern(wasm, module: "bjs", name: "init_memory_with_result")
13+
private func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
14+
15+
@_extern(wasm, module: "bjs", name: "free_jsobject")
16+
private func _free_jsobject(_ ptr: Int32) -> Void
17+
18+
func jsRoundTripVoid() -> Void {
19+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripVoid")
20+
func bjs_jsRoundTripVoid() -> Void
21+
bjs_jsRoundTripVoid()
22+
}
23+
24+
func jsRoundTripNumber(_ v: Double) -> Double {
25+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripNumber")
26+
func bjs_jsRoundTripNumber(_ v: Float64) -> Float64
27+
let ret = bjs_jsRoundTripNumber(v)
28+
return Double(ret)
29+
}
30+
31+
func jsRoundTripBool(_ v: Bool) -> Bool {
32+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripBool")
33+
func bjs_jsRoundTripBool(_ v: Int32) -> Int32
34+
let ret = bjs_jsRoundTripBool(Int32(v ? 1 : 0))
35+
return ret == 1
36+
}
37+
38+
func jsRoundTripString(_ v: String) -> String {
39+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripString")
40+
func bjs_jsRoundTripString(_ v: Int32) -> Int32
41+
var v = v
42+
let vId = v.withUTF8 { b in
43+
_make_jsstring(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
44+
}
45+
let ret = bjs_jsRoundTripString(vId)
46+
return String(unsafeUninitializedCapacity: Int(ret)) { b in
47+
_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
48+
return Int(ret)
49+
}
50+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"children" : [
3+
{
4+
"functions" : [
5+
{
6+
"name" : "jsRoundTripVoid",
7+
"parameters" : [
8+
9+
],
10+
"returnType" : {
11+
"void" : {
12+
13+
}
14+
}
15+
},
16+
{
17+
"name" : "jsRoundTripNumber",
18+
"parameters" : [
19+
{
20+
"name" : "v",
21+
"type" : {
22+
"double" : {
23+
24+
}
25+
}
26+
}
27+
],
28+
"returnType" : {
29+
"double" : {
30+
31+
}
32+
}
33+
},
34+
{
35+
"name" : "jsRoundTripBool",
36+
"parameters" : [
37+
{
38+
"name" : "v",
39+
"type" : {
40+
"bool" : {
41+
42+
}
43+
}
44+
}
45+
],
46+
"returnType" : {
47+
"bool" : {
48+
49+
}
50+
}
51+
},
52+
{
53+
"name" : "jsRoundTripString",
54+
"parameters" : [
55+
{
56+
"name" : "v",
57+
"type" : {
58+
"string" : {
59+
60+
}
61+
}
62+
}
63+
],
64+
"returnType" : {
65+
"string" : {
66+
67+
}
68+
}
69+
}
70+
],
71+
"types" : [
72+
73+
]
74+
}
75+
],
76+
"moduleName" : "BridgeJSRuntimeTests"
77+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import XCTest
2+
import JavaScriptKit
3+
4+
class ImportAPITests: XCTestCase {
5+
func testRoundTripVoid() {
6+
jsRoundTripVoid()
7+
}
8+
9+
func testRoundTripNumber() {
10+
for v in [
11+
0, 1, -1,
12+
Double(Int32.max), Double(Int32.min),
13+
Double(Int64.max), Double(Int64.min),
14+
Double(UInt32.max), Double(UInt32.min),
15+
Double(UInt64.max), Double(UInt64.min),
16+
Double.greatestFiniteMagnitude, Double.leastNonzeroMagnitude,
17+
Double.infinity,
18+
Double.pi,
19+
] {
20+
XCTAssertEqual(jsRoundTripNumber(v), v)
21+
}
22+
23+
XCTAssert(jsRoundTripNumber(Double.nan).isNaN)
24+
}
25+
26+
func testRoundTripBool() {
27+
for v in [true, false] {
28+
XCTAssertEqual(jsRoundTripBool(v), v)
29+
}
30+
}
31+
32+
func testRoundTripString() {
33+
for v in ["", "Hello, world!", "🧑‍🧑‍🧒"] {
34+
XCTAssertEqual(jsRoundTripString(v), v)
35+
}
36+
}
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function jsRoundTripVoid(): void
2+
export function jsRoundTripNumber(v: number): number
3+
export function jsRoundTripBool(v: boolean): boolean
4+
export function jsRoundTripString(v: string): string

Tests/prelude.mjs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
1-
/** @type {import('./../.build/plugins/PackageToJS/outputs/PackageTests/test.d.ts').Prelude["setupOptions"]} */
1+
/** @type {import('../.build/plugins/PackageToJS/outputs/PackageTests/test.d.ts').SetupOptions} */
22
export function setupOptions(options, context) {
33
Error.stackTraceLimit = 100;
44
setupTestGlobals(globalThis);
55
return {
66
...options,
7+
imports: {
8+
"jsRoundTripVoid": () => {
9+
return;
10+
},
11+
"jsRoundTripNumber": (v) => {
12+
return v;
13+
},
14+
"jsRoundTripBool": (v) => {
15+
return v;
16+
},
17+
"jsRoundTripString": (v) => {
18+
return v;
19+
},
20+
},
721
addToCoreImports(importObject, importsContext) {
822
const { getInstance, getExports } = importsContext;
923
options.addToCoreImports?.(importObject, importsContext);
1024
importObject["JavaScriptEventLoopTestSupportTests"] = {
1125
"isMainThread": () => context.isMainThread,
1226
}
13-
importObject["BridgeJSRuntimeTests"] = {
14-
"runJsWorks": () => {
15-
return BridgeJSRuntimeTests_runJsWorks(getInstance(), getExports());
16-
},
27+
const bridgeJSRuntimeTests = importObject["BridgeJSRuntimeTests"] || {};
28+
bridgeJSRuntimeTests["runJsWorks"] = () => {
29+
return BridgeJSRuntimeTests_runJsWorks(getInstance(), getExports());
1730
}
31+
importObject["BridgeJSRuntimeTests"] = bridgeJSRuntimeTests;
1832
}
1933
}
2034
}

0 commit comments

Comments
 (0)
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