Handle<Value> ScriptSystem::ExecuteJS(const std::string& code, const std::string& path) { // Init JavaScript context HandleScope handle_scope; Context::Scope context_scope(GetGlobalContext()); // We're just about to compile the script; set up an error handler to // catch any exceptions the script might throw. TryCatch try_catch; // Compile the source code. Local<Script> compiled_script = Script::Compile(ToJSString(code), ToJSString(path)); // if an exception occured if(try_catch.HasCaught()) { ReportException(&try_catch); // return try_catch; } // Run the script! Local<Value> ret = compiled_script->Run(); if(try_catch.HasCaught()) { ReportException(&try_catch); // return try_catch; } FetchGlobalTickFunction(); return handle_scope.Close(ret); }
Handle<Value> MSGetSpawnerComponents(const Arguments& args) { dtEntity::MapSystem* ms = UnwrapMapSystem(args.This()); dtEntity::Spawner* spawner; if(!ms->GetSpawner(ToStdString(args[0]), spawner)) { return Null(); } HandleScope scope; Handle<Object> comps = Object::New(); dtEntity::Spawner::ComponentProperties props; spawner->GetAllComponentPropertiesRecursive(props); dtEntity::Spawner::ComponentProperties::iterator i; for(i = props.begin(); i != props.end(); ++i) { std::string compname = dtEntity::GetStringFromSID(i->first); Handle<Object> jscomp = Object::New(); const dtEntity::GroupProperty props = i->second; dtEntity::PropertyGroup g = props.Get(); for(dtEntity::PropertyGroup::const_iterator j = g.begin(); j != g.end(); ++j) { std::string propname = dtEntity::GetStringFromSID(j->first); const dtEntity::Property* prop = j->second; jscomp->Set(ToJSString(propname), ConvertPropertyToValue(args.This()->CreationContext(), prop)); } comps->Set(ToJSString(compname), jscomp); } return scope.Close(comps); }
Handle<Value> MSGetSpawner(const Arguments& args) { dtEntity::MapSystem* ms = UnwrapMapSystem(args.This()); dtEntity::Spawner* spawner; if(!ms->GetSpawner(ToStdString(args[0]), spawner)) { return Null(); } HandleScope scope; Handle<Object> obj = Object::New(); if(spawner->GetParent()) { obj->Set(String::New("parent"), String::New(spawner->GetParent()->GetName().c_str())); } else { obj->Set(String::New("parent"), String::New("")); } obj->Set(String::New("name"), String::New(spawner->GetName().c_str())); obj->Set(String::New("guicategory"), String::New(spawner->GetGUICategory().c_str())); obj->Set(String::New("mapname"), String::New(spawner->GetMapName().c_str())); obj->Set(String::New("addtospawnerstore"), Boolean::New(spawner->GetAddToSpawnerStore())); obj->Set(String::New("iconpath"), String::New(spawner->GetIconPath().c_str())); Handle<Object> comps = Object::New(); dtEntity::Spawner::ComponentProperties props; spawner->GetAllComponentProperties(props); dtEntity::Spawner::ComponentProperties::iterator i; for(i = props.begin(); i != props.end(); ++i) { std::string compname = dtEntity::GetStringFromSID(i->first); Handle<Object> jscomp = Object::New(); const dtEntity::GroupProperty props = i->second; dtEntity::PropertyGroup g = props.Get(); for(dtEntity::PropertyGroup::const_iterator j = g.begin(); j != g.end(); ++j) { std::string propname = dtEntity::GetStringFromSID(j->first); const dtEntity::Property* prop = j->second; jscomp->Set(ToJSString(propname), ConvertPropertyToValue(args.This()->CreationContext(), prop)); } comps->Set(ToJSString(compname), jscomp); } obj->Set(String::New("components"), comps); return scope.Close(obj); }
nsresult MediaKeyStatusMap::UpdateInternal(const nsTArray<CDMCaps::KeyStatus>& keys) { AutoJSAPI jsapi; if (NS_WARN_IF(!jsapi.Init(mParent))) { return NS_ERROR_FAILURE; } jsapi.TakeOwnershipOfErrorReporting(); JSContext* cx = jsapi.cx(); JS::Rooted<JSObject*> map(cx, mMap); if (!JS::MapClear(cx, map)) { return NS_ERROR_FAILURE; } for (size_t i = 0; i < keys.Length(); i++) { const auto& ks = keys[i]; JS::Rooted<JS::Value> key(cx); JS::Rooted<JS::Value> val(cx); if (!ToJSValue(cx, TypedArrayCreator<ArrayBuffer>(ks.mId), &key) || !ToJSString(cx, ks.mStatus, &val) || !JS::MapSet(cx, map, key, val)) { return NS_ERROR_OUT_OF_MEMORY; } } return NS_OK; }
Handle<Value> COGetType(const Arguments& args) { dtEntity::Component* component = UnwrapComponent(args.This()); if(component == NULL) { return ThrowError("Accessing a deleted component!"); } return ToJSString(dtEntity::GetStringFromSID(component->GetType())); }
void Process() { HandleScope scope; Context::Scope context_scope(mFunction->CreationContext()); Handle<String> loglevel; while(!mMessages.Empty()) { Msg msg = mMessages.Pop(); switch(msg.mLevel) { case dtEntity::LogLevel::LVL_ALWAYS: loglevel = mAlways; break; case dtEntity::LogLevel::LVL_DEBUG: loglevel = mDebug; break; case dtEntity::LogLevel::LVL_ERROR: loglevel = mError; break; case dtEntity::LogLevel::LVL_INFO : loglevel = mInfo; break; case dtEntity::LogLevel::LVL_WARNING: loglevel = mWarning; break; } #if defined (_DEBUG) bool is_debug = true; #else bool is_debug = false; #endif Handle<Value> argv[6] = { loglevel, ToJSString(msg.mFilename), ToJSString(msg.mMethodname), Integer::New(msg.mLinenumber), ToJSString(msg.mMsg), Boolean::New(is_debug) }; TryCatch try_catch; Handle<Value> result = mFunction->Call(mFunction, 6, argv); if(result.IsEmpty()) { ReportException(&try_catch); } } }
Handle<Value> MSGetAllSpawnerNames(const Arguments& args) { dtEntity::MapSystem* ms = UnwrapMapSystem(args.This()); std::vector<std::string> names; ms->GetAllSpawnerNames(names); HandleScope scope; Handle<Array> arr = Array::New(); for(unsigned int i = 0; i < names.size(); ++i) { arr->Set(Integer::New(i), ToJSString(names[i])); } return scope.Close(arr); }
Handle<Value> MSGetLoadedMaps(const Arguments& args) { dtEntity::MapSystem* ms = UnwrapMapSystem(args.This()); std::vector<std::string> maps = ms->GetLoadedMaps(); HandleScope scope; Handle<Array> arr = Array::New(maps.size()); int idx = 0; for(std::vector<std::string>::iterator i = maps.begin(); i != maps.end(); ++i) { arr->Set(Integer::New(idx), ToJSString(*i)); ++idx; } return scope.Close(arr); }
Handle<Script> ScriptSystem::GetScriptFromFile(const std::string& path) { std::string code; bool success = GetFileContents(path, code); if(!success) { LOG_ERROR("Could not load script file from " + path); return Handle<Script>(); } HandleScope handle_scope; Context::Scope context_scope(GetGlobalContext()); TryCatch try_catch; Local<Script> compiled_script = Script::Compile(ToJSString(code), ToJSString(path)); if(try_catch.HasCaught()) { ReportException(&try_catch); return Handle<Script>(); } return handle_scope.Close(compiled_script); }
Handle<Value> COProperties(const Arguments& args) { dtEntity::Component* component = UnwrapComponent(args.This()); if(component == NULL) { return ThrowError("Accessing a deleted component!"); } HandleScope scope; Handle<Object> obj = Object::New(); const dtEntity::PropertyGroup& props = component->Get(); dtEntity::PropertyGroup::const_iterator i; for(i = props.begin(); i != props.end(); ++i) { std::string propname = dtEntity::GetStringFromSID(i->first); const dtEntity::Property* prop = i->second; obj->Set(ToJSString(propname), ConvertPropertyToValue(args.This()->CreationContext(), prop)); } return scope.Close(obj); }