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 ;}