Skip to content

Commit 281249a

Browse files
BridgeJS: Require the imports object for static-only imported types
An imported type looked up from `getImports` contributes its static methods too, but only a constructor marked the imports object as needed, so the glue referenced an undeclared `imports`.
1 parent bd8494b commit 281249a

5 files changed

Lines changed: 8 additions & 7 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ public struct BridgeJSLink {
267267
try renderImportedFunction(importObjectBuilder: importObjectBuilder, function: function)
268268
}
269269
for type in fileSkeleton.types {
270-
if type.constructor != nil, type.from == nil {
270+
// Matches the condition rendering the type into the `getImports` result
271+
// type: both its constructor and its static methods are looked up there.
272+
if type.from == nil, type.constructor != nil || !type.staticMethods.isEmpty {
271273
data.needsImportsObject = true
272274
}
273275
try renderImportedType(importObjectBuilder: importObjectBuilder, type: type)
@@ -351,11 +353,7 @@ public struct BridgeJSLink {
351353
for skeleton in skeletons {
352354
guard skeleton.typeRegistrationEntries != nil else { continue }
353355
let name = ABINameGenerator.typeRegistrationFunctionName(moduleName: skeleton.moduleName)
354-
// The glue may describe more modules than the instance links in: `js test`
355-
// generates it from every test target's skeletons, while the SwiftBuild
356-
// build system produces one binary per test target. A module that isn't
357-
// linked in can't have its types used either, so skip its registration.
358-
declarations.append(" \(JSGlueVariableScope.reservedInstance).exports[\"\(name)\"]?.();")
356+
declarations.append(" \(JSGlueVariableScope.reservedInstance).exports[\"\(name)\"]();")
359357
}
360358
declarations.append("}")
361359
declarations.append(contentsOf: GenericJSCodegen.runtimeHelperDeclarations())

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export async function createInstantiator(options, swift) {
153153
addImports: (importObject, importsContext) => {
154154
bjs = {};
155155
importObject["bjs"] = bjs;
156+
const imports = options.getImports(importsContext);
156157
bjs["swift_js_return_string"] = function(ptr, len) {
157158
tmpRetString = decodeString(ptr, len);
158159
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export async function createInstantiator(options, swift) {
8383
addImports: (importObject, importsContext) => {
8484
bjs = {};
8585
importObject["bjs"] = bjs;
86+
const imports = options.getImports(importsContext);
8687
bjs["swift_js_return_string"] = function(ptr, len) {
8788
tmpRetString = decodeString(ptr, len);
8889
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export async function createInstantiator(options, swift) {
4444
addImports: (importObject, importsContext) => {
4545
bjs = {};
4646
importObject["bjs"] = bjs;
47+
const imports = options.getImports(importsContext);
4748
bjs["swift_js_return_string"] = function(ptr, len) {
4849
tmpRetString = decodeString(ptr, len);
4950
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function createInstantiator(options, swift) {
5252
}
5353
__bjs_typeHandlesRegistered = true;
5454
instance.exports["bjs_core_register_type_handles"]();
55-
instance.exports["bjs_TestModule_register_type_handles"]?.();
55+
instance.exports["bjs_TestModule_register_type_handles"]();
5656
}
5757
function __bjs_codecForTypeId(typeId) {
5858
__bjs_registerTypeHandles();

0 commit comments

Comments
 (0)