Line data Source code
1 : /* Copyright (C) 2021 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "JSON.h"
21 :
22 : #include "ps/CStr.h"
23 : #include "ps/Filesystem.h"
24 : #include "scriptinterface/FunctionWrapper.h"
25 : #include "scriptinterface/ScriptExceptions.h"
26 : #include "scriptinterface/ScriptRequest.h"
27 : #include "scriptinterface/ScriptTypes.h"
28 :
29 : #include <string>
30 :
31 : // Ignore warnings in SM headers.
32 : #if GCC_VERSION || CLANG_VERSION
33 : # pragma GCC diagnostic push
34 : # pragma GCC diagnostic ignored "-Wunused-parameter"
35 : # pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
36 : #elif MSC_VERSION
37 : # pragma warning(push, 1)
38 : #endif
39 :
40 : #include "js/JSON.h"
41 :
42 : #if GCC_VERSION || CLANG_VERSION
43 : # pragma GCC diagnostic pop
44 : #elif MSC_VERSION
45 : # pragma warning(pop)
46 : #endif
47 :
48 154 : bool Script::ParseJSON(const ScriptRequest& rq, const std::string& string_utf8, JS::MutableHandleValue out)
49 : {
50 308 : std::wstring attrsW = wstring_from_utf8(string_utf8);
51 308 : std::u16string string(attrsW.begin(), attrsW.end());
52 154 : if (JS_ParseJSON(rq.cx, string.c_str(), (u32)string.size(), out))
53 149 : return true;
54 :
55 5 : ScriptException::CatchPending(rq);
56 5 : return false;
57 : }
58 :
59 84 : void Script::ReadJSONFile(const ScriptRequest& rq, const VfsPath& path, JS::MutableHandleValue out)
60 : {
61 84 : if (!VfsFileExists(path))
62 : {
63 0 : LOGERROR("File '%s' does not exist", path.string8());
64 0 : return;
65 : }
66 :
67 168 : CVFSFile file;
68 :
69 84 : PSRETURN ret = file.Load(g_VFS, path);
70 :
71 84 : if (ret != PSRETURN_OK)
72 : {
73 0 : LOGERROR("Failed to load file '%s': %s", path.string8(), GetErrorString(ret));
74 0 : return;
75 : }
76 :
77 168 : std::string content(file.DecodeUTF8()); // assume it's UTF-8
78 :
79 84 : if (!ParseJSON(rq, content, out))
80 0 : LOGERROR("Failed to parse '%s'", path.string8());
81 : }
82 :
83 : namespace
84 : {
85 20 : struct Stringifier
86 : {
87 9 : static bool callback(const char16_t* buf, u32 len, void* data)
88 : {
89 18 : std::u16string str(buf, buf+len);
90 18 : std::wstring strw(str.begin(), str.end());
91 :
92 : Status err; // ignore Unicode errors
93 9 : static_cast<Stringifier*>(data)->stream << utf8_from_wstring(strw, &err);
94 18 : return true;
95 : }
96 :
97 : std::stringstream stream;
98 : };
99 : } // anonymous namespace
100 :
101 : // JS_Stringify takes a mutable object because ToJSON may arbitrarily mutate the value.
102 : // TODO: it'd be nice to have a non-mutable variant.
103 1 : std::string Script::StringifyJSON(const ScriptRequest& rq, JS::MutableHandleValue obj, bool indent)
104 : {
105 2 : Stringifier str;
106 2 : JS::RootedValue indentVal(rq.cx, indent ? JS::Int32Value(2) : JS::UndefinedValue());
107 1 : if (!JS_Stringify(rq.cx, obj, nullptr, indentVal, &Stringifier::callback, &str))
108 : {
109 0 : ScriptException::CatchPending(rq);
110 0 : return std::string();
111 : }
112 :
113 1 : return str.stream.str();
114 : }
115 :
116 :
117 21 : std::string Script::ToString(const ScriptRequest& rq, JS::MutableHandleValue obj, bool pretty)
118 : {
119 21 : if (obj.isUndefined())
120 0 : return "(void 0)";
121 :
122 : // Try to stringify as JSON if possible
123 : // (TODO: this is maybe a bad idea since it'll drop 'undefined' values silently)
124 21 : if (pretty)
125 : {
126 10 : Stringifier str;
127 10 : JS::RootedValue indentVal(rq.cx, JS::Int32Value(2));
128 :
129 9 : if (JS_Stringify(rq.cx, obj, nullptr, indentVal, &Stringifier::callback, &str))
130 8 : return str.stream.str();
131 :
132 : // Drop exceptions raised by cyclic values before trying something else
133 1 : JS_ClearPendingException(rq.cx);
134 : }
135 :
136 : // Caller didn't want pretty output, or JSON conversion failed (e.g. due to cycles),
137 : // so fall back to obj.toSource()
138 :
139 26 : std::wstring source = L"(error)";
140 13 : ScriptFunction::Call(rq, obj, "toSource", source);
141 13 : return utf8_from_wstring(source);
142 : }
|