Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Apr 25, 2024
1 parent a6f4556 commit c37c623
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ pub enum Error {

#[error("failed to get js value")]
FailedToGetV8Value,

#[error("undefined export")]
UndefinedExport,
}
4 changes: 2 additions & 2 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct JsModule {
}

impl JsModule {
pub fn export_exists(&self, key: &String) -> bool {
self.exports.contains(key)
pub fn export_exists(&self, key: &str) -> bool {
self.exports.contains(&key.to_string())
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ globalThis.require = __internalCreateRequire____("{}");"#,
},
})
}

/// get module object instance
pub fn get_main_module_instance(&mut self) -> Result<v8::Global<v8::Object>, Error> {
let mod_namespace = self
.main_worker
.js_runtime
.get_module_namespace(self.main_module.id)?;
Ok(mod_namespace)
}

/// get the export value
pub fn get_export(&mut self, name: &str) -> Result<v8::Global<v8::Value>, Error> {
if !self.main_module.export_exists(name) {
return Err(Error::UndefinedExport);
}

let module = self.get_main_module_instance()?;
let mut scope = self.main_worker.js_runtime.handle_scope();
let module = module.open(&mut scope);

let key = v8::String::new(&mut scope, name).ok_or(Error::FailedToGetV8Value)?;
let value = module
.get(&mut scope, key.into())
.ok_or(Error::FailedToGetV8Value)?;

Ok(v8::Global::new(&mut scope, value))
}
}

#[cfg(test)]
Expand Down

0 comments on commit c37c623

Please sign in to comment.