diff --git a/tslang/lib/TypeScript/MLIRGenModule.cpp b/tslang/lib/TypeScript/MLIRGenModule.cpp index b1b1d4125..433fec27f 100644 --- a/tslang/lib/TypeScript/MLIRGenModule.cpp +++ b/tslang/lib/TypeScript/MLIRGenModule.cpp @@ -1086,8 +1086,13 @@ namespace mlirgen filesInProgress.insert(canonicalPath); auto inProgress = llvm::make_scope_exit([&]() { filesInProgress.erase(canonicalPath); }); + // An import that points to a .ts file: compiled, it is a declaration - the module is + // compiled into an object of its own, and the objects are linked into one program. Under + // the JIT there is no other object, so it is included with its bodies, as a referenced + // file is. (An import that points to a library is mlirGenImportSharedLib.) A .d.ts is + // read as declarations either way. MLIRValueGuard vg(declarationMode); - declarationMode = true; + declarationMode = !compileOptions.isJit; // What the imported module exports is its own to export: it is compiled separately, with // its declarations in its own __decls. Added to this module's too, a library holding both diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index fb1c35991..8e2da2d1d 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2598,6 +2598,13 @@ add_test(NAME test-compile-reference-path-import-cycle-quiet "${reference_path_dir}/cycle_class_node.ts" -o "${CMAKE_CURRENT_BINARY_DIR}/cycle_class_node.obj") set_tests_properties(test-compile-reference-path-import-cycle-quiet PROPERTIES FAIL_REGULAR_EXPRESSION "error:") +# Under the JIT an import pointing to a .ts file includes it with its bodies: a program run on its +# own found no `S..new` for the imported class, which was only declared. Compiled, the import is a +# declaration and the module's own object is linked beside it. +set(import_source_dir "${PROJECT_SOURCE_DIR}/test/tester/import-source") +tslang_add_test(NAME test-jit-import-source-new-imported-class COMMAND test-runner -jit "${import_source_dir}/new_imported_class.ts") +tslang_add_import_tests(import-source-new-imported-class "${import_source_dir}" new_imported_class imported_class_module) + # `import { a as b }` and `import * as M` - each used to be "can't resolve name". set(import_bindings_dir "${PROJECT_SOURCE_DIR}/test/tester/import-bindings") tslang_add_import_tests(import-bindings-renamed "${import_bindings_dir}" import_renamed names_module) diff --git a/tslang/test/tester/import-source/imported_class_module.ts b/tslang/test/tester/import-source/imported_class_module.ts new file mode 100644 index 000000000..631029a5c --- /dev/null +++ b/tslang/test/tester/import-source/imported_class_module.ts @@ -0,0 +1,6 @@ +export class S +{ + toString() { + return "Hi"; + } +} diff --git a/tslang/test/tester/import-source/new_imported_class.ts b/tslang/test/tester/import-source/new_imported_class.ts new file mode 100644 index 000000000..02f0091c0 --- /dev/null +++ b/tslang/test/tester/import-source/new_imported_class.ts @@ -0,0 +1,9 @@ +// Under the JIT an import pointing to a .ts file includes it with its bodies: run on its own, the +// program used to fail with "Symbols not found: [ S..new ]" - the imported class was only declared. +import './imported_class_module' + +const s = new S(); +assert(s.toString() == "Hi"); +print("Hello World", s); + +print("done."); diff --git a/tslang/test/tester/test-runner.cpp b/tslang/test/tester/test-runner.cpp index 70be76a15..73344f274 100644 --- a/tslang/test/tester/test-runner.cpp +++ b/tslang/test/tester/test-runner.cpp @@ -686,7 +686,8 @@ void createSharedMultiBatchFile(std::string tempOutputFileNameNoExt, std::vector if (jitRun) { // one extra "../" because we run from the per-test working directory - 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" + // the library is not passed with --shared-libs: `import './'` loads it at run time + batFile << "$TSLANGEXEPATH/tslang --emit=jit " << tslang_opt << " --shared-libs=../../../lib/libTypeScriptRuntime.so " << *files.begin() << " 1> ../$FILENAME.txt 2> ../$FILENAME.err" << std::endl; } else