diff --git a/Cargo.lock b/Cargo.lock index 0bd1bd9..d667c02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1905,7 +1905,7 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" [[package]] name = "nova_vm" version = "0.1.0" -source = "git+https://github.com/trynova/nova?rev=7d1da0bd906ab7b8409878b41bd611f172789ce9#7d1da0bd906ab7b8409878b41bd611f172789ce9" +source = "git+https://github.com/trynova/nova?rev=662b0b3d865e62edc7e83229cf0194fe2c981a35#662b0b3d865e62edc7e83229cf0194fe2c981a35" dependencies = [ "ahash", "fast-float", @@ -2993,7 +2993,7 @@ dependencies = [ [[package]] name = "small_string" version = "0.1.0" -source = "git+https://github.com/trynova/nova?rev=7d1da0bd906ab7b8409878b41bd611f172789ce9#7d1da0bd906ab7b8409878b41bd611f172789ce9" +source = "git+https://github.com/trynova/nova?rev=662b0b3d865e62edc7e83229cf0194fe2c981a35#662b0b3d865e62edc7e83229cf0194fe2c981a35" [[package]] name = "smallvec" diff --git a/Cargo.toml b/Cargo.toml index 624ed4f..0a2965e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ dprint-plugin-json = "0.20.0" indexmap = "2.9.0" image = "0.25.6" libsui = "0.10.0" -nova_vm = { git = "https://github.com/trynova/nova", rev = "7d1da0bd906ab7b8409878b41bd611f172789ce9", features = [ +nova_vm = { git = "https://github.com/trynova/nova", rev = "662b0b3d865e62edc7e83229cf0194fe2c981a35", features = [ "typescript" ] } nu-ansi-term = "0.50.1" diff --git a/core/src/runtime.rs b/core/src/runtime.rs index 09587d2..f3aa30a 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs @@ -16,12 +16,19 @@ use nova_vm::{ builtins::promise_objects::promise_abstract_operations::promise_capability_records::PromiseCapability, execution::{ Agent, JsResult, - agent::{GcAgent, HostHooks, Job, Options, RealmRoot}, + agent::{ExceptionType, GcAgent, HostHooks, Job, Options, RealmRoot}, }, - scripts_and_modules::script::{parse_script, script_evaluation}, - types::{self, Object, Value}, + scripts_and_modules::{ + module::module_semantics::{ + cyclic_module_records::GraphLoadingStateRecord, + finish_loading_imported_module, + source_text_module_records::{SourceTextModule, parse_module}, + }, + script::{HostDefined, parse_script, script_evaluation}, + }, + types::{Object, String as JsString, Value}, }, - engine::context::{Bindable, GcScope}, + engine::context::{Bindable, GcScope, NoGcScope}, }; use crate::{ @@ -64,6 +71,44 @@ impl HostHooks for RuntimeHostHooks { fn get_host_data(&self) -> &dyn Any { &self.host_data } + + fn load_imported_module<'gc>( + &self, + agent: &mut Agent, + referrer: SourceTextModule<'gc>, + module_request: &str, + host_defined: Option, + payload: &mut GraphLoadingStateRecord<'gc>, + gc: NoGcScope<'gc, '_>, + ) { + let file = match std::fs::read_to_string(module_request) { + Ok(file) => file, + Err(err) => { + let result = Err(agent.throw_exception(ExceptionType::Error, err.to_string(), gc)); + finish_loading_imported_module( + agent, + referrer, + module_request, + payload, + result, + gc, + ); + return; + } + }; + let source_text = JsString::from_string(agent, file, gc); + let result = parse_module( + agent, + source_text, + referrer.realm(agent), + host_defined.clone(), + gc, + ) + .map_err(|err| { + agent.throw_exception(ExceptionType::Error, err.first().unwrap().to_string(), gc) + }); + finish_loading_imported_module(agent, referrer, module_request, payload, result, gc); + } } pub type EventLoopHandler = fn( @@ -206,7 +251,7 @@ impl Runtime { self.agent.run_in_realm(&self.realm_root, |agent, mut gc| { for builtin in &self.config.builtins { let realm = agent.current_realm(gc.nogc()); - let source_text = types::String::from_str(agent, builtin, gc.nogc()); + let source_text = JsString::from_str(agent, builtin, gc.nogc()); let script = match parse_script( agent, source_text, @@ -253,7 +298,7 @@ impl Runtime { continue; } result = self.agent.run_in_realm(&self.realm_root, |agent, mut gc| { - let source_text = types::String::from_string(agent, file_content, gc.nogc()); + let source_text = JsString::from_string(agent, file_content, gc.nogc()); let realm = agent.current_realm(gc.nogc()); let script = match parse_script( diff --git a/examples/export.ts b/examples/export.ts new file mode 100644 index 0000000..046f68e --- /dev/null +++ b/examples/export.ts @@ -0,0 +1 @@ +export const tryAndromeda = true; \ No newline at end of file diff --git a/examples/import.ts b/examples/import.ts new file mode 100644 index 0000000..9b22f85 --- /dev/null +++ b/examples/import.ts @@ -0,0 +1,3 @@ +import { tryAndromeda } from './export.ts' + +console.log(tryAndromeda) \ No newline at end of file