Skip to content
Open
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
37 changes: 3 additions & 34 deletions Plugins/PackageToJS/Templates/instantiate.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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) => {},
Expand Down
40 changes: 39 additions & 1 deletion Plugins/PackageToJS/Templates/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -206,5 +244,5 @@ declare class SwiftRuntime {
declare class UnsafeEventLoopYield extends Error {
}

export { SwiftRuntime };
export { SwiftRuntime, createBridgeJSStubs };
export type { SwiftRuntimeOptions, SwiftRuntimeThreadChannel };
45 changes: 44 additions & 1 deletion Plugins/PackageToJS/Templates/runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -855,4 +898,4 @@ class SwiftRuntime {
class UnsafeEventLoopYield extends Error {
}

export { SwiftRuntime };
export { SwiftRuntime, createBridgeJSStubs };
42 changes: 42 additions & 0 deletions Runtime/src/bridge-js.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
3 changes: 2 additions & 1 deletion Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ 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 { createBridgeJSStubs } from "./bridge-js.js";

export type SwiftRuntimeOptions = {
/**
Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}