From 522f7fb1b6b01ae1f9af52e9f5ed7a30420d6d80 Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Mon, 15 Dec 2025 12:53:40 +0100 Subject: [PATCH 1/3] fixed runtime module build --- Runtime/src/index.ts | 2 +- Runtime/tsconfig.json | 3 ++- package-lock.json | 5 ++--- package.json | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts index 27b52c7d..e00b36bb 100644 --- a/Runtime/src/index.ts +++ b/Runtime/src/index.ts @@ -10,7 +10,7 @@ import * as JSValue from "./js-value.js"; import { deserializeError, MainToWorkerMessage, MessageBroker, ResponseMessage, ITCInterface, serializeError, SwiftRuntimeThreadChannel, WorkerToMainMessage } from "./itc.js"; import { decodeObjectRefs } from "./js-value.js"; import { JSObjectSpace } from "./object-heap.js"; -export { SwiftRuntimeThreadChannel }; +export type { SwiftRuntimeThreadChannel }; export type SwiftRuntimeOptions = { /** diff --git a/Runtime/tsconfig.json b/Runtime/tsconfig.json index bd4f8a1a..a0c7cb87 100644 --- a/Runtime/tsconfig.json +++ b/Runtime/tsconfig.json @@ -8,7 +8,8 @@ "strict": true, "target": "es2017", "lib": ["es2020", "DOM", "ESNext.WeakRef"], - "skipLibCheck": true + "skipLibCheck": true, + "isolatedModules": true }, "include": ["src/**/*"], "exclude": ["node_modules"] diff --git a/package-lock.json b/package-lock.json index e12af9c9..f101419b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "prettier": "3.5.3", "rollup": "^4.37.0", "rollup-plugin-dts": "^6.2.1", + "tslib": "^2.8.1", "typescript": "^5.8.2" } }, @@ -653,9 +654,7 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "dev": true, - "license": "0BSD", - "optional": true, - "peer": true + "license": "0BSD" }, "node_modules/typescript": { "version": "5.8.2", diff --git a/package.json b/package.json index 96443ad9..da7d5356 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "prettier": "3.5.3", "rollup": "^4.37.0", "rollup-plugin-dts": "^6.2.1", + "tslib": "^2.8.1", "typescript": "^5.8.2" } } From 3a28bef5a7804b20c66da25c23a8b7afa89a66af Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Mon, 15 Dec 2025 13:31:52 +0100 Subject: [PATCH 2/3] move BridgeJS stubs to runtime bundle --- Plugins/PackageToJS/Templates/instantiate.js | 37 ++-------------- Plugins/PackageToJS/Templates/runtime.d.ts | 40 ++++++++++++++++- Plugins/PackageToJS/Templates/runtime.mjs | 45 +++++++++++++++++++- Runtime/src/bridge-js.ts | 42 ++++++++++++++++++ Runtime/src/index.ts | 1 + 5 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 Runtime/src/bridge-js.ts diff --git a/Plugins/PackageToJS/Templates/instantiate.js b/Plugins/PackageToJS/Templates/instantiate.js index 1ab35de3..1fcb24d4 100644 --- a/Plugins/PackageToJS/Templates/instantiate.js +++ b/Plugins/PackageToJS/Templates/instantiate.js @@ -1,5 +1,5 @@ // @ts-check -import { SwiftRuntime } from "./runtime.js" +import { SwiftRuntime, createBridgeJSStubs } from "./runtime.js" export const MODULE_PATH = "@PACKAGE_TO_JS_MODULE_PATH@"; /* #if USE_SHARED_MEMORY */ @@ -30,39 +30,8 @@ async function createInstantiator(options, swift) { addImports: (importObject, importsContext) => { // Provide a default implementation for BridgeJS functions that are not // used at runtime without BridgeJS but required to instantiate the module. - const unexpectedBjsCall = () => { throw new Error("Unexpected call to BridgeJS function") } - importObject["bjs"] = { - swift_js_return_string: unexpectedBjsCall, - swift_js_init_memory: unexpectedBjsCall, - swift_js_make_js_string: unexpectedBjsCall, - swift_js_init_memory_with_result: unexpectedBjsCall, - swift_js_throw: unexpectedBjsCall, - swift_js_retain: unexpectedBjsCall, - swift_js_release: unexpectedBjsCall, - swift_js_push_tag: unexpectedBjsCall, - swift_js_push_int: unexpectedBjsCall, - swift_js_push_f32: unexpectedBjsCall, - swift_js_push_f64: unexpectedBjsCall, - swift_js_push_string: unexpectedBjsCall, - swift_js_pop_param_int32: unexpectedBjsCall, - swift_js_pop_param_f32: unexpectedBjsCall, - swift_js_pop_param_f64: unexpectedBjsCall, - swift_js_return_optional_bool: unexpectedBjsCall, - swift_js_return_optional_int: unexpectedBjsCall, - swift_js_return_optional_string: unexpectedBjsCall, - swift_js_return_optional_double: unexpectedBjsCall, - swift_js_return_optional_float: unexpectedBjsCall, - swift_js_return_optional_heap_object: unexpectedBjsCall, - swift_js_return_optional_object: unexpectedBjsCall, - swift_js_get_optional_int_presence: unexpectedBjsCall, - swift_js_get_optional_int_value: unexpectedBjsCall, - swift_js_get_optional_string: unexpectedBjsCall, - swift_js_get_optional_float_presence: unexpectedBjsCall, - swift_js_get_optional_float_value: unexpectedBjsCall, - swift_js_get_optional_double_presence: unexpectedBjsCall, - swift_js_get_optional_double_value: unexpectedBjsCall, - swift_js_get_optional_heap_object_pointer: unexpectedBjsCall, - } + + importObject["bjs"] = createBridgeJSStubs(); }, /** @param {WebAssembly.Instance} instance */ setInstance: (instance) => {}, diff --git a/Plugins/PackageToJS/Templates/runtime.d.ts b/Plugins/PackageToJS/Templates/runtime.d.ts index 353db389..23c73559 100644 --- a/Plugins/PackageToJS/Templates/runtime.d.ts +++ b/Plugins/PackageToJS/Templates/runtime.d.ts @@ -158,6 +158,44 @@ type WorkerToMainMessage = { data: number; } | RequestMessage | ResponseMessage; +/** + * Create a set of throwing stub functions for the BridgeJS API. + * If no generate BridgeJS code is used, pass this object as "bjs" in the wasm imports. + * @returns A set of throwing stub functions for the BridgeJS API. + */ +declare function createBridgeJSStubs(): { + swift_js_return_string: () => never; + swift_js_init_memory: () => never; + swift_js_make_js_string: () => never; + swift_js_init_memory_with_result: () => never; + swift_js_throw: () => never; + swift_js_retain: () => never; + swift_js_release: () => never; + swift_js_push_tag: () => never; + swift_js_push_int: () => never; + swift_js_push_f32: () => never; + swift_js_push_f64: () => never; + swift_js_push_string: () => never; + swift_js_pop_param_int32: () => never; + swift_js_pop_param_f32: () => never; + swift_js_pop_param_f64: () => never; + swift_js_return_optional_bool: () => never; + swift_js_return_optional_int: () => never; + swift_js_return_optional_string: () => never; + swift_js_return_optional_double: () => never; + swift_js_return_optional_float: () => never; + swift_js_return_optional_heap_object: () => never; + swift_js_return_optional_object: () => never; + swift_js_get_optional_int_presence: () => never; + swift_js_get_optional_int_value: () => never; + swift_js_get_optional_string: () => never; + swift_js_get_optional_float_presence: () => never; + swift_js_get_optional_float_value: () => never; + swift_js_get_optional_double_presence: () => never; + swift_js_get_optional_double_value: () => never; + swift_js_get_optional_heap_object_pointer: () => never; +}; + type SwiftRuntimeOptions = { /** * If `true`, the memory space of the WebAssembly instance can be shared @@ -206,5 +244,5 @@ declare class SwiftRuntime { declare class UnsafeEventLoopYield extends Error { } -export { SwiftRuntime }; +export { SwiftRuntime, createBridgeJSStubs }; export type { SwiftRuntimeOptions, SwiftRuntimeThreadChannel }; diff --git a/Plugins/PackageToJS/Templates/runtime.mjs b/Plugins/PackageToJS/Templates/runtime.mjs index a40fc3df..92bf7d57 100644 --- a/Plugins/PackageToJS/Templates/runtime.mjs +++ b/Plugins/PackageToJS/Templates/runtime.mjs @@ -279,6 +279,49 @@ class JSObjectSpace { } } +/** + * Create a set of throwing stub functions for the BridgeJS API. + * If no generate BridgeJS code is used, pass this object as "bjs" in the wasm imports. + * @returns A set of throwing stub functions for the BridgeJS API. + */ +function createBridgeJSStubs() { + const unexpectedBjsCall = () => { + throw new Error("Unexpected call to BridgeJS function"); + }; + return { + swift_js_return_string: unexpectedBjsCall, + swift_js_init_memory: unexpectedBjsCall, + swift_js_make_js_string: unexpectedBjsCall, + swift_js_init_memory_with_result: unexpectedBjsCall, + swift_js_throw: unexpectedBjsCall, + swift_js_retain: unexpectedBjsCall, + swift_js_release: unexpectedBjsCall, + swift_js_push_tag: unexpectedBjsCall, + swift_js_push_int: unexpectedBjsCall, + swift_js_push_f32: unexpectedBjsCall, + swift_js_push_f64: unexpectedBjsCall, + swift_js_push_string: unexpectedBjsCall, + swift_js_pop_param_int32: unexpectedBjsCall, + swift_js_pop_param_f32: unexpectedBjsCall, + swift_js_pop_param_f64: unexpectedBjsCall, + swift_js_return_optional_bool: unexpectedBjsCall, + swift_js_return_optional_int: unexpectedBjsCall, + swift_js_return_optional_string: unexpectedBjsCall, + swift_js_return_optional_double: unexpectedBjsCall, + swift_js_return_optional_float: unexpectedBjsCall, + swift_js_return_optional_heap_object: unexpectedBjsCall, + swift_js_return_optional_object: unexpectedBjsCall, + swift_js_get_optional_int_presence: unexpectedBjsCall, + swift_js_get_optional_int_value: unexpectedBjsCall, + swift_js_get_optional_string: unexpectedBjsCall, + swift_js_get_optional_float_presence: unexpectedBjsCall, + swift_js_get_optional_float_value: unexpectedBjsCall, + swift_js_get_optional_double_presence: unexpectedBjsCall, + swift_js_get_optional_double_value: unexpectedBjsCall, + swift_js_get_optional_heap_object_pointer: unexpectedBjsCall, + }; +} + class SwiftRuntime { constructor(options) { this.version = 708; @@ -855,4 +898,4 @@ class SwiftRuntime { class UnsafeEventLoopYield extends Error { } -export { SwiftRuntime }; +export { SwiftRuntime, createBridgeJSStubs }; diff --git a/Runtime/src/bridge-js.ts b/Runtime/src/bridge-js.ts new file mode 100644 index 00000000..d2a9f240 --- /dev/null +++ b/Runtime/src/bridge-js.ts @@ -0,0 +1,42 @@ +/** + * Create a set of throwing stub functions for the BridgeJS API. + * If no generate BridgeJS code is used, pass this object as "bjs" in the wasm imports. + * @returns A set of throwing stub functions for the BridgeJS API. + */ +export function createBridgeJSStubs() { + const unexpectedBjsCall = () => { + throw new Error("Unexpected call to BridgeJS function"); + }; + return { + swift_js_return_string: unexpectedBjsCall, + swift_js_init_memory: unexpectedBjsCall, + swift_js_make_js_string: unexpectedBjsCall, + swift_js_init_memory_with_result: unexpectedBjsCall, + swift_js_throw: unexpectedBjsCall, + swift_js_retain: unexpectedBjsCall, + swift_js_release: unexpectedBjsCall, + swift_js_push_tag: unexpectedBjsCall, + swift_js_push_int: unexpectedBjsCall, + swift_js_push_f32: unexpectedBjsCall, + swift_js_push_f64: unexpectedBjsCall, + swift_js_push_string: unexpectedBjsCall, + swift_js_pop_param_int32: unexpectedBjsCall, + swift_js_pop_param_f32: unexpectedBjsCall, + swift_js_pop_param_f64: unexpectedBjsCall, + swift_js_return_optional_bool: unexpectedBjsCall, + swift_js_return_optional_int: unexpectedBjsCall, + swift_js_return_optional_string: unexpectedBjsCall, + swift_js_return_optional_double: unexpectedBjsCall, + swift_js_return_optional_float: unexpectedBjsCall, + swift_js_return_optional_heap_object: unexpectedBjsCall, + swift_js_return_optional_object: unexpectedBjsCall, + swift_js_get_optional_int_presence: unexpectedBjsCall, + swift_js_get_optional_int_value: unexpectedBjsCall, + swift_js_get_optional_string: unexpectedBjsCall, + swift_js_get_optional_float_presence: unexpectedBjsCall, + swift_js_get_optional_float_value: unexpectedBjsCall, + swift_js_get_optional_double_presence: unexpectedBjsCall, + swift_js_get_optional_double_value: unexpectedBjsCall, + swift_js_get_optional_heap_object_pointer: unexpectedBjsCall, + }; +} diff --git a/Runtime/src/index.ts b/Runtime/src/index.ts index e00b36bb..fa41d791 100644 --- a/Runtime/src/index.ts +++ b/Runtime/src/index.ts @@ -11,6 +11,7 @@ import { deserializeError, MainToWorkerMessage, MessageBroker, ResponseMessage, import { decodeObjectRefs } from "./js-value.js"; import { JSObjectSpace } from "./object-heap.js"; export type { SwiftRuntimeThreadChannel }; +export { createBridgeJSStubs } from "./bridge-js.js"; export type SwiftRuntimeOptions = { /** From 80433bd7b50e555f06d7bf9f8fb818311571bdeb Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Mon, 15 Dec 2025 14:00:26 +0100 Subject: [PATCH 3/3] this messes up regenerate_swiftpm_resources because of the enums.... --- Runtime/tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Runtime/tsconfig.json b/Runtime/tsconfig.json index a0c7cb87..bd4f8a1a 100644 --- a/Runtime/tsconfig.json +++ b/Runtime/tsconfig.json @@ -8,8 +8,7 @@ "strict": true, "target": "es2017", "lib": ["es2020", "DOM", "ESNext.WeakRef"], - "skipLibCheck": true, - "isolatedModules": true + "skipLibCheck": true }, "include": ["src/**/*"], "exclude": ["node_modules"]