2017-05-26 01:56:50 +08:00
|
|
|
#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;
|
|
|
|
|
2018-05-10 00:32:59 +08:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-26 01:56:50 +08:00
|
|
|
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 =
|
2020-07-08 04:03:17 +08:00
|
|
|
String::NewFromUtf8(isolate, "methöd").ToLocalChecked();
|
2017-05-26 01:56:50 +08:00
|
|
|
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 =
|
2020-07-08 04:03:17 +08:00
|
|
|
String::NewFromUtf8(isolate, "methöd").ToLocalChecked();
|
2017-05-26 01:56:50 +08:00
|
|
|
|
|
|
|
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>()));
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:20:03 +08:00
|
|
|
void GetAsyncId(const FunctionCallbackInfo<Value>& args) {
|
2017-05-26 01:56:50 +08:00
|
|
|
assert(args[0]->IsExternal());
|
|
|
|
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
|
2017-07-06 14:20:03 +08:00
|
|
|
args.GetReturnValue().Set(r->get_async_id());
|
2017-05-26 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:20:03 +08:00
|
|
|
void GetTriggerAsyncId(const FunctionCallbackInfo<Value>& args) {
|
2017-05-26 01:56:50 +08:00
|
|
|
assert(args[0]->IsExternal());
|
|
|
|
auto r = static_cast<AsyncResource*>(args[0].As<External>()->Value());
|
2017-07-06 14:20:03 +08:00
|
|
|
args.GetReturnValue().Set(r->get_trigger_async_id());
|
2017-05-26 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:20:03 +08:00
|
|
|
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());
|
2017-05-26 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 00:32:59 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-26 01:56:50 +08:00
|
|
|
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);
|
2017-07-06 14:20:03 +08:00
|
|
|
NODE_SET_METHOD(exports, "getAsyncId", GetAsyncId);
|
|
|
|
NODE_SET_METHOD(exports, "getTriggerAsyncId", GetTriggerAsyncId);
|
2017-05-26 01:56:50 +08:00
|
|
|
NODE_SET_METHOD(exports, "getResource", GetResource);
|
2018-05-10 00:32:59 +08:00
|
|
|
NODE_SET_METHOD(exports, "runSubclassTest", RunSubclassTest);
|
2017-05-26 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2018-02-06 04:02:18 +08:00
|
|
|
} // anonymous namespace
|
2017-05-26 01:56:50 +08:00
|
|
|
|
2017-09-06 04:53:06 +08:00
|
|
|
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
|