void JNIV8ClassInfo::_registerAccessor(JNIV8ObjectAccessorHolder *holder) { Isolate* isolate = engine->getIsolate(); HandleScope scope(isolate); Local<FunctionTemplate> ft = Local<FunctionTemplate>::New(isolate, functionTemplate); accessorHolders.push_back(holder); Local<External> data = External::New(isolate, (void*)holder); AccessorNameSetterCallback finalSetter = 0; v8::PropertyAttribute settings = v8::PropertyAttribute::None; if(holder->setterCallback.i || holder->setterCallback.s) { finalSetter = v8AccessorSetterCallback; } else { settings = v8::PropertyAttribute::ReadOnly; } if(holder->isStatic) { Local<Function> f = ft->GetFunction(); f->SetAccessor(engine->getContext(), String::NewFromUtf8(isolate, holder->propertyName.c_str()), v8AccessorGetterCallback, finalSetter, data, DEFAULT, settings); } else { Local<ObjectTemplate> instanceTpl = ft->InstanceTemplate(); instanceTpl->SetAccessor(String::NewFromUtf8(isolate, holder->propertyName.c_str()), v8AccessorGetterCallback, finalSetter, data, DEFAULT, settings); } }
v8::Handle<v8::Object> WrapScreen(ScriptSystem* ss) { HandleScope handle_scope; Handle<Context> context = ss->GetGlobalContext(); Context::Scope context_scope(context); Handle<FunctionTemplate> templt = FunctionTemplate::New(); templt->SetClassName(String::New("Screen")); Handle<ObjectTemplate> proto = templt->PrototypeTemplate(); proto->Set("toString", FunctionTemplate::New(SCRToString)); proto->Set("getPickRay", FunctionTemplate::New(SCRGetPickRay)); proto->Set("intersect", FunctionTemplate::New(SCRIntersect)); proto->Set("pickEntity", FunctionTemplate::New(SCRPickEntity)); proto->Set("convertWorldToScreenCoords", FunctionTemplate::New(SCRConvertWorldToScreenCoords)); proto->Set("openWindow", FunctionTemplate::New(SCROpenWindow)); proto->Set("closeWindow", FunctionTemplate::New(SCRCloseWindow)); proto->Set("getWindowGeometry", FunctionTemplate::New(SCRGetWindowGeometry)); proto->Set("setWindowGeometry", FunctionTemplate::New(SCRSetWindowGeometry)); Local<Object> instance = templt->GetFunction()->NewInstance(); instance->SetAccessor(String::New("lockCursor"), SCRGetLockCursor, SCRSetLockCursor); instance->SetAccessor(String::New("showCursor"), SCRGetShowCursor, SCRSetShowCursor); instance->SetAccessor(String::New("width"), SCRGetWidth); instance->SetAccessor(String::New("height"), SCRGetHeight); instance->SetAccessor(String::New("fullScreen"), SCRGetFullScreen, SCRSetFullScreen); return handle_scope.Close(instance); }
Local<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate( Isolate* isolate) { EscapableHandleScope handle_scope(isolate); Local<ObjectTemplate> result = ObjectTemplate::New(isolate); result->SetInternalFieldCount(1); // Add accessors for each of the fields of the request. result->SetAccessor( String::NewFromUtf8(isolate, "path", NewStringType::kInternalized) .ToLocalChecked(), GetPath); result->SetAccessor( String::NewFromUtf8(isolate, "referrer", NewStringType::kInternalized) .ToLocalChecked(), GetReferrer); result->SetAccessor( String::NewFromUtf8(isolate, "host", NewStringType::kInternalized) .ToLocalChecked(), GetHost); result->SetAccessor( String::NewFromUtf8(isolate, "userAgent", NewStringType::kInternalized) .ToLocalChecked(), GetUserAgent); // Again, return the result through the current handle scope. return handle_scope.Escape(result); }
void JNIV8ClassInfo::_registerJavaAccessor(JNIV8ObjectJavaAccessorHolder *holder) { Isolate* isolate = engine->getIsolate(); HandleScope scope(isolate); Local<FunctionTemplate> ft = Local<FunctionTemplate>::New(isolate, functionTemplate); JNIEnv *env = JNIWrapper::getEnvironment(); holder->javaClass = (jclass)env->NewGlobalRef(env->FindClass(container->canonicalName.c_str())); javaAccessorHolders.push_back(holder); Local<External> data = External::New(isolate, (void*)holder); AccessorNameSetterCallback finalSetter = 0; v8::PropertyAttribute settings = v8::PropertyAttribute::None; if(holder->javaSetterId) { finalSetter = v8JavaAccessorSetterCallback; } else { settings = v8::PropertyAttribute::ReadOnly; } if(holder->isStatic) { Local<Function> f = ft->GetFunction(); f->SetAccessor(engine->getContext(), String::NewFromUtf8(isolate, holder->propertyName.c_str(), v8::NewStringType::kNormal).ToLocalChecked().As<Name>(), v8JavaAccessorGetterCallback, finalSetter, data, DEFAULT, settings); } else { Local<ObjectTemplate> instanceTpl = ft->InstanceTemplate(); instanceTpl->SetAccessor(String::NewFromUtf8(isolate, holder->propertyName.c_str()), v8JavaAccessorGetterCallback, finalSetter, data, DEFAULT, settings); } }
Local<Object> PairNodeIterator::New(int type, MapType::const_iterator new_iter, MapType::const_iterator new_end) { Handle<FunctionTemplate> tmplt; if ((PairNodeIterator::KEY_TYPE & type) && (PairNodeIterator::VALUE_TYPE & type)) { tmplt = NanNew<FunctionTemplate>(key_value_tmplt); } else if (KEY_TYPE & type) { tmplt = NanNew<FunctionTemplate>(key_tmplt); } else { tmplt = NanNew<FunctionTemplate>(value_tmplt); } Local<Object> obj = tmplt->InstanceTemplate()->NewInstance(); PairNodeIterator *iter = new PairNodeIterator(new_iter, new_end); iter->Wrap(obj); Local<String> key = NanNew<String>("key"); Local<String> value = NanNew<String>("value"); Local<String> done = NanNew<String>("done"); if (PairNodeIterator::KEY_TYPE & type) { obj->SetAccessor(key, GetKey); } if (PairNodeIterator::VALUE_TYPE & type) { obj->SetAccessor(value, GetValue); } obj->SetAccessor(done, GetDone); return obj; }
Handle<FunctionTemplate> PushbotsModule::getProxyTemplate() { if (!proxyTemplate.IsEmpty()) { return proxyTemplate; } LOGD(TAG, "GetProxyTemplate"); javaClass = titanium::JNIUtil::findClass("com/pushbots/android/PushbotsModule"); HandleScope scope; // use symbol over string for efficiency Handle<String> nameSymbol = String::NewSymbol("Pushbots"); Handle<FunctionTemplate> t = titanium::Proxy::inheritProxyTemplate( titanium::KrollModule::getProxyTemplate() , javaClass, nameSymbol); proxyTemplate = Persistent<FunctionTemplate>::New(t); proxyTemplate->Set(titanium::Proxy::inheritSymbol, FunctionTemplate::New(titanium::Proxy::inherit<PushbotsModule>)->GetFunction()); titanium::ProxyFactory::registerProxyPair(javaClass, *proxyTemplate); // Method bindings -------------------------------------------------------- DEFINE_PROTOTYPE_METHOD(proxyTemplate, "setPushEnabled", PushbotsModule::setPushEnabled); DEFINE_PROTOTYPE_METHOD(proxyTemplate, "tag", PushbotsModule::tag); DEFINE_PROTOTYPE_METHOD(proxyTemplate, "example", PushbotsModule::example); Local<ObjectTemplate> prototypeTemplate = proxyTemplate->PrototypeTemplate(); Local<ObjectTemplate> instanceTemplate = proxyTemplate->InstanceTemplate(); // Delegate indexed property get and set to the Java proxy. instanceTemplate->SetIndexedPropertyHandler(titanium::Proxy::getIndexedProperty, titanium::Proxy::setIndexedProperty); // Constants -------------------------------------------------------------- // Dynamic properties ----------------------------------------------------- instanceTemplate->SetAccessor(String::NewSymbol("alias"), titanium::Proxy::getProperty , PushbotsModule::setter_alias , Handle<Value>(), DEFAULT); instanceTemplate->SetAccessor(String::NewSymbol("enabled"), PushbotsModule::getter_enabled , PushbotsModule::setter_enabled , Handle<Value>(), DEFAULT); instanceTemplate->SetAccessor(String::NewSymbol("exampleProp"), PushbotsModule::getter_exampleProp , PushbotsModule::setter_exampleProp , Handle<Value>(), DEFAULT); // Accessors -------------------------------------------------------------- return proxyTemplate; }
void HttpClientInitBinding(Handle<Object> target) { NanScope(); Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(newHttpClient); NanAssignPersistent(constructor, ctor); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(NanNew("HttpClient")); Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); proto->SetAccessor(NanNew("url"), HttpClientGetUrl, HttpClientSetUrl); proto->SetAccessor(NanNew("returnType"), HttpClientGetReturnType, HttpClientSetReturnType); proto->SetAccessor(NanNew("method"), HttpClientGetMethod, HttpClientSetMethod); proto->SetAccessor(NanNew("requestHeaders"), HttpClientGetRequestHeaders, HttpClientSetRequestHeaders); proto->SetAccessor(NanNew("requestData"), HttpClientGetRequestData, HttpClientSetRequestData); proto->SetAccessor(NanNew("status"), HttpClientGetStatus, HttpClientSetStatus); proto->SetAccessor(NanNew("statusText"), HttpClientGetStatusText, HttpClientSetStatusText); proto->SetAccessor(NanNew("responseHeaders"), HttpClientGetResponseHeaders, HttpClientSetResponseHeaders); proto->SetAccessor(NanNew("responseText"), HttpClientGetResponseText, HttpClientSetResponseText); NAN_SET_PROTOTYPE_METHOD(ctor, "send", HttpClientSend); target->Set(NanNew("HttpClient"), ctor->GetFunction()); HttpClient::init(); }
Handle<FunctionTemplate> ExampleProxy::getProxyTemplate() { if (!proxyTemplate.IsEmpty()) { return proxyTemplate; } LOGD(TAG, "GetProxyTemplate"); javaClass = titanium::JNIUtil::findClass("ro/mihaiblaga/imei/ExampleProxy"); HandleScope scope; // use symbol over string for efficiency Handle<String> nameSymbol = String::NewSymbol("Example"); Handle<FunctionTemplate> t = titanium::Proxy::inheritProxyTemplate( titanium::TiViewProxy::getProxyTemplate() , javaClass, nameSymbol); proxyTemplate = Persistent<FunctionTemplate>::New(t); proxyTemplate->Set(titanium::Proxy::inheritSymbol, FunctionTemplate::New(titanium::Proxy::inherit<ExampleProxy>)->GetFunction()); titanium::ProxyFactory::registerProxyPair(javaClass, *proxyTemplate); // Method bindings -------------------------------------------------------- DEFINE_PROTOTYPE_METHOD(proxyTemplate, "setMessage", ExampleProxy::setMessage); DEFINE_PROTOTYPE_METHOD(proxyTemplate, "getMessage", ExampleProxy::getMessage); DEFINE_PROTOTYPE_METHOD(proxyTemplate, "printMessage", ExampleProxy::printMessage); DEFINE_PROTOTYPE_METHOD(proxyTemplate, "getImei", ExampleProxy::getImei); Local<ObjectTemplate> prototypeTemplate = proxyTemplate->PrototypeTemplate(); Local<ObjectTemplate> instanceTemplate = proxyTemplate->InstanceTemplate(); // Delegate indexed property get and set to the Java proxy. instanceTemplate->SetIndexedPropertyHandler(titanium::Proxy::getIndexedProperty, titanium::Proxy::setIndexedProperty); // Constants -------------------------------------------------------------- // Dynamic properties ----------------------------------------------------- instanceTemplate->SetAccessor(String::NewSymbol("message"), ExampleProxy::getter_message , ExampleProxy::setter_message , Handle<Value>(), DEFAULT); instanceTemplate->SetAccessor(String::NewSymbol("imei"), ExampleProxy::getter_imei , titanium::Proxy::onPropertyChanged , Handle<Value>(), DEFAULT); // Accessors -------------------------------------------------------------- return proxyTemplate; }
Handle<FunctionTemplate> NfcTagProxy::getProxyTemplate() { if (!proxyTemplate.IsEmpty()) { return proxyTemplate; } LOGD(TAG, "GetProxyTemplate"); javaClass = titanium::JNIUtil::findClass("ta/nfc/NfcTagProxy"); HandleScope scope; // use symbol over string for efficiency Handle<String> nameSymbol = String::NewSymbol("NfcTag"); Handle<FunctionTemplate> t = titanium::Proxy::inheritProxyTemplate( titanium::KrollProxy::getProxyTemplate() , javaClass, nameSymbol); proxyTemplate = Persistent<FunctionTemplate>::New(t); proxyTemplate->Set(titanium::Proxy::inheritSymbol, FunctionTemplate::New(titanium::Proxy::inherit<NfcTagProxy>)->GetFunction()); titanium::ProxyFactory::registerProxyPair(javaClass, *proxyTemplate); // Method bindings -------------------------------------------------------- DEFINE_PROTOTYPE_METHOD(proxyTemplate, "getId", NfcTagProxy::getId); DEFINE_PROTOTYPE_METHOD(proxyTemplate, "getTechList", NfcTagProxy::getTechList); Local<ObjectTemplate> prototypeTemplate = proxyTemplate->PrototypeTemplate(); Local<ObjectTemplate> instanceTemplate = proxyTemplate->InstanceTemplate(); // Delegate indexed property get and set to the Java proxy. instanceTemplate->SetIndexedPropertyHandler(titanium::Proxy::getIndexedProperty, titanium::Proxy::setIndexedProperty); // Constants -------------------------------------------------------------- // Dynamic properties ----------------------------------------------------- instanceTemplate->SetAccessor(String::NewSymbol("id"), NfcTagProxy::getter_id , titanium::Proxy::onPropertyChanged , Handle<Value>(), DEFAULT); instanceTemplate->SetAccessor(String::NewSymbol("techList"), NfcTagProxy::getter_techList , titanium::Proxy::onPropertyChanged , Handle<Value>(), DEFAULT); // Accessors -------------------------------------------------------------- return proxyTemplate; }
Handle<Object> FixSession::wrapFixSession(FixSession* fixSession) { Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(NanNew("FixSession")); Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(ctor, "disconnect", disconnect); NODE_SET_PROTOTYPE_METHOD(ctor, "getSessionID", getSessionID); NODE_SET_PROTOTYPE_METHOD(ctor, "isEnabled", isEnabled); NODE_SET_PROTOTYPE_METHOD(ctor, "isLoggedOn", isLoggedOn); NODE_SET_PROTOTYPE_METHOD(ctor, "logon", logon); NODE_SET_PROTOTYPE_METHOD(ctor, "logout", logout); NODE_SET_PROTOTYPE_METHOD(ctor, "refresh", refresh); NODE_SET_PROTOTYPE_METHOD(ctor, "reset", reset); proto->SetAccessor(NanNew("senderSeqNum"), getSenderSeqNum, setSenderSeqNum); proto->SetAccessor(NanNew("targetSeqNum"), getTargetSeqNum, setTargetSeqNum); Handle<Object> obj = ctor->InstanceTemplate()->NewInstance(); //obj->SetAlignedPointerInInternalField(0, NanNew<External>(fixSession)); fixSession->Wrap(obj); return obj; }
void Init(Isolate * isolate) { Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(v8::String::NewFromUtf8(isolate, "Transformable")); tpl->InstanceTemplate()->SetInternalFieldCount(2); Local<ObjectTemplate> proto = tpl->PrototypeTemplate(); // proto->SetInternalFieldCount(1); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "x"), GetX, SetX); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "y"), GetY, SetY); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "rotation"), GetRotation, SetRotation); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "scaleX"), GetScaleX, SetScaleX); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "scaleY"), GetScaleY, SetScaleY); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "originX"), GetOriginX, SetOriginX); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "originY"), GetOriginY, SetOriginY); tpl_ref.Reset(isolate, tpl); }
static v8::Local<v8::Function> initClass(v8::Handle<v8::FunctionTemplate>& temp) { HandleScope scope; Local<ObjectTemplate> obj = temp->PrototypeTemplate(); obj->SetAccessor(String::New("height"), height); obj->SetAccessor(String::New("ascender"), ascender); obj->SetAccessor(String::New("descender"), descender); EXPOSE_METHOD(obj, load, ReadOnly | DontDelete); EXPOSE_METHOD(obj, measure, ReadOnly | DontDelete); EXPOSE_METHOD(obj, glyphs, ReadOnly | DontDelete); EXPOSE_METHOD(obj, outline_type, ReadOnly | DontDelete); EXPOSE_METHOD(obj, outline_thickness, ReadOnly | DontDelete); return scope.Close(temp->GetFunction()); }
void MetadataNode::InjectPrototype(Local<Object>& target, Local<Object>& implementationObject) { auto isolate = Isolate::GetCurrent(); implementationObject->SetAccessor(ConvertToV8String("super"), SuperAccessorGetterCallback, nullptr, implementationObject); implementationObject->SetPrototype(target->GetPrototype()); target->SetPrototype(implementationObject); }
void CanvasRenderingContext2D::Create(const v8::FunctionCallbackInfo<v8::Value>& args) { auto isolate = args.GetIsolate(); HandleScope scope(isolate); Local<ObjectTemplate> contextTemplate = ObjectTemplate::New(isolate); contextTemplate->SetInternalFieldCount(1); contextTemplate->Set(ConvertToV8String("__draw"), FunctionTemplate::New(isolate, &Draw)); contextTemplate->Set(ConvertToV8String("__sizeChanged"), FunctionTemplate::New(isolate, &SizeChanged)); contextTemplate->SetAccessor(ConvertToV8String("fillStyle"), &GetFillStyle, &SetFillStyle); contextTemplate->SetAccessor(ConvertToV8String("strokeStyle"), &GetStrokeStyle, &SetStrokeStyle); contextTemplate->Set(ConvertToV8String("arc"), FunctionTemplate::New(isolate, &Arc)); contextTemplate->Set(ConvertToV8String("beginPath"), FunctionTemplate::New(isolate, &BeginPath)); contextTemplate->Set(ConvertToV8String("bezierCurveTo"), FunctionTemplate::New(isolate, &BezierCurveTo)); contextTemplate->Set(ConvertToV8String("clearRect"), FunctionTemplate::New(isolate, &ClearRect)); contextTemplate->Set(ConvertToV8String("closePath"), FunctionTemplate::New(isolate, &ClosePath)); contextTemplate->Set(ConvertToV8String("drawImage"), FunctionTemplate::New(isolate, &DrawImage)); contextTemplate->Set(ConvertToV8String("fill"), FunctionTemplate::New(isolate, &Fill)); contextTemplate->Set(ConvertToV8String("fillRect"), FunctionTemplate::New(isolate, &FillRect)); contextTemplate->Set(ConvertToV8String("fillText"), FunctionTemplate::New(isolate, &FillText)); contextTemplate->Set(ConvertToV8String("getImageData"), FunctionTemplate::New(isolate, &GetImageData)); contextTemplate->Set(ConvertToV8String("lineTo"), FunctionTemplate::New(isolate, &LineTo)); contextTemplate->Set(ConvertToV8String("measureText"), FunctionTemplate::New(isolate, &MeasureText)); contextTemplate->Set(ConvertToV8String("moveTo"), FunctionTemplate::New(isolate, &MoveTo)); contextTemplate->Set(ConvertToV8String("quadraticCurveTo"), FunctionTemplate::New(isolate, &QuadraticCurveTo)); contextTemplate->Set(ConvertToV8String("restore"), FunctionTemplate::New(isolate, &Restore)); contextTemplate->Set(ConvertToV8String("rotate"), FunctionTemplate::New(isolate, &Rotate)); contextTemplate->Set(ConvertToV8String("save"), FunctionTemplate::New(isolate, &Save)); contextTemplate->Set(ConvertToV8String("stroke"), FunctionTemplate::New(isolate, &Stroke)); contextTemplate->Set(ConvertToV8String("translate"), FunctionTemplate::New(isolate, &Translate)); Local<Object> newContext = contextTemplate->NewInstance(); newContext->ForceSet(ConvertToV8String("canvas"), args[0]); newContext->ForceSet(ConvertToV8String("__kind"), ConvertToV8String("2d")); auto nativeContext = new CanvasRenderingContext2D(); newContext->SetInternalField(0, External::New(isolate, nativeContext)); Persistent<Object> persistentHandle(isolate, newContext); persistentHandle.SetWeak(nativeContext, &Deallocate); args.GetReturnValue().Set(newContext); }
void RegisterMyClass(Isolate* isolate, Local<ObjectTemplate> global) { Local<ObjectTemplate> localTemplate = ObjectTemplate::New(isolate); localTemplate->SetInternalFieldCount(1); localTemplate->SetAccessor( String::NewFromUtf8(isolate, "num", NewStringType::kInternalized) .ToLocalChecked(), jsMyClassGetNumber, jsMyClassSetNumber); localTemplate->SetAccessor( String::NewFromUtf8(isolate, "name", NewStringType::kInternalized) .ToLocalChecked(), jsMyClassGetName, jsMyClassSetName); localTemplate->Set( String::NewFromUtf8(isolate, "Method1", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, jsMyClassMethod1)); localTemplate->Set( String::NewFromUtf8(isolate, "Method2", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, jsMyClassMethod2)); gMyClassTemplate.Reset(isolate, localTemplate); global->Set( String::NewFromUtf8(isolate, "MyClass", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, jsCreateMyClass)); global->Set( String::NewFromUtf8(isolate, "jsMyFunction", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, jsMyFunction)); global->Set( String::NewFromUtf8(isolate, "jsMyFunction1", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, jsMyFunction1)); global->Set( String::NewFromUtf8(isolate, "jsMyFunction2", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(isolate, jsMyFunction2)); }
static v8::Local<v8::Function> initClass(v8::Handle<v8::FunctionTemplate>& temp) { HandleScope scope; Local<ObjectTemplate> obj = temp->PrototypeTemplate(); obj->SetAccessor(String::New("byteLength"), byteLength); EXPOSE_METHOD(obj, slice, ReadOnly | DontDelete); EXPOSE_METHOD(obj, isView, ReadOnly | DontDelete); obj->Set(String::New("clone"), FunctionTemplate::New(ClassWrap<NodeBuffer>::clone)); return scope.Close(temp->GetFunction()); }
void ImageInitBinding(Handle<Object> target) { NanScope(); Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(newImage); NanAssignPersistent(constructor, ctor); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(NanNew("Image")); Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); proto->SetAccessor(NanNew("id"), ImageGetId, ImageSetId); proto->SetAccessor(NanNew("src"), ImageGetSrc, ImageSetSrc); proto->SetAccessor(NanNew("width"), ImageGetWidth, ImageSetWidth); proto->SetAccessor(NanNew("height"), ImageGetHeight, ImageSetHeight); NAN_SET_PROTOTYPE_METHOD(ctor, "load", ImageLoad); NAN_SET_PROTOTYPE_METHOD(ctor, "unload", ImageUnload); target->Set(NanNew("Image"), ctor->GetFunction()); }
void PointInitBinding(Handle<Object> target) { NanScope(); Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(newPoint); NanAssignPersistent(constructor, ctor); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(NanNew("Point")); Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); proto->SetAccessor(NanNew("x"), PointGetX, PointSetX); proto->SetAccessor(NanNew("y"), PointGetY, PointSetY); proto->SetAccessor(NanNew("magic"), PointGetMagic, PointSetMagic); NAN_SET_PROTOTYPE_METHOD(ctor, "move", PointMove); NAN_SET_PROTOTYPE_METHOD(ctor, "add", PointAdd); NAN_SET_PROTOTYPE_METHOD(ctor, "copy", PointCopy); NanSetTemplate(proto, "DEFAULT_NAME", NanNew<String>(POINT_DEFAULT_NAME)); NanSetTemplate(proto, "DEFAULT_X", NanNew<Int32>(POINT_DEFAULT_X)); NanSetTemplate(proto, "DEFAULT_Y", NanNew<Int32>(POINT_DEFAULT_Y)); target->Set(NanNew("Point"), ctor->GetFunction()); }
void Point::Bind( Isolate* isolate, Handle< Context > context ) { // Get creation scope HandleScope scope( isolate ); // Create constructor function and object template Local< FunctionTemplate > constructor = FunctionTemplate::New( Construct ); Local< ObjectTemplate > templ = constructor->InstanceTemplate(); templ->SetInternalFieldCount( 1 ); // Set properties and methods templ->SetAccessor( String::New( "x" ), GetX, SetX ); templ->SetAccessor( String::New( "y" ), GetY, SetY ); templ->Set( String::New( "print" ), FunctionTemplate::New( Print ) ); // Register constructor context->Global()->Set( String::New( "Point" ), constructor->GetFunction() ); }
void FreeImage::Initialize(Handle<Object> target) { NanScope(); // constructor Local<FunctionTemplate> ctor = FunctionTemplate::New(FreeImage::New); NanAssignPersistent(FunctionTemplate, constructor_template, ctor); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(JS_STR("FreeImage")); // prototype Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(ctor, "load", load); NODE_SET_PROTOTYPE_METHOD(ctor, "save", save); proto->SetAccessor(JS_STR("getVersion"), getVersion); target->Set(JS_STR("FreeImage"), ctor->GetFunction()); }
void JsSprite::Init(Isolate * isolate, Handle<ObjectTemplate> exports) { // Prepare constructor template Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); tpl->SetClassName(v8::String::NewFromUtf8(isolate, "Sprite")); tpl->InstanceTemplate()->SetInternalFieldCount(2); tpl->Inherit(v8_transformable::GetFunctionTemplate(isolate)); // Prototype Local<ObjectTemplate> proto = tpl->PrototypeTemplate(); // proto->SetInternalFieldCount(1); proto->SetAccessor(v8::String::NewFromUtf8(isolate, "texture"), JS_GetTexture, JS_SetTexture); constructor.Reset(isolate, tpl); exports->Set(v8::String::NewFromUtf8(isolate, "Sprite"), PersistentToLocal(isolate, constructor)); }
/* arg0: Record constructed over the appropriate column list arg1: Array of field names arg2: Array of typeConverters Returns: a constructor function that can be used to create native-backed objects */ Handle<Value> getValueObjectConstructor(const Arguments &args) { DEBUG_MARKER(UDEB_DEBUG); HandleScope scope; Local<FunctionTemplate> ft = FunctionTemplate::New(); Local<ObjectTemplate> inst = ft->InstanceTemplate(); inst->SetInternalFieldCount(2); /* Initialize the mapData */ Local<Object> mapData = Object::New(); /* Store the record in the mapData at 0 */ mapData->Set(0, args[0]); /* Build the ColumnHandlers and store them in the mapData at 1 */ const Record * record = unwrapPointer<const Record *>(args[0]->ToObject()); const uint32_t ncol = record->getNoOfColumns(); ColumnHandlerSet *columnHandlers = new ColumnHandlerSet(ncol); for(unsigned int i = 0 ; i < ncol ; i++) { const NdbDictionary::Column * col = record->getColumn(i); size_t offset = record->getColumnOffset(i); ColumnHandler * handler = columnHandlers->getHandler(i); handler->init(col, offset, args[2]->ToObject()->Get(i)); } Local<Object> jsHandlerSet = columnHandlerSetEnvelope.newWrapper(); wrapPointerInObject<ColumnHandlerSet *>(columnHandlers, columnHandlerSetEnvelope, jsHandlerSet); mapData->Set(1, jsHandlerSet); /* Create accessors for the mapped fields in the instance template. AccessorInfo.Data() for the accessor will hold the field number. */ Local<Object> jsFields = args[1]->ToObject(); for(unsigned int i = 0 ; i < ncol; i++) { Handle<String> fieldName = jsFields->Get(i)->ToString(); inst->SetAccessor(fieldName, nroGetter, nroSetter, Number::New(i), DEFAULT, DontDelete); } /* The generic constructor is the CallHandler */ ft->SetCallHandler(nroConstructor, Persistent<Object>::New(mapData)); return scope.Close(ft->GetFunction()); }
void APIModule::Initialize(Local<Object> target, Local<Context> context) { Isolate* isolate = context->GetIsolate(); HandleScope scope(isolate); Local<FunctionTemplate> constructor = FunctionTemplate::New(isolate); constructor->SetClassName(NEW_SYMBOL(isolate, "API")); constructorTemplate.Reset(isolate, constructor); // Hook methods to the API prototype, notice these aren't hooked to API // itself, instead we return a singleton of an API instance and export it // as Ti.API // Not sure why we then hook apiName as instance proprty, since // the difference is made moot by the singleton! SetProtoMethod(isolate, constructor, "debug", logDebug); SetProtoMethod(isolate, constructor, "info", logInfo); SetProtoMethod(isolate, constructor, "warn", logWarn); SetProtoMethod(isolate, constructor, "error", logError); SetProtoMethod(isolate, constructor, "trace", logTrace); SetProtoMethod(isolate, constructor, "notice", logNotice); SetProtoMethod(isolate, constructor, "critical", logCritical); SetProtoMethod(isolate, constructor, "fatal", logFatal); SetProtoMethod(isolate, constructor, "log", log); SetProtoMethod(isolate, constructor, "getApiName", APIModule::getApiName); // Add an "apiName" property to instances, which delegates to APIModule::getter_apiName // TODO Use a constant here? Local<ObjectTemplate> instanceTemplate = constructor->InstanceTemplate(); instanceTemplate->SetAccessor(NEW_SYMBOL(isolate, "apiName"), APIModule::getter_apiName); // Expose a method for terminating the application for the debugger. // Debugger will send an evaluation request calling this method // when it wants the application to terminate immediately. if (V8Runtime::debuggerEnabled) { SetProtoMethod(isolate, constructor, "terminate", terminate); SetProtoMethod(isolate, constructor, "debugBreak", debugBreak); } // Make API extend from KrollModule constructor->Inherit(KrollModule::getProxyTemplate(isolate)); // export an instance of API as "API" (basically make a static singleton) target->Set(NEW_SYMBOL(isolate, "API"), constructor->GetFunction(context).ToLocalChecked()->NewInstance(context).ToLocalChecked()); }
/*static*/ void singleton::Init(Handle<Object> exports) { // Prepare constructor template Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(singleton::New); tpl->SetClassName(NanNew<String>("singleton")); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype Local<ObjectTemplate> proto = tpl->PrototypeTemplate(); // proto->Set(NanNew<String>("instance"), // NanNew<FunctionTemplate>(singleton::Instance)->GetFunction()); proto->SetAccessor( NanNew<String>("value"), singleton::GetValue, singleton::SetValue); constructor = Persistent<Function>::New(tpl->GetFunction()); exports->Set(NanNew<String>("singleton"), constructor); }
int ArrayType::PlatformNew(JNIEnv *jniEnv, Handle<Object> *val) { int result = OK; if(function.IsEmpty()) { functionTemplate = Persistent<FunctionTemplate>::New(FunctionTemplate::New(PlatformCtor)); functionTemplate->SetClassName(sClassName); Local<ObjectTemplate> instanceTemplate = functionTemplate->InstanceTemplate(); instanceTemplate->SetInternalFieldCount(2); instanceTemplate->SetAccessor(env->getConv()->getArrayConv()->getSLength(), PlatformLengthGet, PlatformLengthSet); instanceTemplate->SetIndexedPropertyHandler(PlatformElementGet, PlatformElementSet); function = Persistent<Function>::New(functionTemplate->GetFunction()); /* set prototype to inherit from Array */ function->Set(String::New("prototype"), Array::New()->GetPrototype()); } if(result == OK) { Local<Object> local = function->NewInstance(); result = local.IsEmpty() ? ErrorVM : OK; if(result == OK) { local->SetPointerInInternalField(1, this); *val = local; } } return result; }
void ScreenInitBinding(Handle<Object> target) { NanScope(); Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(newScreen); NanAssignPersistent(constructor, ctor); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(NanNew("Screen")); Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); proto->SetAccessor(NanNew("colorDepth"), ScreenGetColorDepth, ScreenSetColorDepth); proto->SetAccessor(NanNew("pixelDepth"), ScreenGetPixelDepth, ScreenSetPixelDepth); proto->SetAccessor(NanNew("availLeft"), ScreenGetAvailLeft, ScreenSetAvailLeft); proto->SetAccessor(NanNew("availTop"), ScreenGetAvailTop, ScreenSetAvailTop); proto->SetAccessor(NanNew("availWidth"), ScreenGetAvailWidth, ScreenSetAvailWidth); proto->SetAccessor(NanNew("availHeight"), ScreenGetAvailHeight, ScreenSetAvailHeight); proto->SetAccessor(NanNew("width"), ScreenGetWidth, ScreenSetWidth); proto->SetAccessor(NanNew("height"), ScreenGetHeight, ScreenSetHeight); target->Set(NanNew("Screen"), ctor->GetFunction()); }
void FixSession::Initialize(Handle<Object> target) { NanScope(); Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(FixSession::New); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(NanNew("FixSession")); Local<ObjectTemplate> proto = ctor->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(ctor, "disconnect", disconnect); NODE_SET_PROTOTYPE_METHOD(ctor, "getSessionID", getSessionID); NODE_SET_PROTOTYPE_METHOD(ctor, "isEnabled", isEnabled); NODE_SET_PROTOTYPE_METHOD(ctor, "isLoggedOn", isLoggedOn); NODE_SET_PROTOTYPE_METHOD(ctor, "logon", logon); NODE_SET_PROTOTYPE_METHOD(ctor, "logout", logout); NODE_SET_PROTOTYPE_METHOD(ctor, "refresh", refresh); NODE_SET_PROTOTYPE_METHOD(ctor, "reset", reset); proto->SetAccessor(NanNew("senderSeqNum"), getSenderSeqNum, setSenderSeqNum); proto->SetAccessor(NanNew("targetSeqNum"), getTargetSeqNum, setTargetSeqNum); target->Set(NanNew("FixSession"), ctor->GetFunction()); }
void Image::Initialize(Handle<ObjectTemplate> target) { HandleScope scope; // Constructor constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(Image::New)); constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->SetClassName(String::NewSymbol("Image")); // Prototype Local<ObjectTemplate> proto = constructor->PrototypeTemplate(); proto->SetAccessor(String::NewSymbol("source"), GetSource, SetSource); proto->SetAccessor(String::NewSymbol("complete"), GetComplete); proto->SetAccessor(String::NewSymbol("width"), GetWidth); proto->SetAccessor(String::NewSymbol("height"), GetHeight); proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload); proto->SetAccessor(String::NewSymbol("onerror"), GetOnerror, SetOnerror); target->Set(String::NewSymbol("Image"), constructor); }
Local<Function> MetadataNode::SetMembersFromRuntimeMetadata(Isolate *isolate, Local<FunctionTemplate>& ctorFuncTemplate, Local<ObjectTemplate>& prototypeTemplate, vector<MethodCallbackData*>& instanceMethodsCallbackData, const vector<MethodCallbackData*>& baseInstanceMethodsCallbackData, MetadataTreeNode *treeNode) { SET_PROFILER_FRAME(); assert(treeNode->metadata != nullptr); string line; const string& metadata = *treeNode->metadata; stringstream s(metadata); string kind; string name; string signature; int paramCount; getline(s, line); // type line getline(s, line); // base class line string lastMethodName; MethodCallbackData *callbackData = nullptr; while (getline(s, line)) { stringstream tmp(line); tmp >> kind >> name >> signature >> paramCount; char chKind = kind[0]; assert((chKind == 'M') || (chKind == 'F')); MetadataEntry entry; entry.name = name; entry.sig = signature; MetadataReader::FillReturnType(entry); entry.paramCount = paramCount; entry.isStatic = false; if (chKind == 'M') { if (entry.name != lastMethodName) { callbackData = new MethodCallbackData(this); instanceMethodsCallbackData.push_back(callbackData); auto itBegin = baseInstanceMethodsCallbackData.begin(); auto itEnd = baseInstanceMethodsCallbackData.end(); auto itFound = find_if(itBegin, itEnd, [&entry] (MethodCallbackData *x) { return x->candidates.front().name == entry.name; }); if (itFound != itEnd) { callbackData->parent = *itFound; } auto funcData = External::New(isolate, callbackData); auto funcTemplate = FunctionTemplate::New(isolate, MethodCallback, funcData); auto funcName = ConvertToV8String(entry.name); prototypeTemplate->Set(funcName, funcTemplate->GetFunction()); lastMethodName = entry.name; } callbackData->candidates.push_back(entry); } else if (chKind == 'F') { auto fieldName = ConvertToV8String(entry.name); auto fieldData = External::New(isolate, new FieldCallbackData(entry)); auto access = entry.isFinal ? AccessControl::ALL_CAN_READ : AccessControl::DEFAULT; prototypeTemplate->SetAccessor(fieldName, FieldAccessorGetterCallback, FieldAccessorSetterCallback, fieldData, access, PropertyAttribute::DontDelete); } } auto ctorFunction = ctorFuncTemplate->GetFunction(); return ctorFunction; }
Local<Function> MetadataNode::SetMembersFromStaticMetadata(Isolate *isolate, Local<FunctionTemplate>& ctorFuncTemplate, Local<ObjectTemplate>& prototypeTemplate, vector<MethodCallbackData*>& instanceMethodsCallbackData, const vector<MethodCallbackData*>& baseInstanceMethodsCallbackData, MetadataTreeNode *treeNode) { SET_PROFILER_FRAME(); Local<Function> ctorFunction; uint8_t *curPtr = s_metadataReader.GetValueData() + treeNode->offsetValue + 1; auto nodeType = s_metadataReader.GetNodeType(treeNode); auto curType = s_metadataReader.ReadTypeName(treeNode); curPtr += sizeof(uint16_t /* baseClassId */); if (s_metadataReader.IsNodeTypeInterface(nodeType)) { curPtr += sizeof(uint8_t) + sizeof(uint32_t); } //get candidates from instance methods metadata auto instanceMethodCout = *reinterpret_cast<uint16_t*>(curPtr); curPtr += sizeof(uint16_t); string lastMethodName; MethodCallbackData *callbackData = nullptr; for (auto i = 0; i < instanceMethodCout; i++) { auto entry = s_metadataReader.ReadInstanceMethodEntry(&curPtr); if (entry.name != lastMethodName) { callbackData = new MethodCallbackData(this); instanceMethodsCallbackData.push_back(callbackData); auto itBegin = baseInstanceMethodsCallbackData.begin(); auto itEnd = baseInstanceMethodsCallbackData.end(); auto itFound = find_if(itBegin, itEnd, [&entry] (MethodCallbackData *x) { return x->candidates.front().name == entry.name; }); if (itFound != itEnd) { callbackData->parent = *itFound; } auto funcData = External::New(isolate, callbackData); auto funcTemplate = FunctionTemplate::New(isolate, MethodCallback, funcData); auto funcName = ConvertToV8String(entry.name); prototypeTemplate->Set(funcName, funcTemplate->GetFunction()); lastMethodName = entry.name; } callbackData->candidates.push_back(entry); } //get candidates from instance fields metadata auto instanceFieldCout = *reinterpret_cast<uint16_t*>(curPtr); curPtr += sizeof(uint16_t); for (auto i = 0; i < instanceFieldCout; i++) { auto entry = s_metadataReader.ReadInstanceFieldEntry(&curPtr); auto fieldName = ConvertToV8String(entry.name); auto fieldInfo = new FieldCallbackData(entry); fieldInfo->declaringType = curType; auto fieldData = External::New(isolate, fieldInfo); prototypeTemplate->SetAccessor(fieldName, FieldAccessorGetterCallback, FieldAccessorSetterCallback, fieldData, AccessControl::DEFAULT, PropertyAttribute::DontDelete); } ctorFunction = ctorFuncTemplate->GetFunction(); //get candidates from static methods metadata callbackData = nullptr; lastMethodName.clear(); auto staticMethodCout = *reinterpret_cast<uint16_t*>(curPtr); curPtr += sizeof(uint16_t); for (auto i = 0; i < staticMethodCout; i++) { auto entry = s_metadataReader.ReadStaticMethodEntry(&curPtr); if (entry.name != lastMethodName) { callbackData = new MethodCallbackData(this); auto funcData = External::New(isolate, callbackData); auto funcTemplate = FunctionTemplate::New(isolate, MethodCallback, funcData); auto funcName = ConvertToV8String(entry.name); ctorFunction->Set(funcName, funcTemplate->GetFunction()); lastMethodName = entry.name; } callbackData->candidates.push_back(entry); } auto extendFuncName = V8StringConstants::GetExtend(); auto extendFuncTemplate = FunctionTemplate::New(isolate, ExtendCallMethodHandler, External::New(isolate, this)); ctorFunction->Set(extendFuncName, extendFuncTemplate->GetFunction()); //get candidates from static fields metadata auto staticFieldCout = *reinterpret_cast<uint16_t*>(curPtr); curPtr += sizeof(uint16_t); for (auto i = 0; i < staticFieldCout; i++) { auto entry = s_metadataReader.ReadStaticFieldEntry(&curPtr); auto fieldName = ConvertToV8String(entry.name); auto fieldData = External::New(isolate, new FieldCallbackData(entry)); ctorFunction->SetAccessor(fieldName, FieldAccessorGetterCallback, FieldAccessorSetterCallback, fieldData, AccessControl::DEFAULT, PropertyAttribute::DontDelete); } SetClassAccessor(ctorFunction); return ctorFunction; }