• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "node.h"
2 #include "v8.h"
3 
4 #include <assert.h>
5 #include <vector>
6 
7 namespace {
8 
MakeCallback(const v8::FunctionCallbackInfo<v8::Value> & args)9 void MakeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
10   assert(args[0]->IsObject());
11   assert(args[1]->IsFunction() || args[1]->IsString());
12   auto isolate = args.GetIsolate();
13   auto recv = args[0].As<v8::Object>();
14   std::vector<v8::Local<v8::Value>> argv;
15   for (size_t n = 2; n < static_cast<size_t>(args.Length()); n += 1) {
16     argv.push_back(args[n]);
17   }
18   v8::Local<v8::Value> result;
19   if (args[1]->IsFunction()) {
20     auto method = args[1].As<v8::Function>();
21     result =
22         node::MakeCallback(isolate, recv, method, argv.size(), argv.data(),
23                            node::async_context{0, 0}).ToLocalChecked();
24   } else if (args[1]->IsString()) {
25     auto method = args[1].As<v8::String>();
26     result =
27         node::MakeCallback(isolate, recv, method, argv.size(), argv.data(),
28                            node::async_context{0, 0}).ToLocalChecked();
29   } else {
30     assert(0 && "unreachable");
31   }
32   args.GetReturnValue().Set(result);
33 }
34 
Initialize(v8::Local<v8::Object> exports)35 void Initialize(v8::Local<v8::Object> exports) {
36   NODE_SET_METHOD(exports, "makeCallback", MakeCallback);
37 }
38 
39 }  // anonymous namespace
40 
41 NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
42