Skip to content

Commit edaf802

Browse files
JIT: an import pointing to a .ts file is included with its bodies (#404)
* An import pointing to a .ts file includes it with its bodies mlirGenInclude generated an imported .ts module as declarations only, so a program run on its own (tslang main.ts, JIT) found no bodies for what it imported: 'Symbols not found: [ S..new ]' for new S() of an imported class. An import pointing to a .ts file now includes it with its bodies, like a referenced file; one pointing to a library (lib<name>.so / <name>.dll) still takes them from the library. A .d.ts stays declarations only. Known: test-runner still compiles each imported module into an object of its own and links it beside the importer, so the multi-module export-import, reference-path, shared-class-members and import-bindings tests now fail with 'multiple definition' link errors until the runner stops doing that. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPAHhg8c9NEdoYNw52AtTg * test-runner -jit -shared: the imported library is loaded by the import, not --shared-libs A library an `import` points to is loaded by the program at run time; passing it with --shared-libs as well is not how it is meant to be used (and matches the Windows runner, which already did not). The 56 test-jit-shared tests pass, including the reference-path diamond/cycle and shared-class-members ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPAHhg8c9NEdoYNw52AtTg * Compiled, an import pointing to a .ts file is a declaration; under the JIT it is included Compiled, each .ts module is an object of its own and the objects are linked into one program, so an import pointing to a .ts file only declares what the module has. Under the JIT there is no other object: the file is included with its bodies. The import-source test now also runs compiled, with the module's object linked beside the program. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPAHhg8c9NEdoYNw52AtTg --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6e76af1 commit edaf802

5 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎tslang/lib/TypeScript/MLIRGenModule.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,13 @@ namespace mlirgen
10861086
filesInProgress.insert(canonicalPath);
10871087
auto inProgress = llvm::make_scope_exit([&]() { filesInProgress.erase(canonicalPath); });
10881088

1089+
// An import that points to a .ts file: compiled, it is a declaration - the module is
1090+
// compiled into an object of its own, and the objects are linked into one program. Under
1091+
// the JIT there is no other object, so it is included with its bodies, as a referenced
1092+
// file is. (An import that points to a library is mlirGenImportSharedLib.) A .d.ts is
1093+
// read as declarations either way.
10891094
MLIRValueGuard<bool> vg(declarationMode);
1090-
declarationMode = true;
1095+
declarationMode = !compileOptions.isJit;
10911096

10921097
// What the imported module exports is its own to export: it is compiled separately, with
10931098
// its declarations in its own __decls. Added to this module's too, a library holding both

‎tslang/test/tester/CMakeLists.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,13 @@ add_test(NAME test-compile-reference-path-import-cycle-quiet
26222622
"${reference_path_dir}/cycle_class_node.ts" -o "${CMAKE_CURRENT_BINARY_DIR}/cycle_class_node.obj")
26232623
set_tests_properties(test-compile-reference-path-import-cycle-quiet PROPERTIES FAIL_REGULAR_EXPRESSION "error:")
26242624

2625+
# Under the JIT an import pointing to a .ts file includes it with its bodies: a program run on its
2626+
# own found no `S..new` for the imported class, which was only declared. Compiled, the import is a
2627+
# declaration and the module's own object is linked beside it.
2628+
set(import_source_dir "${PROJECT_SOURCE_DIR}/test/tester/import-source")
2629+
tslang_add_test(NAME test-jit-import-source-new-imported-class COMMAND test-runner -jit "${import_source_dir}/new_imported_class.ts")
2630+
tslang_add_import_tests(import-source-new-imported-class "${import_source_dir}" new_imported_class imported_class_module)
2631+
26252632
# `import { a as b }` and `import * as M` - each used to be "can't resolve name".
26262633
set(import_bindings_dir "${PROJECT_SOURCE_DIR}/test/tester/import-bindings")
26272634
tslang_add_import_tests(import-bindings-renamed "${import_bindings_dir}" import_renamed names_module)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class S
2+
{
3+
toString() {
4+
return "Hi";
5+
}
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Under the JIT an import pointing to a .ts file includes it with its bodies: run on its own, the
2+
// program used to fail with "Symbols not found: [ S..new ]" - the imported class was only declared.
3+
import './imported_class_module'
4+
5+
const s = new S();
6+
assert(s.toString() == "Hi");
7+
print("Hello World", s);
8+
9+
print("done.");

‎tslang/test/tester/test-runner.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ void createSharedMultiBatchFile(std::string tempOutputFileNameNoExt, std::vector
686686
if (jitRun)
687687
{
688688
// one extra "../" because we run from the per-test working directory
689-
batFile << "$TSLANGEXEPATH/tslang --emit=jit " << tslang_opt << " --shared-libs=../../../lib/libTypeScriptRuntime.so --shared-libs=./lib" << shared_filenameNoExt << ".so " << *files.begin() << " 1> ../$FILENAME.txt 2> ../$FILENAME.err"
689+
// the library is not passed with --shared-libs: `import './<stem>'` loads it at run time
690+
batFile << "$TSLANGEXEPATH/tslang --emit=jit " << tslang_opt << " --shared-libs=../../../lib/libTypeScriptRuntime.so " << *files.begin() << " 1> ../$FILENAME.txt 2> ../$FILENAME.err"
690691
<< std::endl;
691692
}
692693
else

0 commit comments

Comments
 (0)