Index: ps/trunk/binaries/data/mods/_test.sim/globalscripts/test-global-helper.js =================================================================== --- ps/trunk/binaries/data/mods/_test.sim/globalscripts/test-global-helper.js +++ ps/trunk/binaries/data/mods/_test.sim/globalscripts/test-global-helper.js @@ -39,7 +39,3 @@ this.z += v.z; return this; }; - -// make the prototypes easily accessible to C++ -const Vector2Dprototype = Vector2D.prototype; -const Vector3Dprototype = Vector3D.prototype; Index: ps/trunk/binaries/data/mods/public/globalscripts/vector.js =================================================================== --- ps/trunk/binaries/data/mods/public/globalscripts/vector.js +++ ps/trunk/binaries/data/mods/public/globalscripts/vector.js @@ -429,8 +429,3 @@ { return new Vector3D(v.x / f, v.y / f, v.z / f); }; - - -// make the prototypes easily accessible to C++ -const Vector2Dprototype = Vector2D.prototype; -const Vector3Dprototype = Vector3D.prototype; Index: ps/trunk/source/scriptinterface/ScriptInterface.h =================================================================== --- ps/trunk/source/scriptinterface/ScriptInterface.h +++ ps/trunk/source/scriptinterface/ScriptInterface.h @@ -115,9 +115,6 @@ */ bool LoadGlobalScripts(); - enum CACHED_VAL { CACHE_VECTOR2DPROTO, CACHE_VECTOR3DPROTO }; - JS::Value GetCachedValue(CACHED_VAL valueIdentifier) const; - /** * Replace the default JS random number geenrator with a seeded, network-sync'd one. */ Index: ps/trunk/source/scriptinterface/ScriptInterface.cpp =================================================================== --- ps/trunk/source/scriptinterface/ScriptInterface.cpp +++ ps/trunk/source/scriptinterface/ScriptInterface.cpp @@ -67,9 +67,6 @@ JSCompartment* m_comp; boost::rand48* m_rng; JS::PersistentRootedObject m_nativeScope; // native function scope object - - typedef std::map ScriptValCache; - ScriptValCache m_ScriptValCache; }; namespace @@ -450,13 +447,6 @@ return pCxPrivate; } -JS::Value ScriptInterface::GetCachedValue(CACHED_VAL valueIdentifier) const -{ - std::map::const_iterator it = m->m_ScriptValCache.find(valueIdentifier); - ENSURE(it != m->m_ScriptValCache.end()); - return it->second.get(); -} - bool ScriptInterface::LoadGlobalScripts() { @@ -474,13 +464,6 @@ return false; } - JSAutoRequest rq(m->m_cx); - JS::RootedValue proto(m->m_cx); - JS::RootedObject global(m->m_cx, m->m_glob); - if (JS_GetProperty(m->m_cx, global, "Vector2Dprototype", &proto)) - m->m_ScriptValCache[CACHE_VECTOR2DPROTO].init(GetJSRuntime(), proto); - if (JS_GetProperty(m->m_cx, global, "Vector3Dprototype", &proto)) - m->m_ScriptValCache[CACHE_VECTOR3DPROTO].init(GetJSRuntime(), proto); return true; } Index: ps/trunk/source/simulation2/scripting/EngineScriptConversions.cpp =================================================================== --- ps/trunk/source/simulation2/scripting/EngineScriptConversions.cpp +++ ps/trunk/source/simulation2/scripting/EngineScriptConversions.cpp @@ -32,6 +32,7 @@ #include "simulation2/system/ParamNode.h" #define FAIL(msg) STMT(JS_ReportError(cx, msg); return false) +#define FAILVOID(msg) STMT(JS_ReportError(cx, msg)) template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, IComponent* const& val) { @@ -180,29 +181,19 @@ { JSAutoRequest rq(cx); - // apply the Vector3D prototype to the return value; ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); - JS::RootedObject proto(cx, &pCxPrivate->pScriptInterface->GetCachedValue(ScriptInterface::CACHE_VECTOR3DPROTO).toObject()); - JS::RootedObject obj(cx, JS_NewObjectWithGivenProto(cx, nullptr, proto)); + JS::RootedObject global(cx, &pCxPrivate->pScriptInterface->GetGlobalObject().toObject()); + JS::RootedValue valueVector3D(cx); + if (!JS_GetProperty(cx, global, "Vector3D", &valueVector3D)) + FAILVOID("Failed to get Vector3D constructor"); + + JS::AutoValueArray<3> args(cx); + args[0].setNumber(val.X.ToDouble()); + args[1].setNumber(val.Y.ToDouble()); + args[2].setNumber(val.Z.ToDouble()); - if (!obj) - { - ret.setUndefined(); - return; - } - - JS::RootedValue x(cx); - JS::RootedValue y(cx); - JS::RootedValue z(cx); - ToJSVal(cx, &x, val.X); - ToJSVal(cx, &y, val.Y); - ToJSVal(cx, &z, val.Z); - - JS_SetProperty(cx, obj, "x", x); - JS_SetProperty(cx, obj, "y", y); - JS_SetProperty(cx, obj, "z", z); - - ret.setObject(*obj); + if (!JS::Construct(cx, valueVector3D, args, ret)) + FAILVOID("Failed to construct Vector3D object"); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CFixedVector2D& out) @@ -227,25 +218,18 @@ { JSAutoRequest rq(cx); - // apply the Vector2D prototype to the return value ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); - JS::RootedObject proto(cx, &pCxPrivate->pScriptInterface->GetCachedValue(ScriptInterface::CACHE_VECTOR2DPROTO).toObject()); - JS::RootedObject obj(cx, JS_NewObjectWithGivenProto(cx, nullptr, proto)); - if (!obj) - { - ret.setUndefined(); - return; - } - - JS::RootedValue x(cx); - JS::RootedValue y(cx); - ToJSVal(cx, &x, val.X); - ToJSVal(cx, &y, val.Y); + JS::RootedObject global(cx, &pCxPrivate->pScriptInterface->GetGlobalObject().toObject()); + JS::RootedValue valueVector2D(cx); + if (!JS_GetProperty(cx, global, "Vector2D", &valueVector2D)) + FAILVOID("Failed to get Vector2D constructor"); + + JS::AutoValueArray<2> args(cx); + args[0].setNumber(val.X.ToDouble()); + args[1].setNumber(val.Y.ToDouble()); - JS_SetProperty(cx, obj, "x", x); - JS_SetProperty(cx, obj, "y", y); - - ret.setObject(*obj); + if (!JS::Construct(cx, valueVector2D, args, ret)) + FAILVOID("Failed to construct Vector2D object"); } template<> void ScriptInterface::ToJSVal >(JSContext* cx, JS::MutableHandleValue ret, const Grid& val)