Skip to content

Commit 3a28bef

Browse files
committed
move BridgeJS stubs to runtime bundle
1 parent 522f7fb commit 3a28bef

File tree

5 files changed

+129
-36
lines changed

5 files changed

+129
-36
lines changed

Plugins/PackageToJS/Templates/instantiate.js

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-check
2-
import { SwiftRuntime } from "./runtime.js"
2+
import { SwiftRuntime, createBridgeJSStubs } from "./runtime.js"
33

44
export const MODULE_PATH = "@PACKAGE_TO_JS_MODULE_PATH@";
55
/* #if USE_SHARED_MEMORY */
@@ -30,39 +30,8 @@ async function createInstantiator(options, swift) {
3030
addImports: (importObject, importsContext) => {
3131
// Provide a default implementation for BridgeJS functions that are not
3232
// used at runtime without BridgeJS but required to instantiate the module.
33-
const unexpectedBjsCall = () => { throw new Error("Unexpected call to BridgeJS function") }
34-
importObject["bjs"] = {
35-
swift_js_return_string: unexpectedBjsCall,
36-
swift_js_init_memory: unexpectedBjsCall,
37-
swift_js_make_js_string: unexpectedBjsCall,
38-
swift_js_init_memory_with_result: unexpectedBjsCall,
39-
swift_js_throw: unexpectedBjsCall,
40-
swift_js_retain: unexpectedBjsCall,
41-
swift_js_release: unexpectedBjsCall,
42-
swift_js_push_tag: unexpectedBjsCall,
43-
swift_js_push_int: unexpectedBjsCall,
44-
swift_js_push_f32: unexpectedBjsCall,
45-
swift_js_push_f64: unexpectedBjsCall,
46-
swift_js_push_string: unexpectedBjsCall,
47-
swift_js_pop_param_int32: unexpectedBjsCall,
48-
swift_js_pop_param_f32: unexpectedBjsCall,
49-
swift_js_pop_param_f64: unexpectedBjsCall,
50-
swift_js_return_optional_bool: unexpectedBjsCall,
51-
swift_js_return_optional_int: unexpectedBjsCall,
52-
swift_js_return_optional_string: unexpectedBjsCall,
53-
swift_js_return_optional_double: unexpectedBjsCall,
54-
swift_js_return_optional_float: unexpectedBjsCall,
55-
swift_js_return_optional_heap_object: unexpectedBjsCall,
56-
swift_js_return_optional_object: unexpectedBjsCall,
57-
swift_js_get_optional_int_presence: unexpectedBjsCall,
58-
swift_js_get_optional_int_value: unexpectedBjsCall,
59-
swift_js_get_optional_string: unexpectedBjsCall,
60-
swift_js_get_optional_float_presence: unexpectedBjsCall,
61-
swift_js_get_optional_float_value: unexpectedBjsCall,
62-
swift_js_get_optional_double_presence: unexpectedBjsCall,
63-
swift_js_get_optional_double_value: unexpectedBjsCall,
64-
swift_js_get_optional_heap_object_pointer: unexpectedBjsCall,
65-
}
33+
34+
importObject["bjs"] = createBridgeJSStubs();
6635
},
6736
/** @param {WebAssembly.Instance} instance */
6837
setInstance: (instance) => {},

Plugins/PackageToJS/Templates/runtime.d.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,44 @@ type WorkerToMainMessage = {
158158
data: number;
159159
} | RequestMessage | ResponseMessage;
160160

161+
/**
162+
* Create a set of throwing stub functions for the BridgeJS API.
163+
* If no generate BridgeJS code is used, pass this object as "bjs" in the wasm imports.
164+
* @returns A set of throwing stub functions for the BridgeJS API.
165+
*/
166+
declare function createBridgeJSStubs(): {
167+
swift_js_return_string: () => never;
168+
swift_js_init_memory: () => never;
169+
swift_js_make_js_string: () => never;
170+
swift_js_init_memory_with_result: () => never;
171+
swift_js_throw: () => never;
172+
swift_js_retain: () => never;
173+
swift_js_release: () => never;
174+
swift_js_push_tag: () => never;
175+
swift_js_push_int: () => never;
176+
swift_js_push_f32: () => never;
177+
swift_js_push_f64: () => never;
178+
swift_js_push_string: () => never;
179+
swift_js_pop_param_int32: () => never;
180+
swift_js_pop_param_f32: () => never;
181+
swift_js_pop_param_f64: () => never;
182+
swift_js_return_optional_bool: () => never;
183+
swift_js_return_optional_int: () => never;
184+
swift_js_return_optional_string: () => never;
185+
swift_js_return_optional_double: () => never;
186+
swift_js_return_optional_float: () => never;
187+
swift_js_return_optional_heap_object: () => never;
188+
swift_js_return_optional_object: () => never;
189+
swift_js_get_optional_int_presence: () => never;
190+
swift_js_get_optional_int_value: () => never;
191+
swift_js_get_optional_string: () => never;
192+
swift_js_get_optional_float_presence: () => never;
193+
swift_js_get_optional_float_value: () => never;
194+
swift_js_get_optional_double_presence: () => never;
195+
swift_js_get_optional_double_value: () => never;
196+
swift_js_get_optional_heap_object_pointer: () => never;
197+
};
198+
161199
type SwiftRuntimeOptions = {
162200
/**
163201
* If `true`, the memory space of the WebAssembly instance can be shared
@@ -206,5 +244,5 @@ declare class SwiftRuntime {
206244
declare class UnsafeEventLoopYield extends Error {
207245
}
208246

209-
export { SwiftRuntime };
247+
export { SwiftRuntime, createBridgeJSStubs };
210248
export type { SwiftRuntimeOptions, SwiftRuntimeThreadChannel };

Plugins/PackageToJS/Templates/runtime.mjs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,49 @@ class JSObjectSpace {
279279
}
280280
}
281281

282+
/**
283+
* Create a set of throwing stub functions for the BridgeJS API.
284+
* If no generate BridgeJS code is used, pass this object as "bjs" in the wasm imports.
285+
* @returns A set of throwing stub functions for the BridgeJS API.
286+
*/
287+
function createBridgeJSStubs() {
288+
const unexpectedBjsCall = () => {
289+
throw new Error("Unexpected call to BridgeJS function");
290+
};
291+
return {
292+
swift_js_return_string: unexpectedBjsCall,
293+
swift_js_init_memory: unexpectedBjsCall,
294+
swift_js_make_js_string: unexpectedBjsCall,
295+
swift_js_init_memory_with_result: unexpectedBjsCall,
296+
swift_js_throw: unexpectedBjsCall,
297+
swift_js_retain: unexpectedBjsCall,
298+
swift_js_release: unexpectedBjsCall,
299+
swift_js_push_tag: unexpectedBjsCall,
300+
swift_js_push_int: unexpectedBjsCall,
301+
swift_js_push_f32: unexpectedBjsCall,
302+
swift_js_push_f64: unexpectedBjsCall,
303+
swift_js_push_string: unexpectedBjsCall,
304+
swift_js_pop_param_int32: unexpectedBjsCall,
305+
swift_js_pop_param_f32: unexpectedBjsCall,
306+
swift_js_pop_param_f64: unexpectedBjsCall,
307+
swift_js_return_optional_bool: unexpectedBjsCall,
308+
swift_js_return_optional_int: unexpectedBjsCall,
309+
swift_js_return_optional_string: unexpectedBjsCall,
310+
swift_js_return_optional_double: unexpectedBjsCall,
311+
swift_js_return_optional_float: unexpectedBjsCall,
312+
swift_js_return_optional_heap_object: unexpectedBjsCall,
313+
swift_js_return_optional_object: unexpectedBjsCall,
314+
swift_js_get_optional_int_presence: unexpectedBjsCall,
315+
swift_js_get_optional_int_value: unexpectedBjsCall,
316+
swift_js_get_optional_string: unexpectedBjsCall,
317+
swift_js_get_optional_float_presence: unexpectedBjsCall,
318+
swift_js_get_optional_float_value: unexpectedBjsCall,
319+
swift_js_get_optional_double_presence: unexpectedBjsCall,
320+
swift_js_get_optional_double_value: unexpectedBjsCall,
321+
swift_js_get_optional_heap_object_pointer: unexpectedBjsCall,
322+
};
323+
}
324+
282325
class SwiftRuntime {
283326
constructor(options) {
284327
this.version = 708;
@@ -855,4 +898,4 @@ class SwiftRuntime {
855898
class UnsafeEventLoopYield extends Error {
856899
}
857900

858-
export { SwiftRuntime };
901+
export { SwiftRuntime, createBridgeJSStubs };

Runtime/src/bridge-js.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Create a set of throwing stub functions for the BridgeJS API.
3+
* If no generate BridgeJS code is used, pass this object as "bjs" in the wasm imports.
4+
* @returns A set of throwing stub functions for the BridgeJS API.
5+
*/
6+
export function createBridgeJSStubs() {
7+
const unexpectedBjsCall = () => {
8+
throw new Error("Unexpected call to BridgeJS function");
9+
};
10+
return {
11+
swift_js_return_string: unexpectedBjsCall,
12+
swift_js_init_memory: unexpectedBjsCall,
13+
swift_js_make_js_string: unexpectedBjsCall,
14+
swift_js_init_memory_with_result: unexpectedBjsCall,
15+
swift_js_throw: unexpectedBjsCall,
16+
swift_js_retain: unexpectedBjsCall,
17+
swift_js_release: unexpectedBjsCall,
18+
swift_js_push_tag: unexpectedBjsCall,
19+
swift_js_push_int: unexpectedBjsCall,
20+
swift_js_push_f32: unexpectedBjsCall,
21+
swift_js_push_f64: unexpectedBjsCall,
22+
swift_js_push_string: unexpectedBjsCall,
23+
swift_js_pop_param_int32: unexpectedBjsCall,
24+
swift_js_pop_param_f32: unexpectedBjsCall,
25+
swift_js_pop_param_f64: unexpectedBjsCall,
26+
swift_js_return_optional_bool: unexpectedBjsCall,
27+
swift_js_return_optional_int: unexpectedBjsCall,
28+
swift_js_return_optional_string: unexpectedBjsCall,
29+
swift_js_return_optional_double: unexpectedBjsCall,
30+
swift_js_return_optional_float: unexpectedBjsCall,
31+
swift_js_return_optional_heap_object: unexpectedBjsCall,
32+
swift_js_return_optional_object: unexpectedBjsCall,
33+
swift_js_get_optional_int_presence: unexpectedBjsCall,
34+
swift_js_get_optional_int_value: unexpectedBjsCall,
35+
swift_js_get_optional_string: unexpectedBjsCall,
36+
swift_js_get_optional_float_presence: unexpectedBjsCall,
37+
swift_js_get_optional_float_value: unexpectedBjsCall,
38+
swift_js_get_optional_double_presence: unexpectedBjsCall,
39+
swift_js_get_optional_double_value: unexpectedBjsCall,
40+
swift_js_get_optional_heap_object_pointer: unexpectedBjsCall,
41+
};
42+
}

Runtime/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { deserializeError, MainToWorkerMessage, MessageBroker, ResponseMessage,
1111
import { decodeObjectRefs } from "./js-value.js";
1212
import { JSObjectSpace } from "./object-heap.js";
1313
export type { SwiftRuntimeThreadChannel };
14+
export { createBridgeJSStubs } from "./bridge-js.js";
1415

1516
export type SwiftRuntimeOptions = {
1617
/**

0 commit comments

Comments
 (0)