From e06c7bc2d266a0ccc0534ae6925ba8c55b97369c Mon Sep 17 00:00:00 2001 From: dcode Date: Sun, 22 Jan 2023 18:08:45 +0100 Subject: [PATCH] fix: Handle all possible element kinds when walking exports (#2631) --- src/bindings/util.ts | 29 ++++++++++++++++++++++++++--- tests/compiler/bindings/esm.ts | 7 +++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/bindings/util.ts b/src/bindings/util.ts index e8c767ea05..04cf849fa0 100644 --- a/src/bindings/util.ts +++ b/src/bindings/util.ts @@ -19,7 +19,8 @@ import { Global, Program, Property, - PropertyPrototype + PropertyPrototype, + InterfacePrototype } from "../program"; /** Walker base class. */ @@ -94,6 +95,10 @@ export abstract class ExportsWalker { this.visitClassInstances(name, element); break; } + case ElementKind.InterfacePrototype: { + this.visitInterfaceInstances(name, element); + break; + } case ElementKind.PropertyPrototype: { let propertyInstance = (element).instance; if (!propertyInstance) break; @@ -112,8 +117,13 @@ export abstract class ExportsWalker { if (hasCompiledMember(element)) this.visitNamespace(name, element); break; } - case ElementKind.TypeDefinition: break; - default: assert(false); + case ElementKind.TypeDefinition: + case ElementKind.IndexSignature: break; + default: { + // Not (directly) reachable exports: + // File, Local, Function, Class, Interface + assert(false); + } } } @@ -134,11 +144,24 @@ export abstract class ExportsWalker { // TODO: for (let instance of instances.values()) { for (let _values = Map_values(instances), i = 0, k = _values.length; i < k; ++i) { let instance = unchecked(_values[i]); + assert(instance.kind == ElementKind.Class); if (instance.is(CommonFlags.Compiled)) this.visitClass(name, instance); } } } + private visitInterfaceInstances(name: string, element: InterfacePrototype): void { + let instances = element.instances; + if (instances) { + // TODO: for (let instance of instances.values()) { + for (let _values = Map_values(instances), i = 0, k = _values.length; i < k; ++i) { + let instance = unchecked(_values[i]); + assert(instance.kind == ElementKind.Interface); + if (instance.is(CommonFlags.Compiled)) this.visitInterface(name, instance); + } + } + } + abstract visitGlobal(name: string, element: Global): void; abstract visitEnum(name: string, element: Enum): void; abstract visitFunction(name: string, element: Function): void; diff --git a/tests/compiler/bindings/esm.ts b/tests/compiler/bindings/esm.ts index 37d2e03d00..275d962050 100644 --- a/tests/compiler/bindings/esm.ts +++ b/tests/compiler/bindings/esm.ts @@ -169,3 +169,10 @@ immutableGlobalNested; declare function Date_getTimezoneOffset(): i32; Date_getTimezoneOffset(); + +// Not yet instrumented element kinds: + +export class ExportedClass {} +export interface ExportedInterface {} +export type ExportedType = ExportedClass; +export namespace ExportedNamespace {}