fix(compile): initialize namespace fields in the multi-module entry point (#1245)#1247
Merged
Conversation
…oint (#1245) A `namespace` declaration combined with any top-level `import` (which turns the file into an ES module) crashed compiled programs at startup with a NullReferenceException in `$TSNamespace.Set`, called from a module's `$Initialize`. The interpreter ran the same program correctly. Namespace objects live in flat static fields (`$ns_*`) on `$Program`, shared across every module. A module's `$Initialize` body only POPULATES them (`EmitNamespace` emits `$ns_X.Set(...)`); it never creates them. The single-file entry point calls `InitializeNamespaceFields` at the top of `Main` before any top-level code runs, but the multi-module entry point (`EmitModulesEntryPoint`) omitted it — so the first `Set()` dereferenced a null namespace field. Fix: call `InitializeNamespaceFields(il)` in `EmitModulesEntryPoint` before the module `$Initialize` dispatch loop, mirroring the single-file path. Adds regression tests (both interpreter and compiled) covering namespace + import in the entry file (const/function members, import before/after), nested namespaces, and a namespace alongside a sibling-module import with class/enum members.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1245. In compiled mode, any program containing both a
namespacedeclaration and a top-levelimport(which makes the file an ES module) crashed at startup with aNullReferenceExceptionin$TSNamespace.Set, called from the module's$Initialize. The interpreter ran the same program correctly.Root cause
Namespace objects live in flat static fields (
$ns_*) on$Program, shared across every module. A module's$Initializebody only populates them —EmitNamespaceemits$ns_X.Set(...)— it never creates them.EmitSingleFileEntryPoint/EmitExeEntryPointWithUserMain) callsInitializeNamespaceFields(il)at the top ofMain, before any top-level code runs.EmitModulesEntryPoint, used whenever the program has imports) omitted that call.So when a module's
$Initializeran$ns_NS.Set("x", 1),$ns_NSwas still null → NRE. Anamespacein a plain script (no import) was fine because that path uses the single-file entry point.Fix
Call
InitializeNamespaceFields(il)inEmitModulesEntryPointbefore the module$Initializedispatch loop, mirroring the single-file path. Namespace fields are global/flat and defined once, so initializing them all inMainbefore any module init covers ES-module, script, and lazily-required CommonJS modules alike.Tests
New
NamespaceInModuleTests(runs both interpreter and compiled viaExecutionModes.All):const, import after (the exact issue repro)functionmember, import before the namespaceclassandenummembersConfirmed the 4 compiled-mode tests fail with
NullReferenceExceptionwithout the fix and pass with it (interpreted always passed).Verification
--verifyon the fs-free namespace-in-module case is clean; the$M_fs_*/$M_path_*IL-verify errors that appear when importing those facades are pre-existing and orthogonal (tracked as Compiled fs facade emits IL-unverifiable methods (68 --verify errors: $Promise vs Task<object>, object vs string) although it runs #1246).