Skip to content

Commit 6fc3f01

Browse files
committed
factored out decodeString global
1 parent e0a9b3b commit 6fc3f01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+336
-444
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public struct BridgeJSLink {
289289
"let \(JSGlueVariableScope.reservedInstance);",
290290
"let \(JSGlueVariableScope.reservedMemory);",
291291
"let \(JSGlueVariableScope.reservedSetException);",
292+
"let \(JSGlueVariableScope.reservedDecodeString);",
292293
"const \(JSGlueVariableScope.reservedTextDecoder) = new TextDecoder(\"utf-8\");",
293294
"const \(JSGlueVariableScope.reservedTextEncoder) = new TextEncoder(\"utf-8\");",
294295
"let \(JSGlueVariableScope.reservedStorageToReturnString);",
@@ -337,10 +338,7 @@ public struct BridgeJSLink {
337338
printer.write("bjs[\"swift_js_return_string\"] = function(ptr, len) {")
338339
printer.indent {
339340
printer.write(
340-
"const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len)\(sharedMemory ? ".slice()" : "");"
341-
)
342-
printer.write(
343-
"\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedTextDecoder).decode(bytes);"
341+
"\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedDecodeString)(ptr, len);"
344342
)
345343
}
346344
printer.write("}")
@@ -361,10 +359,7 @@ public struct BridgeJSLink {
361359
printer.write("bjs[\"swift_js_make_js_string\"] = function(ptr, len) {")
362360
printer.indent {
363361
printer.write(
364-
"const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len)\(sharedMemory ? ".slice()" : "");"
365-
)
366-
printer.write(
367-
"return \(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).retain(\(JSGlueVariableScope.reservedTextDecoder).decode(bytes));"
362+
"return \(JSGlueVariableScope.reservedSwift).\(JSGlueVariableScope.reservedMemory).retain(\(JSGlueVariableScope.reservedDecodeString)(ptr, len));"
368363
)
369364
}
370365
printer.write("}")
@@ -413,10 +408,7 @@ public struct BridgeJSLink {
413408
printer.write("}")
414409
printer.write("bjs[\"swift_js_push_string\"] = function(ptr, len) {")
415410
printer.indent {
416-
printer.write(
417-
"const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len)\(sharedMemory ? ".slice()" : "");"
418-
)
419-
printer.write("const value = \(JSGlueVariableScope.reservedTextDecoder).decode(bytes);")
411+
printer.write("const value = \(JSGlueVariableScope.reservedDecodeString)(ptr, len);")
420412
printer.write("\(JSGlueVariableScope.reservedStringStack).push(value);")
421413
}
422414
printer.write("}")
@@ -529,8 +521,7 @@ public struct BridgeJSLink {
529521
printer.write("} else {")
530522
printer.indent {
531523
printer.write(lines: [
532-
"const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr, len);",
533-
"\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedTextDecoder).decode(bytes);",
524+
"\(JSGlueVariableScope.reservedStorageToReturnString) = \(JSGlueVariableScope.reservedDecodeString)(ptr, len);"
534525
])
535526
}
536527
printer.write("}")
@@ -1039,6 +1030,16 @@ public struct BridgeJSLink {
10391030
"\(JSGlueVariableScope.reservedMemory) = \(JSGlueVariableScope.reservedInstance).exports.memory;",
10401031
])
10411032
printer.nextLine()
1033+
if sharedMemory {
1034+
printer.write(
1035+
"\(JSGlueVariableScope.reservedDecodeString) = (ptr, len) => { const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr >>> 0, len >>> 0).slice(); return \(JSGlueVariableScope.reservedTextDecoder).decode(bytes); }"
1036+
)
1037+
} else {
1038+
printer.write(
1039+
"\(JSGlueVariableScope.reservedDecodeString) = (ptr, len) => { const bytes = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, ptr >>> 0, len >>> 0); return \(JSGlueVariableScope.reservedTextDecoder).decode(bytes); }"
1040+
)
1041+
}
1042+
printer.nextLine()
10421043
// Error handling
10431044
printer.write("\(JSGlueVariableScope.reservedSetException) = (error) => {")
10441045
printer.indent {

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class JSGlueVariableScope {
1313
static let reservedInstance = "instance"
1414
static let reservedMemory = "memory"
1515
static let reservedSetException = "setException"
16+
static let reservedDecodeString = "decodeString"
1617
static let reservedStorageToReturnString = "tmpRetString"
1718
static let reservedStorageToReturnBytes = "tmpRetBytes"
1819
static let reservedStorageToReturnException = "tmpRetException"
@@ -40,6 +41,7 @@ final class JSGlueVariableScope {
4041
reservedInstance,
4142
reservedMemory,
4243
reservedSetException,
44+
reservedDecodeString,
4345
reservedStorageToReturnString,
4446
reservedStorageToReturnBytes,
4547
reservedStorageToReturnException,
@@ -266,17 +268,9 @@ struct IntrinsicJSFragment: Sendable {
266268
let (scope, printer) = (context.scope, context.printer)
267269
let bytesExpr = arguments[0]
268270
let countExpr = arguments[1]
269-
let bytesLabel = scope.variable("bytesView")
270-
let bytesToDecodeLabel = scope.variable("bytesToDecode")
271271
let stringLabel = scope.variable("string")
272272
printer.write(
273-
"const \(bytesLabel) = new Uint8Array(\(JSGlueVariableScope.reservedMemory).buffer, \(bytesExpr), \(countExpr));"
274-
)
275-
printer.write(
276-
"const \(bytesToDecodeLabel) = (typeof SharedArrayBuffer !== \"undefined\" && \(bytesLabel).buffer instanceof SharedArrayBuffer) ? \(bytesLabel).slice() : \(bytesLabel);"
277-
)
278-
printer.write(
279-
"const \(stringLabel) = \(JSGlueVariableScope.reservedTextDecoder).decode(\(bytesToDecodeLabel));"
273+
"const \(stringLabel) = \(JSGlueVariableScope.reservedDecodeString)(\(bytesExpr), \(countExpr));"
280274
)
281275
return [stringLabel]
282276
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function createInstantiator(options, swift) {
2121
let instance;
2222
let memory;
2323
let setException;
24+
let decodeString;
2425
const textDecoder = new TextDecoder("utf-8");
2526
const textEncoder = new TextEncoder("utf-8");
2627
let tmpRetString;
@@ -62,8 +63,7 @@ export async function createInstantiator(options, swift) {
6263
importObject["bjs"] = bjs;
6364
const imports = options.getImports(importsContext);
6465
bjs["swift_js_return_string"] = function(ptr, len) {
65-
const bytes = new Uint8Array(memory.buffer, ptr, len);
66-
tmpRetString = textDecoder.decode(bytes);
66+
tmpRetString = decodeString(ptr, len);
6767
}
6868
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
6969
const source = swift.memory.getObject(sourceId);
@@ -72,8 +72,7 @@ export async function createInstantiator(options, swift) {
7272
bytes.set(source);
7373
}
7474
bjs["swift_js_make_js_string"] = function(ptr, len) {
75-
const bytes = new Uint8Array(memory.buffer, ptr, len);
76-
return swift.memory.retain(textDecoder.decode(bytes));
75+
return swift.memory.retain(decodeString(ptr, len));
7776
}
7877
bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
7978
const target = new Uint8Array(memory.buffer, ptr, len);
@@ -99,8 +98,7 @@ export async function createInstantiator(options, swift) {
9998
f64Stack.push(v);
10099
}
101100
bjs["swift_js_push_string"] = function(ptr, len) {
102-
const bytes = new Uint8Array(memory.buffer, ptr, len);
103-
const value = textDecoder.decode(bytes);
101+
const value = decodeString(ptr, len);
104102
strStack.push(value);
105103
}
106104
bjs["swift_js_pop_i32"] = function() {
@@ -157,8 +155,7 @@ export async function createInstantiator(options, swift) {
157155
if (isSome === 0) {
158156
tmpRetString = null;
159157
} else {
160-
const bytes = new Uint8Array(memory.buffer, ptr, len);
161-
tmpRetString = textDecoder.decode(bytes);
158+
tmpRetString = decodeString(ptr, len);
162159
}
163160
}
164161
bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
@@ -330,6 +327,8 @@ export async function createInstantiator(options, swift) {
330327
instance = i;
331328
memory = instance.exports.memory;
332329

330+
decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); }
331+
333332
setException = (error) => {
334333
instance.exports._swift_js_exception.value = swift.memory.retain(error)
335334
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) {
88
let instance;
99
let memory;
1010
let setException;
11+
let decodeString;
1112
const textDecoder = new TextDecoder("utf-8");
1213
const textEncoder = new TextEncoder("utf-8");
1314
let tmpRetString;
@@ -37,8 +38,7 @@ export async function createInstantiator(options, swift) {
3738
bjs = {};
3839
importObject["bjs"] = bjs;
3940
bjs["swift_js_return_string"] = function(ptr, len) {
40-
const bytes = new Uint8Array(memory.buffer, ptr, len);
41-
tmpRetString = textDecoder.decode(bytes);
41+
tmpRetString = decodeString(ptr, len);
4242
}
4343
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
4444
const source = swift.memory.getObject(sourceId);
@@ -47,8 +47,7 @@ export async function createInstantiator(options, swift) {
4747
bytes.set(source);
4848
}
4949
bjs["swift_js_make_js_string"] = function(ptr, len) {
50-
const bytes = new Uint8Array(memory.buffer, ptr, len);
51-
return swift.memory.retain(textDecoder.decode(bytes));
50+
return swift.memory.retain(decodeString(ptr, len));
5251
}
5352
bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
5453
const target = new Uint8Array(memory.buffer, ptr, len);
@@ -74,8 +73,7 @@ export async function createInstantiator(options, swift) {
7473
f64Stack.push(v);
7574
}
7675
bjs["swift_js_push_string"] = function(ptr, len) {
77-
const bytes = new Uint8Array(memory.buffer, ptr, len);
78-
const value = textDecoder.decode(bytes);
76+
const value = decodeString(ptr, len);
7977
strStack.push(value);
8078
}
8179
bjs["swift_js_pop_i32"] = function() {
@@ -125,8 +123,7 @@ export async function createInstantiator(options, swift) {
125123
if (isSome === 0) {
126124
tmpRetString = null;
127125
} else {
128-
const bytes = new Uint8Array(memory.buffer, ptr, len);
129-
tmpRetString = textDecoder.decode(bytes);
126+
tmpRetString = decodeString(ptr, len);
130127
}
131128
}
132129
bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
@@ -189,6 +186,8 @@ export async function createInstantiator(options, swift) {
189186
instance = i;
190187
memory = instance.exports.memory;
191188

189+
decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); }
190+
192191
setException = (error) => {
193192
instance.exports._swift_js_exception.value = swift.memory.retain(error)
194193
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export async function createInstantiator(options, swift) {
1414
let instance;
1515
let memory;
1616
let setException;
17+
let decodeString;
1718
const textDecoder = new TextDecoder("utf-8");
1819
const textEncoder = new TextEncoder("utf-8");
1920
let tmpRetString;
@@ -79,8 +80,7 @@ export async function createInstantiator(options, swift) {
7980
bjs = {};
8081
importObject["bjs"] = bjs;
8182
bjs["swift_js_return_string"] = function(ptr, len) {
82-
const bytes = new Uint8Array(memory.buffer, ptr, len);
83-
tmpRetString = textDecoder.decode(bytes);
83+
tmpRetString = decodeString(ptr, len);
8484
}
8585
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
8686
const source = swift.memory.getObject(sourceId);
@@ -89,8 +89,7 @@ export async function createInstantiator(options, swift) {
8989
bytes.set(source);
9090
}
9191
bjs["swift_js_make_js_string"] = function(ptr, len) {
92-
const bytes = new Uint8Array(memory.buffer, ptr, len);
93-
return swift.memory.retain(textDecoder.decode(bytes));
92+
return swift.memory.retain(decodeString(ptr, len));
9493
}
9594
bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
9695
const target = new Uint8Array(memory.buffer, ptr, len);
@@ -116,8 +115,7 @@ export async function createInstantiator(options, swift) {
116115
f64Stack.push(v);
117116
}
118117
bjs["swift_js_push_string"] = function(ptr, len) {
119-
const bytes = new Uint8Array(memory.buffer, ptr, len);
120-
const value = textDecoder.decode(bytes);
118+
const value = decodeString(ptr, len);
121119
strStack.push(value);
122120
}
123121
bjs["swift_js_pop_i32"] = function() {
@@ -181,8 +179,7 @@ export async function createInstantiator(options, swift) {
181179
if (isSome === 0) {
182180
tmpRetString = null;
183181
} else {
184-
const bytes = new Uint8Array(memory.buffer, ptr, len);
185-
tmpRetString = textDecoder.decode(bytes);
182+
tmpRetString = decodeString(ptr, len);
186183
}
187184
}
188185
bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
@@ -261,6 +258,8 @@ export async function createInstantiator(options, swift) {
261258
instance = i;
262259
memory = instance.exports.memory;
263260

261+
decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); }
262+
264263
setException = (error) => {
265264
instance.exports._swift_js_exception.value = swift.memory.retain(error)
266265
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export async function createInstantiator(options, swift) {
88
let instance;
99
let memory;
1010
let setException;
11+
let decodeString;
1112
const textDecoder = new TextDecoder("utf-8");
1213
const textEncoder = new TextEncoder("utf-8");
1314
let tmpRetString;
@@ -38,8 +39,7 @@ export async function createInstantiator(options, swift) {
3839
importObject["bjs"] = bjs;
3940
const imports = options.getImports(importsContext);
4041
bjs["swift_js_return_string"] = function(ptr, len) {
41-
const bytes = new Uint8Array(memory.buffer, ptr, len);
42-
tmpRetString = textDecoder.decode(bytes);
42+
tmpRetString = decodeString(ptr, len);
4343
}
4444
bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
4545
const source = swift.memory.getObject(sourceId);
@@ -48,8 +48,7 @@ export async function createInstantiator(options, swift) {
4848
bytes.set(source);
4949
}
5050
bjs["swift_js_make_js_string"] = function(ptr, len) {
51-
const bytes = new Uint8Array(memory.buffer, ptr, len);
52-
return swift.memory.retain(textDecoder.decode(bytes));
51+
return swift.memory.retain(decodeString(ptr, len));
5352
}
5453
bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
5554
const target = new Uint8Array(memory.buffer, ptr, len);
@@ -75,8 +74,7 @@ export async function createInstantiator(options, swift) {
7574
f64Stack.push(v);
7675
}
7776
bjs["swift_js_push_string"] = function(ptr, len) {
78-
const bytes = new Uint8Array(memory.buffer, ptr, len);
79-
const value = textDecoder.decode(bytes);
77+
const value = decodeString(ptr, len);
8078
strStack.push(value);
8179
}
8280
bjs["swift_js_pop_i32"] = function() {
@@ -126,8 +124,7 @@ export async function createInstantiator(options, swift) {
126124
if (isSome === 0) {
127125
tmpRetString = null;
128126
} else {
129-
const bytes = new Uint8Array(memory.buffer, ptr, len);
130-
tmpRetString = textDecoder.decode(bytes);
127+
tmpRetString = decodeString(ptr, len);
131128
}
132129
}
133130
bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
@@ -223,6 +220,8 @@ export async function createInstantiator(options, swift) {
223220
instance = i;
224221
memory = instance.exports.memory;
225222

223+
decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); }
224+
226225
setException = (error) => {
227226
instance.exports._swift_js_exception.value = swift.memory.retain(error)
228227
}

0 commit comments

Comments
 (0)