dicksword_is_breaking_gdpr
dicksword_is_breaking_gdpr
DDeno
Created by dicksword_is_breaking_gdpr on 8/15/2023 in #help
How to have private state in embedded deno_core JsRuntime?
I need to either keep a reference in rust to an object I then delete globalThis.__context, but I can't do this since values I get from runtime.handle_scope() are Local and need a mutable reference to runtime, and I don't think I can make them global, since that also requires a mutable reference to runtime to get the isolate. Another option that works would be keeping the state in rust and accessing it through ops, but I'm not sure how to do that either.
{
let mut scope = runtime.handle_scope();
let global = scope.get_current_context().global(&mut scope);

let key = v8::String::new(&mut scope, "__context").unwrap();
let context = deno_core::serde_v8::to_v8(&mut scope, &context)?;
global.set(&mut scope, key.into(), context.into());
};

runtime.execute_script_static("<runtime>", include_str!("runtime.js"))?; // deletes __context
runtime.execute_script_static("<freeze>", include_str!("freeze.js"))?;

let value = runtime.execute_script("<eval>", context.code.into())?; // shouldn't have access to __context
let value = runtime.resolve_value(value).await?;

// get the __context here somehow?
{
let mut scope = runtime.handle_scope();
let global = scope.get_current_context().global(&mut scope);

let key = v8::String::new(&mut scope, "__context").unwrap();
let context = deno_core::serde_v8::to_v8(&mut scope, &context)?;
global.set(&mut scope, key.into(), context.into());
};

runtime.execute_script_static("<runtime>", include_str!("runtime.js"))?; // deletes __context
runtime.execute_script_static("<freeze>", include_str!("freeze.js"))?;

let value = runtime.execute_script("<eval>", context.code.into())?; // shouldn't have access to __context
let value = runtime.resolve_value(value).await?;

// get the __context here somehow?
19 replies