Types of transfers in the Node.js native addon

Is it possible to create several enumin C ++ code from a Node.js addon and then expose this type to js code? I found that native types of enums exist in js, but there is no information about their implementation in the v8 engine.

+5
source share
3 answers

Please note that in the example provided by Kevin , you need to create an instance Local<Object>before using it, so make sure you call Object::New().

Local<Object> obj = Object::New();
const char* k = "HEADERS_RECEIVED";
int v = 1;
obj->Set(v8::String::NewSymbol(k), v8::Int32::New(v), ReadOnly); // Use PropertyAttribute ReadOnly so that value won't be changed by javascript.
+4
source

javascript , int, , web, UNSENT,OPENED, HEADERS_RECEIVED,LOADING,DONE of XMLHttpRequest ++. v8 javascript, :

Local<Object> obj;
const char* k = "HEADERS_RECEIVED";
int v = 1;
obj->Set(v8::String::NewSymbol(k), v8::Int32::New(v), ReadOnly); // Use PropertyAttribute ReadOnly so that value won't be changed by javascript.
+3

Now this has changed according to the latest v8 API ref

Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> obj;
obj->DefineOwnProperty(
                       context,
                       String::NewFromUtf8(isolate,"enum"),
                       Number::New(isolate,1), v8::ReadOnly
                       );
+1
source

All Articles