Skip to content

Commit

Permalink
add Module::module_namespace and Module::module_environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Feb 12, 2024
1 parent 402a4a8 commit 5b628cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
20 changes: 19 additions & 1 deletion ion/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ impl ModuleError {

/// Represents a compiled module.
#[derive(Debug)]
pub struct Module<'m>(pub Object<'m>);
pub struct Module<'m>(Object<'m>);

impl<'cx> Module<'cx> {
pub fn from_local(l: Local<'cx, *mut JSObject>) -> Self {
Self(l.into())
}

/// Compiles a [Module] with the given source and filename.
/// On success, returns the compiled module object and a promise. The promise resolves with the return value of the module.
/// The promise is a byproduct of enabling top-level await.
Expand Down Expand Up @@ -173,6 +177,20 @@ impl<'cx> Module<'cx> {
Err(ErrorReport::new_with_exception_stack(cx).unwrap())
}
}

pub fn module_object(&self) -> &Object {
&self.0
}

pub fn module_namespace(&self, cx: &'cx Context) -> Object<'cx> {
cx.root_object(unsafe { mozjs::jsapi::GetModuleNamespace(cx.as_ptr(), self.0.handle().into()) })
.into()
}

pub fn module_environment(&self, cx: &'cx Context) -> Object<'cx> {
cx.root_object(unsafe { mozjs::jsapi::GetModuleEnvironment(cx.as_ptr(), self.0.handle().into()) })
.into()
}
}

/// Represents an ES module loader.
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/modules/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ModuleLoader for Loader {
// canonicalization process is incompatible with built-in modules
// that don't have an address on disk.
if let Some(heap) = self.registry.get(&specifier) {
return Ok(Module(heap.root(cx).into()));
return Ok(Module::from_local(heap.root(cx)));
}

let path = if specifier.starts_with("./") || specifier.starts_with("../") {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl ModuleLoader for Loader {

let str = String::from(path.to_str().unwrap());
match self.registry.get(&str) {
Some(heap) => Ok(Module(heap.root(cx).into())),
Some(heap) => Ok(Module::from_local(heap.root(cx))),
None => {
let script = read_to_string(&path).map_err(|e| {
Error::new(
Expand All @@ -90,7 +90,7 @@ impl ModuleLoader for Loader {

if let Ok(module) = module {
let request = ModuleRequest::new(cx, path.to_str().unwrap());
self.register(cx, &module.0, &request);
self.register(cx, module.module_object(), &request);
Ok(module)
} else {
Err(Error::new(&format!("Unable to compile module: {}\0", specifier), None))
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/modules/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn init_module<M: NativeModule>(cx: &Context, global: &mut Object) -> bool {
let loader = unsafe { &mut (*cx.get_inner_data().as_ptr()).module_loader };
return loader.as_mut().is_some_and(|loader| {
let request = ModuleRequest::new(cx, M::NAME);
loader.register(cx, &module.0, &request);
loader.register(cx, module.module_object(), &request);
true
});
}
Expand Down

0 comments on commit 5b628cb

Please sign in to comment.