DenoDDeno
Powered by
dicksword_is_breaking_gdprD
Denoβ€’3y agoβ€’
18 replies
dicksword_is_breaking_gdpr

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
delete globalThis.__context
, but I can't do this since values I get from
runtime.handle_scope()
runtime.handle_scope()
are
Local
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?
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

How to Extensions in JSRuntime ?
Ram Durga SaiRRam Durga Sai / help
3y ago
How to use performance.now in deno_core
JasithJJasith / help
4mo ago