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

runtime::MainWorker::execute_script support return value #16065

Closed
gofenix opened this issue Sep 28, 2022 · 1 comment
Closed

runtime::MainWorker::execute_script support return value #16065

gofenix opened this issue Sep 28, 2022 · 1 comment
Labels
help wanted community help requested runtime Relates to code in the runtime crate

Comments

@gofenix
Copy link
Contributor

gofenix commented Sep 28, 2022

Now, the execute_script:

  /// See [JsRuntime::execute_script](deno_core::JsRuntime::execute_script)
  pub fn execute_script(
    &mut self,
    script_name: &str,
    source_code: &str,
  ) -> Result<(), AnyError> {
    self.js_runtime.execute_script(script_name, source_code)?;
    Ok(())
  }

I want to get the value from execute_script function. Maybe we could add a function like this:

  pub fn execute_script_with_return(
    &mut self,
    script_name: &str,
    source_code: &str,
  ) -> Result<v8::Global<v8::Value>, AnyError> {
    self.js_runtime.execute_script(script_name, source_code)
  }

Then:

// other code

let res = worker.execute_script_with_return(
    "test",
    r#"
    console.log("123");
    console.log(Deno);
    1+1;
  "#,
  );

  let v1 = res.unwrap().into_raw();

  unsafe {
    // Create a new Isolate and make it the current one.
    let isolate = &mut v8::Isolate::new(v8::CreateParams::default());

    // Create a stack-allocated handle scope.
    let handle_scope = &mut v8::HandleScope::new(isolate);

    // Create a new context.
    let context = v8::Context::new(handle_scope);

    // Enter the context for compiling and running the hello world script.
    let scope = &mut v8::ContextScope::new(handle_scope, context);

    println!("{:?}", v1.as_ref().to_string(scope).unwrap().to_rust_string_lossy(scope));
  }

// other code
@bartlomieju
Copy link
Member

Seems reasonable, PRs are welcome

@bartlomieju bartlomieju added help wanted community help requested runtime Relates to code in the runtime crate labels Dec 15, 2022
@gofenix gofenix closed this as completed Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted community help requested runtime Relates to code in the runtime crate
Projects
None yet
Development

No branches or pull requests

2 participants