Skip to content

Commit d736b37

Browse files
committed
NFC: BridgeJS: Refactor JSGlueGen with compositional optional handling and helper consolidation
1 parent 8bbf832 commit d736b37

21 files changed

+2404
-2863
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ public struct BridgeJSLink {
10481048
for skeleton in skeletons.compactMap(\.exported) {
10491049
for enumDef in skeleton.enums where enumDef.enumType == .associatedValue {
10501050
printer.write(
1051-
"const \(enumDef.name)Helpers = __bjs_create\(enumDef.valuesName)Helpers()();"
1051+
"const \(enumDef.name)Helpers = __bjs_create\(enumDef.valuesName)Helpers();"
10521052
)
10531053
printer.write("\(JSGlueVariableScope.reservedEnumHelpers).\(enumDef.name) = \(enumDef.name)Helpers;")
10541054
printer.nextLine()
@@ -1064,7 +1064,7 @@ public struct BridgeJSLink {
10641064
for skeleton in skeletons.compactMap(\.exported) {
10651065
for structDef in skeleton.structs {
10661066
printer.write(
1067-
"const \(structDef.name)Helpers = __bjs_create\(structDef.name)Helpers()();"
1067+
"const \(structDef.name)Helpers = __bjs_create\(structDef.name)Helpers();"
10681068
)
10691069
printer.write(
10701070
"\(JSGlueVariableScope.reservedStructHelpers).\(structDef.name) = \(structDef.name)Helpers;"

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 1153 additions & 1517 deletions
Large diffs are not rendered by default.

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,6 @@ extension BridgeType {
12081208
}
12091209

12101210
extension WasmCoreType {
1211-
/// Returns a Swift statement that returns a placeholder value for this Wasm core type.
12121211
public var swiftReturnPlaceholderStmt: String {
12131212
switch self {
12141213
case .i32: return "return 0"

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,17 @@ export async function createInstantiator(options, swift) {
4141

4242
let _exports = null;
4343
let bjs = null;
44-
const __bjs_createPointHelpers = () => {
45-
return () => ({
46-
lower: (value) => {
47-
f64Stack.push(value.x);
48-
f64Stack.push(value.y);
49-
},
50-
lift: () => {
51-
const f64 = f64Stack.pop();
52-
const f641 = f64Stack.pop();
53-
return { x: f641, y: f64 };
54-
}
55-
});
56-
};
44+
const __bjs_createPointHelpers = () => ({
45+
lower: (value) => {
46+
f64Stack.push(value.x);
47+
f64Stack.push(value.y);
48+
},
49+
lift: () => {
50+
const f64 = f64Stack.pop();
51+
const f641 = f64Stack.pop();
52+
return { x: f641, y: f64 };
53+
}
54+
});
5755

5856
return {
5957
/**
@@ -370,7 +368,7 @@ export async function createInstantiator(options, swift) {
370368
}
371369

372370
}
373-
const PointHelpers = __bjs_createPointHelpers()();
371+
const PointHelpers = __bjs_createPointHelpers();
374372
structHelpers.Point = PointHelpers;
375373

376374
const exports = {
@@ -638,7 +636,6 @@ export async function createInstantiator(options, swift) {
638636
const isSome = elem != null ? 1 : 0;
639637
if (isSome) {
640638
structHelpers.Point.lower(elem);
641-
} else {
642639
}
643640
i32Stack.push(isSome);
644641
}

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

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,42 @@ export async function createInstantiator(options, swift) {
3434

3535
let _exports = null;
3636
let bjs = null;
37-
const __bjs_createConfigHelpers = () => {
38-
return () => ({
39-
lower: (value) => {
40-
const bytes = textEncoder.encode(value.name);
41-
const id = swift.memory.retain(bytes);
42-
i32Stack.push(bytes.length);
43-
i32Stack.push(id);
44-
i32Stack.push((value.value | 0));
45-
i32Stack.push(value.enabled ? 1 : 0);
46-
},
47-
lift: () => {
48-
const bool = i32Stack.pop() !== 0;
49-
const int = i32Stack.pop();
50-
const string = strStack.pop();
51-
return { name: string, value: int, enabled: bool };
52-
}
53-
});
54-
};
55-
const __bjs_createMathOperationsHelpers = () => {
56-
return () => ({
57-
lower: (value) => {
58-
f64Stack.push(value.baseValue);
59-
},
60-
lift: () => {
61-
const f64 = f64Stack.pop();
62-
const instance1 = { baseValue: f64 };
63-
instance1.add = function(a, b = 10.0) {
64-
structHelpers.MathOperations.lower(this);
65-
const ret = instance.exports.bjs_MathOperations_add(a, b);
66-
return ret;
67-
}.bind(instance1);
68-
instance1.multiply = function(a, b) {
69-
structHelpers.MathOperations.lower(this);
70-
const ret = instance.exports.bjs_MathOperations_multiply(a, b);
71-
return ret;
72-
}.bind(instance1);
73-
return instance1;
74-
}
75-
});
76-
};
37+
const __bjs_createConfigHelpers = () => ({
38+
lower: (value) => {
39+
const bytes = textEncoder.encode(value.name);
40+
const id = swift.memory.retain(bytes);
41+
i32Stack.push(bytes.length);
42+
i32Stack.push(id);
43+
i32Stack.push((value.value | 0));
44+
i32Stack.push(value.enabled ? 1 : 0);
45+
},
46+
lift: () => {
47+
const bool = i32Stack.pop() !== 0;
48+
const int = i32Stack.pop();
49+
const string = strStack.pop();
50+
return { name: string, value: int, enabled: bool };
51+
}
52+
});
53+
const __bjs_createMathOperationsHelpers = () => ({
54+
lower: (value) => {
55+
f64Stack.push(value.baseValue);
56+
},
57+
lift: () => {
58+
const f64 = f64Stack.pop();
59+
const instance1 = { baseValue: f64 };
60+
instance1.add = function(a, b = 10.0) {
61+
structHelpers.MathOperations.lower(this);
62+
const ret = instance.exports.bjs_MathOperations_add(a, b);
63+
return ret;
64+
}.bind(instance1);
65+
instance1.multiply = function(a, b) {
66+
structHelpers.MathOperations.lower(this);
67+
const ret = instance.exports.bjs_MathOperations_multiply(a, b);
68+
return ret;
69+
}.bind(instance1);
70+
return instance1;
71+
}
72+
});
7773

7874
return {
7975
/**
@@ -343,7 +339,7 @@ export async function createInstantiator(options, swift) {
343339
const nameBytes = textEncoder.encode(name);
344340
const nameId = swift.memory.retain(nameBytes);
345341
const isSome = tag != null;
346-
let tagId, tagBytes;
342+
let tagBytes, tagId;
347343
if (isSome) {
348344
tagBytes = textEncoder.encode(tag);
349345
tagId = swift.memory.retain(tagBytes);
@@ -391,18 +387,18 @@ export async function createInstantiator(options, swift) {
391387
}
392388
set tag(value) {
393389
const isSome = value != null;
394-
let valueId, valueBytes;
390+
let valueBytes, valueId;
395391
if (isSome) {
396392
valueBytes = textEncoder.encode(value);
397393
valueId = swift.memory.retain(valueBytes);
398394
}
399395
instance.exports.bjs_ConstructorDefaults_tag_set(this.pointer, +isSome, isSome ? valueId : 0, isSome ? valueBytes.length : 0);
400396
}
401397
}
402-
const ConfigHelpers = __bjs_createConfigHelpers()();
398+
const ConfigHelpers = __bjs_createConfigHelpers();
403399
structHelpers.Config = ConfigHelpers;
404400

405-
const MathOperationsHelpers = __bjs_createMathOperationsHelpers()();
401+
const MathOperationsHelpers = __bjs_createMathOperationsHelpers();
406402
structHelpers.MathOperations = MathOperationsHelpers;
407403

408404
const exports = {
@@ -435,7 +431,7 @@ export async function createInstantiator(options, swift) {
435431
},
436432
testOptionalDefault: function bjs_testOptionalDefault(name = null) {
437433
const isSome = name != null;
438-
let nameId, nameBytes;
434+
let nameBytes, nameId;
439435
if (isSome) {
440436
nameBytes = textEncoder.encode(name);
441437
nameId = swift.memory.retain(nameBytes);
@@ -447,7 +443,7 @@ export async function createInstantiator(options, swift) {
447443
},
448444
testOptionalStringDefault: function bjs_testOptionalStringDefault(greeting = "Hi") {
449445
const isSome = greeting != null;
450-
let greetingId, greetingBytes;
446+
let greetingBytes, greetingId;
451447
if (isSome) {
452448
greetingBytes = textEncoder.encode(greeting);
453449
greetingId = swift.memory.retain(greetingBytes);
@@ -485,12 +481,7 @@ export async function createInstantiator(options, swift) {
485481
i32Stack.push(+isSome);
486482
instance.exports.bjs_testOptionalStructDefault();
487483
const isSome1 = i32Stack.pop();
488-
let optResult;
489-
if (isSome1) {
490-
optResult = structHelpers.Config.lift();
491-
} else {
492-
optResult = null;
493-
}
484+
const optResult = isSome1 ? structHelpers.Config.lift() : null;
494485
return optResult;
495486
},
496487
testOptionalStructWithValueDefault: function bjs_testOptionalStructWithValueDefault(point = { name: "default", value: 42, enabled: true }) {
@@ -501,12 +492,7 @@ export async function createInstantiator(options, swift) {
501492
i32Stack.push(+isSome);
502493
instance.exports.bjs_testOptionalStructWithValueDefault();
503494
const isSome1 = i32Stack.pop();
504-
let optResult;
505-
if (isSome1) {
506-
optResult = structHelpers.Config.lift();
507-
} else {
508-
optResult = null;
509-
}
495+
const optResult = isSome1 ? structHelpers.Config.lift() : null;
510496
return optResult;
511497
},
512498
testIntArrayDefault: function bjs_testIntArrayDefault(values = [1, 2, 3]) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,17 @@ export async function createInstantiator(options, swift) {
290290
},
291291
optionalDictionary: function bjs_optionalDictionary(values) {
292292
const isSome = values != null;
293+
let entries, [key, value], bytes, id, bytes1, id1;
293294
if (isSome) {
294-
const entries = Object.entries(values);
295+
entries = Object.entries(values);
295296
for (const entry of entries) {
296-
const [key, value] = entry;
297-
const bytes = textEncoder.encode(key);
298-
const id = swift.memory.retain(bytes);
297+
[key, value] = entry;
298+
bytes = textEncoder.encode(key);
299+
id = swift.memory.retain(bytes);
299300
i32Stack.push(bytes.length);
300301
i32Stack.push(id);
301-
const bytes1 = textEncoder.encode(value);
302-
const id1 = swift.memory.retain(bytes1);
302+
bytes1 = textEncoder.encode(value);
303+
id1 = swift.memory.retain(bytes1);
303304
i32Stack.push(bytes1.length);
304305
i32Stack.push(id1);
305306
}

0 commit comments

Comments
 (0)