forked from XadillaX/nyaa-nodejs-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_field.cpp
75 lines (59 loc) · 1.87 KB
/
internal_field.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <node.h>
namespace __internal_field_right__ {
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::ObjectTemplate;
using v8::FunctionTemplate;
using v8::Function;
using v8::Isolate;
using v8::Local;
using v8::Persistent;
using v8::External;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Number;
using v8::Array;
using v8::MaybeLocal;
struct PersistentWrapper {
Persistent<Object> persistent;
int value;
};
void WeakCallback(const v8::WeakCallbackInfo<PersistentWrapper>& data)
{
PersistentWrapper* wrapper = data.GetParameter();
if(!wrapper->persistent.IsNearDeath())
{
printf("WARN...\n");
}
printf("deleting 0x%.8X: %d...", wrapper, wrapper->value); // NOLINT
wrapper->persistent.Reset();
delete wrapper;
printf("ok\n");
}
void CreateObject(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
// 新建对象模板
Local<ObjectTemplate> templ = ObjectTemplate::New(isolate);
templ->SetInternalFieldCount(1);
// 新建对象以及设置内置字段
PersistentWrapper* wrapper = new PersistentWrapper();
wrapper->value = args[0]->ToNumber()->Int32Value();
Local<Object> obj = templ->NewInstance();
obj->SetInternalField(0, External::New(isolate, wrapper));
// 基于 obj 新建持久句柄
wrapper->persistent.Reset(isolate, obj);
// 将持久句柄设置为弱持久句柄
wrapper->persistent.SetWeak(wrapper, WeakCallback, v8::WeakCallbackType::kInternalFields);
wrapper->persistent.MarkIndependent();
args.GetReturnValue().Set(obj);
}
void Init(Local<Object> exports, Local<Object> module)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
exports->Set(String::NewFromUtf8(isolate, "create"), FunctionTemplate::New(isolate, CreateObject)->GetFunction());
}
NODE_MODULE(_template, Init)
}