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); }
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()); }
void Shape::Initialize(Handle<Object> target) { HandleScope scope; Handle<FunctionTemplate> c = FunctionTemplate::New(Shape::New); c->InstanceTemplate()->SetInternalFieldCount(1); c->SetClassName(String::NewSymbol("shapeObj")); SET_ATTRIBUTE_RO(c, "numvalues", getProp); SET_ATTRIBUTE_RO(c, "numlines", getProp); SET_ATTRIBUTE_RO(c, "index", getProp); SET_ATTRIBUTE_RO(c, "type", getProp); SET_ATTRIBUTE_RO(c, "tileindex", getProp); SET_ATTRIBUTE_RO(c, "classindex", getProp); SET_ATTRIBUTE(c, "text", getProp, setProp); NODE_SET_PROTOTYPE_METHOD(c, "clone", clone); NODE_SET_PROTOTYPE_METHOD(c, "line", getLine); NODE_SET_PROTOTYPE_METHOD(c, "add", addLine); NODE_SET_PROTOTYPE_METHOD(c, "setGeometry", setGeometry); NODE_DEFINE_CONSTANT(c->GetFunction(), "Point",MS_SHAPE_POINT); NODE_DEFINE_CONSTANT(c->GetFunction(), "Line",MS_SHAPE_LINE); NODE_DEFINE_CONSTANT(c->GetFunction(), "Polygon",MS_SHAPE_POLYGON); NODE_DEFINE_CONSTANT(c->GetFunction(), "Null",MS_SHAPE_NULL); target->Set(String::NewSymbol("shapeObj"), c->GetFunction()); constructor.Reset(Isolate::GetCurrent(), c); }
void ScriptSystem::SetupContext() { HandleScope handle_scope; if(!mGlobalContext.IsEmpty()) { mGlobalContext.Dispose(); } // create a template for the global object Handle<ObjectTemplate> global = ObjectTemplate::New(); // create persistent global context mGlobalContext = Persistent<Context>::New(Context::New(NULL, global)); // store pointer to script system into isolate data to have it globally available in javascript Isolate::GetCurrent()->SetData(this); RegisterGlobalFunctions(this, mGlobalContext); RegisterPropertyFunctions(this, mGlobalContext); InitializeAllWrappers(GetEntityManager()); Handle<Context> context = GetGlobalContext(); Context::Scope context_scope(context); Handle<FunctionTemplate> tmplt = FunctionTemplate::New(); tmplt->InstanceTemplate()->SetInternalFieldCount(2); tmplt->SetClassName(String::New("ScriptSystem")); context->Global()->Set(String::New("Screen"), WrapScreen(this)); dtEntity::InputInterface* ipiface = dtEntity::GetInputInterface(); if(ipiface) { context->Global()->Set(String::New("Input"), WrapInputInterface(GetGlobalContext(), ipiface)); context->Global()->Set(String::New("Axis"), WrapAxes(ipiface)); context->Global()->Set(String::New("Key"), WrapKeys(ipiface)); } context->Global()->Set(String::New("TouchPhase"), WrapTouchPhases()); context->Global()->Set(String::New("Priority"), WrapPriorities()); context->Global()->Set(String::New("Order"), WrapPriorities()); }
void test_Name() { HandleScope handle_scope; Handle<ObjectTemplate> templ = ObjectTemplate::New(); Handle<FunctionTemplate> fnT = v8::FunctionTemplate::New(AddOne); templ->Set("AddOne", fnT); fnT->SetClassName(String::NewSymbol("AddOne")); Persistent<Context> context = Context::New(NULL, templ); Handle<Script> script = Script::New(String::New("AddOne.name;")); Context::Scope scope(context); Handle<Value> v = script->Run(); do_check_true(!v.IsEmpty()); Handle<String> s = v->ToString(); do_check_eq(s, String::NewSymbol("AddOne")); context.Dispose(); }
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(); */ }