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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include "node.h"
#include <assert.h>
#include <vector>
namespace {
using node::AsyncResource;
using v8::External;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;
int custom_async_resource_destructor_calls = 0;
class CustomAsyncResource : public AsyncResource {
public:
CustomAsyncResource(Isolate* isolate, Local<Object> resource)
: AsyncResource(isolate, resource, "CustomAsyncResource") {}
~CustomAsyncResource() {
custom_async_resource_destructor_calls++;
}
};
void CreateAsyncResource(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
assert(args[0]->IsObject());
AsyncResource* r;
if (args[1]->IsInt32()) {
r = new AsyncResource(isolate, args[0].As<Object>(), "foobär",
args[1].As<Integer>()->Value());
} else {
r = new AsyncResource(isolate, args[0].As<Object>(), "foobär");
}
args.GetReturnValue().Set(
External::New(isolate, static_cast<void*>(r)));
}
void DestroyAsyncResource(const FunctionCallbackInfo<Value>& args) {
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
delete r;
}
void CallViaFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
Local<String> name =
String::NewFromUtf8(isolate, "methöd").ToLocalChecked();
Local<Value> fn =
r->get_resource()->Get(isolate->GetCurrentContext(), name)
.ToLocalChecked();
assert(fn->IsFunction());
Local<Value> arg = Integer::New(isolate, 42);
MaybeLocal<Value> ret = r->MakeCallback(fn.As<Function>(), 1, &arg);
args.GetReturnValue().Set(ret.FromMaybe(Local<Value>()));
}
void CallViaString(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
Local<String> name =
String::NewFromUtf8(isolate, "methöd").ToLocalChecked();
Local<Value> arg = Integer::New(isolate, 42);
MaybeLocal<Value> ret = r->MakeCallback(name, 1, &arg);
args.GetReturnValue().Set(ret.FromMaybe(Local<Value>()));
}
void CallViaUtf8Name(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
Local<Value> arg = Integer::New(isolate, 42);
MaybeLocal<Value> ret = r->MakeCallback("methöd", 1, &arg);
args.GetReturnValue().Set(ret.FromMaybe(Local<Value>()));
}
void GetAsyncId(const FunctionCallbackInfo<Value>& args) {
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
args.GetReturnValue().Set(r->get_async_id());
}
void GetTriggerAsyncId(const FunctionCallbackInfo<Value>& args) {
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
args.GetReturnValue().Set(r->get_trigger_async_id());
}
void GetResource(const FunctionCallbackInfo<Value>& args) {
assert(args[0]->IsExternal());
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
args.GetReturnValue().Set(r->get_resource());
}
void RunSubclassTest(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Object> obj = Object::New(isolate);
assert(custom_async_resource_destructor_calls == 0);
CustomAsyncResource* resource = new CustomAsyncResource(isolate, obj);
delete static_cast<AsyncResource*>(resource);
assert(custom_async_resource_destructor_calls == 1);
}
void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "createAsyncResource", CreateAsyncResource);
NODE_SET_METHOD(exports, "destroyAsyncResource", DestroyAsyncResource);
NODE_SET_METHOD(exports, "callViaFunction", CallViaFunction);
NODE_SET_METHOD(exports, "callViaString", CallViaString);
NODE_SET_METHOD(exports, "callViaUtf8Name", CallViaUtf8Name);
NODE_SET_METHOD(exports, "getAsyncId", GetAsyncId);
NODE_SET_METHOD(exports, "getTriggerAsyncId", GetTriggerAsyncId);
NODE_SET_METHOD(exports, "getResource", GetResource);
NODE_SET_METHOD(exports, "runSubclassTest", RunSubclassTest);
}
} // anonymous namespace
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
|