2010-06-30 23:23:41 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
#include "ScriptingHost.h"
|
|
|
|
#include "ScriptGlue.h"
|
2010-06-30 23:23:41 +02:00
|
|
|
#include "lib/utf8.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "ps/Profile.h"
|
2005-07-22 20:41:49 +02:00
|
|
|
#include "ps/CLogger.h"
|
2007-12-20 21:21:45 +01:00
|
|
|
#include "ps/Filesystem.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2004-12-14 18:58:37 +01:00
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
ScriptingHost::ScriptingHost()
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2011-01-16 00:35:20 +01:00
|
|
|
m_ScriptInterface = new ScriptInterface("Engine", "GUI", ScriptInterface::CreateRuntime());
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2010-07-20 01:01:58 +02:00
|
|
|
m_Context = m_ScriptInterface->GetContext();
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2010-07-20 01:01:58 +02:00
|
|
|
m_GlobalObject = JS_GetGlobalObject(m_Context);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2010-07-20 01:01:58 +02:00
|
|
|
if (!JS_DefineFunctions(m_Context, m_GlobalObject, ScriptFunctionTable))
|
2005-07-22 20:41:49 +02:00
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2010-07-20 01:01:58 +02:00
|
|
|
if (!JS_DefineProperties(m_Context, m_GlobalObject, ScriptGlobalTable))
|
2005-07-22 20:41:49 +02:00
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptingHost::~ScriptingHost()
|
|
|
|
{
|
2010-01-09 20:20:14 +01:00
|
|
|
delete m_ScriptInterface;
|
2007-06-03 17:37:26 +02:00
|
|
|
}
|
2004-07-18 20:42:45 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
ScriptInterface& ScriptingHost::GetScriptInterface()
|
|
|
|
{
|
|
|
|
return *m_ScriptInterface;
|
|
|
|
}
|
|
|
|
|
2007-06-03 17:37:26 +02:00
|
|
|
void ScriptingHost::FinalShutdown()
|
|
|
|
{
|
|
|
|
// This should only be called once per process, just to clean up before
|
|
|
|
// we report memory leaks. (Otherwise, if it's called while there are
|
|
|
|
// other contexts active in other threads, things will break.)
|
2004-07-18 20:42:45 +02:00
|
|
|
JS_ShutDown();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
// filename, line and globalObject default to 0 (in which case we execute
|
|
|
|
// the whole script / use our m_GlobalObject)
|
|
|
|
void ScriptingHost::RunMemScript(const char* script, size_t size, const char* filename, int line, JSObject* globalObject)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2005-08-09 17:55:44 +02:00
|
|
|
if(!filename)
|
|
|
|
filename = "unspecified file";
|
|
|
|
if(!globalObject)
|
|
|
|
globalObject = m_GlobalObject;
|
|
|
|
|
2005-08-09 23:26:40 +02:00
|
|
|
// Maybe TODO: support Unicode input formats?
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
jsval rval;
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool ok = JS_EvaluateScript(m_Context, globalObject, script, (uintN)size, filename, line, &rval);
|
2005-08-09 17:55:44 +02:00
|
|
|
|
|
|
|
if (ok == JS_FALSE)
|
|
|
|
throw PSERROR_Scripting_LoadFile_EvalErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
// globalObject defaults to 0 (in which case we use our m_GlobalObject).
|
2009-11-03 22:46:35 +01:00
|
|
|
void ScriptingHost::RunScript(const VfsPath& pathname, JSObject* globalObject)
|
2005-08-09 17:55:44 +02:00
|
|
|
{
|
2010-06-30 23:23:41 +02:00
|
|
|
if(!globalObject)
|
|
|
|
globalObject = m_GlobalObject;
|
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
shared_ptr<u8> buf; size_t size;
|
2009-11-03 22:46:35 +01:00
|
|
|
if(g_VFS->LoadFile(pathname, buf, size) != INFO::OK) // ERRTODO: translate/pass it on
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_LoadFile_OpenFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2010-06-30 23:23:41 +02:00
|
|
|
std::wstring scriptw = wstring_from_utf8(std::string(buf.get(), buf.get() + size));
|
|
|
|
utf16string script(scriptw.begin(), scriptw.end());
|
|
|
|
|
|
|
|
jsval rval;
|
2010-11-17 00:00:52 +01:00
|
|
|
JSBool ok = JS_EvaluateUCScript(m_Context, globalObject,
|
|
|
|
reinterpret_cast<const jschar*>(script.c_str()), (uintN)script.size(),
|
|
|
|
CStr(pathname.string()).c_str(), 1, &rval);
|
2010-06-30 23:23:41 +02:00
|
|
|
|
|
|
|
if (ok == JS_FALSE)
|
|
|
|
throw PSERROR_Scripting_LoadFile_EvalErrors();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2004-09-03 16:10:48 +02:00
|
|
|
jsval ScriptingHost::ExecuteScript(const CStrW& script, const CStrW& calledFrom, JSObject* contextObject )
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
jsval rval;
|
|
|
|
|
2007-02-01 15:46:14 +01:00
|
|
|
JSBool ok = JS_EvaluateUCScript(m_Context, contextObject ? contextObject : m_GlobalObject,
|
2010-11-17 00:00:52 +01:00
|
|
|
reinterpret_cast<const jschar*>(script.utf16().c_str()), (int)script.length(),
|
|
|
|
CStr(calledFrom), 1, &rval);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-07-11 14:37:52 +02:00
|
|
|
if (!ok) return JSVAL_NULL;
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingHost::DefineCustomObjectType(JSClass *clasp, JSNative constructor, uintN minArgs, JSPropertySpec *ps, JSFunctionSpec *fs, JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
|
|
|
|
{
|
|
|
|
std::string typeName = clasp->name;
|
|
|
|
|
|
|
|
if (m_CustomObjectTypes.find(typeName) != m_CustomObjectTypes.end())
|
|
|
|
{
|
|
|
|
// This type already exists
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_DefineType_AlreadyExists();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject * obj = JS_InitClass( m_Context, m_GlobalObject, 0,
|
|
|
|
clasp,
|
|
|
|
constructor, minArgs, // Constructor, min args
|
|
|
|
ps, fs, // Properties, methods
|
|
|
|
static_ps, static_fs); // Constructor properties, methods
|
|
|
|
|
2006-09-26 03:44:20 +02:00
|
|
|
if (obj == NULL)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_DefineType_CreationFailed();
|
2006-09-26 03:44:20 +02:00
|
|
|
|
|
|
|
CustomType type;
|
|
|
|
|
|
|
|
type.m_Object = obj;
|
|
|
|
type.m_Class = clasp;
|
|
|
|
|
|
|
|
m_CustomObjectTypes[typeName] = type;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject * ScriptingHost::CreateCustomObject(const std::string & typeName)
|
|
|
|
{
|
|
|
|
std::map < std::string, CustomType > ::iterator it = m_CustomObjectTypes.find(typeName);
|
|
|
|
|
|
|
|
if (it == m_CustomObjectTypes.end())
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_TypeDoesNotExist();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-07-04 17:35:04 +02:00
|
|
|
return JS_ConstructObject(m_Context, (*it).second.m_Class, (*it).second.m_Object, NULL);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-09-03 16:10:48 +02:00
|
|
|
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
void ScriptingHost::SetObjectProperty(JSObject * object, const std::string & propertyName, jsval value)
|
|
|
|
{
|
|
|
|
JS_SetProperty(m_Context, object, propertyName.c_str(), &value);
|
|
|
|
}
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
jsval ScriptingHost::GetObjectProperty( JSObject* object, const std::string& propertyName )
|
|
|
|
{
|
|
|
|
jsval vp;
|
|
|
|
JS_GetProperty( m_Context, object, propertyName.c_str(), &vp );
|
|
|
|
return( vp );
|
|
|
|
}
|
|
|
|
|
2004-09-03 16:10:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ScriptingHost::SetObjectProperty_Double(JSObject* object, const char* propertyName, double value)
|
|
|
|
{
|
2010-11-17 00:00:52 +01:00
|
|
|
jsval v;
|
|
|
|
if (! JS_NewNumberValue(m_Context, value, &v))
|
2004-09-03 16:10:48 +02:00
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
|
|
|
|
|
|
|
if (! JS_SetProperty(m_Context, object, propertyName, &v))
|
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
|
|
|
}
|
|
|
|
|
|
|
|
double ScriptingHost::GetObjectProperty_Double(JSObject* object, const char* propertyName)
|
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
if (! JS_GetProperty(m_Context, object, propertyName, &v))
|
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
|
|
|
if (! JS_ValueToNumber(m_Context, v, &d))
|
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-07-04 17:35:04 +02:00
|
|
|
void ScriptingHost::SetGlobal(const std::string &globalName, jsval value)
|
|
|
|
{
|
|
|
|
JS_SetProperty(m_Context, m_GlobalObject, globalName.c_str(), &value);
|
|
|
|
}
|
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// conversions
|
|
|
|
//----------------------------------------------------------------------------
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
CStrW ScriptingHost::ValueToUCString( const jsval value )
|
2004-11-07 22:30:47 +01:00
|
|
|
{
|
|
|
|
JSString* string = JS_ValueToString(m_Context, value);
|
|
|
|
if (string == NULL)
|
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
|
|
|
|
|
|
|
jschar *strptr=JS_GetStringChars(string);
|
|
|
|
size_t length=JS_GetStringLength(string);
|
2010-01-09 20:20:14 +01:00
|
|
|
return std::wstring(strptr, strptr+length);
|
2004-11-07 22:30:47 +01:00
|
|
|
}
|