forked from bellard/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 55
/
quickjs-jsx.h
368 lines (333 loc) · 10 KB
/
quickjs-jsx.h
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* JSX translates XML literals like this
*
* var jsx = <div foo="bar">some text</div>;
*
* into calls of "JSX driver function":
*
* __jsx__("div", {foo:"bar"}, ["some text"]);
*
* note:
* a) the call always have 3 arguments: string, object|null, array|null
* b) __jsx__ can be redefined, e.g. for https://mithril.js.org it will be just
*
* __jsx__ = m; // using mithril as JSX driver
*/
static __exception int next_web_token(JSParseState *s) {
s->allow_web_name_token = 1;
int r = next_token(s);
s->allow_web_name_token = 0;
return r;
}
static int is_non_space_run(const uint8_t* start, const uint8_t* end) {
for (; start < end; ++start) if (!lre_is_space(*start)) break;
for (; end > start; --end) if (!lre_is_space(*(end-1))) break;
return end - start;
}
static int invalid_name_token(int t) {
//return t != TOK_IDENT && !(t >= TOK_IF && t <= TOK_OF);
return !token_is_ident(t);
}
static int js_parse_jsx_expr(JSParseState *s, int level)
{
int atts_count = 0;
int kids_count = 0;
JSAtom tag_atom = JS_ATOM_NULL;
JSValue tag = JS_UNINITIALIZED;
JSAtom attr_name = JS_ATOM_NULL;
JSValue attr_value = JS_UNINITIALIZED;
int token_read = 0;
int is_bodyless = 0;
const char* errmsg = "invalid JSX expression";
char msg_buffer[512] = { 0 };
#if defined(CONFIG_JSX_SCITER) // HTML shortcuts used by Sciter
char class_buffer[512] = { 0 };
#endif
// NOTE: caller already consumed '<'
if (next_web_token(s)) goto fail;
if (invalid_name_token(s->token.val)) {
errmsg = "Expecting tag name";
goto fail;
}
//tag
tag_atom = JS_DupAtom(s->ctx,s->token.u.ident.atom);
tag = JS_AtomToString(s->ctx,tag_atom);
// load JSX function - driver of JSX expressions:
#if 1 // load it as a global function
emit_op(s, OP_get_var);
emit_atom(s, JS_ATOM_JSX);
#else // load it as a local/scope function - do we need that?
emit_op(s, OP_scope_get_var);
emit_atom(s, JS_ATOM_JSX);
emit_u16(s, s->cur_func->scope_level);
#endif
// #0 #1 #2
// JSX(tag, atts ,kids); where
// - atts - object {...}, can be empty
// - kids - array [...], can be empty
char buf[ATOM_GET_STR_BUF_SIZE];
const char* tag_chars = JS_AtomGetStr(s->ctx, buf, countof(buf), tag_atom);
// check for tag name starting from capital letter
uint32_t res[3] = {0};
lre_case_conv(res, tag_chars[0], 1);
if (res[0] != tag_chars[0]) { // tag name starts from upper case.
// ReactJS convention, if tag started from capital letter - it is either class or function
// Fetch it here, so the tag in JSX call can be reference to class(constructor) or a function
emit_op(s, OP_scope_get_var);
emit_atom(s, tag_atom);
emit_u16(s, s->cur_func->scope_level);
}
else {
if (emit_push_const(s, tag, 0))
goto fail;
}
// parse attributes
if (next_web_token(s)) goto fail;
emit_op(s, OP_object);
while (s->token.val != '>') {
if (s->token.val == '/') {
if (next_token(s))
goto fail;
//json_parse_expect(s, '>');
if (s->token.val != '>') {
errmsg = "expecting '>'";
goto fail;
}
//goto GENERATE_KIDS;
is_bodyless = 1;
break;
}
#if defined(CONFIG_JSX_SCITER) // HTML shortcuts used by Sciter
if (s->token.val == '#') { // <div #some> -> <div id="some">
if (next_web_token(s)) goto fail;
if (invalid_name_token(s->token.val)) {
errmsg = "expecting identifier";
goto fail;
}
attr_name = JS_NewAtom(s->ctx,"id");
attr_value = JS_AtomToString(s->ctx, s->token.u.ident.atom);
goto PUSH_ATTR_VALUE;
}
if (s->token.val == '|') { // <input|text> -> <input type="text">
if (next_web_token(s)) goto fail;
if (invalid_name_token(s->token.val)) {
errmsg = "expecting identifier";
goto fail;
}
attr_name = JS_NewAtom(s->ctx, "type");
attr_value = JS_AtomToString(s->ctx, s->token.u.ident.atom);
goto PUSH_ATTR_VALUE;
}
if (s->token.val == '(') { // <input(foo)> -> <input name="foo">
if (next_web_token(s)) goto fail;
if (invalid_name_token(s->token.val)) {
errmsg = "expecting identifier";
goto fail;
}
attr_name = JS_NewAtom(s->ctx, "name");
attr_value = JS_AtomToString(s->ctx, s->token.u.ident.atom);
if (next_token(s)) goto fail;
if (s->token.val != ')') {
errmsg = "expecting identifier";
goto fail;
}
goto PUSH_ATTR_VALUE;
}
if (s->token.val == '.') { // <div.some> -> <div class="some">
if (next_web_token(s)) goto fail;
if (invalid_name_token(s->token.val)) {
errmsg = "expecting identifier";
goto fail;
}
char cls1[256];
const char *name = JS_AtomGetStr(s->ctx, cls1, countof(cls1), s->token.u.ident.atom);
if (strlen(class_buffer) + strlen(name) + 2 < countof(class_buffer)) {
if(class_buffer[0]) strcat(class_buffer, " ");
strcat(class_buffer, name);
}
if (next_web_token(s)) goto fail;
continue;
}
#endif
if (s->token.val == '{') // <a {atts}>foo</a>
{
if (next_token(s))
goto fail;
if (js_parse_assign_expr(s))
goto fail;
if (s->token.val != '}') {
errmsg = "expecting '}'";
goto fail;
}
//attr_name = JS_NewAtomUInt32(s->ctx, unnamed_atts_count++);
emit_op(s, OP_null); /* dummy excludeList */
emit_op(s, OP_copy_data_properties);
emit_u8(s, 2 | (1 << 2) | (0 << 5));
emit_op(s, OP_drop); /* pop excludeList */
emit_op(s, OP_drop); /* pop src object */
if (next_web_token(s))
goto fail;
continue;
}
else if (token_is_ident(s->token.val))
{
/* keywords and reserved words have a valid atom */
attr_name = JS_DupAtom(s->ctx, s->token.u.ident.atom);
if (next_token(s))
goto fail;
}
if (s->token.val != '=') {
token_read = 1;
attr_value = JS_AtomToString(s->ctx, JS_ATOM_empty_string);
goto PUSH_ATTR_VALUE;
}
else {
token_read = 0;
if (next_token(s)) // eat `=`
goto fail;
}
if (s->token.val == TOK_STRING) {
attr_value = JS_DupValue(s->ctx, s->token.u.str.str);
PUSH_ATTR_VALUE:
if (emit_push_const(s, attr_value, 0))
goto fail;
JS_FreeValue(s->ctx, attr_value);
}
else if (s->token.val == TOK_TEMPLATE) {
if (js_parse_template(s, 0, NULL))
goto fail;
token_read = 1;
}
else if (s->token.val == '{')
{
if (next_token(s))
goto fail;
if (js_parse_assign_expr(s))
goto fail;
if (s->token.val != '}') {
errmsg = "expecting '}'";
goto fail;
}
}
else if(s->token.val == TOK_NUMBER) {
attr_value = JS_DupValue(s->ctx,s->token.u.num.val);
goto PUSH_ATTR_VALUE;
}
else if (s->token.val == TOK_FALSE) {
emit_op(s, OP_push_false);
}
else if (s->token.val == TOK_TRUE) {
emit_op(s, OP_push_true);
}
else if (s->token.val == TOK_NULL) {
emit_op(s, OP_null);
}
else {
errmsg = "bad attribute value";
goto fail;
}
set_object_name(s, attr_name);
emit_op(s, OP_define_field);
emit_atom(s, attr_name);
JS_FreeAtom(s->ctx, attr_name);
if (!token_read) {
if (next_web_token(s))
goto fail;
}
}
#if defined(CONFIG_JSX_SCITER) // HTML shortcuts used by Sciter
if (class_buffer[0]) { // add remaining classes
attr_value = JS_NewString(s->ctx, class_buffer);
int r = emit_push_const(s,attr_value, 0);
JS_FreeValue(s->ctx, attr_value);
if (r < 0) goto fail;
attr_name = JS_NewAtom(s->ctx, "class");
set_object_name(s, attr_name);
emit_op(s, OP_define_field);
emit_atom(s, attr_name);
JS_FreeAtom(s->ctx, attr_name);
}
#endif
// parse content of the element
while(!is_bodyless)
{
const uint8_t *p;
p = s->last_ptr = s->buf_ptr;
s->last_line_num = s->token.line_num;
if (js_parse_string(s, '<', TRUE, p, &s->token, &p))
goto fail;
if (s->buf_ptr != p) {
const uint8_t *start = s->buf_ptr;
s->buf_ptr = p;
if (is_non_space_run(start, p)) {
JSValue str = JS_NewStringLen(s->ctx, start, p - start);
if(str == JS_EXCEPTION)
goto fail;
if (emit_push_const(s,str, 1)) {
JS_FreeValue(s->ctx, str);
goto fail;
}
JS_FreeValue(s->ctx, str);
++kids_count;
}
}
if (next_token(s))
goto fail;
if (s->token.val == '<') {
if (*s->buf_ptr == '/') {
if (next_token(s)) // skip '/'
goto fail;
if (next_web_token(s)) // get tail tag name
goto fail;
if (token_is_ident(s->token.val)) { /* keywords and reserved words have a valid atom */
if (s->token.u.ident.atom != tag_atom) {
char atail[64];
char ahead[64];
snprintf(msg_buffer, countof(msg_buffer), "head <%s> and tail </%s> tags do not match",
JS_AtomGetStr(s->ctx, ahead, countof(ahead), tag_atom),
JS_AtomGetStr(s->ctx, atail, countof(atail), s->token.u.ident.atom));
errmsg = msg_buffer;
goto fail;
}
if (next_token(s))
goto fail;
if (s->token.val != '>') {
errmsg = "expecting '>' in tail tag";
goto fail;
}
break;
}
}
else {
js_parse_jsx_expr(s, level + 1);
++kids_count;
}
}
else if (s->token.val == '{') {
if (next_token(s))
goto fail;
if (js_parse_assign_expr(s))
goto fail;
if (s->token.val != '}') {
errmsg = "expected '}'";
goto fail;
}
++kids_count;
}
}
//GENERATE_KIDS:
emit_op(s, OP_array_from);
emit_u16(s, kids_count);
emit_op(s, OP_call);
emit_u16(s, 3);
if (level == 0) {
if (next_token(s))
goto fail;
}
JS_FreeValue(s->ctx, tag);
JS_FreeAtom(s->ctx, tag_atom);
return 0;
fail:
JS_FreeValue(s->ctx, tag);
JS_FreeAtom(s->ctx, tag_atom);
return js_parse_error(s, errmsg);
}