Skip to content

Commit d659994

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

20 files changed

+2738
-2965
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: 1145 additions & 1521 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: 80 additions & 70 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,12 +339,18 @@ 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 result;
343+
let result1;
347344
if (isSome) {
348-
tagBytes = textEncoder.encode(tag);
349-
tagId = swift.memory.retain(tagBytes);
345+
const tagBytes = textEncoder.encode(tag);
346+
const tagId = swift.memory.retain(tagBytes);
347+
result = tagId;
348+
result1 = tagBytes.length;
349+
} else {
350+
result = 0;
351+
result1 = 0;
350352
}
351-
const ret = instance.exports.bjs_ConstructorDefaults_init(nameId, nameBytes.length, count, enabled, status, +isSome, isSome ? tagId : 0, isSome ? tagBytes.length : 0);
353+
const ret = instance.exports.bjs_ConstructorDefaults_init(nameId, nameBytes.length, count, enabled, status, +isSome, result, result1);
352354
return ConstructorDefaults.__construct(ret);
353355
}
354356
get name() {
@@ -391,18 +393,24 @@ export async function createInstantiator(options, swift) {
391393
}
392394
set tag(value) {
393395
const isSome = value != null;
394-
let valueId, valueBytes;
396+
let result;
397+
let result1;
395398
if (isSome) {
396-
valueBytes = textEncoder.encode(value);
397-
valueId = swift.memory.retain(valueBytes);
399+
const valueBytes = textEncoder.encode(value);
400+
const valueId = swift.memory.retain(valueBytes);
401+
result = valueId;
402+
result1 = valueBytes.length;
403+
} else {
404+
result = 0;
405+
result1 = 0;
398406
}
399-
instance.exports.bjs_ConstructorDefaults_tag_set(this.pointer, +isSome, isSome ? valueId : 0, isSome ? valueBytes.length : 0);
407+
instance.exports.bjs_ConstructorDefaults_tag_set(this.pointer, +isSome, result, result1);
400408
}
401409
}
402-
const ConfigHelpers = __bjs_createConfigHelpers()();
410+
const ConfigHelpers = __bjs_createConfigHelpers();
403411
structHelpers.Config = ConfigHelpers;
404412

405-
const MathOperationsHelpers = __bjs_createMathOperationsHelpers()();
413+
const MathOperationsHelpers = __bjs_createMathOperationsHelpers();
406414
structHelpers.MathOperations = MathOperationsHelpers;
407415

408416
const exports = {
@@ -435,24 +443,36 @@ export async function createInstantiator(options, swift) {
435443
},
436444
testOptionalDefault: function bjs_testOptionalDefault(name = null) {
437445
const isSome = name != null;
438-
let nameId, nameBytes;
446+
let result;
447+
let result1;
439448
if (isSome) {
440-
nameBytes = textEncoder.encode(name);
441-
nameId = swift.memory.retain(nameBytes);
449+
const nameBytes = textEncoder.encode(name);
450+
const nameId = swift.memory.retain(nameBytes);
451+
result = nameId;
452+
result1 = nameBytes.length;
453+
} else {
454+
result = 0;
455+
result1 = 0;
442456
}
443-
instance.exports.bjs_testOptionalDefault(+isSome, isSome ? nameId : 0, isSome ? nameBytes.length : 0);
457+
instance.exports.bjs_testOptionalDefault(+isSome, result, result1);
444458
const optResult = tmpRetString;
445459
tmpRetString = undefined;
446460
return optResult;
447461
},
448462
testOptionalStringDefault: function bjs_testOptionalStringDefault(greeting = "Hi") {
449463
const isSome = greeting != null;
450-
let greetingId, greetingBytes;
464+
let result;
465+
let result1;
451466
if (isSome) {
452-
greetingBytes = textEncoder.encode(greeting);
453-
greetingId = swift.memory.retain(greetingBytes);
467+
const greetingBytes = textEncoder.encode(greeting);
468+
const greetingId = swift.memory.retain(greetingBytes);
469+
result = greetingId;
470+
result1 = greetingBytes.length;
471+
} else {
472+
result = 0;
473+
result1 = 0;
454474
}
455-
instance.exports.bjs_testOptionalStringDefault(+isSome, isSome ? greetingId : 0, isSome ? greetingBytes.length : 0);
475+
instance.exports.bjs_testOptionalStringDefault(+isSome, result, result1);
456476
const optResult = tmpRetString;
457477
tmpRetString = undefined;
458478
return optResult;
@@ -485,12 +505,7 @@ export async function createInstantiator(options, swift) {
485505
i32Stack.push(+isSome);
486506
instance.exports.bjs_testOptionalStructDefault();
487507
const isSome1 = i32Stack.pop();
488-
let optResult;
489-
if (isSome1) {
490-
optResult = structHelpers.Config.lift();
491-
} else {
492-
optResult = null;
493-
}
508+
const optResult = isSome1 ? structHelpers.Config.lift() : null;
494509
return optResult;
495510
},
496511
testOptionalStructWithValueDefault: function bjs_testOptionalStructWithValueDefault(point = { name: "default", value: 42, enabled: true }) {
@@ -501,12 +516,7 @@ export async function createInstantiator(options, swift) {
501516
i32Stack.push(+isSome);
502517
instance.exports.bjs_testOptionalStructWithValueDefault();
503518
const isSome1 = i32Stack.pop();
504-
let optResult;
505-
if (isSome1) {
506-
optResult = structHelpers.Config.lift();
507-
} else {
508-
optResult = null;
509-
}
519+
const optResult = isSome1 ? structHelpers.Config.lift() : null;
510520
return optResult;
511521
},
512522
testIntArrayDefault: function bjs_testIntArrayDefault(values = [1, 2, 3]) {

0 commit comments

Comments
 (0)