11cb0ef41Sopenharmony_ci#include "node.h"
21cb0ef41Sopenharmony_ci#include "v8.h"
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#include <assert.h>
51cb0ef41Sopenharmony_ci#include <vector>
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cinamespace {
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_civoid MakeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
101cb0ef41Sopenharmony_ci  assert(args[0]->IsObject());
111cb0ef41Sopenharmony_ci  assert(args[1]->IsFunction() || args[1]->IsString());
121cb0ef41Sopenharmony_ci  auto isolate = args.GetIsolate();
131cb0ef41Sopenharmony_ci  auto recv = args[0].As<v8::Object>();
141cb0ef41Sopenharmony_ci  std::vector<v8::Local<v8::Value>> argv;
151cb0ef41Sopenharmony_ci  for (size_t n = 2; n < static_cast<size_t>(args.Length()); n += 1) {
161cb0ef41Sopenharmony_ci    argv.push_back(args[n]);
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci  v8::Local<v8::Value> result;
191cb0ef41Sopenharmony_ci  if (args[1]->IsFunction()) {
201cb0ef41Sopenharmony_ci    auto method = args[1].As<v8::Function>();
211cb0ef41Sopenharmony_ci    result =
221cb0ef41Sopenharmony_ci        node::MakeCallback(isolate, recv, method, argv.size(), argv.data(),
231cb0ef41Sopenharmony_ci                           node::async_context{0, 0}).ToLocalChecked();
241cb0ef41Sopenharmony_ci  } else if (args[1]->IsString()) {
251cb0ef41Sopenharmony_ci    auto method = args[1].As<v8::String>();
261cb0ef41Sopenharmony_ci    result =
271cb0ef41Sopenharmony_ci        node::MakeCallback(isolate, recv, method, argv.size(), argv.data(),
281cb0ef41Sopenharmony_ci                           node::async_context{0, 0}).ToLocalChecked();
291cb0ef41Sopenharmony_ci  } else {
301cb0ef41Sopenharmony_ci    assert(0 && "unreachable");
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(result);
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_civoid Initialize(v8::Local<v8::Object> exports) {
361cb0ef41Sopenharmony_ci  NODE_SET_METHOD(exports, "makeCallback", MakeCallback);
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci}  // anonymous namespace
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciNODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
42