D
Deno

help

How to have private state in embedded deno_core JsRuntime?

Ddicksword_is_breaking_gdpr8/15/2023
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?
Mmmastrac8/15/2023
Check out the slot methods on the isolate, which you can get from a handlescope: https://docs.rs/v8/latest/v8/struct.Isolate.html#method.set_slot
Isolate in v8 - Rust
Isolate represents an isolated instance of the V8 engine. V8 isolates have completely separate states. Objects from one isolate must not be used in other isolates. The embedder can create multiple isolates and use them in parallel in multiple threads. An isolate can be entered by at most one thread at any given time. The Locker/Unlocker API...
Ddicksword_is_breaking_gdpr8/15/2023
Can i access that through JS? I need to take a reference to __context in runtime before i delete it
Mmmastrac8/15/2023
You can't access it through JS directly, but you could create #[op]s that read and write it. You can also just turn a local into a global and stash that -- as long as you don't store it anywhere the JS can't reach it
Ddicksword_is_breaking_gdpr8/15/2023
Yes I tried to create a global It needs a reference to Isolate, and converting the struct to V8 object needs the scope Both take a reference to &mut runtime
Mmmastrac8/15/2023
Oh, I mean you can create a v8::Global (this is different than context.global(...)) let global = v8::Global::new(&mut scope, local); Then you can stash that global anywhere you like... that'll keep the JS object alive, but inaccessible to JS
Mmmastrac8/15/2023
The open method would then allow you to get access to the underlying object (ie: v8::Global<v8::String> -> &v8::String) https://docs.rs/v8/latest/src/v8/handle.rs.html#209-211
handle.rs - source
Source of the Rust file src/handle.rs.
Ddicksword_is_breaking_gdpr8/15/2023
Oh, v8::Global accepts a &mut scope
Mmmastrac8/15/2023
Yeah, it's a bit confusing because &mut scope has an as_ref to &mut Isolate
Ddicksword_is_breaking_gdpr8/15/2023
Yep this seems to work, thanks If I delete __context it can't be recovered from outside the runtime.js file, right?
Mmmastrac8/15/2023
Correct.. it's no longer accessible from JS unless you put it back into a JS global or pass it to a function
Ddicksword_is_breaking_gdpr8/15/2023
I've been wondering if its possible to somehow extract it by overwriting object prototypes or something like that, which is why i copied freeze_instrinsics.js from node Not sure if there's an actual risk though

Looking for more? Join the community!

Recommended Posts
Issues getting local environment running in VSCode and Visual Studio 2022I'm a windows user trying to contribute on an github issue, but I've run into a few blockers. Using Relative import path "ioredis" not prefixed with / or ./ or ../```ts // main.ts import Redis from "ioredis"; ``` ```json // deno.json { "imports": { "ioredisPixi.js breaks DenoI'm trying to import Pixi in a Fresh island: ``` import { Sprite, Stage } from "npm:@pixi/react@7.1Confused by deno vendor-related behaviorsI created a main.ts containing this code: ``` import * as thrift from "npm:thrift"; let connection =How to release node:zlib resourcesI have some testcases which use the `node:zlib` polyfill and I'm having a bit of trouble getting theDeno Repl ProgrammaticallyPython has a really nice not-well-known tool for creating a repl at any point in the code (kinda likDeno for SASS dependencies?I'm trying to understand how I would migrate from Jekyll and NPM to Lume and Deno. Specifically, I uHow can I send a number across a Deno.TcpConn as stream of bytes?I need to send a number across a Deno.TcpConn as 4 bytes, so it can be read by a server as an int32.Migrating `Deno.run` to `Deno.command` with `readlines`I had the following: ```ts browserProcess = Deno.run({ cmd: buildArgs, // eg chrome exe path, thError: Not implemented: crypto.KeyObject.prototype.asymmetricKeyTypeHello. Im trying to create a RSA Keypair, using the node **crypto.generateKeyPair** function, but i `Uncaught TypeError: encode is not a function` when using npm module `@kubernetes-client/node`I'm trying to use a particular call in the npm module `@kubernetes/client-node` from deno, but I'm gLinking to external codeif i want to link to external code, do i have to import it from https://deno.land?Impossible to install Deno on Linux Ubuntu LTS (22.04) x86_64Hello Context & Tech : Trying to install Deno on Linux Ubuntu LTS v22.04 on x86_64 architecture PrGet length of video (stream. m3u8)In deno/typescript, how can i get the length of a specific video stream (m3u8)?github account blocked errori changed email my old account to another account and when i login to deno deploy with that account npm:axios not returning anythingI'm trying to troubleshoot a problem with axios (the same snippet works fine on node). I'm callingextension method returning undefined for `text/plain`Given the following: ```ts console.log( "text/plain", extension("text/plain"), extensioServing multiple static files to an HTTP requestIf a client sends a request for an array of static files names like so. ```["component01.js", "compoKv encodes integer as doubleAnyone know why Deno.kv encodes an integer in a mutli-part key as a double? ``` const key = ["int",Looking for caching API ResponsesHello there, I'm looking for a package/or examples to cache API responses for a fresh project whic