1
0
forked from 0ad/0ad

Enable latest JS language features.

Load GUI scripts as UTF-8.

This was SVN commit r7650.
This commit is contained in:
Ykkrosh 2010-06-30 21:23:41 +00:00
parent e88391e5d8
commit c5d204c7ff
3 changed files with 46 additions and 4 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* 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
@ -21,6 +21,7 @@
#include "ScriptingHost.h"
#include "ScriptGlue.h"
#include "lib/utf8.h"
#include "ps/Profile.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
@ -58,6 +59,8 @@ ScriptingHost::ScriptingHost()
JS_SetErrorReporter(m_Context, ScriptingHost::ErrorReporter);
JS_SetVersion(m_Context, JSVERSION_LATEST);
JS_BeginRequest(m_Context);
m_GlobalObject = JS_NewObject(m_Context, &GlobalClass, NULL, NULL);
@ -135,13 +138,21 @@ void ScriptingHost::RunMemScript(const char* script, size_t size, const char* fi
// globalObject defaults to 0 (in which case we use our m_GlobalObject).
void ScriptingHost::RunScript(const VfsPath& pathname, JSObject* globalObject)
{
if(!globalObject)
globalObject = m_GlobalObject;
shared_ptr<u8> buf; size_t size;
if(g_VFS->LoadFile(pathname, buf, size) != INFO::OK) // ERRTODO: translate/pass it on
throw PSERROR_Scripting_LoadFile_OpenFailed();
const char* script = (const char*)buf.get();
CStr pathname_c(pathname.string());
RunMemScript(script, size, pathname_c.c_str(), 1, globalObject);
std::wstring scriptw = wstring_from_utf8(std::string(buf.get(), buf.get() + size));
utf16string script(scriptw.begin(), scriptw.end());
jsval rval;
JSBool ok = JS_EvaluateUCScript(m_Context, globalObject, script.c_str(), (uintN)script.size(), CStr(pathname.string()).c_str(), 1, &rval);
if (ok == JS_FALSE)
throw PSERROR_Scripting_LoadFile_EvalErrors();
}
jsval ScriptingHost::CallFunction(const std::string & functionName, jsval * params, int numParams)

View File

@ -233,6 +233,8 @@ ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, JSContex
| JSOPTION_VAROBJFIX // "recommended" (fixes variable scoping)
);
JS_SetVersion(m_cx, JSVERSION_LATEST);
JS_SetExtraGCRoots(m_rt, jshook_trace, this);
// Threadsafe SpiderMonkey requires that we have a request before doing anything much
@ -601,6 +603,15 @@ bool ScriptInterface::Eval_(const wchar_t* code, jsval& rval)
return ok ? true : false;
}
std::wstring ScriptInterface::ToString(jsval obj)
{
if (JSVAL_IS_VOID(obj))
return L"(void 0)";
std::wstring source = L"(error)";
CallFunction(obj, "toSource", source);
return source;
}
void ScriptInterface::ReportError(const char* msg)
{
// JS_ReportError by itself doesn't seem to set a JS-style exception, and so

View File

@ -91,6 +91,12 @@ public:
template<typename T0, typename T1>
bool CallFunctionVoid(jsval val, const char* name, const T0& a0, const T1& a1);
/**
* Call the named property on the given object, with void return type and 3 arguments
*/
template<typename T0, typename T1, typename T2>
bool CallFunctionVoid(jsval val, const char* name, const T0& a0, const T1& a1, const T2& a2);
/**
* Call the named property on the given object, with return type R and 0 arguments
*/
@ -145,6 +151,8 @@ public:
template<typename T, typename CHAR> bool Eval(const CHAR* code, T& out);
std::wstring ToString(jsval obj);
/**
* Report the given error message through the JS error reporting mechanism,
* and throw a JS exception. (Callers can check IsPendingException, and must
@ -275,6 +283,18 @@ bool ScriptInterface::CallFunctionVoid(jsval val, const char* name, const T0& a0
return CallFunction_(val, name, argv, jsRet);
}
template<typename T0, typename T1, typename T2>
bool ScriptInterface::CallFunctionVoid(jsval val, const char* name, const T0& a0, const T1& a1, const T2& a2)
{
LOCAL_ROOT_SCOPE;
jsval jsRet;
std::vector<jsval> argv;
argv.push_back(ToJSVal(GetContext(), a0));
argv.push_back(ToJSVal(GetContext(), a1));
argv.push_back(ToJSVal(GetContext(), a2));
return CallFunction_(val, name, argv, jsRet);
}
template<typename T0, typename R>
bool ScriptInterface::CallFunction(jsval val, const char* name, const T0& a0, R& ret)
{