extern "C" void NSMain(const v8::FunctionCallbackInfo<v8::Value>& args) { auto isolate = args.GetIsolate(); auto len = args.Length(); if (len != 5) { auto errMsg = v8::String::NewFromUtf8(isolate, "Wrong number of arguments (expected 5)"); auto err = v8::Exception::Error(errMsg); isolate->ThrowException(err); return; } auto exports = args[1].As<v8::Object>(); auto ft = v8::FunctionTemplate::New(isolate, AddFuncCallback); auto ctx = isolate->GetCurrentContext(); auto maybeFunc = ft->GetFunction(ctx); if (maybeFunc.IsEmpty()) { auto errMsg = v8::String::NewFromUtf8(isolate, "Cannot create 'add' function"); auto err = v8::Exception::Error(errMsg); isolate->ThrowException(err); return; } auto func = maybeFunc.ToLocalChecked(); auto propName = v8::String::NewFromUtf8(isolate, "add"); auto result = exports->Set(ctx, propName, func); }
inline v8::Local<v8::Object> node::Function::construct(v8::Isolate* isolate, const v8::Local<v8::Function> &function, size_t argc, const v8::Local<v8::Value> arguments[]) { Nan::TryCatch trycatch; auto result = Nan::NewInstance(function, (int)argc, const_cast<v8::Local<v8::Value>*>(arguments)); if (trycatch.HasCaught()) { throw node::Exception(isolate, trycatch.Exception()); } return result.ToLocalChecked(); }
inline v8::Local<v8::Value> node::Function::call(v8::Isolate* isolate, const v8::Local<v8::Function> &function, const v8::Local<v8::Object> &this_object, size_t argc, const v8::Local<v8::Value> arguments[]) { Nan::TryCatch trycatch; auto recv = this_object.IsEmpty() ? isolate->GetCurrentContext()->Global() : this_object; auto result = Nan::Call(function, recv, (int)argc, const_cast<v8::Local<v8::Value>*>(arguments)); if (trycatch.HasCaught()) { throw node::Exception(isolate, trycatch.Exception()); } return result.ToLocalChecked(); }
Local<Value> valToString(MDB_val &data) { auto resource = new CustomExternalStringResource(&data); auto str = Nan::New<v8::String>(resource); return str.ToLocalChecked(); }