2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 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/>.
|
|
|
|
*/
|
|
|
|
|
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"
|
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"
|
2004-12-14 18:58:37 +01:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-11-06 18:58:47 +01:00
|
|
|
#if OS_WIN
|
2004-07-07 12:52:45 +02:00
|
|
|
#ifdef NDEBUG
|
|
|
|
# pragma comment (lib, "js32.lib")
|
|
|
|
#else
|
|
|
|
# pragma comment (lib, "js32d.lib")
|
|
|
|
#endif
|
2005-11-06 18:58:47 +01:00
|
|
|
#endif
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
#define LOG_CATEGORY L"scriptinghost"
|
2005-07-22 20:41:49 +02:00
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
const int RUNTIME_MEMORY_ALLOWANCE = 16 * 1024 * 1024;
|
|
|
|
const int STACK_CHUNK_SIZE = 16 * 1024;
|
|
|
|
|
|
|
|
JSClass GlobalClass =
|
|
|
|
{
|
|
|
|
"global", 0,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
|
|
|
JS_ConvertStub, JS_FinalizeStub
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
ScriptingHost::ScriptingHost()
|
|
|
|
: m_RunTime(NULL), m_Context(NULL), m_GlobalObject(NULL)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
m_RunTime = JS_NewRuntime(RUNTIME_MEMORY_ALLOWANCE);
|
2005-07-22 20:41:49 +02:00
|
|
|
if(!m_RunTime)
|
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
m_Context = JS_NewContext(m_RunTime, STACK_CHUNK_SIZE);
|
2005-07-22 20:41:49 +02:00
|
|
|
if(!m_Context)
|
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
JS_SetErrorReporter(m_Context, ScriptingHost::ErrorReporter);
|
|
|
|
|
|
|
|
m_GlobalObject = JS_NewObject(m_Context, &GlobalClass, NULL, NULL);
|
2005-07-22 20:41:49 +02:00
|
|
|
if(!m_GlobalObject)
|
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-03-30 18:14:19 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Register our script and function handlers - note: docs say they don't like
|
|
|
|
// nulls passed as closures, nor should they return nulls.
|
|
|
|
JS_SetExecuteHook( m_RunTime, jshook_script, this );
|
|
|
|
JS_SetCallHook( m_RunTime, jshook_function, this );
|
|
|
|
#endif
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
if (JS_InitStandardClasses(m_Context, m_GlobalObject) == JSVAL_FALSE)
|
2005-07-22 20:41:49 +02:00
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
if (JS_DefineFunctions(m_Context, m_GlobalObject, ScriptFunctionTable) == JS_FALSE)
|
2005-07-22 20:41:49 +02:00
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
if( JS_DefineProperties( m_Context, m_GlobalObject, ScriptGlobalTable ) == JS_FALSE )
|
2005-07-22 20:41:49 +02:00
|
|
|
throw PSERROR_Scripting_SetupFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptingHost::~ScriptingHost()
|
|
|
|
{
|
|
|
|
if (m_Context != NULL)
|
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
JS_DestroyContext(m_Context);
|
2004-05-30 02:46:58 +02:00
|
|
|
m_Context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_RunTime != NULL)
|
|
|
|
{
|
|
|
|
JS_DestroyRuntime(m_RunTime);
|
|
|
|
m_RunTime = NULL;
|
|
|
|
}
|
2007-06-03 17:37:26 +02:00
|
|
|
}
|
2004-07-18 20:42:45 +02:00
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
2007-12-20 21:21:45 +01:00
|
|
|
const char* script = (const char*)buf.get();
|
2009-11-03 22:46:35 +01:00
|
|
|
CStr pathname_c(pathname.string());
|
|
|
|
RunMemScript(script, size, pathname_c.c_str(), 1, globalObject);
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jsval ScriptingHost::CallFunction(const std::string & functionName, jsval * params, int numParams)
|
|
|
|
{
|
|
|
|
jsval result;
|
|
|
|
|
|
|
|
JSBool ok = JS_CallFunctionName(m_Context, m_GlobalObject, functionName.c_str(), numParams, params, &result);
|
|
|
|
|
|
|
|
if (ok == JS_FALSE)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_CallFunctionFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
// unused
|
2004-05-30 02:46:58 +02:00
|
|
|
void ScriptingHost::RegisterFunction(const std::string & functionName, JSNative function, int numArgs)
|
|
|
|
{
|
|
|
|
JSFunction * func = JS_DefineFunction(m_Context, m_GlobalObject, functionName.c_str(), function, numArgs, 0);
|
|
|
|
|
|
|
|
if (func == NULL)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_RegisterFunctionFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingHost::DefineConstant(const std::string & name, int value)
|
|
|
|
{
|
|
|
|
// First remove this constant if it already exists
|
|
|
|
JS_DeleteProperty(m_Context, m_GlobalObject, name.c_str());
|
|
|
|
|
|
|
|
JSBool ok = JS_DefineProperty( m_Context, m_GlobalObject, name.c_str(), INT_TO_JSVAL(value),
|
|
|
|
NULL, NULL, JSPROP_READONLY);
|
|
|
|
|
|
|
|
if (ok == JS_FALSE)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_DefineConstantFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptingHost::DefineConstant(const std::string & name, double value)
|
|
|
|
{
|
|
|
|
// First remove this constant if it already exists
|
|
|
|
JS_DeleteProperty(m_Context, m_GlobalObject, name.c_str());
|
|
|
|
|
|
|
|
struct JSConstDoubleSpec spec[2];
|
|
|
|
|
|
|
|
spec[0].name = name.c_str();
|
|
|
|
spec[0].dval = value;
|
|
|
|
spec[0].flags = JSPROP_READONLY;
|
|
|
|
|
|
|
|
spec[1].name = 0;
|
|
|
|
spec[1].dval = 0.0;
|
|
|
|
spec[1].flags = 0;
|
|
|
|
|
|
|
|
JSBool ok = JS_DefineConstDoubles(m_Context, m_GlobalObject, spec);
|
|
|
|
|
|
|
|
if (ok == JS_FALSE)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_DefineConstantFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
jsdouble* d = JS_NewDouble(m_Context, value);
|
|
|
|
if (! d)
|
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
|
|
|
|
|
|
|
jsval v = DOUBLE_TO_JSVAL(d);
|
|
|
|
|
|
|
|
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
|
|
|
// unused
|
2004-07-04 17:35:04 +02:00
|
|
|
jsval ScriptingHost::GetGlobal(const std::string &globalName)
|
|
|
|
{
|
|
|
|
jsval vp;
|
|
|
|
JS_GetProperty(m_Context, m_GlobalObject, globalName.c_str(), &vp);
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// conversions
|
|
|
|
//----------------------------------------------------------------------------
|
2005-08-09 17:55:44 +02:00
|
|
|
/*
|
2005-08-09 23:26:40 +02:00
|
|
|
// These have been removed in favour of ToPrimitive<int>(value)
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
int ScriptingHost::ValueToInt(const jsval value)
|
|
|
|
{
|
2004-06-10 00:47:02 +02:00
|
|
|
int32 i = 0;
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-06-10 00:47:02 +02:00
|
|
|
JSBool ok = JS_ValueToInt32(m_Context, value, &i);
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-09-01 21:49:23 +02:00
|
|
|
if (!ok)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptingHost::ValueToBool(const jsval value)
|
|
|
|
{
|
|
|
|
JSBool b;
|
|
|
|
|
|
|
|
JSBool ok = JS_ValueToBoolean(m_Context, value, &b);
|
|
|
|
|
2004-09-01 21:49:23 +02:00
|
|
|
if (!ok)
|
2004-07-17 19:09:33 +02:00
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
return b == JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
|
|
|
|
double ScriptingHost::ValueToDouble(const jsval value)
|
|
|
|
{
|
2005-08-09 23:26:40 +02:00
|
|
|
jsdouble d;
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2005-08-09 23:26:40 +02:00
|
|
|
JSBool ok = JS_ValueToNumber(m_Context, value, &d);
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2006-07-26 16:04:52 +02:00
|
|
|
if (ok == JS_FALSE || !isfinite(d))
|
2005-08-09 23:26:40 +02:00
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2005-08-09 23:26:40 +02:00
|
|
|
return d;
|
2005-08-09 17:55:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2004-05-30 02:46:58 +02:00
|
|
|
std::string ScriptingHost::ValueToString(const jsval value)
|
|
|
|
{
|
2004-08-24 13:07:50 +02:00
|
|
|
JSString* string = JS_ValueToString(m_Context, value);
|
|
|
|
if (string == NULL)
|
|
|
|
throw PSERROR_Scripting_ConversionFailed();
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-09-01 21:49:23 +02:00
|
|
|
return std::string(JS_GetStringBytes(string), JS_GetStringLength(string));
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
CStrW ScriptingHost::ValueToUCString( const jsval value )
|
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
return CStrW(ValueToUTF16(value));
|
|
|
|
}
|
2004-08-24 13:07:50 +02:00
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
jsval ScriptingHost::UCStringToValue( const CStrW& str )
|
2005-02-21 18:13:31 +01:00
|
|
|
{
|
|
|
|
utf16string utf16=str.utf16();
|
|
|
|
return UTF16ToValue(utf16);
|
2004-07-20 21:30:35 +02:00
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
|
|
|
|
|
2004-11-07 22:30:47 +01:00
|
|
|
utf16string ScriptingHost::ValueToUTF16( const jsval value )
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
return utf16string(strptr, strptr+length);
|
|
|
|
}
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
jsval ScriptingHost::UTF16ToValue(const utf16string &str)
|
2004-08-16 17:19:17 +02:00
|
|
|
{
|
|
|
|
return STRING_TO_JSVAL(JS_NewUCStringCopyZ(m_Context, str.c_str()));
|
|
|
|
}
|
|
|
|
|
2005-07-22 20:41:49 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// called by SpiderMonkey whenever someone does JS_ReportError.
|
|
|
|
// prints that message as well as locus to log, debug output and console.
|
2009-11-03 22:46:35 +01:00
|
|
|
void ScriptingHost::ErrorReporter(JSContext* UNUSED(cx), const char* pmessage, JSErrorReport* report)
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
const CStrW file = report->filename? report->filename : "(current document)";
|
2005-07-22 20:41:49 +02:00
|
|
|
const int line = report->lineno;
|
2009-11-03 22:46:35 +01:00
|
|
|
const CStrW message = pmessage? pmessage : "No error message available";
|
2005-07-22 20:41:49 +02:00
|
|
|
// apparently there is no further information in this struct we can use
|
|
|
|
// because linebuf/tokenptr require a buffer to have been allocated.
|
|
|
|
// that doesn't look possible since we are a callback and there is
|
2009-11-04 16:29:28 +01:00
|
|
|
// no mention in the docs about where this would happen (typical).
|
2004-07-07 12:52:45 +02:00
|
|
|
|
2005-12-14 19:32:02 +01:00
|
|
|
// for developer convenience: write to output window so they can
|
2009-11-04 16:29:28 +01:00
|
|
|
// double-click on that line and be taken to the error locus.
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"%ls(%d): %ls\n", file.c_str(), line, message.c_str());
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2005-12-14 19:32:02 +01:00
|
|
|
// note: CLogger's LOG already takes care of writing to the console,
|
|
|
|
// so don't do that here.
|
2005-07-22 20:41:49 +02:00
|
|
|
|
2009-11-04 16:29:28 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"JavaScript Error (%ls, line %d): %ls", file.c_str(), line, message.c_str());
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
|
2005-03-30 18:14:19 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
void* ScriptingHost::jshook_script( JSContext* UNUSED(cx), JSStackFrame* UNUSED(fp),
|
|
|
|
JSBool before, JSBool* UNUSED(ok), void* closure )
|
2005-03-30 18:14:19 +02:00
|
|
|
{
|
|
|
|
if( before )
|
|
|
|
{
|
|
|
|
g_Profiler.StartScript( "script invocation" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_Profiler.Stop();
|
|
|
|
|
|
|
|
return( closure );
|
|
|
|
}
|
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
void* ScriptingHost::jshook_function( JSContext* cx, JSStackFrame* fp, JSBool before, JSBool* UNUSED(ok), void* closure )
|
2005-03-30 18:14:19 +02:00
|
|
|
{
|
|
|
|
JSFunction* fn = JS_GetFrameFunction( cx, fp );
|
|
|
|
if( before )
|
|
|
|
{
|
|
|
|
if( fn )
|
|
|
|
{
|
|
|
|
g_Profiler.StartScript( JS_GetFunctionName( fn ) );
|
|
|
|
}
|
|
|
|
else
|
2006-09-22 19:43:00 +02:00
|
|
|
g_Profiler.StartScript( "function invocation" );
|
2005-03-30 18:14:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
g_Profiler.Stop();
|
|
|
|
|
|
|
|
return( closure );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|