Google V8 programming (3) handle & handlescope

Source: Internet
Author: User

The previous chapter briefly demonstrated a helloworld demo. This section describes the basic types and concepts of V8.

Here we will post the demo to facilitate subsequent analysis:

#include <v8.h>using namespace v8;int main(int argc, char* argv[]) {  // Create a stack-allocated handle scope.  HandleScope handle_scope;  // Create a new context.  Persistent<Context> context = Context::New();  // Enter the created context for compiling and  // running the hello world script.  Context::Scope context_scope(context);  // Create a string containing the JavaScript source code.  Handle<String> source = String::New("'Hello' + ', World!'");  // Compile the source code.  Handle<Script> script = Script::Compile(source);  // Run the script to get the result.  Handle<Value> result = script->Run();  // Dispose the persistent context.  context.Dispose();  // Convert the result to an ASCII string and print it.  String::AsciiValue ascii(result);  printf("%s\n", *ascii);  return 0;}

Handle

In V8, memory allocation is performed in V8 heap, and JavaScript values and objects are also stored in V8 heap. This heap is maintained independently by V8. The unreferenced objects will be GC by V8 and can be re-allocated to other objects. Handle is a reference to objects in heap. To manage memory allocation, GC needs to track all objects in V8, and the objects are referenced in handle mode. Therefore, GC needs to manage handle, in this way, GC can know the reference of an object in heap. When the handle reference of an object is changed, GC can recycle or move the object. Therefore, handle must be used in V8 programming to reference an object, rather than directly obtaining the object reference through C ++, directly referencing an object in the C ++ way will make the object unable to be managed by V8.

Handle can be divided into local and persistent. Literally, local is local, and it is managed by handlescope at the same time. Persistent, similar to the global one, is not managed by handlescope. Its scope can be extended to different functions, while local is local and the scope is relatively small. Persistent handle objects must be paired with persistent: new, persistent: dispose, similar to new and delete. Persistent: makeweak in C ++, which can be used to weaken a persistent
Handle. If the unique reference of an object is a persistent, you can use the makeweak method to retrieve the referenced object.

Handlescope

There can be many handle in a function, while handlescope is equivalent to a container used to hold handle (local). When the handlescope lifecycle ends, handle will also be released, it will update the object reference in heap. Handlescope is allocated to the stack and cannot be created using the new method. There can be multiple handlescopes in the same scope. The new handlescope will overwrite the previous handlescope and manage the local handle. The following code describes the lifecycle of handlescope:

# Include <v8.h> using namespace V8; int main (INT argc, char * argv []) {// create a stack-allocated handle scope. handlescope handle_scope; // >>>>>>>>>>>>>>>>>>>>>>> from here, is the beginning of the lifecycle of handlescope // all the local handle thereafter are managed by this handle_scope object // create a new context. persistent <context> context = context: New (); // persistent handle // enter the created context for compiling and // running the Hello world s Invalid. context: Scope context_scope (context); // create a string containing the JavaScript source code. handle <string> source = string: New ("'hello' + ', world! '"); // Local handle // compile the source code. handle <SCRIPT> script = Script: Compile (source); // local handle // run the script to get the result. handle <value> result = script-> Run (); // local handle // dispose the persistent context. context. dispose (); // convert the result to an ASCII string and print it. string: asciivalue ASCII (result); printf ("% s \ n", * ASCII); Return 0; // <> the lifecycle of handle_scope ends, the Destructor will be called, and all local handle in it will be released}

Context

Context is the execution environment of JavaScript. Each JavaScript must be executed in a context. There are multiple contexts, and you can switch between different contexts.

  Persistent<Context> context = Context::New();   Context::Scope context_scope(context);

This code is to apply for a persistent contetxt and switch to the context through context: scope. In this demo,

 Context::Scope context_scope(context);

All subsequent operations are executed in context.

We can also use

Persistent<Context> context_Ex = Context::New(); Context::Scope context_scope_Ex(context_Ex);

To switch to context_ex.

Here is just a simple understanding of the context concept, and a separate chapter will be opened later to detail the V8 context.


From this figure, we can clearly see the relationships between handle, handlescope, and the objects referenced by handle. As you can see, V8 objects exist in the heap of V8, while handle is a reference to this object.

Copyright statement:
Reprinted articles please indicate the source of the original, any for commercial purposes, please contact me: hyman_tan@126.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.