Skip to content

Commit

Permalink
Add support for defining globals via JS scripts (wasmerio#84)
Browse files Browse the repository at this point in the history
* feat: add js_modules for define global scope object

* feat: adjust type declaration, js_modules and add readme

* refactor: remove unused file and formatting

* refactor: change module name and add readme

* feat: change error message

* Rework the global scripts to work in script mode

---------

Co-authored-by: @MasatoDev <[email protected]>
  • Loading branch information
Arshia001 and @MasatoDev authored May 26, 2024
1 parent 931369d commit 134924c
Show file tree
Hide file tree
Showing 11 changed files with 897 additions and 771 deletions.
14 changes: 12 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ fn main() {
// once in debug mode, and the JS scripts will be updated and pushed.
let profile = std::env::var("PROFILE").unwrap();
if profile == "debug" {
let mut dir = std::env::current_dir().unwrap();
dir.extend(["src", "builtins", "internal_js_modules"]);
let builtins_dir = std::env::current_dir().unwrap().join("src/builtins");

let dir = builtins_dir.join("internal_js_modules");
assert!(Command::new("npx")
.arg("tsc")
.current_dir(dir)
.output()
.unwrap()
.status
.success());

let dir = builtins_dir.join("js_globals");
assert!(Command::new("npx")
.arg("tsc")
.current_dir(dir)
Expand Down
25 changes: 14 additions & 11 deletions src/builtins/internal_js_modules.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Here, we initialize those modules that are defined in JS, as
//! opposed to native modules defined in Rust. The sources for
//! these modules should be put under the internal_js_modules dir
//! that lives next to this source file.
//!
//! The name of the modules will be equivalent to their path on disk,
//! but forward slashes (/) will be changed to a colon (:) and the .js
//! extension will be stripped. So, to create a node:buffer module,
//! one must put the source under `internal_js_modules/node/buffer.js`.
//!
//! Note: Files that don't have a .js extension will be ignored.
use anyhow::{anyhow, Context as _};
use clap::builder::OsStr;
/// Here, we initialize those modules that are defined in JS, as
/// opposed to native modules defined in Rust. The sources for
/// these modules should be put under the internal_js_modules dir
/// that lives next to this source file.
///
/// The name of the modules will be equivalent to their path on disk,
/// but forward slashes (/) will be changed to a colon (:) and the .js
/// extension will be stripped. So, to create a node:buffer module,
/// one must put the source under `internal_js_modules/node/buffer.js`.
///
/// Note: Files that don't have a .js extension will be ignored.
use include_dir::{include_dir, Dir, File};
use ion::{
module::{Module, ModuleRequest},
Expand Down Expand Up @@ -47,6 +48,8 @@ fn scan_dir(cx: &Context, dir: &Dir) -> anyhow::Result<()> {
}

fn compile_and_register(cx: &Context, script_file: &File) -> anyhow::Result<()> {
tracing::debug!("Registering internal module at {:?}", script_file.path());

let module_name = script_file
.path()
.to_str()
Expand Down
5 changes: 1 addition & 4 deletions src/builtins/internal_js_modules/node/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ export class AsyncLocalStorage {
return AsyncResource.bind(fn);
}
static snapshot() {
return AsyncLocalStorage.bind((
cb,
...args
) => cb(...args));
return AsyncLocalStorage.bind((cb, ...args) => cb(...args));
}
}
export function executionAsyncId() {
Expand Down
Loading

0 comments on commit 134924c

Please sign in to comment.