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

我是研究僧i 提交于 2019-11-28 17:24:08

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/19383724/what-exactly-is-the-difference-between-v8isolate-and-v8context

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!