Zookeeper
The previous article introduced the encoding section in the Node. js C ++ module. Before that, I would like to list some information in this area for your reference.
V8 Data Structure Manual
Node C ++ module entry
Understanding node from the perspective of C ++
(Google Baidu, these materials can guide us to write a complete Node. js C ++ extension. However, the following content may make the writing process easier .)
First, let's understand
Function Parameters
The methods that can be called by node in the C ++ module are as follows:
Handle
Method(const Arguments& args) { //code here}
The input parameter args object has two common operations:
Local
Arg = args [0]; // [] operator to obtain a Local
The object int length = args. Length (); // Length method to obtain the parameter length
The above code shows the local, v8 inheritance relationship as follows:
Handle is an object reference maintained by v8. v8 is responsible for the collection of objects and can be seen as a smart pointer in v8. Local Century City is a lightweight object reference
Data Type Conversion
The overall data structure diagram in v8 is as follows:
We can see that allString, Number, Boolean, Object, ArrayAnd other objects are fromValue. ThereforeArgumentsObtained inLocalThe object can easily determine the specific type in its js and convert it.
Type determination:
Local
arg = args[0];bool isArray = arg->IsArray();bool isBoolean = arg->IsBoolean();bool isNumber = arg->IsNumber();bool isInt32 = arg->IsInt32();
V8 provides a series of interfaces for type determination. You can find all the judgment interfaces in its documentation.
Type conversion:
After determining the type, you can convert the type based on the result:
Local
Arg = args [0]; Local
= Arg-> ToObject (); Local
= Arg-> ToBoolean (); Local
= Arg-> ToNumber (); Local
= Arg-> ToInt32 (); // more
Similarly, v8 provides a series of interfaces for type conversion. You can find all the conversion interfaces in its documentation. Note that v8 does not provide direct conversion from Value to Array, but we find that Array inherits from the Object. In fact, the Array Object does not provide more interfaces than the Object. When you contact js, you will find that the Array and Object operations in js are the same. Therefore, although v8 does not provide a conversion from Value to Array, it is enough to convert to an Object because Array can be operated as an Object.
All objects such as Boolean, Number, and Int32 in v8 can be converted to C ++ native bool, double, and int types. Of course, there are also interfaces for reverse conversion. Therefore, data type conversion from javascript to C ++ is not a problem at all.
Object and Array
Basic types are relatively simple, while objects and arrays require more interface methods for setting and obtaining content.
// Set ObjectHandle
V8Obj = v8: Object: New (); v8Obj-> Set (v8: String: NewSymbol ("key"), v8: Integer :: new (1); // query whether this key exists and obtain the corresponding valueif (v8Obj-> Has (v8: String: New ("key "))) {Handle
Value = v8Obj-> Get (v8: String: New ("key"); // obtain all keyHandle keys = v8Obj-> GetOwnPropertyNames (); // get the number of elements in the object int len = keys-> Length (); // Delete the element v8Obj-> Delete (v8: String :: new ("key "));
Do you think the above code is very familiar?
var obj = {};obj.key = 1;if (obj.hasOwnProperty('key')) { var value = obj.key;}var keys = Object.keys(obj);var len = keys.length;delete obj.key;
As you can see, the JavaScript code is fully mapped to the C ++ code.
The same is true for Array. First, use a graph to describe the relationship between Array and Object:
In V8, the Array interface is basically only one moreLengthMethod to obtain the length of the Array, while other methods are inherited from the Object, so the Array operation is very similar to the Object.
Handle
v8Arr = v8::Array::New(length);int length = 10;for (int i = 0; i != length; ++i) { v8Arr->Set(i, v8::Integer::New(i));}Handle
item = v8Arr->Get(10);
At this point, the data structure in js corresponds to the Section in v8, and the data exchange between js and c ++ is not a problem at all. At this time, use c ++ to write Node. there is no problem with js extension.
Zookeeper