forked from Phala-Network/qjs-sys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qjs-pink.c
226 lines (196 loc) · 6.14 KB
/
qjs-pink.c
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
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cutils.h"
#include "list.h"
#include "quickjs.h"
#include "qjs-pink.h"
static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val) {
const char *str;
str = JS_ToCString(ctx, val);
if (str) {
fprintf(f, "%s\n", str);
JS_FreeCString(ctx, str);
} else {
fprintf(f, "[exception]\n");
}
}
void js_std_dump_error(JSContext *ctx, JSValueConst exception_val) {
JSValue val;
BOOL is_error;
is_error = JS_IsError(ctx, exception_val);
js_dump_obj(ctx, stderr, exception_val);
if (is_error) {
val = JS_GetPropertyStr(ctx, exception_val, "stack");
if (!JS_IsUndefined(val)) {
js_dump_obj(ctx, stderr, val);
}
JS_FreeValue(ctx, val);
}
}
static JSValue js_print(JSContext *ctx, JSValueConst this_val, int argc,
JSValueConst *argv) {
int i;
const char *str;
size_t len;
(void)this_val;
for (i = 0; i < argc; i++) {
if (i != 0)
putchar(' ');
str = JS_ToCStringLen(ctx, &len, argv[i]);
if (!str)
return JS_EXCEPTION;
fwrite(str, 1, len, stdout);
JS_FreeCString(ctx, str);
}
putchar('\n');
fflush(stdout);
return JS_UNDEFINED;
}
JSValue __host_call(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
static void js_env_add_helpers(JSContext *ctx) {
JSValue global_obj, console;
global_obj = JS_GetGlobalObject(ctx);
console = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, console, "log",
JS_NewCFunction(ctx, js_print, "log", 1));
JS_SetPropertyStr(ctx, console, "error",
JS_NewCFunction(ctx, js_print, "error", 1));
JS_SetPropertyStr(ctx, global_obj, "console", console);
JS_SetPropertyStr(ctx, global_obj, "print",
JS_NewCFunction(ctx, js_print, "print", 1));
JS_SetPropertyStr(ctx, global_obj, "__hostCall",
JS_NewCFunction(ctx, __host_call, "__hostCall", 1));
JS_FreeValue(ctx, global_obj);
}
struct callback_data {
JSContext *ctx;
JSValue args;
};
static int build_args(void *userdata, int i, const char* arg, int len)
{
int ret;
struct callback_data *data = (struct callback_data*)userdata;
ret = JS_DefinePropertyValueUint32(data->ctx, data->args, i,
JS_NewStringLen(data->ctx, arg, len),
JS_PROP_C_W_E);
if (ret < 0) {
return -1;
} else {
return 0;
}
}
static int js_env_add_args(JSContext *ctx, callbacks_t* callbacks) {
JSValue global_obj, args;
int ret = -1;
global_obj = JS_GetGlobalObject(ctx);
args = JS_NewArray(ctx);
if (!JS_IsException(args)) {
struct callback_data data = {
ctx, args
};
ret = callbacks->read_args(callbacks->userdata, &data, build_args);
}
if (ret < 0) {
JS_FreeValue(ctx, args);
} else {
JS_SetPropertyStr(ctx, global_obj, "scriptArgs", args);
}
JS_FreeValue(ctx, global_obj);
return ret;
}
static JSValue get_output(JSContext *ctx) {
JSValue global_obj, output;
global_obj = JS_GetGlobalObject(ctx);
output = JS_GetPropertyStr(ctx, global_obj, "scriptOutput");
JS_FreeValue(ctx, global_obj);
return output;
}
static void put_val(JSContext *ctx, JSValue val, callbacks_t* callbacks) {
if (JS_IsUint8Array(val)) {
uint32_t size = 0;
uint8_t* buffer = JS_Uint8ArrayGetBuffer(val, &size);
if(buffer != NULL) {
callbacks->output_bytes(callbacks->userdata, (const char*)buffer, size);
} else {
}
} else {
const char *str = JS_ToCString(ctx, val);
if (str == NULL) {
callbacks->output_str(callbacks->userdata, "<NullValue>");
} else {
callbacks->output_str(callbacks->userdata, str);
JS_FreeCString(ctx, str);
}
}
}
static JSValue eval_bytecode(JSContext *ctx, const uint8_t *buf, size_t buf_len) {
JSValue obj = JS_ReadObject(ctx, buf, buf_len, JS_READ_OBJ_BYTECODE);
if (JS_IsException(obj))
return obj;
return JS_EvalFunction(ctx, obj);
}
static int eval_buf(JSContext *ctx, const void *buf, int buf_len, int is_bytecode, callbacks_t* callbacks) {
JSValue val;
int ret;
if (is_bytecode) {
val = eval_bytecode(ctx, buf, buf_len);
} else {
val = JS_Eval(ctx, buf, buf_len, "<eval>", 0);
}
if (JS_IsException(val)) {
JSValue exception_val = JS_GetException(ctx);
fprintf(stderr, "Exception:\n");
put_val(ctx, exception_val, callbacks);
js_std_dump_error(ctx, exception_val);
JS_FreeValue(ctx, exception_val);
ret = -1;
} else {
JSValue output = get_output(ctx);
if (!JS_IsUndefined(output)) {
put_val(ctx, output, callbacks);
} else {
put_val(ctx, val, callbacks);
}
JS_FreeValue(ctx, output);
ret = 0;
}
JS_FreeValue(ctx, val);
return ret;
}
int js_eval(code_t *codes, size_t n_codes, callbacks_t *callbacks)
{
JSRuntime *rt;
JSContext *ctx;
int ret, i;
rt = JS_NewRuntime();
if (!rt) {
fprintf(stderr, "Failed to create JS runtime\n");
callbacks->output_str(callbacks->userdata, "<RuntimeCreationError>");
return 1;
}
ctx = JS_NewContext(rt);
if (!ctx) {
fprintf(stderr, "Failed to create JS context\n");
callbacks->output_str(callbacks->userdata, "<ContextCreationError>");
return 2;
}
js_env_add_helpers(ctx);
if (js_env_add_args(ctx, callbacks) != 0) {
fprintf(stderr, "Failed to build arguments \n");
callbacks->output_str(callbacks->userdata, "<InvalidArgs>");
return 3;
}
for (i = 0; i < n_codes; i++) {
ret = eval_buf(ctx, codes[i].code, codes[i].code_len, codes[i].is_bytecode, callbacks);
if (ret != 0) {
break;
}
}
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return ret;
}