V8 JavaScript Engine Getting Started Guide 4.1-basic concepts (handle, scope, context)

Source: Internet
Author: User
Handle, handle

There are two types of handles in V8:

1. Local handles: Local handle. The local handle is on the stack and deleted when the specified destructor is called. The survival time of these handles is determined by the handle range. The handle range is often created at the beginning of a function and deleted at the end of the function.

The local handle generated by class local can also be stored in the handle declared by the parent class handle.

2. Persistent handles: a persistent handle. The persistent handle is not allocated on the stack and is deleted only when you specify to remove them. A persistent handle is created by persistent: New and destroyed by persistent: dispose.

The persistent handle generated by the class persistent can also be stored in the handle declared by the parent class handle.

Scope: handle range

The handle range is the container of a handle, so you do not need to delete the handle one by one!

Context: Execution Environment

In V8, context is a javascript execution environment. After you create a context, you can enter and exit any time. You can also switch from one context to another.

 

Let's look at Hello world.
# Include using namespace V8; int main (INT argc, char * argv []) {// create a handle range. handlescope handle_scope; // create a context, which is a persistent handle. persistent <context> context = context: New (); // enter the previously created Context
Context: Scope context_scope (context); // This is a local handle. Handle <string> source = string: New ("'hello' + ', world! '"); // This is a local handle. Handle <SCRIPT> script = Script: Compile (source); // This is a local handle.
Handle <value> result = script-> Run (); // delete a permanent handle that must be displayed
Context. Dispose ();
// Before the function ends, handlescope will be destructed, and the local handle within the function range will also be deleted return 0 ;}

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.