Skip to content

Commit 32737f1

Browse files
committed
feat: imports
1 parent a60b7c2 commit 32737f1

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dprint-plugin-json = "0.20.0"
2424
indexmap = "2.9.0"
2525
image = "0.25.6"
2626
libsui = "0.10.0"
27-
nova_vm = { git = "https://github.com/trynova/nova", rev = "7d1da0bd906ab7b8409878b41bd611f172789ce9", features = [
27+
nova_vm = { git = "https://github.com/trynova/nova", rev = "662b0b3d865e62edc7e83229cf0194fe2c981a35", features = [
2828
"typescript"
2929
] }
3030
nu-ansi-term = "0.50.1"

core/src/runtime.rs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ use nova_vm::{
1616
builtins::promise_objects::promise_abstract_operations::promise_capability_records::PromiseCapability,
1717
execution::{
1818
Agent, JsResult,
19-
agent::{GcAgent, HostHooks, Job, Options, RealmRoot},
19+
agent::{ExceptionType, GcAgent, HostHooks, Job, Options, RealmRoot},
2020
},
21-
scripts_and_modules::script::{parse_script, script_evaluation},
22-
types::{self, Object, Value},
21+
scripts_and_modules::{
22+
module::module_semantics::{
23+
cyclic_module_records::GraphLoadingStateRecord,
24+
finish_loading_imported_module,
25+
source_text_module_records::{SourceTextModule, parse_module},
26+
},
27+
script::{HostDefined, parse_script, script_evaluation},
28+
},
29+
types::{Object, String as JsString, Value},
2330
},
24-
engine::context::{Bindable, GcScope},
31+
engine::context::{Bindable, GcScope, NoGcScope},
2532
};
2633

2734
use crate::{
@@ -64,6 +71,44 @@ impl<UserMacroTask: 'static> HostHooks for RuntimeHostHooks<UserMacroTask> {
6471
fn get_host_data(&self) -> &dyn Any {
6572
&self.host_data
6673
}
74+
75+
fn load_imported_module<'gc>(
76+
&self,
77+
agent: &mut Agent,
78+
referrer: SourceTextModule<'gc>,
79+
module_request: &str,
80+
host_defined: Option<HostDefined>,
81+
payload: &mut GraphLoadingStateRecord<'gc>,
82+
gc: NoGcScope<'gc, '_>,
83+
) {
84+
let file = match std::fs::read_to_string(module_request) {
85+
Ok(file) => file,
86+
Err(err) => {
87+
let result = Err(agent.throw_exception(ExceptionType::Error, err.to_string(), gc));
88+
finish_loading_imported_module(
89+
agent,
90+
referrer,
91+
module_request,
92+
payload,
93+
result,
94+
gc,
95+
);
96+
return;
97+
}
98+
};
99+
let source_text = JsString::from_string(agent, file, gc);
100+
let result = parse_module(
101+
agent,
102+
source_text,
103+
referrer.realm(agent),
104+
host_defined.clone(),
105+
gc,
106+
)
107+
.map_err(|err| {
108+
agent.throw_exception(ExceptionType::Error, err.first().unwrap().to_string(), gc)
109+
});
110+
finish_loading_imported_module(agent, referrer, module_request, payload, result, gc);
111+
}
67112
}
68113

69114
pub type EventLoopHandler<UserMacroTask> = fn(
@@ -206,7 +251,7 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
206251
self.agent.run_in_realm(&self.realm_root, |agent, mut gc| {
207252
for builtin in &self.config.builtins {
208253
let realm = agent.current_realm(gc.nogc());
209-
let source_text = types::String::from_str(agent, builtin, gc.nogc());
254+
let source_text = JsString::from_str(agent, builtin, gc.nogc());
210255
let script = match parse_script(
211256
agent,
212257
source_text,
@@ -253,7 +298,7 @@ impl<UserMacroTask> Runtime<UserMacroTask> {
253298
continue;
254299
}
255300
result = self.agent.run_in_realm(&self.realm_root, |agent, mut gc| {
256-
let source_text = types::String::from_string(agent, file_content, gc.nogc());
301+
let source_text = JsString::from_string(agent, file_content, gc.nogc());
257302
let realm = agent.current_realm(gc.nogc());
258303

259304
let script = match parse_script(

examples/export.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const tryAndromeda = true;

examples/import.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { tryAndromeda } from './export.ts'
2+
3+
console.log(tryAndromeda)

0 commit comments

Comments
 (0)