Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate updated module #885

Open
gearonixx opened this issue Aug 28, 2024 · 3 comments
Open

Evaluate updated module #885

gearonixx opened this issue Aug 28, 2024 · 3 comments

Comments

@gearonixx
Copy link

async fn run_js(file_path: &str) -> Result<(), AnyError> {
    let main_module = deno_core::resolve_path(file_path, env::current_dir()?.as_path())?;
    let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
        module_loader: Some(Rc::new(TsModuleLoader)),
        startup_snapshot: Some(RUNTIME_SNAPSHOT),
        extensions: vec![runjs::init_ops()],
        ..Default::default()
    });

    fs::write(file_path, "console.log(\"1\")")?;

    // js_runtime.lazy_load_es_module_with_code()
    let mod_id = js_runtime.load_main_es_module(&main_module).await?;
    let _ = js_runtime.mod_evaluate(mod_id).await?;
    // [out]: "hello 1"
    js_runtime.run_event_loop(Default::default()).await?;

    fs::write(file_path, "console.log(\"2\")")?;
    
    // should log [out]: "hello 2"
    let _ = js_runtime.mod_evaluate(mod_id).await?;
    // got error
    // assertion `left == right` failed: Module already evaluated.
    // Perhaps you've re-provided a module or extension that was already included in the snapshot

    Ok(())
}

Should I create a new JsRuntime instance and load the updated file?

@marvinhagemeister
Copy link
Contributor

marvinhagemeister commented Aug 28, 2024

.mod_evaluate() evaluates modules that have already been loaded into memory. It doesn't reload files from disk.

@gearonixx
Copy link
Author

    let main_module = deno_core::resolve_path(file_path, env::current_dir()?.as_path())?;
 
    fs::write(file_path, "console.log(\"1\")")?;

    // js_runtime.lazy_load_es_module_with_code()
    let mod_id = js_runtime.load_side_es_module(&main_module).await?;
    let _ = js_runtime.mod_evaluate(mod_id).await?;
    // [out]: "hello 1"
    js_runtime.run_event_loop(Default::default()).await?;

    // main_module updated here, now it contains 
    // source code below
    fs::write(file_path, "console.log(\"2\")")?;

    // Maybe something like .update_side_es_module?
    let mod_id = js_runtime.load_side_es_module(&main_module).await?;

    let _ = js_runtime.mod_evaluate(mod_id).await?;
    // should log [out]: "hello 2"
    // but got error
    // assertion `left == right` failed: Module already evaluated.
    // Perhaps you've re-provided a module or extension that was already included in the snapshot
    js_runtime.run_event_loop(Default::default()).await?;

Ok, now I understand. Is this possible in deno_core? How can such functionality be achieved?

@gearonixx
Copy link
Author

@marvinhagemeister opened a pull request here #890

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants