1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MozJsScriptEngine.hpp"
namespace ousia {
namespace script {
/*
* Some important links to the SpiderMonkey (mozjs) documentation:
*
* Documentation overview:
* https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/
*
* User Guide:
* https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_User_Guide
*
* API Reference:
* https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference
*/
/* Constants */
static const uint32_t MOZJS_RT_MEMSIZE = 64L * 1024L * 1024L;
static const uint32_t MOZJS_CTX_STACK_CHUNK_SIZE = 8192;
/* Class MozJsScriptEngineScope */
/**
* The class of the global object.
*/
static JSClass globalClass = {
"global", JSCLASS_GLOBAL_FLAGS, JS_PropertyStub,
JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
nullptr, nullptr, nullptr,
nullptr, nullptr};
MozJsScriptEngineScope::MozJsScriptEngineScope(JSRuntime *rt) : rt(rt)
{
// Create the execution context
cx = JS_NewContext(rt, MOZJS_CTX_STACK_CHUNK_SIZE);
if (!cx) {
throw ScriptEngineException{"MozJs JS_NewContext failed"};
}
// Start a context request
JS_BeginRequest(cx);
// Set some context options
JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_EXTRA_WARNINGS |
JSOPTION_VAROBJFIX | JSOPTION_DONT_REPORT_UNCAUGHT);
// Create the rooted global object
global =
new JS::RootedObject(cx, JS_NewGlobalObject(cx, &globalClass, nullptr));
// Enter a compartment (heap memory region) surrounding the global object
oldCompartment = JS_EnterCompartment(cx, *global);
// Populate the global object with the standard classes
if (!JS_InitStandardClasses(cx, *global)) {
throw ScriptEngineException{"MozJS JS_InitStandardClasses failed"};
}
}
MozJsScriptEngineScope::~MozJsScriptEngineScope()
{
// Leave the compartment
JS_LeaveCompartment(cx, oldCompartment);
// Free the reference to the local object
delete global;
// End the request
JS_EndRequest(cx);
// Destroy the execution context
JS_DestroyContext(cx);
}
Variant MozJsScriptEngineScope::toVariant(const JS::Value &val)
{
if (val.isBoolean()) {
return Variant{val.toBoolean()};
}
if (val.isInt32()) {
return Variant{(int64_t)val.toInt32()};
}
if (val.isDouble()) {
return Variant{val.toDouble()};
}
if (val.isString()) {
// TODO: Remove the need for using "c_str"!
return Variant{toString(val.toString()).c_str()};
}
return Variant::Null;
}
void MozJsScriptEngineScope::handleErr(bool ok)
{
if (!ok && JS_IsExceptionPending(cx)) {
JS::Value exception;
if (JS_GetPendingException(cx, &exception)) {
// Fetch messgage string, line and column
JS::Value msg, line, col;
JS_GetPendingException(cx, &exception);
JS_GetProperty(cx, JSVAL_TO_OBJECT(exception), "message", &msg);
JS_GetProperty(cx, JSVAL_TO_OBJECT(exception), "lineNumber", &line);
JS_GetProperty(cx, JSVAL_TO_OBJECT(exception), "columnNumber",
&col);
// Clear the exception
JS_ClearPendingException(cx);
// Produce a nice error message in case the caught exception is of
// the "Error" class
if (msg.isString() && line.isInt32() && col.isInt32()) {
// Throw a script engine exception with the corresponding line,
// column and string
throw ScriptEngineException{line.toInt32(), col.toInt32(),
toString(msg)};
}
// Otherwise simply convert the exception to a string
throw ScriptEngineException{toString(exception)};
}
}
}
std::string MozJsScriptEngineScope::toString(const JS::Value &val)
{
// If the given value already is a Javascript string, return it directly.
if (val.isString()) {
return toString(val.toString());
}
// The given value is not really a string, so convert it to one first
JSString *str = JS_ValueToString(cx, val);
if (!str) {
throw ScriptEngineException{"Cannot convert value to string"};
}
return toString(str);
}
std::string MozJsScriptEngineScope::toString(JSString *str)
{
// Encode the string
char *buf = JS_EncodeStringToUTF8(cx, str);
if (!buf) {
throw ScriptEngineException{"JS_EncodeStringToUTF8 failed"};
}
// Copy the string into a std::string, free the original buffer and return
std::string res{buf};
JS_free(cx, buf);
return res;
}
Variant MozJsScriptEngineScope::doRun(const std::string &code)
{
JS::Value rval;
handleErr(JS_EvaluateScript(cx, *global, code.c_str(), code.length(), "", 0,
&rval));
return toVariant(rval);
}
void MozJsScriptEngineScope::doSetVariable(const std::string &name,
const Variant &val, bool constant)
{
// TODO
}
Variant MozJsScriptEngineScope::doGetVariable(const std::string &name)
{
// TODO
return Variant::Null;
}
/* Class MozJsScriptEngine */
MozJsScriptEngine::MozJsScriptEngine()
{
rt = JS_NewRuntime(MOZJS_RT_MEMSIZE, JS_NO_HELPER_THREADS);
if (!rt) {
throw ScriptEngineException{"MozJs JS_NewRuntime failed"};
}
}
MozJsScriptEngine::~MozJsScriptEngine()
{
JS_DestroyRuntime(rt);
JS_ShutDown();
}
MozJsScriptEngineScope *MozJsScriptEngine::createScope()
{
return new MozJsScriptEngineScope(rt);
}
}
}
|