Skip to content

Commit 6445db1

Browse files
committed
BridgeJS: Few more array import tests
1 parent 16a3894 commit 6445db1

File tree

11 files changed

+1258
-0
lines changed

11 files changed

+1258
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@JSFunction func roundtrip(_ items: [Int]) throws(JSException) -> [Int]
2+
@JSFunction func logStrings(_ items: [String]) throws(JSException)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"imported" : {
3+
"children" : [
4+
{
5+
"functions" : [
6+
{
7+
"name" : "roundtrip",
8+
"parameters" : [
9+
{
10+
"name" : "items",
11+
"type" : {
12+
"array" : {
13+
"_0" : {
14+
"int" : {
15+
16+
}
17+
}
18+
}
19+
}
20+
}
21+
],
22+
"returnType" : {
23+
"array" : {
24+
"_0" : {
25+
"int" : {
26+
27+
}
28+
}
29+
}
30+
}
31+
},
32+
{
33+
"name" : "logStrings",
34+
"parameters" : [
35+
{
36+
"name" : "items",
37+
"type" : {
38+
"array" : {
39+
"_0" : {
40+
"string" : {
41+
42+
}
43+
}
44+
}
45+
}
46+
}
47+
],
48+
"returnType" : {
49+
"void" : {
50+
51+
}
52+
}
53+
}
54+
],
55+
"types" : [
56+
57+
]
58+
}
59+
]
60+
},
61+
"moduleName" : "TestModule"
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if arch(wasm32)
2+
@_extern(wasm, module: "TestModule", name: "bjs_roundtrip")
3+
fileprivate func bjs_roundtrip() -> Void
4+
#else
5+
fileprivate func bjs_roundtrip() -> Void {
6+
fatalError("Only available on WebAssembly")
7+
}
8+
#endif
9+
10+
func _$roundtrip(_ items: [Int]) throws(JSException) -> [Int] {
11+
let _ = items.bridgeJSLowerParameter()
12+
bjs_roundtrip()
13+
if let error = _swift_js_take_exception() {
14+
throw error
15+
}
16+
return [Int].bridgeJSLiftReturn()
17+
}
18+
19+
#if arch(wasm32)
20+
@_extern(wasm, module: "TestModule", name: "bjs_logStrings")
21+
fileprivate func bjs_logStrings() -> Void
22+
#else
23+
fileprivate func bjs_logStrings() -> Void {
24+
fatalError("Only available on WebAssembly")
25+
}
26+
#endif
27+
28+
func _$logStrings(_ items: [String]) throws(JSException) -> Void {
29+
let _ = items.bridgeJSLowerParameter()
30+
bjs_logStrings()
31+
if let error = _swift_js_take_exception() {
32+
throw error
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
export type Exports = {
8+
}
9+
export type Imports = {
10+
roundtrip(items: number[]): number[];
11+
logStrings(items: string[]): void;
12+
}
13+
export function createInstantiator(options: {
14+
imports: Imports;
15+
}, swift: any): Promise<{
16+
addImports: (importObject: WebAssembly.Imports) => void;
17+
setInstance: (instance: WebAssembly.Instance) => void;
18+
createExports: (instance: WebAssembly.Instance) => Exports;
19+
}>;
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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+
export async function createInstantiator(options, swift) {
8+
let instance;
9+
let memory;
10+
let setException;
11+
const textDecoder = new TextDecoder("utf-8");
12+
const textEncoder = new TextEncoder("utf-8");
13+
let tmpRetString;
14+
let tmpRetBytes;
15+
let tmpRetException;
16+
let tmpRetOptionalBool;
17+
let tmpRetOptionalInt;
18+
let tmpRetOptionalFloat;
19+
let tmpRetOptionalDouble;
20+
let tmpRetOptionalHeapObject;
21+
let tmpRetTag;
22+
let tmpRetStrings = [];
23+
let tmpRetInts = [];
24+
let tmpRetF32s = [];
25+
let tmpRetF64s = [];
26+
let tmpParamInts = [];
27+
let tmpParamF32s = [];
28+
let tmpParamF64s = [];
29+
let tmpRetPointers = [];
30+
let tmpParamPointers = [];
31+
let tmpStructCleanups = [];
32+
const enumHelpers = {};
33+
const structHelpers = {};
34+
35+
let _exports = null;
36+
let bjs = null;
37+
38+
return {
39+
/**
40+
* @param {WebAssembly.Imports} importObject
41+
*/
42+
addImports: (importObject, importsContext) => {
43+
bjs = {};
44+
importObject["bjs"] = bjs;
45+
const imports = options.getImports(importsContext);
46+
bjs["swift_js_return_string"] = function(ptr, len) {
47+
const bytes = new Uint8Array(memory.buffer, ptr, len);
48+
tmpRetString = textDecoder.decode(bytes);
49+
}
50+
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
51+
const source = swift.memory.getObject(sourceId);
52+
const bytes = new Uint8Array(memory.buffer, bytesPtr);
53+
bytes.set(source);
54+
}
55+
bjs["swift_js_make_js_string"] = function(ptr, len) {
56+
const bytes = new Uint8Array(memory.buffer, ptr, len);
57+
return swift.memory.retain(textDecoder.decode(bytes));
58+
}
59+
bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
60+
const target = new Uint8Array(memory.buffer, ptr, len);
61+
target.set(tmpRetBytes);
62+
tmpRetBytes = undefined;
63+
}
64+
bjs["swift_js_throw"] = function(id) {
65+
tmpRetException = swift.memory.retainByRef(id);
66+
}
67+
bjs["swift_js_retain"] = function(id) {
68+
return swift.memory.retainByRef(id);
69+
}
70+
bjs["swift_js_release"] = function(id) {
71+
swift.memory.release(id);
72+
}
73+
bjs["swift_js_push_tag"] = function(tag) {
74+
tmpRetTag = tag;
75+
}
76+
bjs["swift_js_push_i32"] = function(v) {
77+
tmpRetInts.push(v | 0);
78+
}
79+
bjs["swift_js_push_f32"] = function(v) {
80+
tmpRetF32s.push(Math.fround(v));
81+
}
82+
bjs["swift_js_push_f64"] = function(v) {
83+
tmpRetF64s.push(v);
84+
}
85+
bjs["swift_js_push_string"] = function(ptr, len) {
86+
const bytes = new Uint8Array(memory.buffer, ptr, len);
87+
const value = textDecoder.decode(bytes);
88+
tmpRetStrings.push(value);
89+
}
90+
bjs["swift_js_pop_i32"] = function() {
91+
return tmpParamInts.pop();
92+
}
93+
bjs["swift_js_pop_f32"] = function() {
94+
return tmpParamF32s.pop();
95+
}
96+
bjs["swift_js_pop_f64"] = function() {
97+
return tmpParamF64s.pop();
98+
}
99+
bjs["swift_js_push_pointer"] = function(pointer) {
100+
tmpRetPointers.push(pointer);
101+
}
102+
bjs["swift_js_pop_pointer"] = function() {
103+
return tmpParamPointers.pop();
104+
}
105+
bjs["swift_js_struct_cleanup"] = function(cleanupId) {
106+
if (cleanupId === 0) { return; }
107+
const index = (cleanupId | 0) - 1;
108+
const cleanup = tmpStructCleanups[index];
109+
tmpStructCleanups[index] = null;
110+
if (cleanup) { cleanup(); }
111+
while (tmpStructCleanups.length > 0 && tmpStructCleanups[tmpStructCleanups.length - 1] == null) {
112+
tmpStructCleanups.pop();
113+
}
114+
}
115+
bjs["swift_js_return_optional_bool"] = function(isSome, value) {
116+
if (isSome === 0) {
117+
tmpRetOptionalBool = null;
118+
} else {
119+
tmpRetOptionalBool = value !== 0;
120+
}
121+
}
122+
bjs["swift_js_return_optional_int"] = function(isSome, value) {
123+
if (isSome === 0) {
124+
tmpRetOptionalInt = null;
125+
} else {
126+
tmpRetOptionalInt = value | 0;
127+
}
128+
}
129+
bjs["swift_js_return_optional_float"] = function(isSome, value) {
130+
if (isSome === 0) {
131+
tmpRetOptionalFloat = null;
132+
} else {
133+
tmpRetOptionalFloat = Math.fround(value);
134+
}
135+
}
136+
bjs["swift_js_return_optional_double"] = function(isSome, value) {
137+
if (isSome === 0) {
138+
tmpRetOptionalDouble = null;
139+
} else {
140+
tmpRetOptionalDouble = value;
141+
}
142+
}
143+
bjs["swift_js_return_optional_string"] = function(isSome, ptr, len) {
144+
if (isSome === 0) {
145+
tmpRetString = null;
146+
} else {
147+
const bytes = new Uint8Array(memory.buffer, ptr, len);
148+
tmpRetString = textDecoder.decode(bytes);
149+
}
150+
}
151+
bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
152+
if (isSome === 0) {
153+
tmpRetString = null;
154+
} else {
155+
tmpRetString = swift.memory.getObject(objectId);
156+
}
157+
}
158+
bjs["swift_js_return_optional_heap_object"] = function(isSome, pointer) {
159+
if (isSome === 0) {
160+
tmpRetOptionalHeapObject = null;
161+
} else {
162+
tmpRetOptionalHeapObject = pointer;
163+
}
164+
}
165+
bjs["swift_js_get_optional_int_presence"] = function() {
166+
return tmpRetOptionalInt != null ? 1 : 0;
167+
}
168+
bjs["swift_js_get_optional_int_value"] = function() {
169+
const value = tmpRetOptionalInt;
170+
tmpRetOptionalInt = undefined;
171+
return value;
172+
}
173+
bjs["swift_js_get_optional_string"] = function() {
174+
const str = tmpRetString;
175+
tmpRetString = undefined;
176+
if (str == null) {
177+
return -1;
178+
} else {
179+
const bytes = textEncoder.encode(str);
180+
tmpRetBytes = bytes;
181+
return bytes.length;
182+
}
183+
}
184+
bjs["swift_js_get_optional_float_presence"] = function() {
185+
return tmpRetOptionalFloat != null ? 1 : 0;
186+
}
187+
bjs["swift_js_get_optional_float_value"] = function() {
188+
const value = tmpRetOptionalFloat;
189+
tmpRetOptionalFloat = undefined;
190+
return value;
191+
}
192+
bjs["swift_js_get_optional_double_presence"] = function() {
193+
return tmpRetOptionalDouble != null ? 1 : 0;
194+
}
195+
bjs["swift_js_get_optional_double_value"] = function() {
196+
const value = tmpRetOptionalDouble;
197+
tmpRetOptionalDouble = undefined;
198+
return value;
199+
}
200+
bjs["swift_js_get_optional_heap_object_pointer"] = function() {
201+
const pointer = tmpRetOptionalHeapObject;
202+
tmpRetOptionalHeapObject = undefined;
203+
return pointer || 0;
204+
}
205+
const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
206+
TestModule["bjs_roundtrip"] = function bjs_roundtrip() {
207+
try {
208+
const arrayLen = tmpRetInts.pop();
209+
const arrayResult = [];
210+
for (let i = 0; i < arrayLen; i++) {
211+
const int = tmpRetInts.pop();
212+
arrayResult.push(int);
213+
}
214+
arrayResult.reverse();
215+
let ret = imports.roundtrip(arrayResult);
216+
const arrayCleanups = [];
217+
for (const elem of ret) {
218+
tmpParamInts.push((elem | 0));
219+
}
220+
tmpParamInts.push(ret.length);
221+
} catch (error) {
222+
setException(error);
223+
}
224+
}
225+
TestModule["bjs_logStrings"] = function bjs_logStrings() {
226+
try {
227+
const arrayLen = tmpRetInts.pop();
228+
const arrayResult = [];
229+
for (let i = 0; i < arrayLen; i++) {
230+
const string = tmpRetStrings.pop();
231+
arrayResult.push(string);
232+
}
233+
arrayResult.reverse();
234+
imports.logStrings(arrayResult);
235+
} catch (error) {
236+
setException(error);
237+
}
238+
}
239+
},
240+
setInstance: (i) => {
241+
instance = i;
242+
memory = instance.exports.memory;
243+
244+
setException = (error) => {
245+
instance.exports._swift_js_exception.value = swift.memory.retain(error)
246+
}
247+
},
248+
/** @param {WebAssembly.Instance} instance */
249+
createExports: (instance) => {
250+
const js = swift.memory.heap;
251+
const exports = {
252+
};
253+
_exports = exports;
254+
return exports;
255+
},
256+
}
257+
}

0 commit comments

Comments
 (0)