void CRF::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new CRF(...)` CRF* obj = new CRF(); CRFPP::Tagger* tag = CRFPP::createTagger(get(args[0])); if(!tag){ isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, (const char *) CRFPP::getTaggerError()))); return; } v8::Local<v8::External> handle = v8::External::New(isolate, tag); v8::Persistent<v8::External, v8::CopyablePersistentTraits<v8::External> > tagger(isolate, handle); obj -> tagger = tagger; obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
void IdPool::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new IdPool(...)` double value = args[0]->IsUndefined() || !args[0]->IsNumber()? 0 : args[0]->NumberValue() / 8; if (value > DEFAULT_CTOR_MAX_POSSIBLE_SIZE) { JS_THROW(isolate, "Size too large"); return; } // ceil size_t poolSize = value + (size_t)((size_t)value != value); try { IdPool* obj = new IdPool(poolSize); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } catch (std::Exception e) { JS_THROW(isolate, e.what()); return; } } else { // Invoked as plain function `IdPool(...)`, turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
void ETW::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { if (args[0]->IsUndefined()) { Nan::ThrowTypeError("Session name is required."); return; } int wchars_num = MultiByteToWideChar(CP_UTF8 , 0 , *String::Utf8Value(args[0]), -1, NULL , 0 ); wchar_t* szSessionName = new wchar_t[wchars_num]; MultiByteToWideChar(CP_UTF8, 0, *String::Utf8Value(args[0]), -1, szSessionName, wchars_num); // Invoked as constructor: `new ETW(...)` ETW* obj = new ETW(szSessionName); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function `ETW(...)`, turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
void Connection::New(const FunctionCallbackInfo<Value>& info) { if (!info.IsConstructCall()) { return; } auto c = new Connection(); c->Wrap(info.This()); info.GetReturnValue().Set(info.This()); }
//! \verbatim //! Quaternion() //! Quaternion( Radian rotation, Vector3 axis ) //! Quaternion( Real w, Real x, Real y, Real z ) //! Quaternion([Real w, Real x, Real y, Real z]) //! Quaternion({Real w, Real x, Real y, Real z}) //! \endverbatim void Quaternion::create( const FunctionCallbackInfo<v8::Value>& args ) { HandleScope handleScope( args.GetIsolate() ); if ( !args.IsConstructCall() ) { args.GetIsolate()->ThrowException( Util::allocString( "Function called as non-constructor" ) ); return; } Ogre::Quaternion qtn( Ogre::Quaternion::IDENTITY ); if ( args.Length() == 4 ) { qtn.w = args[0]->NumberValue(); qtn.x = args[1]->NumberValue(); qtn.y = args[2]->NumberValue(); qtn.z = args[3]->NumberValue(); } else if ( args.Length() == 2 ) { Vector3* axis = Util::extractVector3( 1, args ); qtn.FromAngleAxis( Ogre::Radian( args[0]->NumberValue() ), *axis ); } else if ( args.Length() == 1 ) { if ( args[0]->IsArray() ) { v8::Local<v8::Array> arr = v8::Local<v8::Array>::Cast( args[0] ); if ( arr->Length() == 4 ) { qtn.w = arr->Get( 0 )->NumberValue(); qtn.x = arr->Get( 1 )->NumberValue(); qtn.y = arr->Get( 2 )->NumberValue(); qtn.z = arr->Get( 3 )->NumberValue(); } } else if ( args[0]->IsObject() ) { v8::Local<v8::Value> val = args[0]->ToObject()->Get( Util::allocString( "w" ) ); if ( val->IsNumber() ) qtn.w = val->NumberValue(); val = args[0]->ToObject()->Get( Util::allocString( "x" ) ); if ( val->IsNumber() ) qtn.x = val->NumberValue(); val = args[0]->ToObject()->Get( Util::allocString( "y" ) ); if ( val->IsNumber() ) qtn.y = val->NumberValue(); val = args[0]->ToObject()->Get( Util::allocString( "z" ) ); if ( val->IsNumber() ) qtn.z = val->NumberValue(); } } Quaternion* object = new Quaternion( qtn ); object->wrap( args.This() ); args.GetReturnValue().Set( args.This() ); }
void Buffer::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); HandleScope handle_scope(isolate); if (args.IsConstructCall()) { Buffer* obj = new Buffer(args); args.GetReturnValue().Set(args.This()); } else { std::array<Local<Value>, 1> argv{ args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor.Get(isolate)); args.GetReturnValue().Set(cons->NewInstance(SafeInt<int>(argv.size()), argv.data())); } }
void Server(const FunctionCallbackInfo<Value> &args) { if (args.IsConstructCall()) { try { args.This()->SetAlignedPointerInInternalField(0, new uWS::Server(args[0]->IntegerValue(), true, args[1]->IntegerValue(), args[2]->IntegerValue())); // todo: these needs to be removed on destruction args.This()->SetAlignedPointerInInternalField(CONNECTION_CALLBACK, new Persistent<Function>); args.This()->SetAlignedPointerInInternalField(DISCONNECTION_CALLBACK, new Persistent<Function>); args.This()->SetAlignedPointerInInternalField(MESSAGE_CALLBACK, new Persistent<Function>); } catch (...) { args.This()->Set(String::NewFromUtf8(args.GetIsolate(), "error"), Boolean::New(args.GetIsolate(), true)); } args.GetReturnValue().Set(args.This()); } }
void DeviceNode::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new DeviceNode()` DeviceNode* obj = new DeviceNode(); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function `DeviceNode(...)`, turn into construct call. Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance()); } }
static void gum_v8_file_on_new_file (const FunctionCallbackInfo<Value> & info) { GumV8File * self = static_cast<GumV8File *> ( info.Data ().As<External> ()->Value ()); Isolate * isolate = self->core->isolate; if (!info.IsConstructCall ()) { isolate->ThrowException (Exception::TypeError (String::NewFromUtf8 ( isolate, "Use `new File()` to create a new instance"))); return; } Local<Value> filename_val = info[0]; if (!filename_val->IsString ()) { isolate->ThrowException (Exception::TypeError (String::NewFromUtf8 (isolate, "File: first argument must be a string specifying filename"))); return; } String::Utf8Value filename (filename_val); Local<Value> mode_val = info[1]; if (!mode_val->IsString ()) { isolate->ThrowException (Exception::TypeError (String::NewFromUtf8 (isolate, "File: second argument must be a string specifying mode"))); return; } String::Utf8Value mode (mode_val); FILE * handle = fopen (*filename, *mode); if (handle == NULL) { gchar * message = g_strdup_printf ("File: failed to open file (%s)", strerror (errno)); isolate->ThrowException (Exception::TypeError (String::NewFromUtf8 (isolate, message))); g_free (message); return; } Local<Object> instance (info.Holder ()); GumFile * file = gum_file_new (instance, handle, self); instance->SetAlignedPointerInInternalField (0, file); }
void ABPFilterParserWrap::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new ABPFilterParser(...)` ABPFilterParserWrap* obj = new ABPFilterParserWrap(); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function `ABPFilterParser(...)`, // turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
void UiWindow::New(const FunctionCallbackInfo<Value>& args) { Isolate *isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall() || args[0]->IsUndefined()) { UiWindow* _this = CreateUiWindow(); Handle<Object> obj = Handle<Object>::Cast(args[0]); _this->_config = new WindowConfig(obj); _this->Wrap(args.This()); Local<Value> emit = _this->handle()->Get(String::NewFromUtf8(isolate, "emit")); Local<Function> emitFn = Local<Function>::Cast(emit); _this->_emitFn.Reset(isolate, emitFn); args.GetReturnValue().Set(args.This()); } }
void MyObject::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor: `new MyObject(...)` double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); MyObject* obj = new MyObject(value); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function `MyObject(...)`, turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
void WeakRef::ConstructorCallbackImpl(const FunctionCallbackInfo<Value>& args) { auto isolate = args.GetIsolate(); if (args.IsConstructCall()) { if (args.Length() == 1) { auto target = args[0]; if (target->IsObject()) { auto targetObj = target.As<Object>(); auto weakRef = m_objectManager->GetEmptyObject(isolate); auto poTarget = new Persistent<Object>(isolate, targetObj); auto poHolder = new Persistent<Object>(isolate, weakRef); auto callbackState = new CallbackState(poTarget, poHolder); poTarget->SetWeak(callbackState, WeakTargetCallback); poHolder->SetWeak(callbackState, WeakHolderCallback); weakRef->Set(ConvertToV8String("get"), GetGetterFunction(isolate)); weakRef->Set(ConvertToV8String("clear"), GetClearFunction(isolate)); weakRef->SetHiddenValue(V8StringConstants::GetTarget(), External::New(isolate, poTarget)); args.GetReturnValue().Set(weakRef); } else { throw NativeScriptException(string("The WeakRef constructor expects an object argument.")); } } else { throw NativeScriptException(string("The WeakRef constructor expects single parameter.")); } } else { throw NativeScriptException(string("WeakRef must be used as a construct call.")); } }
void TempWrapper::New(const FunctionCallbackInfo<Value>& args){ Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); uint8_t _addr = 0x00, _port = 0x01; // If there are two params: First Param => i2c address, second => Port number // - Only one Param, this means that the given param is the Port Number, // printf("Args Count: %d\n",args.Length()); TempWrapper* obj; uint8_t _argc = args.Length(); if(args.IsConstructCall()){ // Invoked as constructor: `new MyObject(...)` switch(_argc){ case 1: // Only the BCMWrapper is passed _port = (uint8_t) args[0]->NumberValue(); obj = new TempWrapper(_port); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); break; case 2: _port = (uint8_t) args[0]->NumberValue(); _addr = (uint8_t) args[1]->NumberValue(); obj = new TempWrapper(_port,_addr); obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); break; default: isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong arguments..."))); } }else{ // Invoked as plain function `MyObject(...)`, turn into construct call. if(_argc > 2){ isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Wrong arguments..."))); } Local<Value>* argv = new Local<Value>[_argc]; for(uint8_t i = 0; i < _argc; i++){ argv[i] = args[i]; } Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(_argc, argv)); } }
void Bindings::construct(const FunctionCallbackInfo<Value>& args) { Isolate* isolate(args.GetIsolate()); HandleScope scope(isolate); if (args.IsConstructCall()) { // Invoked as constructor with 'new'. Bindings* obj = new Bindings(); obj->Wrap(args.Holder()); args.GetReturnValue().Set(args.Holder()); } else { // Invoked as a function, turn into construct call. Local<Function> ctor(Local<Function>::New(isolate, constructor)); args.GetReturnValue().Set(ctor->NewInstance()); } }
void FunctionTemplateProxy::InvocationCallbackProxy(const FunctionCallbackInfo<Value>& args) { auto proxy = static_cast<ProxyBase*>(args.Data().As<External>()->Value()); V8EngineProxy *engine; ManagedJSFunctionCallback callback; if (proxy->GetType() == FunctionTemplateProxyClass) { engine = static_cast<FunctionTemplateProxy*>(proxy)->_EngineProxy; callback = static_cast<FunctionTemplateProxy*>(proxy)->_ManagedCallback; } else if (proxy->GetType() == ObjectTemplateProxyClass) { engine = static_cast<ObjectTemplateProxy*>(proxy)->_EngineProxy; callback = static_cast<ObjectTemplateProxy*>(proxy)->_ManagedCallback; } else throw exception("'args.Data()' is not recognized."); if (callback != nullptr) // (note: '_ManagedCallback' may not be set on the proxy, and thus 'callback' may be null) { auto argLength = args.Length(); auto _args = argLength > 0 ? new HandleProxy*[argLength] : nullptr; for (auto i = 0; i < argLength; i++) _args[i] = engine->GetHandleProxy(args[i]); auto _this = engine->GetHandleProxy(args.Holder()); auto result = callback(0, args.IsConstructCall(), _this, _args, argLength); if (result != nullptr) if (result->IsError()) args.GetReturnValue().Set(ThrowException(Exception::Error(result->Handle()->ToString()))); else args.GetReturnValue().Set(result->Handle()); // (note: the returned value was created via p/invoke calls from the managed side, so the managed side is expected to tracked and free this handle when done) // (result == null == undefined [which means the managed side didn't return anything]) } }
void constructByteArray(const FunctionCallbackInfo<Value>& args) { if (!args.IsConstructCall()) { Utility::throwException(args.GetIsolate(), Utility::ExceptionMustCallAsConstructor); return; } if (args.Length() == 0) { Utility::throwException(args.GetIsolate(), Utility::ExceptionInvalidArgumentCount); return; } QByteArray data; if (args[0]->IsArrayBuffer()) { data = Utility::toByteArray(args[0]); } else if (args[0]->IsString()) { int format = 0; if (args.Length() >= 2 && args[1]->IsInt32()) format = args[1]->Int32Value(); if (format < 0 || format > 3) { Utility::throwException(args.GetIsolate(), Utility::ExceptionInvalidArgumentValue); return; } data = byteArrayFromString(Utility::toString(args[0]).toLatin1(), format); } else { Utility::throwException(args.GetIsolate(), Utility::ExceptionInvalidArgumentType); return; } HandleScope handle_scope(args.GetIsolate()); // Return the constructed object args.GetReturnValue().Set(ModuleByteArray::wrapByteArray(args.GetIsolate(), data)); }
// static void Query::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.IsConstructCall()) { if (args.Length() != 1) { Local<String> str = String::NewFromUtf8(isolate, "Consturctor expects 1 agument"); isolate->ThrowException(Exception::TypeError(str)); return; } if (!args[0]->IsString()) { Local<String> str = String::NewFromUtf8(isolate, "Argument must be a string"); isolate->ThrowException(Exception::TypeError(str)); return; } Local<String> str = args[0]->ToString(); QByteArray arr; arr.resize(str->Utf8Length()); str->WriteUtf8(arr.data(), arr.length()); QString queryString = QString::fromUtf8(arr); Query* query = new Query(queryString); query->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { // Invoked as plain function, turn into construct call. const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, s_constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
// Create a new instance of the BookWrap class void BookWrap::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); // constructor take no arguments if (args.IsConstructCall()) { // constructor take no arguments if (args.Length() == 0) { BookWrap* bw = new BookWrap(); Book *p = new Book(); bw->m_book = p; bw->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { isolate->ThrowException(Exception::SyntaxError(String::NewFromUtf8(isolate, "No arguments ecpected"))); args.GetReturnValue().SetUndefined(); } } else { isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Called as plain function is not permitted"))); args.GetReturnValue().SetUndefined(); } }
void constructor(const FunctionCallbackInfo<Value> &args) { if (args.IsConstructCall()) { Local<Object> options = args[0]->ToObject(); int port = options->Get(String::NewFromUtf8(args.GetIsolate(), "port"))->ToInteger()->Value(); String::Utf8Value path(options->Get(String::NewFromUtf8(args.GetIsolate(), "path"))); int keepAliveTime = options->Get(String::NewFromUtf8(args.GetIsolate(), "keepAliveTime"))->ToInteger()->Value(); int keepAliveInterval = options->Get(String::NewFromUtf8(args.GetIsolate(), "keepAliveInterval"))->ToInteger()->Value(); int keepAliveRetry = options->Get(String::NewFromUtf8(args.GetIsolate(), "keepAliveRetry"))->ToInteger()->Value(); MaybeLocal<Value> perMessageDeflate = options->Get(String::NewFromUtf8(args.GetIsolate(), "perMessageDeflate")); bool usePerMessageDeflate = true; static string strPerMessageDeflate = "permessage-deflate"; if (perMessageDeflate.ToLocalChecked()->IsObject()) { Local<Array> propertyNames = perMessageDeflate.ToLocalChecked()->ToObject()->GetPropertyNames(); for (int i = 0; i < propertyNames->Length(); i++) { String::Utf8Value keyName(propertyNames->Get(i)); MaybeLocal<Value> optionalValue = perMessageDeflate.ToLocalChecked()->ToObject()->Get(propertyNames->Get(i)); if (!(optionalValue.ToLocalChecked()->IsBoolean() && !optionalValue.ToLocalChecked()->BooleanValue())) { strPerMessageDeflate += "; " + camelCaseToUnderscore(string(*keyName, keyName.length())); if (!optionalValue.IsEmpty() && !optionalValue.ToLocalChecked()->IsBoolean()) { String::Utf8Value valueString(optionalValue.ToLocalChecked()->ToString()); strPerMessageDeflate += "=" + string(*valueString, valueString.length()); } } } } else if(perMessageDeflate.ToLocalChecked()->IsBoolean()) { usePerMessageDeflate = perMessageDeflate.ToLocalChecked()->BooleanValue(); } MaybeLocal<Value> ssl = options->Get(String::NewFromUtf8(args.GetIsolate(), "ssl")); static string certPath, keyPath, caPath, ciphers; static bool rejectUnauthorized = true; if (!ssl.IsEmpty()) { Local<Object> sslOptions = ssl.ToLocalChecked()->ToObject(); certPath = getString(args.GetIsolate(), sslOptions, "cert"); keyPath = getString(args.GetIsolate(), sslOptions, "key"); caPath = getString(args.GetIsolate(), sslOptions, "ca"); ciphers = getString(args.GetIsolate(), sslOptions, "ciphers"); rejectUnauthorized = sslOptions->Get(String::NewFromUtf8(args.GetIsolate(), "rejectUnauthorized"))->BooleanValue(); } #ifdef VERBOSE_SERVER cout << "Using port = " << port << ", path = " << string(*path, path.length()) << ", keepAliveTime = " << keepAliveTime << ", keepAliveInterval = " << keepAliveInterval << ", keepAliveRetry = " << keepAliveRetry << endl; if (usePerMessageDeflate) { cout << "Using perMessageDeflate:" << endl << strPerMessageDeflate << endl; } #endif lws::Server *server; try { server = new lws::Server(port, string(*path, path.length()).c_str(), keepAliveTime, keepAliveRetry, keepAliveInterval, usePerMessageDeflate, strPerMessageDeflate.c_str(), certPath.c_str(), keyPath.c_str(), caPath.c_str(), ciphers.c_str(), rejectUnauthorized); } catch (...) { server = nullptr; } args.This()->SetAlignedPointerInInternalField(0, server); args.GetReturnValue().Set(args.This()); } }