Skip to content

Commit 7906050

Browse files
Adjusted imported-property JS glue to prefer dot access when the property name is a normal identifier, falling back to bracket access only when needed.
- Implementation: `Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift` - Updated snapshots (including `Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.Import.js:215`) - `swift test --package-path ./Plugins/BridgeJS` passes
1 parent ed57d5e commit 7906050

File tree

6 files changed

+53
-46
lines changed

6 files changed

+53
-46
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,9 +2168,8 @@ extension BridgeJSLink {
21682168
}
21692169

21702170
func callPropertyGetter(name: String, returnType: BridgeType) throws -> String? {
2171-
let escapedName = BridgeJSLink.escapeForJavaScriptStringLiteral(name)
2172-
let accessExpr =
2173-
"\(JSGlueVariableScope.reservedSwift).memory.getObject(self)[\"\(escapedName)\"]"
2171+
let objectExpr = "\(JSGlueVariableScope.reservedSwift).memory.getObject(self)"
2172+
let accessExpr = Self.propertyAccessExpr(objectExpr: objectExpr, propertyName: name)
21742173
if context == .exportSwift, returnType.usesSideChannelForOptionalReturn() {
21752174
guard case .optional(let wrappedType) = returnType else {
21762175
fatalError("usesSideChannelForOptionalReturn returned true for non-optional type")
@@ -2194,9 +2193,9 @@ extension BridgeJSLink {
21942193
}
21952194

21962195
func callPropertySetter(name: String, returnType: BridgeType) {
2197-
let escapedName = BridgeJSLink.escapeForJavaScriptStringLiteral(name)
2198-
let call =
2199-
"\(JSGlueVariableScope.reservedSwift).memory.getObject(self)[\"\(escapedName)\"] = \(parameterForwardings.joined(separator: ", "))"
2196+
let objectExpr = "\(JSGlueVariableScope.reservedSwift).memory.getObject(self)"
2197+
let accessExpr = Self.propertyAccessExpr(objectExpr: objectExpr, propertyName: name)
2198+
let call = "\(accessExpr) = \(parameterForwardings.joined(separator: ", "))"
22002199
body.write("\(call);")
22012200
}
22022201

@@ -2236,6 +2235,14 @@ extension BridgeJSLink {
22362235
assert(loweredValues.count <= 1, "Lowering fragment should produce at most one value")
22372236
return loweredValues.first
22382237
}
2238+
2239+
private static func propertyAccessExpr(objectExpr: String, propertyName: String) -> String {
2240+
if propertyName.range(of: #"^[$A-Z_][0-9A-Z_$]*$"#, options: [.regularExpression, .caseInsensitive]) != nil {
2241+
return "\(objectExpr).\(propertyName)"
2242+
}
2243+
let escapedName = BridgeJSLink.escapeForJavaScriptStringLiteral(propertyName)
2244+
return "\(objectExpr)[\"\(escapedName)\"]"
2245+
}
22392246
}
22402247

22412248
class ImportObjectBuilder {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/InvalidPropertyNames.Import.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export async function createInstantiator(options, swift) {
212212
}
213213
TestModule["bjs_ArrayBufferLike_byteLength_get"] = function bjs_ArrayBufferLike_byteLength_get(self) {
214214
try {
215-
let ret = swift.memory.getObject(self)["byteLength"];
215+
let ret = swift.memory.getObject(self).byteLength;
216216
return ret;
217217
} catch (error) {
218218
setException(error);
@@ -230,7 +230,7 @@ export async function createInstantiator(options, swift) {
230230
}
231231
TestModule["bjs_WeirdNaming_normalProperty_get"] = function bjs_WeirdNaming_normalProperty_get(self) {
232232
try {
233-
let ret = swift.memory.getObject(self)["normalProperty"];
233+
let ret = swift.memory.getObject(self).normalProperty;
234234
tmpRetBytes = textEncoder.encode(ret);
235235
return tmpRetBytes.length;
236236
} catch (error) {
@@ -275,7 +275,7 @@ export async function createInstantiator(options, swift) {
275275
}
276276
TestModule["bjs_WeirdNaming_constructor_get"] = function bjs_WeirdNaming_constructor_get(self) {
277277
try {
278-
let ret = swift.memory.getObject(self)["constructor"];
278+
let ret = swift.memory.getObject(self).constructor;
279279
tmpRetBytes = textEncoder.encode(ret);
280280
return tmpRetBytes.length;
281281
} catch (error) {
@@ -284,7 +284,7 @@ export async function createInstantiator(options, swift) {
284284
}
285285
TestModule["bjs_WeirdNaming_for_get"] = function bjs_WeirdNaming_for_get(self) {
286286
try {
287-
let ret = swift.memory.getObject(self)["for"];
287+
let ret = swift.memory.getObject(self).for;
288288
tmpRetBytes = textEncoder.encode(ret);
289289
return tmpRetBytes.length;
290290
} catch (error) {
@@ -293,7 +293,7 @@ export async function createInstantiator(options, swift) {
293293
}
294294
TestModule["bjs_WeirdNaming_Any_get"] = function bjs_WeirdNaming_Any_get(self) {
295295
try {
296-
let ret = swift.memory.getObject(self)["Any"];
296+
let ret = swift.memory.getObject(self).Any;
297297
tmpRetBytes = textEncoder.encode(ret);
298298
return tmpRetBytes.length;
299299
} catch (error) {
@@ -304,7 +304,7 @@ export async function createInstantiator(options, swift) {
304304
try {
305305
const newValueObject = swift.memory.getObject(newValue);
306306
swift.memory.release(newValue);
307-
swift.memory.getObject(self)["normalProperty"] = newValueObject;
307+
swift.memory.getObject(self).normalProperty = newValueObject;
308308
} catch (error) {
309309
setException(error);
310310
}
@@ -343,7 +343,7 @@ export async function createInstantiator(options, swift) {
343343
try {
344344
const newValueObject = swift.memory.getObject(newValue);
345345
swift.memory.release(newValue);
346-
swift.memory.getObject(self)["constructor"] = newValueObject;
346+
swift.memory.getObject(self).constructor = newValueObject;
347347
} catch (error) {
348348
setException(error);
349349
}
@@ -352,7 +352,7 @@ export async function createInstantiator(options, swift) {
352352
try {
353353
const newValueObject = swift.memory.getObject(newValue);
354354
swift.memory.release(newValue);
355-
swift.memory.getObject(self)["for"] = newValueObject;
355+
swift.memory.getObject(self).for = newValueObject;
356356
} catch (error) {
357357
setException(error);
358358
}
@@ -361,7 +361,7 @@ export async function createInstantiator(options, swift) {
361361
try {
362362
const newValueObject = swift.memory.getObject(newValue);
363363
swift.memory.release(newValue);
364-
swift.memory.getObject(self)["Any"] = newValueObject;
364+
swift.memory.getObject(self).Any = newValueObject;
365365
} catch (error) {
366366
setException(error);
367367
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/MultipleImportedTypes.Import.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export async function createInstantiator(options, swift) {
223223
}
224224
TestModule["bjs_DatabaseConnection_isConnected_get"] = function bjs_DatabaseConnection_isConnected_get(self) {
225225
try {
226-
let ret = swift.memory.getObject(self)["isConnected"];
226+
let ret = swift.memory.getObject(self).isConnected;
227227
return ret ? 1 : 0;
228228
} catch (error) {
229229
setException(error);
@@ -232,7 +232,7 @@ export async function createInstantiator(options, swift) {
232232
}
233233
TestModule["bjs_DatabaseConnection_connectionTimeout_get"] = function bjs_DatabaseConnection_connectionTimeout_get(self) {
234234
try {
235-
let ret = swift.memory.getObject(self)["connectionTimeout"];
235+
let ret = swift.memory.getObject(self).connectionTimeout;
236236
return ret;
237237
} catch (error) {
238238
setException(error);
@@ -241,7 +241,7 @@ export async function createInstantiator(options, swift) {
241241
}
242242
TestModule["bjs_DatabaseConnection_connectionTimeout_set"] = function bjs_DatabaseConnection_connectionTimeout_set(self, newValue) {
243243
try {
244-
swift.memory.getObject(self)["connectionTimeout"] = newValue;
244+
swift.memory.getObject(self).connectionTimeout = newValue;
245245
} catch (error) {
246246
setException(error);
247247
}
@@ -268,7 +268,7 @@ export async function createInstantiator(options, swift) {
268268
}
269269
TestModule["bjs_Logger_level_get"] = function bjs_Logger_level_get(self) {
270270
try {
271-
let ret = swift.memory.getObject(self)["level"];
271+
let ret = swift.memory.getObject(self).level;
272272
tmpRetBytes = textEncoder.encode(ret);
273273
return tmpRetBytes.length;
274274
} catch (error) {
@@ -295,7 +295,7 @@ export async function createInstantiator(options, swift) {
295295
}
296296
TestModule["bjs_ConfigManager_configPath_get"] = function bjs_ConfigManager_configPath_get(self) {
297297
try {
298-
let ret = swift.memory.getObject(self)["configPath"];
298+
let ret = swift.memory.getObject(self).configPath;
299299
tmpRetBytes = textEncoder.encode(ret);
300300
return tmpRetBytes.length;
301301
} catch (error) {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Protocol.Export.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export async function createInstantiator(options, swift) {
269269
const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
270270
TestModule["bjs_MyViewControllerDelegate_eventCount_get"] = function bjs_MyViewControllerDelegate_eventCount_get(self) {
271271
try {
272-
let ret = swift.memory.getObject(self)["eventCount"];
272+
let ret = swift.memory.getObject(self).eventCount;
273273
return ret;
274274
} catch (error) {
275275
setException(error);
@@ -278,14 +278,14 @@ export async function createInstantiator(options, swift) {
278278
}
279279
TestModule["bjs_MyViewControllerDelegate_eventCount_set"] = function bjs_MyViewControllerDelegate_eventCount_set(self, value) {
280280
try {
281-
swift.memory.getObject(self)["eventCount"] = value;
281+
swift.memory.getObject(self).eventCount = value;
282282
} catch (error) {
283283
setException(error);
284284
}
285285
}
286286
TestModule["bjs_MyViewControllerDelegate_delegateName_get"] = function bjs_MyViewControllerDelegate_delegateName_get(self) {
287287
try {
288-
let ret = swift.memory.getObject(self)["delegateName"];
288+
let ret = swift.memory.getObject(self).delegateName;
289289
tmpRetBytes = textEncoder.encode(ret);
290290
return tmpRetBytes.length;
291291
} catch (error) {
@@ -294,7 +294,7 @@ export async function createInstantiator(options, swift) {
294294
}
295295
TestModule["bjs_MyViewControllerDelegate_optionalName_get"] = function bjs_MyViewControllerDelegate_optionalName_get(self) {
296296
try {
297-
let ret = swift.memory.getObject(self)["optionalName"];
297+
let ret = swift.memory.getObject(self).optionalName;
298298
tmpRetString = ret;
299299
} catch (error) {
300300
setException(error);
@@ -307,14 +307,14 @@ export async function createInstantiator(options, swift) {
307307
obj = swift.memory.getObject(valueWrappedValue);
308308
swift.memory.release(valueWrappedValue);
309309
}
310-
swift.memory.getObject(self)["optionalName"] = valueIsSome ? obj : null;
310+
swift.memory.getObject(self).optionalName = valueIsSome ? obj : null;
311311
} catch (error) {
312312
setException(error);
313313
}
314314
}
315315
TestModule["bjs_MyViewControllerDelegate_optionalRawEnum_get"] = function bjs_MyViewControllerDelegate_optionalRawEnum_get(self) {
316316
try {
317-
let ret = swift.memory.getObject(self)["optionalRawEnum"];
317+
let ret = swift.memory.getObject(self).optionalRawEnum;
318318
tmpRetString = ret;
319319
} catch (error) {
320320
setException(error);
@@ -327,14 +327,14 @@ export async function createInstantiator(options, swift) {
327327
obj = swift.memory.getObject(valueWrappedValue);
328328
swift.memory.release(valueWrappedValue);
329329
}
330-
swift.memory.getObject(self)["optionalRawEnum"] = valueIsSome ? obj : null;
330+
swift.memory.getObject(self).optionalRawEnum = valueIsSome ? obj : null;
331331
} catch (error) {
332332
setException(error);
333333
}
334334
}
335335
TestModule["bjs_MyViewControllerDelegate_rawStringEnum_get"] = function bjs_MyViewControllerDelegate_rawStringEnum_get(self) {
336336
try {
337-
let ret = swift.memory.getObject(self)["rawStringEnum"];
337+
let ret = swift.memory.getObject(self).rawStringEnum;
338338
tmpRetBytes = textEncoder.encode(ret);
339339
return tmpRetBytes.length;
340340
} catch (error) {
@@ -345,14 +345,14 @@ export async function createInstantiator(options, swift) {
345345
try {
346346
const valueObject = swift.memory.getObject(value);
347347
swift.memory.release(value);
348-
swift.memory.getObject(self)["rawStringEnum"] = valueObject;
348+
swift.memory.getObject(self).rawStringEnum = valueObject;
349349
} catch (error) {
350350
setException(error);
351351
}
352352
}
353353
TestModule["bjs_MyViewControllerDelegate_result_get"] = function bjs_MyViewControllerDelegate_result_get(self) {
354354
try {
355-
let ret = swift.memory.getObject(self)["result"];
355+
let ret = swift.memory.getObject(self).result;
356356
const { caseId: caseId, cleanup: cleanup } = enumHelpers.Result.lower(ret);
357357
return caseId;
358358
} catch (error) {
@@ -362,14 +362,14 @@ export async function createInstantiator(options, swift) {
362362
TestModule["bjs_MyViewControllerDelegate_result_set"] = function bjs_MyViewControllerDelegate_result_set(self, value) {
363363
try {
364364
const enumValue = enumHelpers.Result.raise(value, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s);
365-
swift.memory.getObject(self)["result"] = enumValue;
365+
swift.memory.getObject(self).result = enumValue;
366366
} catch (error) {
367367
setException(error);
368368
}
369369
}
370370
TestModule["bjs_MyViewControllerDelegate_optionalResult_get"] = function bjs_MyViewControllerDelegate_optionalResult_get(self) {
371371
try {
372-
let ret = swift.memory.getObject(self)["optionalResult"];
372+
let ret = swift.memory.getObject(self).optionalResult;
373373
const isSome = ret != null;
374374
if (isSome) {
375375
const { caseId: caseId, cleanup: cleanup } = enumHelpers.Result.lower(ret);
@@ -387,14 +387,14 @@ export async function createInstantiator(options, swift) {
387387
if (valueIsSome) {
388388
enumValue = enumHelpers.Result.raise(valueWrappedValue, tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s);
389389
}
390-
swift.memory.getObject(self)["optionalResult"] = valueIsSome ? enumValue : null;
390+
swift.memory.getObject(self).optionalResult = valueIsSome ? enumValue : null;
391391
} catch (error) {
392392
setException(error);
393393
}
394394
}
395395
TestModule["bjs_MyViewControllerDelegate_direction_get"] = function bjs_MyViewControllerDelegate_direction_get(self) {
396396
try {
397-
let ret = swift.memory.getObject(self)["direction"];
397+
let ret = swift.memory.getObject(self).direction;
398398
return ret;
399399
} catch (error) {
400400
setException(error);
@@ -403,14 +403,14 @@ export async function createInstantiator(options, swift) {
403403
}
404404
TestModule["bjs_MyViewControllerDelegate_direction_set"] = function bjs_MyViewControllerDelegate_direction_set(self, value) {
405405
try {
406-
swift.memory.getObject(self)["direction"] = value;
406+
swift.memory.getObject(self).direction = value;
407407
} catch (error) {
408408
setException(error);
409409
}
410410
}
411411
TestModule["bjs_MyViewControllerDelegate_directionOptional_get"] = function bjs_MyViewControllerDelegate_directionOptional_get(self) {
412412
try {
413-
let ret = swift.memory.getObject(self)["directionOptional"];
413+
let ret = swift.memory.getObject(self).directionOptional;
414414
const isSome = ret != null;
415415
return isSome ? (ret | 0) : -1;
416416
} catch (error) {
@@ -419,14 +419,14 @@ export async function createInstantiator(options, swift) {
419419
}
420420
TestModule["bjs_MyViewControllerDelegate_directionOptional_set"] = function bjs_MyViewControllerDelegate_directionOptional_set(self, valueIsSome, valueWrappedValue) {
421421
try {
422-
swift.memory.getObject(self)["directionOptional"] = valueIsSome ? valueWrappedValue : null;
422+
swift.memory.getObject(self).directionOptional = valueIsSome ? valueWrappedValue : null;
423423
} catch (error) {
424424
setException(error);
425425
}
426426
}
427427
TestModule["bjs_MyViewControllerDelegate_priority_get"] = function bjs_MyViewControllerDelegate_priority_get(self) {
428428
try {
429-
let ret = swift.memory.getObject(self)["priority"];
429+
let ret = swift.memory.getObject(self).priority;
430430
return ret;
431431
} catch (error) {
432432
setException(error);
@@ -435,22 +435,22 @@ export async function createInstantiator(options, swift) {
435435
}
436436
TestModule["bjs_MyViewControllerDelegate_priority_set"] = function bjs_MyViewControllerDelegate_priority_set(self, value) {
437437
try {
438-
swift.memory.getObject(self)["priority"] = value;
438+
swift.memory.getObject(self).priority = value;
439439
} catch (error) {
440440
setException(error);
441441
}
442442
}
443443
TestModule["bjs_MyViewControllerDelegate_priorityOptional_get"] = function bjs_MyViewControllerDelegate_priorityOptional_get(self) {
444444
try {
445-
let ret = swift.memory.getObject(self)["priorityOptional"];
445+
let ret = swift.memory.getObject(self).priorityOptional;
446446
tmpRetOptionalInt = ret;
447447
} catch (error) {
448448
setException(error);
449449
}
450450
}
451451
TestModule["bjs_MyViewControllerDelegate_priorityOptional_set"] = function bjs_MyViewControllerDelegate_priorityOptional_set(self, valueIsSome, valueWrappedValue) {
452452
try {
453-
swift.memory.getObject(self)["priorityOptional"] = valueIsSome ? valueWrappedValue : null;
453+
swift.memory.getObject(self).priorityOptional = valueIsSome ? valueWrappedValue : null;
454454
} catch (error) {
455455
setException(error);
456456
}

0 commit comments

Comments
 (0)