v8libv8

How is javascript compiled by v8?


Take the hello-world as an example. I have couple of questions:

  1. What does v8::Isolate do? Does it create an new thread
  2. What does v8::Isolate::Scope do?
  3. What does v8::HandleScope do?
  4. What does v8::Local<v8::Context> do?
  5. What does v8::Script::Compile do? Does it compile the js code directly to machine code?

Thanks your for your help!


Solution

  • See the official wiki:

    • An isolate is a VM instance with its own heap.
    • A local handle is a pointer to an object. All V8 objects are accessed using handles, they are necessary because of the way the V8 garbage collector works.
    • A handle scope can be thought of as a container for any number of handles. When you've finished with your handles, instead of deleting each one individually you can simply delete their scope.
    • 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.

    These concepts are discussed in greater detail in the Embedder's Guide.

    Once you've read the existing documentation, if anything is still unclear, please ask more specific questions.

    Regarding (5): In current versions of V8, v8::Script::Compile compiles bytecode for V8's interpreter. In earlier versions, it compiled unoptimized machine code. The difference is an internal implementation detail that you don't need to worry about :-)