Skip to content

Commit 444393f

Browse files
authored
BridgeJS: Fix Dictionary bridging after Stack ABI refactor (#594)
1 parent faf9d12 commit 444393f

File tree

2 files changed

+70
-74
lines changed

2 files changed

+70
-74
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ struct IntrinsicJSFragment: Sendable {
10091009
printer.write("}")
10101010
case .dictionary(let valueType):
10111011
let isSomeVar = scope.variable("isSome")
1012-
printer.write("const \(isSomeVar) = \(JSGlueVariableScope.reservedTmpRetInts).pop();")
1012+
printer.write("const \(isSomeVar) = \(scope.popI32());")
10131013
printer.write("let \(resultVar);")
10141014
printer.write("if (\(isSomeVar)) {")
10151015
printer.indent {
@@ -1217,11 +1217,11 @@ struct IntrinsicJSFragment: Sendable {
12171217
}
12181218
}
12191219
printer.write("}")
1220-
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(entriesVar).length);")
1220+
scope.emitPushI32Parameter("\(entriesVar).length", printer: printer)
12211221
cleanupCode.write("for (const cleanup of \(cleanupArrayVar)) { cleanup(); }")
12221222
}
12231223
printer.write("}")
1224-
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);")
1224+
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
12251225
default:
12261226
()
12271227
}
@@ -2749,7 +2749,7 @@ struct IntrinsicJSFragment: Sendable {
27492749
}
27502750
}
27512751
printer.write("}")
2752-
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(entriesVar).length);")
2752+
scope.emitPushI32Parameter("\(entriesVar).length", printer: printer)
27532753
cleanupCode.write("for (const cleanup of \(cleanupArrayVar)) { cleanup(); }")
27542754
return []
27552755
}
@@ -2791,7 +2791,7 @@ struct IntrinsicJSFragment: Sendable {
27912791
let lenVar = scope.variable("dictLen")
27922792
let iVar = scope.variable("i")
27932793

2794-
printer.write("const \(lenVar) = \(JSGlueVariableScope.reservedTmpRetInts).pop();")
2794+
printer.write("const \(lenVar) = \(scope.popI32());")
27952795
printer.write("const \(resultVar) = {};")
27962796
printer.write("for (let \(iVar) = 0; \(iVar) < \(lenVar); \(iVar)++) {")
27972797
printer.indent {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DictionaryTypes.js

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ export async function createInstantiator(options, swift) {
1818
let tmpRetOptionalFloat;
1919
let tmpRetOptionalDouble;
2020
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 = [];
21+
let tagStack = [];
22+
let strStack = [];
23+
let i32Stack = [];
24+
let f32Stack = [];
25+
let f64Stack = [];
26+
let ptrStack = [];
3127
let tmpStructCleanups = [];
3228
const enumHelpers = {};
3329
const structHelpers = {};
@@ -71,36 +67,36 @@ export async function createInstantiator(options, swift) {
7167
swift.memory.release(id);
7268
}
7369
bjs["swift_js_push_tag"] = function(tag) {
74-
tmpRetTag.push(tag);
70+
tagStack.push(tag);
7571
}
7672
bjs["swift_js_push_i32"] = function(v) {
77-
tmpRetInts.push(v | 0);
73+
i32Stack.push(v | 0);
7874
}
7975
bjs["swift_js_push_f32"] = function(v) {
80-
tmpRetF32s.push(Math.fround(v));
76+
f32Stack.push(Math.fround(v));
8177
}
8278
bjs["swift_js_push_f64"] = function(v) {
83-
tmpRetF64s.push(v);
79+
f64Stack.push(v);
8480
}
8581
bjs["swift_js_push_string"] = function(ptr, len) {
8682
const bytes = new Uint8Array(memory.buffer, ptr, len);
8783
const value = textDecoder.decode(bytes);
88-
tmpRetStrings.push(value);
84+
strStack.push(value);
8985
}
9086
bjs["swift_js_pop_i32"] = function() {
91-
return tmpParamInts.pop();
87+
return i32Stack.pop();
9288
}
9389
bjs["swift_js_pop_f32"] = function() {
94-
return tmpParamF32s.pop();
90+
return f32Stack.pop();
9591
}
9692
bjs["swift_js_pop_f64"] = function() {
97-
return tmpParamF64s.pop();
93+
return f64Stack.pop();
9894
}
9995
bjs["swift_js_push_pointer"] = function(pointer) {
100-
tmpRetPointers.push(pointer);
96+
ptrStack.push(pointer);
10197
}
10298
bjs["swift_js_pop_pointer"] = function() {
103-
return tmpParamPointers.pop();
99+
return ptrStack.pop();
104100
}
105101
bjs["swift_js_struct_cleanup"] = function(cleanupId) {
106102
if (cleanupId === 0) { return; }
@@ -213,11 +209,11 @@ export async function createInstantiator(options, swift) {
213209
const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
214210
TestModule["bjs_importMirrorDictionary"] = function bjs_importMirrorDictionary() {
215211
try {
216-
const dictLen = tmpRetInts.pop();
212+
const dictLen = i32Stack.pop();
217213
const dictResult = {};
218214
for (let i = 0; i < dictLen; i++) {
219-
const f64 = tmpRetF64s.pop();
220-
const string = tmpRetStrings.pop();
215+
const f64 = f64Stack.pop();
216+
const string = strStack.pop();
221217
dictResult[string] = f64;
222218
}
223219
let ret = imports.importMirrorDictionary(dictResult);
@@ -227,14 +223,14 @@ export async function createInstantiator(options, swift) {
227223
const [key, value] = entry;
228224
const bytes = textEncoder.encode(key);
229225
const id = swift.memory.retain(bytes);
230-
tmpParamInts.push(bytes.length);
231-
tmpParamInts.push(id);
226+
i32Stack.push(bytes.length);
227+
i32Stack.push(id);
232228
arrayCleanups.push(() => {
233229
swift.memory.release(id);
234230
});
235-
tmpParamF64s.push(value);
231+
f64Stack.push(value);
236232
}
237-
tmpParamInts.push(entries.length);
233+
i32Stack.push(entries.length);
238234
} catch (error) {
239235
setException(error);
240236
}
@@ -285,20 +281,20 @@ export async function createInstantiator(options, swift) {
285281
const [key, value] = entry;
286282
const bytes = textEncoder.encode(key);
287283
const id = swift.memory.retain(bytes);
288-
tmpParamInts.push(bytes.length);
289-
tmpParamInts.push(id);
284+
i32Stack.push(bytes.length);
285+
i32Stack.push(id);
290286
arrayCleanups.push(() => {
291287
swift.memory.release(id);
292288
});
293-
tmpParamInts.push((value | 0));
289+
i32Stack.push((value | 0));
294290
}
295-
tmpParamInts.push(entries.length);
291+
i32Stack.push(entries.length);
296292
instance.exports.bjs_mirrorDictionary();
297-
const dictLen = tmpRetInts.pop();
293+
const dictLen = i32Stack.pop();
298294
const dictResult = {};
299295
for (let i = 0; i < dictLen; i++) {
300-
const int = tmpRetInts.pop();
301-
const string = tmpRetStrings.pop();
296+
const int = i32Stack.pop();
297+
const string = strStack.pop();
302298
dictResult[string] = int;
303299
}
304300
for (const cleanup of arrayCleanups) { cleanup(); }
@@ -314,31 +310,31 @@ export async function createInstantiator(options, swift) {
314310
const [key, value] = entry;
315311
const bytes = textEncoder.encode(key);
316312
const id = swift.memory.retain(bytes);
317-
tmpParamInts.push(bytes.length);
318-
tmpParamInts.push(id);
313+
i32Stack.push(bytes.length);
314+
i32Stack.push(id);
319315
arrayCleanups.push(() => {
320316
swift.memory.release(id);
321317
});
322318
const bytes1 = textEncoder.encode(value);
323319
const id1 = swift.memory.retain(bytes1);
324-
tmpParamInts.push(bytes1.length);
325-
tmpParamInts.push(id1);
320+
i32Stack.push(bytes1.length);
321+
i32Stack.push(id1);
326322
arrayCleanups.push(() => {
327323
swift.memory.release(id1);
328324
});
329325
}
330-
tmpParamInts.push(entries.length);
326+
i32Stack.push(entries.length);
331327
valuesCleanups.push(() => { for (const cleanup of arrayCleanups) { cleanup(); } });
332328
}
333329
instance.exports.bjs_optionalDictionary(+isSome);
334-
const isSome1 = tmpRetInts.pop();
330+
const isSome1 = i32Stack.pop();
335331
let optResult;
336332
if (isSome1) {
337-
const dictLen = tmpRetInts.pop();
333+
const dictLen = i32Stack.pop();
338334
const dictResult = {};
339335
for (let i = 0; i < dictLen; i++) {
340-
const string = tmpRetStrings.pop();
341-
const string1 = tmpRetStrings.pop();
336+
const string = strStack.pop();
337+
const string1 = strStack.pop();
342338
dictResult[string1] = string;
343339
}
344340
optResult = dictResult;
@@ -355,33 +351,33 @@ export async function createInstantiator(options, swift) {
355351
const [key, value] = entry;
356352
const bytes = textEncoder.encode(key);
357353
const id = swift.memory.retain(bytes);
358-
tmpParamInts.push(bytes.length);
359-
tmpParamInts.push(id);
354+
i32Stack.push(bytes.length);
355+
i32Stack.push(id);
360356
arrayCleanups.push(() => {
361357
swift.memory.release(id);
362358
});
363359
const arrayCleanups1 = [];
364360
for (const elem of value) {
365-
tmpParamInts.push((elem | 0));
361+
i32Stack.push((elem | 0));
366362
}
367-
tmpParamInts.push(value.length);
363+
i32Stack.push(value.length);
368364
arrayCleanups.push(() => {
369365
for (const cleanup of arrayCleanups1) { cleanup(); }
370366
});
371367
}
372-
tmpParamInts.push(entries.length);
368+
i32Stack.push(entries.length);
373369
instance.exports.bjs_nestedDictionary();
374-
const dictLen = tmpRetInts.pop();
370+
const dictLen = i32Stack.pop();
375371
const dictResult = {};
376372
for (let i = 0; i < dictLen; i++) {
377-
const arrayLen = tmpRetInts.pop();
373+
const arrayLen = i32Stack.pop();
378374
const arrayResult = [];
379375
for (let i1 = 0; i1 < arrayLen; i1++) {
380-
const int = tmpRetInts.pop();
376+
const int = i32Stack.pop();
381377
arrayResult.push(int);
382378
}
383379
arrayResult.reverse();
384-
const string = tmpRetStrings.pop();
380+
const string = strStack.pop();
385381
dictResult[string] = arrayResult;
386382
}
387383
for (const cleanup of arrayCleanups) { cleanup(); }
@@ -394,21 +390,21 @@ export async function createInstantiator(options, swift) {
394390
const [key, value] = entry;
395391
const bytes = textEncoder.encode(key);
396392
const id = swift.memory.retain(bytes);
397-
tmpParamInts.push(bytes.length);
398-
tmpParamInts.push(id);
393+
i32Stack.push(bytes.length);
394+
i32Stack.push(id);
399395
arrayCleanups.push(() => {
400396
swift.memory.release(id);
401397
});
402-
tmpParamPointers.push(value.pointer);
398+
ptrStack.push(value.pointer);
403399
}
404-
tmpParamInts.push(entries.length);
400+
i32Stack.push(entries.length);
405401
instance.exports.bjs_boxDictionary();
406-
const dictLen = tmpRetInts.pop();
402+
const dictLen = i32Stack.pop();
407403
const dictResult = {};
408404
for (let i = 0; i < dictLen; i++) {
409-
const ptr = tmpRetPointers.pop();
405+
const ptr = ptrStack.pop();
410406
const obj = _exports['Box'].__construct(ptr);
411-
const string = tmpRetStrings.pop();
407+
const string = strStack.pop();
412408
dictResult[string] = obj;
413409
}
414410
for (const cleanup of arrayCleanups) { cleanup(); }
@@ -421,34 +417,34 @@ export async function createInstantiator(options, swift) {
421417
const [key, value] = entry;
422418
const bytes = textEncoder.encode(key);
423419
const id = swift.memory.retain(bytes);
424-
tmpParamInts.push(bytes.length);
425-
tmpParamInts.push(id);
420+
i32Stack.push(bytes.length);
421+
i32Stack.push(id);
426422
arrayCleanups.push(() => {
427423
swift.memory.release(id);
428424
});
429425
const isSome = value != null ? 1 : 0;
430426
if (isSome) {
431-
tmpParamPointers.push(value.pointer);
427+
ptrStack.push(value.pointer);
432428
} else {
433-
tmpParamPointers.push(0);
429+
ptrStack.push(0);
434430
}
435-
tmpParamInts.push(isSome);
431+
i32Stack.push(isSome);
436432
}
437-
tmpParamInts.push(entries.length);
433+
i32Stack.push(entries.length);
438434
instance.exports.bjs_optionalBoxDictionary();
439-
const dictLen = tmpRetInts.pop();
435+
const dictLen = i32Stack.pop();
440436
const dictResult = {};
441437
for (let i = 0; i < dictLen; i++) {
442-
const isSome1 = tmpRetInts.pop();
438+
const isSome1 = i32Stack.pop();
443439
let optValue;
444440
if (isSome1 === 0) {
445441
optValue = null;
446442
} else {
447-
const ptr = tmpRetPointers.pop();
443+
const ptr = ptrStack.pop();
448444
const obj = _exports['Box'].__construct(ptr);
449445
optValue = obj;
450446
}
451-
const string = tmpRetStrings.pop();
447+
const string = strStack.pop();
452448
dictResult[string] = optValue;
453449
}
454450
for (const cleanup of arrayCleanups) { cleanup(); }

0 commit comments

Comments
 (0)