Getting exports from an evaluated module with get_module_namespace always results in null values

Trying to get module exports like the example here The module is loaded with load_main_module, I call mod_evaluate on the ID returned, then do the below: But the value returned is always undefined Js:
export function test() { return 2;}
export function test() { return 2;}
Rust:
let module_namespace = self.deno_runtime.get_module_namespace(module_id)?;
let mut scope = self.deno_runtime.handle_scope();
let module_namespace = v8::Local::<v8::Object>::new(&mut scope, module_namespace);

let key = v8::String::new(&mut scope, "test").unwrap();
let value = module_namespace.get(&mut scope, key.into()).unwrap();

println!("{}", value.is_undefined()); // true
let module_namespace = self.deno_runtime.get_module_namespace(module_id)?;
let mut scope = self.deno_runtime.handle_scope();
let module_namespace = v8::Local::<v8::Object>::new(&mut scope, module_namespace);

let key = v8::String::new(&mut scope, "test").unwrap();
let value = module_namespace.get(&mut scope, key.into()).unwrap();

println!("{}", value.is_undefined()); // true
GitHub
feat(core): Adds a new method get_module_namespace to access the na...
Allows users to access the namespace object of a module. Example - Extracting the default export binding of a JavaScript module using deno_core ... let module_namespace = runtime.get_module_namespa...
6 Replies
Deno AI Helper
Deno AI Helper15mo ago
I'm sorry, but the documentation does not provide a clear answer to your question about why you're getting null values when trying to get exports from an evaluated module with get_module_namespace. It might be a good idea to check if the module is correctly loaded and evaluated before trying to access its exports. If the problem persists, you might want to ask for help in a Deno related community or forum, providing them with the complete code you're working with.
baphomet_the_traveller
I know the module is loaded because I can pull values from global. It's just exports that don't work @mmastrac hate to bug you again, but
mmastrac
mmastrac15mo ago
TBH I'm not familiar with that particular v8 API. It looks like it should work, but I'm not sure why it isn't. Sorry about that. Someone else may be able to help, however.
baphomet_the_traveller
Alright!! I figured it out It was a small error in how I captured the module_id from the async move tokio was blocking on Can valid ModuleIds ever be 0? Since they are a usize Because if not, a check for that might be useful
mmastrac
mmastrac15mo ago
NonZeroUsize in std::num - Rust
An integer that is known not to equal zero.
baphomet_the_traveller
https://github.com/denoland/deno_core/blob/2af63b92b1e26871d44c6cf5db57464a761911f4/core/modules/map.rs#L381 Loader is created with next_load_id of 1 So it should never be 0 I think a NonZeroUsize makes more sense in that context, since usize has a default value which is invalid here Seems the 0 module_id is actually being used in a few spots to represent the lack of loaded modules, by reading it as self.handles.len()