evaluate entity scripts in a sandbox before constructing

This commit is contained in:
ZappoMan 2014-12-31 09:58:18 -08:00
parent ec1ef39a7f
commit eb28efa648
2 changed files with 11 additions and 2 deletions

View file

@ -39,6 +39,7 @@ EntityTreeRenderer::EntityTreeRenderer(bool wantScripts, AbstractViewStateInterf
OctreeRenderer(), OctreeRenderer(),
_wantScripts(wantScripts), _wantScripts(wantScripts),
_entitiesScriptEngine(NULL), _entitiesScriptEngine(NULL),
_sandboxScriptEngine(NULL),
_lastMouseEventValid(false), _lastMouseEventValid(false),
_viewState(viewState), _viewState(viewState),
_scriptingServices(scriptingServices), _scriptingServices(scriptingServices),
@ -58,7 +59,10 @@ EntityTreeRenderer::EntityTreeRenderer(bool wantScripts, AbstractViewStateInterf
} }
EntityTreeRenderer::~EntityTreeRenderer() { EntityTreeRenderer::~EntityTreeRenderer() {
// do we need to delete the _entitiesScriptEngine?? or is it deleted by default // NOTE: we don't need to delete _entitiesScriptEngine because it's owned by the application and gets cleaned up
// automatically but we do need to delete our sandbox script engine.
delete _sandboxScriptEngine;
_sandboxScriptEngine = NULL;
} }
void EntityTreeRenderer::clear() { void EntityTreeRenderer::clear() {
@ -79,6 +83,8 @@ void EntityTreeRenderer::init() {
_entitiesScriptEngine = new ScriptEngine(NO_SCRIPT, "Entities", _entitiesScriptEngine = new ScriptEngine(NO_SCRIPT, "Entities",
_scriptingServices->getControllerScriptingInterface()); _scriptingServices->getControllerScriptingInterface());
_scriptingServices->registerScriptEngineWithApplicationServices(_entitiesScriptEngine); _scriptingServices->registerScriptEngineWithApplicationServices(_entitiesScriptEngine);
_sandboxScriptEngine = new ScriptEngine(NO_SCRIPT, "Entities Sandbox", NULL);
} }
// make sure our "last avatar position" is something other than our current position, so that on our // make sure our "last avatar position" is something other than our current position, so that on our
@ -190,13 +196,15 @@ QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) {
if (isURL) { if (isURL) {
_entitiesScriptEngine->setParentURL(entity->getScript()); _entitiesScriptEngine->setParentURL(entity->getScript());
} }
QScriptValue entityScriptConstructor = _entitiesScriptEngine->evaluate(scriptContents); QScriptValue entityScriptConstructor = _sandboxScriptEngine->evaluate(scriptContents);
if (!entityScriptConstructor.isFunction()) { if (!entityScriptConstructor.isFunction()) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID; qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " NOT CONSTRUCTOR"; qDebug() << " NOT CONSTRUCTOR";
qDebug() << " SCRIPT:" << entityScript; qDebug() << " SCRIPT:" << entityScript;
return QScriptValue(); // invalid script return QScriptValue(); // invalid script
} else {
entityScriptConstructor = _entitiesScriptEngine->evaluate(scriptContents);
} }
QScriptValue entityScriptObject = entityScriptConstructor.construct(); QScriptValue entityScriptObject = entityScriptConstructor.construct();

View file

@ -135,6 +135,7 @@ private:
bool _wantScripts; bool _wantScripts;
ScriptEngine* _entitiesScriptEngine; ScriptEngine* _entitiesScriptEngine;
ScriptEngine* _sandboxScriptEngine;
QScriptValue loadEntityScript(EntityItem* entity); QScriptValue loadEntityScript(EntityItem* entity);
QScriptValue loadEntityScript(const EntityItemID& entityItemID); QScriptValue loadEntityScript(const EntityItemID& entityItemID);