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

[question] How do scopes work? #1462

Open
dev-ardi opened this issue Apr 15, 2024 · 3 comments
Open

[question] How do scopes work? #1462

dev-ardi opened this issue Apr 15, 2024 · 3 comments

Comments

@dev-ardi
Copy link
Contributor

fn main() {
    let platform = v8::new_default_platform(0, false).make_shared();
    v8::V8::initialize_platform(platform);
    v8::V8::initialize();

    let isolate = &mut v8::Isolate::new(Default::default());
    let handle_scope = &mut v8::HandleScope::new(isolate);
    let context = v8::Context::new(handle_scope);
    let scope = &mut v8::ContextScope::new(handle_scope, context);

    {
        let scope = &mut v8::ContextScope::new(scope, context);
        let code = v8::String::new(scope, "let x = 3;").unwrap();
        let script = v8::Script::compile(scope, code, None).unwrap();
        script.run(scope).unwrap();
    }
    {
        let scope = &mut v8::ContextScope::new(scope, context);
        let code = v8::String::new(scope, "let x = 3;").unwrap();
        let script = v8::Script::compile(scope, code, None).unwrap();
        script.run(scope).unwrap();
    }
}

I'm not sure how scopes work, I would expect this to work but I get the error <unknown>:0: Uncaught SyntaxError: Identifier 'x' has already been declared.

I would expect that doing something in new scopes would be the equivalent of

{
  let x = 3;
}
{
  let x = 3;
}

which would work.

If I've misunderstood the purpose of scopes could you

  1. Clarify their purpose
  2. Explain how to do what I want to do? (not leak local variables)

After that I can submit a PR adjusting the documentation

@bartlomieju
Copy link
Member

For 1. I suggest to read through this section from the V8 blog: https://v8.dev/docs/embed#handles-and-garbage-collection. TLDR: scopes are not JS scopes, but scopes for handles of V8 objects.

For 2. you wrote the solution yourself - just wrap your code in curly braces and then you will create lexical JS scopes that will not leak variables between them.

@dev-ardi
Copy link
Contributor Author

dev-ardi commented Apr 15, 2024

let script = format!("{{{script}}}");

I temporarilly went with this but it feels really bad. Is there really not a better way?

By the way, thank you so much for pointing me to that article, I didn't know it existed. Maybe it's a good idea for it to be somewhere in the docs?

@bartlomieju
Copy link
Member

let script = format!("{{{script}}}");

I temporarilly went with this but it feels really bad. Is there really not a better way?

I don't really know a native API that allows you to create lexical scopes.

By the way, thank you so much for pointing me to that article, I didn't know it existed. Maybe it's a good idea for it to be somewhere in the docs?

That sounds like a good idea. I think we could add it to docstrings for all scope types. PRs are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants