Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

v8

I understand the idea of a v8::Isolate, and I understand that everything that runs, runs inside of a v8::Context that is associated with an isolate. Contexts associated with different isolates cannot share anything.

My question is, what does it mean to have multiple contexts in a single isolate? What is shared between the contexts? What is discrete between them? When can you make something in one and use it in another? Often in the API it seems almost arbitrary if something takes an isolate or a context while creating it.

Also, any suggestions as to use cases for multiple contexts in a single isolate would be welcome to help me start to understand them better.

I see this question: What exactly is the difference between v8::Isolate and v8::Context? but it doesn't really go into details as to how/why you'd use multiple contexts in a single isolate.

Thank you.

like image 865
xaxxon Avatar asked Jun 23 '16 02:06

xaxxon


People also ask

What is a V8 isolate?

An isolate is a concept of an instance in V8. In Blink, isolates and threads are in 1:1 relationship. One isolate is associated with the main thread. One isolate is associated with one worker thread. An exception is a compositor worker where one isolate is shared by multiple compositor workers.

What is a V8 context?

A context is an execution environment that allows separate, unrelated, JavaScript code to run in a single instance of V8. You must explicitly specify the context in which you want any JavaScript code to be run.


1 Answers

The information I got off the mailing list was that almost everything can be shared across contexts in the same isolate except functions.

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.

like image 61
xaxxon Avatar answered Jan 05 '23 00:01

xaxxon