Nitwel
Nitwel4d ago

rusty_v8, how to mutate scope outside anonymous function

Hi, I'm trying to use the v8 rust create to run javascript on my backend and now I'm stuck at how to modify a Vec<String> declared outside a anonymous function scope, as said function gets used to be the callback of a v8::Function.
let logs = Vec::new();

let log_callback = |scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue| {
let arg = args.get(0);
let arg_string = arg.to_string(scope).unwrap().to_rust_string_lossy(scope);

logs.push(arg_string); // <---- How can I do this?

rv.set(v8::undefined(scope).into());
};

// https://github.com/matejkoncal/v8-experiments/blob/master/src/main.rs
let log_fn = v8::Function::new(scope, log_callback).unwrap().into();
let name = v8::String::new(scope, "log").unwrap().into();
let global = context.global(scope);
global.set(scope, name, log_fn);
let logs = Vec::new();

let log_callback = |scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue| {
let arg = args.get(0);
let arg_string = arg.to_string(scope).unwrap().to_rust_string_lossy(scope);

logs.push(arg_string); // <---- How can I do this?

rv.set(v8::undefined(scope).into());
};

// https://github.com/matejkoncal/v8-experiments/blob/master/src/main.rs
let log_fn = v8::Function::new(scope, log_callback).unwrap().into();
let name = v8::String::new(scope, "log").unwrap().into();
let global = context.global(scope);
global.set(scope, name, log_fn);
The errors I'm getting are the following:
error[E0277]: the trait bound `{closure@src\post_run_js.rs:23:24: 25:49}: MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>` is not satisfied
--> src\post_run_js.rs:35:43
|
35 | let log_fn = v8::Function::new(scope, log_callback).unwrap().into();
| ----------------- ^^^^^^^^^^^^ the trait `for<'a, 's, 'b> Fn(&'a mut HandleScope<'s>, FunctionCallbackArguments<'s>, ReturnValue<'b>)` is not implemented for closure `{closure@src\post_run_js.rs:23:24: 25:49}`, which is required by `{closure@src\post_run_js.rs:23:24: 25:49}: MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>`
| |
| required by a bound introduced by this call
|
= note: required for `extern "C" fn(*const FunctionCallbackInfo)` to implement `v8::support::MapFnFrom<{closure@src\post_run_js.rs:23:24: 25:49}>`
= note: required for `{closure@src\post_run_js.rs:23:24: 25:49}` to implement `MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>`
note: required by a bound in `v8::function::<impl Function>::new`
--> C:\Users\NilsPC\.cargo\registry\src\index.crates.io-6f17d22bba15001f\v8-0.105.1\src\function.rs:1006:20
|
1004 | pub fn new<'s>(
| --- required by a bound in this associated function
1005 | scope: &mut HandleScope<'s>,
1006 | callback: impl MapFnTo<FunctionCallback>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `v8::function::<impl Function>::new`

error[E0277]: the trait bound `&mut Vec<std::string::String>: Copy` is not satisfied in `{closure@src\post_run_js.rs:23:24: 25:49}`
--> src\post_run_js.rs:35:43
|
23 | let log_callback = |scope: &mut v8::HandleScope,
| ________________________-
24 | | args: v8::FunctionCallbackArguments,
25 | | mut rv: v8::ReturnValue| {
| |________________________________________________- within this `{closure@src\post_run_js.rs:23:24: 25:49}`
...
35 | let log_fn = v8::Function::new(scope, log_callback).unwrap().into();
| ----------------- ^^^^^^^^^^^^ within `{closure@src\post_run_js.rs:23:24: 25:49}`, the trait `Copy` is not implemented for `&mut Vec<std::string::String>`, which is required by `{closure@src\post_run_js.rs:23:24: 25:49}: MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>`
| |
| required by a bound introduced by this call
|
= note: `Copy` is implemented for `&Vec<std::string::String>`, but not for `&mut Vec<std::string::String>`
error[E0277]: the trait bound `{closure@src\post_run_js.rs:23:24: 25:49}: MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>` is not satisfied
--> src\post_run_js.rs:35:43
|
35 | let log_fn = v8::Function::new(scope, log_callback).unwrap().into();
| ----------------- ^^^^^^^^^^^^ the trait `for<'a, 's, 'b> Fn(&'a mut HandleScope<'s>, FunctionCallbackArguments<'s>, ReturnValue<'b>)` is not implemented for closure `{closure@src\post_run_js.rs:23:24: 25:49}`, which is required by `{closure@src\post_run_js.rs:23:24: 25:49}: MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>`
| |
| required by a bound introduced by this call
|
= note: required for `extern "C" fn(*const FunctionCallbackInfo)` to implement `v8::support::MapFnFrom<{closure@src\post_run_js.rs:23:24: 25:49}>`
= note: required for `{closure@src\post_run_js.rs:23:24: 25:49}` to implement `MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>`
note: required by a bound in `v8::function::<impl Function>::new`
--> C:\Users\NilsPC\.cargo\registry\src\index.crates.io-6f17d22bba15001f\v8-0.105.1\src\function.rs:1006:20
|
1004 | pub fn new<'s>(
| --- required by a bound in this associated function
1005 | scope: &mut HandleScope<'s>,
1006 | callback: impl MapFnTo<FunctionCallback>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `v8::function::<impl Function>::new`

error[E0277]: the trait bound `&mut Vec<std::string::String>: Copy` is not satisfied in `{closure@src\post_run_js.rs:23:24: 25:49}`
--> src\post_run_js.rs:35:43
|
23 | let log_callback = |scope: &mut v8::HandleScope,
| ________________________-
24 | | args: v8::FunctionCallbackArguments,
25 | | mut rv: v8::ReturnValue| {
| |________________________________________________- within this `{closure@src\post_run_js.rs:23:24: 25:49}`
...
35 | let log_fn = v8::Function::new(scope, log_callback).unwrap().into();
| ----------------- ^^^^^^^^^^^^ within `{closure@src\post_run_js.rs:23:24: 25:49}`, the trait `Copy` is not implemented for `&mut Vec<std::string::String>`, which is required by `{closure@src\post_run_js.rs:23:24: 25:49}: MapFnTo<extern "C" fn(*const FunctionCallbackInfo)>`
| |
| required by a bound introduced by this call
|
= note: `Copy` is implemented for `&Vec<std::string::String>`, but not for `&mut Vec<std::string::String>`
Please be aware that I'm still very new to Rust so extra insights into what's going wrong here would be very helpful!
0 Replies
No replies yetBe the first to reply to this messageJoin