mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-21 21:46:09 +00:00
Split off JSON-related function from ScriptInterface, clean up headers.
Follows 34b1920e7b.
JSON functions and ToString are movec to their own headers.
Also clean out a few PersistentRooted usage to use the 2-phase init to
clean up scriptInterface usage.
With these functions split off, we can finally clean out headers and
remove ScriptInterface.h from most of them, in favour of smaller and
more precise headers.
Take the opportunity to clarify some comments regarding Mutability.
Differential Revision: https://code.wildfiregames.com/D3961
This was SVN commit r25434.
This commit is contained in:
@@ -682,100 +682,3 @@ bool ScriptInterface::Eval(const char* code, JS::MutableHandleValue rval) const
|
||||
ScriptException::CatchPending(rq);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScriptInterface::ParseJSON(const std::string& string_utf8, JS::MutableHandleValue out) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
std::wstring attrsW = wstring_from_utf8(string_utf8);
|
||||
utf16string string(attrsW.begin(), attrsW.end());
|
||||
if (JS_ParseJSON(rq.cx, reinterpret_cast<const char16_t*>(string.c_str()), (u32)string.size(), out))
|
||||
return true;
|
||||
|
||||
ScriptException::CatchPending(rq);
|
||||
return false;
|
||||
}
|
||||
|
||||
void ScriptInterface::ReadJSONFile(const VfsPath& path, JS::MutableHandleValue out) const
|
||||
{
|
||||
if (!VfsFileExists(path))
|
||||
{
|
||||
LOGERROR("File '%s' does not exist", path.string8());
|
||||
return;
|
||||
}
|
||||
|
||||
CVFSFile file;
|
||||
|
||||
PSRETURN ret = file.Load(g_VFS, path);
|
||||
|
||||
if (ret != PSRETURN_OK)
|
||||
{
|
||||
LOGERROR("Failed to load file '%s': %s", path.string8(), GetErrorString(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string content(file.DecodeUTF8()); // assume it's UTF-8
|
||||
|
||||
if (!ParseJSON(content, out))
|
||||
LOGERROR("Failed to parse '%s'", path.string8());
|
||||
}
|
||||
|
||||
struct Stringifier
|
||||
{
|
||||
static bool callback(const char16_t* buf, u32 len, void* data)
|
||||
{
|
||||
utf16string str(buf, buf+len);
|
||||
std::wstring strw(str.begin(), str.end());
|
||||
|
||||
Status err; // ignore Unicode errors
|
||||
static_cast<Stringifier*>(data)->stream << utf8_from_wstring(strw, &err);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::stringstream stream;
|
||||
};
|
||||
|
||||
// TODO: It's not quite clear why JS_Stringify needs JS::MutableHandleValue. |obj| should not get modified.
|
||||
// It probably has historical reasons and could be changed by SpiderMonkey in the future.
|
||||
std::string ScriptInterface::StringifyJSON(JS::MutableHandleValue obj, bool indent) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
Stringifier str;
|
||||
JS::RootedValue indentVal(rq.cx, indent ? JS::Int32Value(2) : JS::UndefinedValue());
|
||||
if (!JS_Stringify(rq.cx, obj, nullptr, indentVal, &Stringifier::callback, &str))
|
||||
{
|
||||
ScriptException::CatchPending(rq);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
return str.stream.str();
|
||||
}
|
||||
|
||||
|
||||
std::string ScriptInterface::ToString(JS::MutableHandleValue obj, bool pretty) const
|
||||
{
|
||||
ScriptRequest rq(this);
|
||||
|
||||
if (obj.isUndefined())
|
||||
return "(void 0)";
|
||||
|
||||
// Try to stringify as JSON if possible
|
||||
// (TODO: this is maybe a bad idea since it'll drop 'undefined' values silently)
|
||||
if (pretty)
|
||||
{
|
||||
Stringifier str;
|
||||
JS::RootedValue indentVal(rq.cx, JS::Int32Value(2));
|
||||
|
||||
if (JS_Stringify(rq.cx, obj, nullptr, indentVal, &Stringifier::callback, &str))
|
||||
return str.stream.str();
|
||||
|
||||
// Drop exceptions raised by cyclic values before trying something else
|
||||
JS_ClearPendingException(rq.cx);
|
||||
}
|
||||
|
||||
// Caller didn't want pretty output, or JSON conversion failed (e.g. due to cycles),
|
||||
// so fall back to obj.toSource()
|
||||
|
||||
std::wstring source = L"(error)";
|
||||
ScriptFunction::Call(rq, obj, "toSource", source);
|
||||
return utf8_from_wstring(source);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user