-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathQuickJSEngine.cpp
More file actions
333 lines (278 loc) · 10 KB
/
QuickJSEngine.cpp
File metadata and controls
333 lines (278 loc) · 10 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "ScriptEngine.hpp"
#include <quickjs/quickjs.h>
static JSClassID QuickJSEngineClass;
static std::string ErrorToString(JSContext *ctx) {
JSValue exception_val, val;
const char *stack;
const char *str;
bool is_error;
std::string ret;
size_t s1, s2;
exception_val = JS_GetException(ctx);
is_error = JS_IsError(ctx, exception_val);
str = JS_ToCStringLen(ctx, &s1, exception_val);
if (!str) {
return std::string("error thrown but no error message");
}
if (!is_error) {
ret = std::string("Throw:\n") + std::string(str);
} else {
val = JS_GetPropertyStr(ctx, exception_val, "stack");
if (!JS_IsUndefined(val)) {
stack = JS_ToCStringLen(ctx, &s2, val);
ret = std::string(str) + std::string("\n") + std::string(stack);
JS_FreeCString(ctx, stack);
}
JS_FreeValue(ctx, val);
}
JS_FreeCString(ctx, str);
JS_FreeValue(ctx, exception_val);
return ret;
}
struct QuickJSEngine : ScriptEngine {
JSRuntime *rt = NULL;
JSContext *ctx = NULL;
QuickJSEngine() {
rt = JS_NewRuntime();
}
~QuickJSEngine() {
if (ctx) {
JS_FreeContext(ctx);
}
if (rt) {
JS_FreeRuntime(rt);
}
}
std::string getEngineName() override {
return "JavaScript";
}
int run(const std::string& path, const std::string& script) override {
assert(!ctx);
// Create quickjs context
ctx = JS_NewContext(rt);
if (!ctx) {
display("Could not create QuickJS context");
return -1;
}
// Initialize globals
// user pointer
JSValue global_obj = JS_GetGlobalObject(ctx);
JSValue handle = JS_NewObjectClass(ctx, QuickJSEngineClass);
JS_SetOpaque(handle, this);
JS_SetPropertyStr(ctx, global_obj, "p", handle);
// console
JSValue console = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, console, "log",
JS_NewCFunction(ctx, native_console_log, "log", 1));
JS_SetPropertyStr(ctx, console, "info",
JS_NewCFunction(ctx, native_console_info, "info", 1));
JS_SetPropertyStr(ctx, console, "debug",
JS_NewCFunction(ctx, native_console_debug, "debug", 1));
JS_SetPropertyStr(ctx, console, "warn",
JS_NewCFunction(ctx, native_console_warn, "warn", 1));
JS_SetPropertyStr(ctx, global_obj, "console", console);
// display
JS_SetPropertyStr(ctx, global_obj, "display",
JS_NewCFunction(ctx, native_display, "display", 1));
// config: Set defaults
JSValue config = JS_NewObject(ctx);
// frameDivider
JSValue fd =JS_NewInt32(ctx, 32);
JS_SetPropertyStr(ctx, config, "frameDivider", fd);
JS_FreeValue(ctx, fd);
// bufferSize
JSValue bs = JS_NewInt32(ctx, 1);
JS_SetPropertyStr(ctx, config, "bufferSize", bs);
JS_SetPropertyStr(ctx, global_obj, "config", config);
JS_FreeValue(ctx, bs);
// Compile string
JSValue val = JS_Eval(ctx, script.c_str(), script.size(), path.c_str(), 0);
if (JS_IsException(val)) {
display(ErrorToString(ctx));
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, global_obj);
return -1;
}
ProcessBlock* block = getProcessBlock();
// config: Read values
config = JS_GetPropertyStr(ctx, global_obj, "config");
{
// frameDivider
JSValue divider = JS_GetPropertyStr(ctx, config, "frameDivider");
int32_t dividerValue;
if (JS_ToInt32(ctx, ÷rValue, divider) == 0) {
setFrameDivider(dividerValue);
}
// bufferSize
JSValue buffer = JS_GetPropertyStr(ctx, config, "bufferSize");
int32_t bufferValue;
if (JS_ToInt32(ctx, &bufferValue, buffer) == 0) {
setBufferSize(bufferValue);
}
JS_FreeValue(ctx, config);
}
// block
JSValue blockIdx = JS_NewObject(ctx);
{
// inputs
JSValue arr = JS_NewArray(ctx);
for (int i = 0; i < NUM_ROWS; i++) {
JSValue buffer = JS_NewArrayBuffer(ctx, (uint8_t *) block->inputs[i], sizeof(float) * block->bufferSize, NULL, NULL, true);
if (JS_SetPropertyUint32(ctx, arr, i, buffer) < 0) {
WARN("Unable to set property %d of inputs array", i);
}
}
JS_SetPropertyStr(ctx, blockIdx, "inputs", arr);
// outputs
arr = JS_NewArray(ctx);
for (int i = 0; i < NUM_ROWS; i++) {
JSValue buffer = JS_NewArrayBuffer(ctx, (uint8_t *) block->outputs[i], sizeof(float) * block->bufferSize, NULL, NULL, true);
if (JS_SetPropertyUint32(ctx, arr, i, buffer) < 0) {
WARN("Unable to set property %d of outputs array", i);
}
}
JS_SetPropertyStr(ctx, blockIdx, "outputs", arr);
// knobs
JSValue knobsIdx = JS_NewArrayBuffer(ctx, (uint8_t *) &block->knobs, sizeof(float) * NUM_ROWS, NULL, NULL, true);
JS_SetPropertyStr(ctx, blockIdx, "knobs", knobsIdx);
// switches
JSValue switchesIdx = JS_NewArrayBuffer(ctx, (uint8_t *) &block->switches, sizeof(bool) * NUM_ROWS, NULL, NULL, true);
JS_SetPropertyStr(ctx, blockIdx, "switches", switchesIdx);
// lights
arr = JS_NewArray(ctx);
for (int i = 0; i < NUM_ROWS; i++) {
JSValue buffer = JS_NewArrayBuffer(ctx, (uint8_t *) &block->lights[i], sizeof(float) * 3, NULL, NULL, true);
if (JS_SetPropertyUint32(ctx, arr, i, buffer) < 0) {
WARN("Unable to set property %d of lights array", i);
}
}
JS_SetPropertyStr(ctx, blockIdx, "lights", arr);
// switchlights
arr = JS_NewArray(ctx);
for (int i = 0; i < NUM_ROWS; i++) {
JSValue buffer = JS_NewArrayBuffer(ctx, (uint8_t *) &block->switchLights[i], sizeof(float) * 3, NULL, NULL, true);
if (JS_SetPropertyUint32(ctx, arr, i, buffer) < 0) {
WARN("Unable to set property %d of switchLights array", i);
}
}
JS_SetPropertyStr(ctx, blockIdx, "switchLights", arr);
}
JS_SetPropertyStr(ctx, global_obj, "block", blockIdx);
// this is a horrible hack to get QuickJS to allocate the correct types
static const std::string updateTypes = R"(
for (var i = 0; i < 6; i++) {
block.inputs[i] = new Float32Array(block.inputs[i]);
block.outputs[i] = new Float32Array(block.outputs[i]);
block.lights[i] = new Float32Array(block.lights[i]);
block.switchLights[i] = new Float32Array(block.switchLights[i]);
}
block.knobs = new Float32Array(block.knobs);
block.switches = new Uint8Array(block.switches);
)";
JSValue hack = JS_Eval(ctx, updateTypes.c_str(), updateTypes.size(), "QuickJS Hack", 0);
if (JS_IsException(hack)) {
std::string errorString = ErrorToString(ctx);
WARN("QuickJS: %s", errorString.c_str());
display(errorString.c_str());
}
JS_FreeValue(ctx, hack);
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, global_obj);
if (JS_IsException(val)) {
std::string errorString = ErrorToString(ctx);
WARN("QuickJS: %s", errorString.c_str());
display(errorString.c_str());
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, blockIdx);
JS_FreeValue(ctx, global_obj);
return -1;
}
return 0;
}
int process() override {
// global object
JSValue global_obj = JS_GetGlobalObject(ctx);
// block
ProcessBlock* block = getProcessBlock();
JSValue blockIdx = JS_GetPropertyStr(ctx, global_obj, "block");
{
// sampleRate
JSValue sampleRate = JS_NewFloat64(ctx, (double) block->sampleRate);
JS_SetPropertyStr(ctx, blockIdx, "sampleRate", sampleRate);
// sampleTime
JSValue sampleTime = JS_NewFloat64(ctx, (double) block->sampleTime);
JS_SetPropertyStr(ctx, blockIdx, "sampleTime", sampleTime);
// bufferSize
JSValue bufferSize = JS_NewInt32(ctx, (double) block->bufferSize);
JS_SetPropertyStr(ctx, blockIdx, "bufferSize", bufferSize);
}
JSValue process = JS_GetPropertyStr(ctx, global_obj, "process");
JSValue val = JS_Call(ctx, process, JS_UNDEFINED, 1, &blockIdx);
if (JS_IsException(val)) {
std::string errorString = ErrorToString(ctx);
WARN("QuickJS: %s", errorString.c_str());
display(errorString.c_str());
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, process);
JS_FreeValue(ctx, blockIdx);
JS_FreeValue(ctx, global_obj);
return -1;
}
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, process);
JS_FreeValue(ctx, global_obj);
return 0;
}
static QuickJSEngine* getQuickJSEngine(JSContext* ctx) {
JSValue global_obj = JS_GetGlobalObject(ctx);
JSValue p = JS_GetPropertyStr(ctx, global_obj, "p");
QuickJSEngine* engine = (QuickJSEngine*) JS_GetOpaque(p, QuickJSEngineClass);
JS_FreeValue(ctx, p);
JS_FreeValue(ctx, global_obj);
return engine;
}
static JSValue native_console_log(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst *argv) {
if (argc) {
const char *s = JS_ToCString(ctx, argv[0]);
INFO("QuickJS: %s", s);
}
return JS_UNDEFINED;
}
static JSValue native_console_debug(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst *argv) {
if (argc) {
const char *s = JS_ToCString(ctx, argv[0]);
DEBUG("QuickJS: %s", s);
}
return JS_UNDEFINED;
}
static JSValue native_console_info(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst *argv) {
if (argc) {
const char *s = JS_ToCString(ctx, argv[0]);
INFO("QuickJS: %s", s);
}
return JS_UNDEFINED;
}
static JSValue native_console_warn(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst *argv) {
if (argc) {
const char *s = JS_ToCString(ctx, argv[0]);
WARN("QuickJS: %s", s);
}
return JS_UNDEFINED;
}
static JSValue native_display(JSContext* ctx, JSValueConst this_val,
int argc, JSValueConst *argv) {
if (argc) {
const char *s = JS_ToCString(ctx, argv[0]);
getQuickJSEngine(ctx)->display(s);
}
return JS_UNDEFINED;
}
};
__attribute__((constructor(1000)))
static void constructor() {
addScriptEngine<QuickJSEngine>(".js");
}