Skip to content

Commit

Permalink
Add global parameter to execute old js code (awslabs#597)
Browse files Browse the repository at this point in the history
* support cjs

* support cjs

* fix ci

* fix typo
  • Loading branch information
ahaoboy authored Sep 22, 2024
1 parent 17a56c0 commit 6af2402
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
42 changes: 28 additions & 14 deletions llrt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async fn start_cli(vm: &Vm) {
},
"-e" | "--eval" => {
if let Some(source) = args.get(i + 1) {
vm.run(source.as_bytes(), false).await;
vm.run(source.as_bytes(), false, true).await;
}
return;
},
Expand Down Expand Up @@ -169,24 +169,37 @@ async fn start_cli(vm: &Vm) {

let filename = Path::new(arg);
let file_exists = filename.exists();
if let ".js" | ".mjs" | ".cjs" = ext {
if file_exists {
return vm.run_file(filename).await;
} else {
eprintln!("No such file: {}", arg);

match ext {
".cjs" => {
if file_exists {
return vm.run_cjs_file(filename, false, true).await;
} else {
eprintln!("No such file: {}", arg);
exit(1);
}
},
".js" | ".mjs" => {
if file_exists {
return vm.run_esm_file(filename, true, false).await;
} else {
eprintln!("No such file: {}", arg);
exit(1);
}
},
_ => {
if file_exists {
return vm.run_esm_file(filename, true, false).await;
}
eprintln!("Unknown command: {}", arg);
usage();
exit(1);
}
}
if file_exists {
return vm.run_file(filename).await;
},
}
eprintln!("Unknown command: {}", arg);
usage();
exit(1);
}
}
} else if let Some(filename) = get_js_path("index") {
vm.run_file(&filename).await;
vm.run_esm_file(&filename, true, false).await;
}
}

Expand Down Expand Up @@ -263,6 +276,7 @@ async fn run_tests(vm: &Vm, args: &[std::string::String]) -> Result<(), String>
import "@llrt/test"
"#,
false,
false,
)
.await;

Expand Down
18 changes: 14 additions & 4 deletions llrt_core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,35 @@ impl Vm {
.await;
}

pub async fn run_file(&self, filename: &Path) {
pub async fn run_esm_file(&self, filename: &Path, strict: bool, global: bool) {
self.run(
[
r#"import(""#,
&filename.to_string_lossy(),
r#"").catch((e) => {{console.error(e);process.exit(1)}})"#,
]
.concat(),
false,
strict,
global,
)
.await;
}
pub async fn run_cjs_file(&self, filename: &Path, strict: bool, global: bool) {
let source = std::fs::read(filename).unwrap_or_else(|_| {
panic!(
"{}",
["No such file: ", &filename.to_string_lossy()].concat()
)
});
self.run(source, strict, global).await;
}

pub async fn run<S: Into<Vec<u8>> + Send>(&self, source: S, strict: bool) {
pub async fn run<S: Into<Vec<u8>> + Send>(&self, source: S, strict: bool, global: bool) {
self.run_with(|ctx| {
let mut options = EvalOptions::default();
options.strict = strict;
options.promise = true;
options.global = false;
options.global = global;
let _ = ctx.eval_with_options::<Value, _>(source, options)?;
Ok::<_, Error>(())
})
Expand Down

0 comments on commit 6af2402

Please sign in to comment.