What exactly is the difference between v8::Isolate and v8::Context?
Asked Answered
M

2

56

What is the difference/connection between these objects in V8? Does a context "belong" to an Isolate or vice versa?

I know that a single Isolate may only be accessed by one thread at a time (and that's what v8::Locker is for I guess?).

I've looked through the docs but I can't seem to get a grasp on these concepts - any help is appreciated!

Motoneuron answered 15/10, 2013 at 14:24 Comment(0)
C
58

I'm sure the following is a simplification, but it works for me.

An isolate is an independent copy of the V8 runtime, including a heap manager, a garbage collector, etc. Only one thread may access a given isolate at a time, but different threads may access different isolates simultaneously.

An isolate is not sufficient for running scripts, however. You also need a global (root) object. A context defines a complete script execution environment by designating an object in an isolate's heap as a global object.

Therefore, not only can many contexts "exist" in a given isolate, but they can also share any or all of their objects easily and safely. That's because their objects actually belong to the isolate and are protected by the isolate's exclusive lock.

Conney answered 16/10, 2013 at 2:44 Comment(4)
The last paragraph confuses me more than the actual concepts of v8isolate and v8context. If the objects are stored in a context, how can multiple contexts share objects?Israelitish
Objects are stored in the isolate's heap and can therefore be shared among all contexts within the isolate.Conney
Knowing that contexts can share objects is a real "aha" moment for me. I haven't seen this stated elsewhere, so thanks for writing this.Sjoberg
One might say a context befriends all other scope-receiver-object-parameter-member-method-contexts.Fishing
U
51

Isolates, as the name suggests, are completely closed to the outside world, so Isolates can run in parallel since they are different instances of V8 entirely. Think of an Isolate as a sandbox--a V8 runtime environment.

Now within an Isolate, you are likely to have numerous unrelated JavaScript applications running simultaneously. JavaScript provides a lot of global-level language facilities, and having multiple unrelated applications mess with these "singletons" is not a good idea. So within an instance of V8 called an Isolate, you can define multiple Contexts so that unrelated applications can do what they need to do without interfering with each other.

This is not a perfect analogy, but if you know Java web stuff, imagine multiple instances of Tomcat deployed on the same machine and then each instance of Tomcat running separate applications with their own web contexts and web.xml's. It's kind of like that.

Hope that helps.

Unblushing answered 15/10, 2013 at 14:46 Comment(1)
isolates share the same Platform information, so they're not ENTIRELY isolated. They aren't different "entirely"Schwitzer

© 2022 - 2024 — McMap. All rights reserved.