mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-10-14 06:58:07 +00:00
Replace JS_Enumerate with manual enumeration, to avoid its memory allocations.
Use LookupProperty to avoid having to check for getters. Add a quicker method of rooting many script values. This was SVN commit r7579.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/* Copyright (C) 2010 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* 0 A.D. is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "precompiled.h"
|
||||
|
||||
#include "AutoRooters.h"
|
||||
|
||||
#include "scriptinterface/ScriptInterface.h"
|
||||
|
||||
AutoGCRooter::AutoGCRooter(ScriptInterface& scriptInterface)
|
||||
: m_ScriptInterface(scriptInterface)
|
||||
{
|
||||
m_Previous = m_ScriptInterface.ReplaceAutoGCRooter(this);
|
||||
}
|
||||
|
||||
AutoGCRooter::~AutoGCRooter()
|
||||
{
|
||||
AutoGCRooter* r = m_ScriptInterface.ReplaceAutoGCRooter(m_Previous);
|
||||
debug_assert(r == this); // must be correctly nested
|
||||
}
|
||||
|
||||
void AutoGCRooter::Trace(JSTracer* trc)
|
||||
{
|
||||
if (m_Previous)
|
||||
m_Previous->Trace(trc);
|
||||
|
||||
for (size_t i = 0; i < m_Objects.size(); ++i)
|
||||
{
|
||||
JS_CALL_OBJECT_TRACER(trc, m_Objects[i], "AutoGCRooter object");
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,13 @@
|
||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef INCLUDED_AUTOROOTERS
|
||||
#define INCLUDED_AUTOROOTERS
|
||||
|
||||
#include "js/jsapi.h"
|
||||
|
||||
class ScriptInterface;
|
||||
|
||||
// Exception-safety and GC-safety wrapper for JSIdArray
|
||||
// (TODO: it'd be nicer to use the existing js::AutoIdArray but currently that
|
||||
// requires a load of semi-private SpiderMonkey headers that cause a load of compile warnings)
|
||||
@@ -41,3 +46,27 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper for rooting large groups of script values.
|
||||
* Construct this object, push values into it, and they will all be rooted until this
|
||||
* object is destroyed.
|
||||
* Many of these objects can be used at once, but their lifetimes must be correctly nested.
|
||||
*/
|
||||
class AutoGCRooter
|
||||
{
|
||||
public:
|
||||
AutoGCRooter(ScriptInterface& scriptInterface);
|
||||
~AutoGCRooter();
|
||||
|
||||
void Push(JSObject* obj) { m_Objects.push_back(obj); }
|
||||
|
||||
void Trace(JSTracer* trc);
|
||||
private:
|
||||
ScriptInterface& m_ScriptInterface;
|
||||
AutoGCRooter* m_Previous;
|
||||
|
||||
std::vector<JSObject*> m_Objects;
|
||||
// TODO: add vectors of other value types
|
||||
};
|
||||
|
||||
#endif // INCLUDED_AUTOROOTERS
|
||||
|
||||
@@ -59,6 +59,8 @@ struct ScriptInterface_impl
|
||||
JSContext* m_cx;
|
||||
JSObject* m_glob; // global scope object
|
||||
JSObject* m_nativeScope; // native function scope object
|
||||
|
||||
AutoGCRooter* m_rooter;
|
||||
};
|
||||
|
||||
namespace
|
||||
@@ -163,7 +165,16 @@ static void* jshook_function(JSContext* cx, JSStackFrame* fp, JSBool before, JSB
|
||||
}
|
||||
#endif
|
||||
|
||||
ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, JSContext* cx)
|
||||
void jshook_trace(JSTracer* trc, void* data)
|
||||
{
|
||||
ScriptInterface_impl* m = static_cast<ScriptInterface_impl*>(data);
|
||||
|
||||
if (m->m_rooter)
|
||||
m->m_rooter->Trace(trc);
|
||||
}
|
||||
|
||||
ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, JSContext* cx) :
|
||||
m_rooter(NULL)
|
||||
{
|
||||
JSBool ok;
|
||||
|
||||
@@ -201,6 +212,8 @@ ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, JSContex
|
||||
| JSOPTION_VAROBJFIX // "recommended" (fixes variable scoping)
|
||||
);
|
||||
|
||||
JS_SetExtraGCRoots(m_rt, jshook_trace, this);
|
||||
|
||||
// Threadsafe SpiderMonkey requires that we have a request before doing anything much
|
||||
JS_BeginRequest(m_cx);
|
||||
|
||||
@@ -296,6 +309,14 @@ bool ScriptInterface::RemoveRoot(void* ptr)
|
||||
return JS_RemoveRoot(m->m_cx, ptr) ? true : false;
|
||||
}
|
||||
|
||||
AutoGCRooter* ScriptInterface::ReplaceAutoGCRooter(AutoGCRooter* rooter)
|
||||
{
|
||||
debug_assert(m->m_rt); // this class must own the runtime, else the rooter won't work
|
||||
AutoGCRooter* ret = m->m_rooter;
|
||||
m->m_rooter = rooter;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ScriptInterface::LocalRootScope::LocalRootScope(JSContext* cx) :
|
||||
m_cx(cx)
|
||||
{
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include "ScriptTypes.h"
|
||||
#include "ScriptVal.h"
|
||||
|
||||
class AutoGCRooter;
|
||||
|
||||
namespace boost { class rand48; }
|
||||
|
||||
// Set the maximum number of function arguments that can be handled
|
||||
@@ -37,7 +39,6 @@ namespace boost { class rand48; }
|
||||
#define SCRIPT_INTERFACE_MAX_ARGS 6
|
||||
|
||||
struct ScriptInterface_impl;
|
||||
class ScriptClass;
|
||||
class ScriptInterface
|
||||
{
|
||||
public:
|
||||
@@ -181,6 +182,8 @@ public:
|
||||
bool AddRoot(void* ptr, const char* name);
|
||||
bool RemoveRoot(void* ptr);
|
||||
|
||||
AutoGCRooter* ReplaceAutoGCRooter(AutoGCRooter* rooter);
|
||||
|
||||
/**
|
||||
* Dump some memory heap debugging information to stderr.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user