属于单个隔离的多个 V8 上下文对象的 shared/discrete 是什么?

What is shared/discrete across multiple V8 context objects belonging to a single isolate?

我了解 v8::Isolate 的概念,并且我了解运行的所有内容都在与隔离关联的 v8::Context 内部运行。与不同隔离区关联的上下文不能共享任何内容。

我的问题是,在单个隔离中有多个上下文是什么意思?上下文之间共享什么?它们之间有什么离散的?你什么时候可以在一个中制作一些东西并在另一个中使用它?通常在 API 中,如果某物在创建时采用隔离或上下文,这似乎几乎是任意的。

此外,欢迎任何有关在单个隔离中使用多个上下文的用例的建议,以帮助我开始更好地理解它们。

我看到了这个问题:What exactly is the difference between v8::Isolate and v8::Context? 但它并没有真正详细说明 how/why 你会在一个单独的环境中使用多个上下文。

谢谢。

我从邮件列表中得到的信息是几乎所有东西都可以在同一隔离区中跨上下文共享,函数除外。

It depends on the security policy. By default, everything is shared.

You can turn on access checks with v8::ObjectTemplate::SetAccessCheckCallback() to block access on a per-property basis or disallow sharing altogether by changing the security token with v8::Context::SetSecurityToken().

Aside: I believe the reason you need to pass a context to v8::Object::Set() is to disambiguate the overloaded function. C++ doesn't allow overloading on just the return type.

It's also slightly faster. The non-context version of Set() looks up the current context and calls the contextified Set().

:

Define "everything"? Could I take any javascript program and take each line and run it in a different context (on the same isolate) and it would work? At least theoretically, with no fundamental changes to the program?

:

Not quite. The fundamental unit of execution in V8 is the function. Functions belong to the context they're compiled in.