diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp index f0015bd412..d835c9efcf 100644 --- a/source/scriptinterface/ScriptInterface.cpp +++ b/source/scriptinterface/ScriptInterface.cpp @@ -312,7 +312,7 @@ bool Math_random(JSContext* cx, uint UNUSED(argc), jsval* vp) { JS::CallReceiver rec = JS::CallReceiverFromVp(vp); double r; - if(!ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->MathRandom(r)) + if (!ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface->MathRandom(r)) return false; rec.rval().setNumber(r); @@ -434,9 +434,9 @@ ScriptInterface::CxPrivate* ScriptInterface::GetScriptInterfaceAndCBData(JSConte return pCxPrivate; } -JS::Value ScriptInterface::GetCachedValue(CACHED_VAL valueIdentifier) +JS::Value ScriptInterface::GetCachedValue(CACHED_VAL valueIdentifier) const { - std::map::iterator it = m->m_ScriptValCache.find(valueIdentifier); + std::map::const_iterator it = m->m_ScriptValCache.find(valueIdentifier); ENSURE(it != m->m_ScriptValCache.end()); return it->second.get(); } @@ -509,7 +509,7 @@ shared_ptr ScriptInterface::GetRuntime() const return m->m_runtime; } -void ScriptInterface::CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) +void ScriptInterface::CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const { JSAutoRequest rq(m->m_cx); if (!ctor.isObject()) @@ -668,12 +668,12 @@ bool ScriptInterface::SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleV return true; } -bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) +bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const { return GetProperty_(obj, name, out); } -bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out) +bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out) const { JSContext* cx = GetContext(); JSAutoRequest rq(cx); @@ -690,12 +690,12 @@ bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, JS::Mut return true; } -bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) +bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) const { return GetPropertyInt_(obj, name, out); } -bool ScriptInterface::GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) +bool ScriptInterface::GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const { JSAutoRequest rq(m->m_cx); if (!obj.isObject()) @@ -707,7 +707,7 @@ bool ScriptInterface::GetProperty_(JS::HandleValue obj, const char* name, JS::Mu return true; } -bool ScriptInterface::GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue out) +bool ScriptInterface::GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue out) const { JSAutoRequest rq(m->m_cx); JS::RootedId nameId(m->m_cx, INT_TO_JSID(name)); @@ -720,7 +720,7 @@ bool ScriptInterface::GetPropertyInt_(JS::HandleValue obj, int name, JS::Mutable return true; } -bool ScriptInterface::HasProperty(JS::HandleValue obj, const char* name) +bool ScriptInterface::HasProperty(JS::HandleValue obj, const char* name) const { // TODO: proper errorhandling JSAutoRequest rq(m->m_cx); @@ -734,7 +734,7 @@ bool ScriptInterface::HasProperty(JS::HandleValue obj, const char* name) return found; } -bool ScriptInterface::EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector& out) +bool ScriptInterface::EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector& out) const { JSAutoRequest rq(m->m_cx); @@ -811,7 +811,7 @@ bool ScriptInterface::SetPrototype(JS::HandleValue objVal, JS::HandleValue proto return JS_SetPrototype(m->m_cx, obj, proto); } -bool ScriptInterface::FreezeObject(JS::HandleValue objVal, bool deep) +bool ScriptInterface::FreezeObject(JS::HandleValue objVal, bool deep) const { JSAutoRequest rq(m->m_cx); if (!objVal.isObject()) @@ -825,9 +825,8 @@ bool ScriptInterface::FreezeObject(JS::HandleValue objVal, bool deep) return JS_FreezeObject(m->m_cx, obj); } -bool ScriptInterface::LoadScript(const VfsPath& filename, const std::string& code) +bool ScriptInterface::LoadScript(const VfsPath& filename, const std::string& code) const { - JSAutoRequest rq(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); utf16string codeUtf16(code.begin(), code.end()); @@ -855,7 +854,7 @@ shared_ptr ScriptInterface::CreateRuntime(shared_ptr(new ScriptRuntime(parentRuntime, runtimeSize, heapGrowthBytesGCTrigger)); } -bool ScriptInterface::LoadGlobalScript(const VfsPath& filename, const std::wstring& code) +bool ScriptInterface::LoadGlobalScript(const VfsPath& filename, const std::wstring& code) const { JSAutoRequest rq(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); @@ -872,7 +871,7 @@ bool ScriptInterface::LoadGlobalScript(const VfsPath& filename, const std::wstri reinterpret_cast(codeUtf16.c_str()), (uint)(codeUtf16.length()), &rval); } -bool ScriptInterface::LoadGlobalScriptFile(const VfsPath& path) +bool ScriptInterface::LoadGlobalScriptFile(const VfsPath& path) const { JSAutoRequest rq(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); @@ -907,14 +906,14 @@ bool ScriptInterface::LoadGlobalScriptFile(const VfsPath& path) reinterpret_cast(codeUtf16.c_str()), (uint)(codeUtf16.length()), &rval); } -bool ScriptInterface::Eval(const char* code) +bool ScriptInterface::Eval(const char* code) const { JSAutoRequest rq(m->m_cx); JS::RootedValue rval(m->m_cx); return Eval_(code, &rval); } -bool ScriptInterface::Eval_(const char* code, JS::MutableHandleValue rval) +bool ScriptInterface::Eval_(const char* code, JS::MutableHandleValue rval) const { JSAutoRequest rq(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); @@ -925,7 +924,7 @@ bool ScriptInterface::Eval_(const char* code, JS::MutableHandleValue rval) return JS::Evaluate(m->m_cx, global, opts, reinterpret_cast(codeUtf16.c_str()), (uint)codeUtf16.length(), rval); } -bool ScriptInterface::Eval_(const wchar_t* code, JS::MutableHandleValue rval) +bool ScriptInterface::Eval_(const wchar_t* code, JS::MutableHandleValue rval) const { JSAutoRequest rq(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); @@ -936,7 +935,7 @@ bool ScriptInterface::Eval_(const wchar_t* code, JS::MutableHandleValue rval) return JS::Evaluate(m->m_cx, global, opts, reinterpret_cast(codeUtf16.c_str()), (uint)codeUtf16.length(), rval); } -bool ScriptInterface::ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) +bool ScriptInterface::ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const { JSAutoRequest rq(m->m_cx); std::wstring attrsW = wstring_from_utf8(string_utf8); @@ -968,7 +967,7 @@ bool ScriptInterface::ParseJSON(const std::string& string_utf8, JS::MutableHandl return false; } -void ScriptInterface::ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) +void ScriptInterface::ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const { if (!VfsFileExists(path)) { @@ -1009,7 +1008,7 @@ struct Stringifier // TODO: It's not quite clear why JS_Stringify needs JS::MutableHandleValue. |obj| should not get modified. // It probably has historical reasons and could be changed by SpiderMonkey in the future. -std::string ScriptInterface::StringifyJSON(JS::MutableHandleValue obj, bool indent) +std::string ScriptInterface::StringifyJSON(JS::MutableHandleValue obj, bool indent) const { JSAutoRequest rq(m->m_cx); Stringifier str; @@ -1025,7 +1024,7 @@ std::string ScriptInterface::StringifyJSON(JS::MutableHandleValue obj, bool inde } -std::string ScriptInterface::ToString(JS::MutableHandleValue obj, bool pretty) +std::string ScriptInterface::ToString(JS::MutableHandleValue obj, bool pretty) const { JSAutoRequest rq(m->m_cx); @@ -1062,7 +1061,7 @@ std::string ScriptInterface::ToString(JS::MutableHandleValue obj, bool pretty) return utf8_from_wstring(source); } -void ScriptInterface::ReportError(const char* msg) +void ScriptInterface::ReportError(const char* msg) const { JSAutoRequest rq(m->m_cx); // JS_ReportError by itself doesn't seem to set a JS-style exception, and so @@ -1136,7 +1135,7 @@ shared_ptr ScriptInterface::WriteStructuredClo return shared_ptr(); } - shared_ptr ret (new StructuredClone); + shared_ptr ret(new StructuredClone); ret->m_Data = data; ret->m_Size = nbytes; return ret; diff --git a/source/scriptinterface/ScriptInterface.h b/source/scriptinterface/ScriptInterface.h index 44b023fa6b..eeb055c75d 100644 --- a/source/scriptinterface/ScriptInterface.h +++ b/source/scriptinterface/ScriptInterface.h @@ -116,7 +116,7 @@ public: bool LoadGlobalScripts(); enum CACHED_VAL { CACHE_VECTOR2DPROTO, CACHE_VECTOR3DPROTO }; - JS::Value GetCachedValue(CACHED_VAL valueIdentifier); + JS::Value GetCachedValue(CACHED_VAL valueIdentifier) const; /** * Replace the default JS random number geenrator with a seeded, network-sync'd one. @@ -129,7 +129,7 @@ public: * @param argv Constructor arguments * @param out The new object; On error an error message gets logged and out is Null (out.isNull() == true). */ - void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out); + void CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out) const; JSObject* CreateCustomObject(const std::string & typeName) const; void DefineCustomObjectType(JSClass *clasp, JSNative constructor, uint minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs); @@ -171,40 +171,40 @@ public: * Get the named property on the given object. */ template - bool GetProperty(JS::HandleValue obj, const char* name, T& out); + bool GetProperty(JS::HandleValue obj, const char* name, T& out) const; /** * Get the named property of the given object. */ - bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out); - bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out); + bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const; + bool GetProperty(JS::HandleValue obj, const char* name, JS::MutableHandleObject out) const; /** * Get the integer-named property on the given object. */ template - bool GetPropertyInt(JS::HandleValue obj, int name, T& out); + bool GetPropertyInt(JS::HandleValue obj, int name, T& out) const; /** * Get the named property of the given object. */ - bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out); + bool GetPropertyInt(JS::HandleValue obj, int name, JS::MutableHandleValue out) const; /** * Check the named property has been defined on the given object. */ - bool HasProperty(JS::HandleValue obj, const char* name); + bool HasProperty(JS::HandleValue obj, const char* name) const; - bool EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector& out); + bool EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector& out) const; bool SetPrototype(JS::HandleValue obj, JS::HandleValue proto); - bool FreezeObject(JS::HandleValue objVal, bool deep); + bool FreezeObject(JS::HandleValue objVal, bool deep) const; - bool Eval(const char* code); + bool Eval(const char* code) const; - template bool Eval(const CHAR* code, JS::MutableHandleValue out); - template bool Eval(const CHAR* code, T& out); + template bool Eval(const CHAR* code, JS::MutableHandleValue out) const; + template bool Eval(const CHAR* code, T& out) const; /** * Convert an object to a UTF-8 encoded string, either with JSON @@ -212,31 +212,31 @@ public: * * We have to use a mutable handle because JS_Stringify requires that for unknown reasons. */ - std::string ToString(JS::MutableHandleValue obj, bool pretty = false); + std::string ToString(JS::MutableHandleValue obj, bool pretty = false) const; /** * Parse a UTF-8-encoded JSON string. Returns the unmodified value on error * and prints an error message. * @return true on success; false otherwise */ - bool ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out); + bool ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const; /** * Read a JSON file. Returns the unmodified value on error and prints an error message. */ - void ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out); + void ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const; /** * Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error. */ - std::string StringifyJSON(JS::MutableHandleValue obj, bool indent = true); + std::string StringifyJSON(JS::MutableHandleValue obj, bool indent = true) const; /** * Report the given error message through the JS error reporting mechanism, * and throw a JS exception. (Callers can check IsPendingException, and must * return false in that case to propagate the exception.) */ - void ReportError(const char* msg); + void ReportError(const char* msg) const; /** * Load and execute the given script in a new function scope. @@ -244,7 +244,7 @@ public: * @param code JS code to execute * @return true on successful compilation and execution; false otherwise */ - bool LoadScript(const VfsPath& filename, const std::string& code); + bool LoadScript(const VfsPath& filename, const std::string& code) const; /** * Load and execute the given script in the global scope. @@ -252,13 +252,13 @@ public: * @param code JS code to execute * @return true on successful compilation and execution; false otherwise */ - bool LoadGlobalScript(const VfsPath& filename, const std::wstring& code); + bool LoadGlobalScript(const VfsPath& filename, const std::wstring& code) const; /** * Load and execute the given script in the global scope. * @return true on successful compilation and execution; false otherwise */ - bool LoadGlobalScriptFile(const VfsPath& path); + bool LoadGlobalScriptFile(const VfsPath& path) const; /** * Construct a new value (usable in this ScriptInterface's context) by cloning @@ -357,14 +357,14 @@ public: private: bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const; - bool Eval_(const char* code, JS::MutableHandleValue ret); - bool Eval_(const wchar_t* code, JS::MutableHandleValue ret); + bool Eval_(const char* code, JS::MutableHandleValue ret) const; + bool Eval_(const wchar_t* code, JS::MutableHandleValue ret) const; bool SetGlobal_(const char* name, JS::HandleValue value, bool replace); bool SetProperty_(JS::HandleValue obj, const char* name, JS::HandleValue value, bool readonly, bool enumerate); bool SetProperty_(JS::HandleValue obj, const wchar_t* name, JS::HandleValue value, bool readonly, bool enumerate); bool SetPropertyInt_(JS::HandleValue obj, int name, JS::HandleValue value, bool readonly, bool enumerate); - bool GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out); - bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value); + bool GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out) const; + bool GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue value) const; static bool IsExceptionPending(JSContext* cx); static const JSClass* GetClass(JS::HandleObject obj); static void* GetPrivate(JS::HandleObject obj); @@ -521,40 +521,40 @@ bool ScriptInterface::SetPropertyInt(JS::HandleValue obj, int name, const T& val } template -bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, T& out) +bool ScriptInterface::GetProperty(JS::HandleValue obj, const char* name, T& out) const { JSContext* cx = GetContext(); JSAutoRequest rq(cx); JS::RootedValue val(cx); - if (! GetProperty_(obj, name, &val)) + if (!GetProperty_(obj, name, &val)) return false; return FromJSVal(cx, val, out); } template -bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, T& out) +bool ScriptInterface::GetPropertyInt(JS::HandleValue obj, int name, T& out) const { JSAutoRequest rq(GetContext()); JS::RootedValue val(GetContext()); - if (! GetPropertyInt_(obj, name, &val)) + if (!GetPropertyInt_(obj, name, &val)) return false; return FromJSVal(GetContext(), val, out); } template -bool ScriptInterface::Eval(const CHAR* code, JS::MutableHandleValue ret) +bool ScriptInterface::Eval(const CHAR* code, JS::MutableHandleValue ret) const { - if (! Eval_(code, ret)) + if (!Eval_(code, ret)) return false; return true; } template -bool ScriptInterface::Eval(const CHAR* code, T& ret) +bool ScriptInterface::Eval(const CHAR* code, T& ret) const { JSAutoRequest rq(GetContext()); JS::RootedValue rval(GetContext()); - if (! Eval_(code, &rval)) + if (!Eval_(code, &rval)) return false; return FromJSVal(GetContext(), rval, ret); }