v8::Handle<v8::Function> CreateDebugDrawManager(Handle<Context> context) { v8::HandleScope handle_scope; Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_debugDrawWrapper); if(templt.IsEmpty()) { templt = FunctionTemplate::New(); templt->SetClassName(String::New("DebugDrawManager")); templt->InstanceTemplate()->SetInternalFieldCount(1); Handle<ObjectTemplate> proto = templt->PrototypeTemplate(); proto->Set("addAABB", FunctionTemplate::New(DebugDrawManagerAddAABB)); proto->Set("addCircle", FunctionTemplate::New(DebugDrawManagerAddCircle)); proto->Set("addCross", FunctionTemplate::New(DebugDrawManagerAddCross)); proto->Set("addLine", FunctionTemplate::New(DebugDrawManagerAddLine)); proto->Set("addLines", FunctionTemplate::New(DebugDrawManagerAddLines)); proto->Set("addSphere", FunctionTemplate::New(DebugDrawManagerAddSphere)); proto->Set("addString", FunctionTemplate::New(DebugDrawManagerAddString)); proto->Set("addTriangle", FunctionTemplate::New(DebugDrawManagerAddTriangle)); proto->Set("clear", FunctionTemplate::New(DebugDrawManagerClear)); proto->Set("isEnabled", FunctionTemplate::New(DebugDrawManagerIsEnabled)); proto->Set("setEnabled", FunctionTemplate::New(DebugDrawManagerSetEnabled)); proto->Set("toString", FunctionTemplate::New(DebugDrawManagerToString)); GetScriptSystem()->SetTemplateBySID(s_debugDrawWrapper, templt); } return handle_scope.Close(templt->GetFunction()); }
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); }
JsEngine* JsEngine::New(int32_t max_young_space = -1, int32_t max_old_space = -1) { JsEngine* engine = new JsEngine(); if (engine != NULL) { engine->isolate_ = Isolate::New(); engine->isolate_->Enter(); if (max_young_space > 0 && max_old_space > 0) { v8::ResourceConstraints constraints; constraints.set_max_young_space_size(max_young_space * Mega); constraints.set_max_old_space_size(max_old_space * Mega); v8::SetResourceConstraints(&constraints); } engine->isolate_->Exit(); Locker locker(engine->isolate_); Isolate::Scope isolate_scope(engine->isolate_); HandleScope scope; // Setup the template we'll use for all managed object references. Handle<FunctionTemplate> fo = FunctionTemplate::New(NULL); Handle<ObjectTemplate> obj_template = fo->InstanceTemplate(); obj_template->SetInternalFieldCount(1); obj_template->SetNamedPropertyHandler( managed_prop_get, managed_prop_set, NULL, managed_prop_delete, managed_prop_enumerate); obj_template->SetCallAsFunctionHandler(managed_call); engine->managed_template_ = new Persistent<FunctionTemplate>(Persistent<FunctionTemplate>::New(fo)); Persistent<FunctionTemplate> fp = Persistent<FunctionTemplate>::New(FunctionTemplate::New(managed_valueof)); engine->valueof_function_template_ = new Persistent<FunctionTemplate>(fp); engine->global_context_ = new Persistent<Context>(Context::New()); (*engine->global_context_)->Enter(); fo->PrototypeTemplate()->Set(String::New("valueOf"), fp->GetFunction()); (*engine->global_context_)->Exit(); } return engine; }
Handle<FunctionTemplate> MetadataNode::GetConstructorFunctionTemplate(Isolate *isolate, MetadataTreeNode *treeNode) { SET_PROFILER_FRAME(); Handle<FunctionTemplate> ctorFuncTemplate; auto itFound = s_ctorFuncCache.find(treeNode); if (itFound != s_ctorFuncCache.end()) { ctorFuncTemplate = Local<FunctionTemplate>::New(isolate, *itFound->second); return ctorFuncTemplate; } auto node = GetOrCreateInternal(treeNode); auto ctorCallbackData = External::New(isolate, node); auto isInterface = s_metadataReader.IsNodeTypeInterface(treeNode->type); auto funcCallback = isInterface ? InterfaceConstructorCallback : ClassConstructorCallback; ctorFuncTemplate = FunctionTemplate::New(isolate, funcCallback, ctorCallbackData); auto baseClass = s_metadataReader.GetBaseClassNode(treeNode); Handle<Function> baseCtorFunc; if ((baseClass != treeNode) && (baseClass != nullptr) && (baseClass->offsetValue > 0)) { auto baseFuncTemplate = GetConstructorFunctionTemplate(isolate, baseClass); if (!baseFuncTemplate.IsEmpty()) { ctorFuncTemplate->Inherit(baseFuncTemplate); baseCtorFunc = baseFuncTemplate->GetFunction(); } } auto prototypeTemplate = ctorFuncTemplate->PrototypeTemplate(); auto ctorFunc = node->SetMembers(isolate, ctorFuncTemplate, prototypeTemplate, treeNode); if (!baseCtorFunc.IsEmpty()) { ctorFunc->SetPrototype(baseCtorFunc); } auto pft = new Persistent<FunctionTemplate>(isolate, ctorFuncTemplate); s_ctorFuncCache.insert(make_pair(treeNode, pft)); SetInnnerTypes(isolate, ctorFunc, treeNode); SetTypeMetadata(isolate, ctorFunc, new TypeMetadata(s_metadataReader.ReadTypeName(treeNode))); return ctorFuncTemplate; }
void InitAnimationSystemWrapper(ScriptSystem* ss) { HandleScope scope; Context::Scope context_scope(ss->GetGlobalContext()); Handle<FunctionTemplate> templt = FunctionTemplate::New(); templt->SetClassName(String::New("SoundSystem")); templt->InstanceTemplate()->SetInternalFieldCount(1); Handle<ObjectTemplate> proto = templt->PrototypeTemplate(); proto->Set("toString", FunctionTemplate::New(ASToString)); proto->Set("playAnimation", FunctionTemplate::New(ASPlayAnimation)); proto->Set("clearAnimation", FunctionTemplate::New(ASClearAnimation)); proto->Set("hasAnimation", FunctionTemplate::New(ASHasAnimation)); proto->Set("isAnimationPlaying", FunctionTemplate::New(ASIsAnimationPlaying)); proto->Set("clearAll", FunctionTemplate::New(ASClearAll)); proto->Set("getRegisteredAnimations", FunctionTemplate::New(ASGetRegisteredAnimations)); RegisterEntitySystempWrapper(ss, dtEntity::AnimationComponent::TYPE, templt); }
v8::Handle<v8::Object> WrapLogger(Handle<Context> context) { v8::HandleScope handle_scope; Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_loggerWrapper); if(templt.IsEmpty()) { templt = FunctionTemplate::New(); templt->SetClassName(String::New("Log")); Handle<ObjectTemplate> proto = templt->PrototypeTemplate(); proto->Set("always", FunctionTemplate::New(LogAlways)); proto->Set("debug", FunctionTemplate::New(LogDebug)); proto->Set("error", FunctionTemplate::New(LogError)); proto->Set("info", FunctionTemplate::New(LogInfo)); proto->Set("warning", FunctionTemplate::New(LogWarning)); proto->Set("addLogListener", FunctionTemplate::New(LogAddListener)); proto->Set("processLogListeners", FunctionTemplate::New(LogProcessListeners)); GetScriptSystem()->SetTemplateBySID(s_loggerWrapper, templt); } Local<Object> instance = templt->GetFunction()->NewInstance(); return handle_scope.Close(instance); }
v8::Handle<v8::Object> WrapElementDocument(v8::Handle<v8::Context> context, Rocket::Core::ElementDocument* v) { v8::HandleScope handle_scope; Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_elementDocumentWrapper); if(templt.IsEmpty()) { templt = FunctionTemplate::New(); templt->Inherit(GetElementTemplate()); templt->SetClassName(String::New("ElementDocument")); templt->InstanceTemplate()->SetInternalFieldCount(1); templt->InstanceTemplate()->SetNamedPropertyHandler( ELNamedPropertyGetter, ELNamedPropertySetter, ELNamedPropertyQuery, ELNamedPropertyDeleter, ELNamedPropertyEnumerator ); Handle<ObjectTemplate> proto = templt->PrototypeTemplate(); proto->Set("hide", FunctionTemplate::New(EDHide)); proto->Set("toString", FunctionTemplate::New(EDToString)); proto->Set("show", FunctionTemplate::New(EDShow)); proto->Set("hide", FunctionTemplate::New(EDHide)); proto->Set("close", FunctionTemplate::New(EDClose)); GetScriptSystem()->SetTemplateBySID(s_elementDocumentWrapper, templt); } Local<Object> instance = templt->GetFunction()->NewInstance(); instance->SetInternalField(0, External::New(v)); return handle_scope.Close(instance); }
Handle<Object> WrapComponent(Handle<Object> wrappedes, ScriptSystem* scriptsys, dtEntity::EntityId eid, dtEntity::Component* v) { HandleScope handle_scope; Handle<Object> wrapped = scriptsys->GetFromComponentMap(v->GetType(), eid); if(!wrapped.IsEmpty()) { return wrapped; } Handle<FunctionTemplate> templt = GetScriptSystem()->GetTemplateBySID(s_componentWrapper); if(templt.IsEmpty()) { templt = FunctionTemplate::New(); templt->SetClassName(String::New("Component")); templt->InstanceTemplate()->SetInternalFieldCount(1); Handle<ObjectTemplate> proto = templt->PrototypeTemplate(); proto->Set("equals", FunctionTemplate::New(COEquals)); proto->Set("getType", FunctionTemplate::New(COGetType)); proto->Set("properties", FunctionTemplate::New(COProperties)); proto->Set("toString", FunctionTemplate::New(COToString)); proto->Set("finished", FunctionTemplate::New(COFinished)); proto->Set("copyPropertyValues", FunctionTemplate::New(COCopyPropertyValues)); GetScriptSystem()->SetTemplateBySID(s_componentWrapper, templt); } Local<Object> instance = templt->GetFunction()->NewInstance(); instance->SetInternalField(0, External::New(v)); instance->SetHiddenValue(scriptsys->GetEntityIdString(), Uint32::New(eid)); // GetStringFromSID and conversion to v8::String is costly, create a // hidden value in entity system wrapper that stores // strings and their string ids as name=>value pairs Handle<Value> propnamesval = wrappedes->GetHiddenValue(scriptsys->GetPropertyNamesString()); if(propnamesval.IsEmpty()) { Handle<Object> names = Object::New(); dtEntity::PropertyGroup::const_iterator i; const dtEntity::PropertyGroup& props = v->Get(); for(i = props.begin(); i != props.end(); ++i) { dtEntity::StringId sid = i->first; std::string propname = dtEntity::GetStringFromSID(sid); names->Set(String::New(propname.c_str()), WrapSID(sid)); } wrappedes->SetHiddenValue(scriptsys->GetPropertyNamesString(), names); propnamesval = names; } Handle<Object> propnames = Handle<Object>::Cast(propnamesval); Handle<Array> keys = propnames->GetPropertyNames(); for(unsigned int i = 0; i < keys->Length(); ++i) { Handle<String> str = keys->Get(i)->ToString(); dtEntity::StringId sid = UnwrapSID(propnames->Get(str)); dtEntity::Property* prop = v->Get(sid); if(prop == NULL) { LOG_ERROR("Could not find property in component: " << ToStdString(str)); continue; } Handle<External> ext = v8::External::New(static_cast<void*>(prop)); instance->SetAccessor(str, COPropertyGetter, COPropertySetter, ext); } // store wrapped component to script system scriptsys->AddToComponentMap(v->GetType(), eid, instance); return handle_scope.Close(instance); }
void CanvasContextV8Bindings::loadScript(const std::string& _filename, OgreCanvas::CanvasContext* _canvasContext, OgreCanvas::CanvasLogger* _console) { CanvasContextV8Bindings::context2D = _canvasContext; HandleScope handle_scope; //Console : //template Handle<FunctionTemplate> consoleTemplate = FunctionTemplate::New(); consoleTemplate->SetClassName(v8::String::New("Console")); CanvasContextV8Bindings::consoleTemplate = Persistent<FunctionTemplate>::New(consoleTemplate); //prototype Handle<ObjectTemplate> consolePrototype = consoleTemplate->PrototypeTemplate(); //attaching method consolePrototype->Set("log", FunctionTemplate::New(log)); //creating instance Handle<ObjectTemplate> consoleInstance = consoleTemplate->InstanceTemplate(); consoleInstance->SetInternalFieldCount(1); //Image : //template Handle<FunctionTemplate> imageTemplate = FunctionTemplate::New(); imageTemplate->SetClassName(v8::String::New("Image")); CanvasContextV8Bindings::imageTemplate = Persistent<FunctionTemplate>::New(imageTemplate); //prototype Handle<ObjectTemplate> imagePrototype = imageTemplate->PrototypeTemplate(); //creating instance Handle<ObjectTemplate> imageInstance = imageTemplate->InstanceTemplate(); imageInstance->SetInternalFieldCount(1); //Canvas gradient : //template Handle<FunctionTemplate> canvasGradientTemplate = FunctionTemplate::New(); canvasGradientTemplate->SetClassName(v8::String::New("CanvasGradient")); CanvasContextV8Bindings::canvasGradientTemplate = Persistent<FunctionTemplate>::New(canvasGradientTemplate); //prototype Handle<ObjectTemplate> canvasGradientPrototype = canvasGradientTemplate->PrototypeTemplate(); //creating instance Handle<ObjectTemplate> canvasGradientInstance = canvasGradientTemplate->InstanceTemplate(); canvasGradientInstance->SetInternalFieldCount(1); //attaching method canvasGradientPrototype->Set("addColorStop", FunctionTemplate::New(addColorStop)); //Canvas Pattern : //template Handle<FunctionTemplate> canvasPatternTemplate = FunctionTemplate::New(); canvasPatternTemplate->SetClassName(v8::String::New("CanvasPattern")); CanvasContextV8Bindings::canvasPatternTemplate = Persistent<FunctionTemplate>::New(canvasPatternTemplate); //prototype Handle<ObjectTemplate> canvasPatternPrototype = canvasPatternTemplate->PrototypeTemplate(); //creating instance Handle<ObjectTemplate> canvasPatternInstance = canvasPatternTemplate->InstanceTemplate(); canvasPatternInstance->SetInternalFieldCount(1); //Canvas context : //template Handle<FunctionTemplate> canvasContextTemplate = FunctionTemplate::New(); canvasContextTemplate->SetClassName(v8::String::New("CanvasContext")); //prototype Handle<ObjectTemplate> canvasContextPrototype = canvasContextTemplate->PrototypeTemplate(); //attaching method //2D Context canvasContextPrototype->Set("save", FunctionTemplate::New(save)); canvasContextPrototype->Set("restore", FunctionTemplate::New(restore)); //Transformation canvasContextPrototype->Set("scale", FunctionTemplate::New(scale)); canvasContextPrototype->Set("rotate", FunctionTemplate::New(rotate)); canvasContextPrototype->Set("translate", FunctionTemplate::New(translate)); canvasContextPrototype->Set("transform", FunctionTemplate::New(transform)); canvasContextPrototype->Set("setTransform", FunctionTemplate::New(setTransform)); //Image drawing canvasContextPrototype->Set("drawImage", FunctionTemplate::New(drawImage)); //Colors, styles and shadows canvasContextPrototype->Set("createLinearGradient", FunctionTemplate::New(createLinearGradient)); canvasContextPrototype->Set("createRadialGradient", FunctionTemplate::New(createRadialGradient)); canvasContextPrototype->Set("createPattern", FunctionTemplate::New(createPattern)); //Paths canvasContextPrototype->Set("beginPath", FunctionTemplate::New(beginPath)); canvasContextPrototype->Set("closePath", FunctionTemplate::New(closePath)); canvasContextPrototype->Set("fill", FunctionTemplate::New(fill)); canvasContextPrototype->Set("stroke", FunctionTemplate::New(stroke)); canvasContextPrototype->Set("clip", FunctionTemplate::New(clip)); canvasContextPrototype->Set("moveTo", FunctionTemplate::New(moveTo)); canvasContextPrototype->Set("lineTo", FunctionTemplate::New(lineTo)); canvasContextPrototype->Set("quadraticCurveTo", FunctionTemplate::New(quadraticCurveTo)); canvasContextPrototype->Set("bezierCurveTo", FunctionTemplate::New(bezierCurveTo)); canvasContextPrototype->Set("arcTo", FunctionTemplate::New(arcTo)); canvasContextPrototype->Set("arc", FunctionTemplate::New(arc)); canvasContextPrototype->Set("rect", FunctionTemplate::New(rect)); canvasContextPrototype->Set("isPointInPath", FunctionTemplate::New(isPointInPath)); //Text canvasContextPrototype->Set("fillText", FunctionTemplate::New(fillText)); canvasContextPrototype->Set("strokeText", FunctionTemplate::New(strokeText)); canvasContextPrototype->Set("measureText", FunctionTemplate::New(measureText)); //Rectangles canvasContextPrototype->Set("clearRect", FunctionTemplate::New(clearRect)); canvasContextPrototype->Set("fillRect", FunctionTemplate::New(fillRect)); canvasContextPrototype->Set("strokeRect", FunctionTemplate::New(strokeRect)); //New canvasContextPrototype->Set("saveToPNG", FunctionTemplate::New(saveToPNG)); canvasContextPrototype->Set("clear", FunctionTemplate::New(clear)); //creating instance Handle<ObjectTemplate> canvasContextInstance = canvasContextTemplate->InstanceTemplate(); canvasContextInstance->SetInternalFieldCount(1); //attaching properties //Compositing canvasContextInstance->SetAccessor(v8::String::New("globalAlpha"), getterGlobalAlpha, setterGlobalAlpha); canvasContextInstance->SetAccessor(v8::String::New("globalCompositeOperation"), getterGlobalCompositeOperation, setterGlobalCompositeOperation); //Line styles canvasContextInstance->SetAccessor(v8::String::New("lineWidth"), getterLineWidth, setterLineWidth); canvasContextInstance->SetAccessor(v8::String::New("lineCap"), getterLineCap, setterLineCap); canvasContextInstance->SetAccessor(v8::String::New("lineJoin"), getterLineJoin, setterLineJoin); canvasContextInstance->SetAccessor(v8::String::New("miterLimit"), getterMiterLimit, setterMiterLimit); canvasContextInstance->SetAccessor(v8::String::New("lineDash"), getterLineDash, setterLineDash); //Colors, styles and shadows canvasContextInstance->SetAccessor(v8::String::New("fillStyle"), getterFillStyle, setterFillStyle); canvasContextInstance->SetAccessor(v8::String::New("strokeStyle"), getterStrokeStyle, setterStrokeStyle); canvasContextInstance->SetAccessor(v8::String::New("shadowOffsetX"), getterShadowOffsetX, setterShadowOffsetX); canvasContextInstance->SetAccessor(v8::String::New("shadowOffsetY"), getterShadowOffsetY, setterShadowOffsetY); canvasContextInstance->SetAccessor(v8::String::New("shadowBlur"), getterShadowBlur, setterShadowBlur); canvasContextInstance->SetAccessor(v8::String::New("shadowColor"), getterShadowColor, setterShadowColor); //Text canvasContextInstance->SetAccessor(v8::String::New("font"), getterFont, setterFont); canvasContextInstance->SetAccessor(v8::String::New("textAlign"), getterTextAlign, setterTextAlign); canvasContextInstance->SetAccessor(v8::String::New("textBaseline"), getterTextBaseline, setterTextBaseline); //New canvasContextInstance->SetAccessor(v8::String::New("antialiasing"), getterAntiAliasing, setterAntiAliasing); //Image Handle<ObjectTemplate> global = ObjectTemplate::New(); global->Set(String::New("loadImage"), FunctionTemplate::New(loadImage)); Persistent<Context> context = Context::New(NULL, global); CanvasContextV8Bindings::contextV8 = context; Context::Scope context_scope(context); //building link between js 'ctx' variable and c++ _canvasContext variable Handle<Function> canvasContextConstructor = canvasContextTemplate->GetFunction(); Local<Object> obj = canvasContextConstructor->NewInstance(); obj->SetInternalField(0, External::New(_canvasContext)); context->Global()->Set(v8::String::New("ctx"), obj); //building link between js 'console' variable and c++ _console variable Handle<Function> consoleConstructor = consoleTemplate->GetFunction(); Local<Object> obj2 = consoleConstructor->NewInstance(); obj2->SetInternalField(0, External::New(_console)); context->Global()->Set(v8::String::New("console"), obj2); Handle<v8::String> source = v8::String::New(readScript(_filename).c_str()); Handle<Script> script = Script::Compile(source); Handle<Value> result = script->Run(); /* CanvasContextV8Bindings::canvasGradientTemplate.Dispose(); CanvasContextV8Bindings::canvasPatternTemplate.Dispose(); CanvasContextV8Bindings::imageTemplate.Dispose(); */ }