Skip to content

Commit d1ea93e

Browse files
committed
Persistence fixes.
1 parent e5d2b16 commit d1ea93e

10 files changed

Lines changed: 3176 additions & 2850 deletions

File tree

cutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <stdlib.h>
2929
#include <inttypes.h>
30+
#include <string.h>
3031

3132
#ifdef _MSC_VER
3233
#include <windows.h>

qjscalc.c

Lines changed: 2019 additions & 2019 deletions
Large diffs are not rendered by default.

quickjs-atom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ DEF(await, "await")
8181
DEF(empty_string, "")
8282
/* identifiers */
8383
DEF(length, "length")
84+
DEF(tag, "tag")
8485
DEF(fileName, "fileName")
8586
DEF(lineNumber, "lineNumber")
8687
DEF(message, "message")

quickjs-bjson.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* QuickJS: binary JSON module (test only)
3+
*
4+
* Copyright (c) 2017-2019 Fabrice Bellard
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
#include "quickjs-libc.h"
25+
#include "cutils.h"
26+
27+
static JSValue js_bjson_read(JSContext *ctx, JSValueConst this_val,
28+
int argc, JSValueConst *argv)
29+
{
30+
uint8_t *buf;
31+
uint64_t pos = 0, len = 0;
32+
JSValue obj;
33+
JSValue cb = JS_UNINITIALIZED;
34+
size_t size;
35+
int flags;
36+
37+
if (argc > 1) {
38+
if (JS_IsFunction(ctx, argv[1]))
39+
cb = argv[1];
40+
else if (JS_ToIndex(ctx, &pos, argv[1]))
41+
return JS_EXCEPTION;
42+
if (argc > 2) {
43+
if (JS_ToIndex(ctx, &len, argv[2]))
44+
return JS_EXCEPTION;
45+
}
46+
}
47+
buf = JS_GetArrayBuffer(ctx, &size, argv[0]);
48+
if (!buf)
49+
return JS_EXCEPTION;
50+
51+
if (len == 0)
52+
len = size - pos;
53+
54+
if (pos + len > size)
55+
return JS_ThrowRangeError(ctx, "array buffer overflow");
56+
flags = 0;
57+
if (argc > 3 && JS_ToBool(ctx, argv[3]))
58+
flags |= JS_READ_OBJ_REFERENCE;
59+
60+
if (cb != JS_UNINITIALIZED) {
61+
cb = JS_DupValue(ctx,cb);
62+
size_t rest = 0;
63+
uint8_t *sbuf = buf;
64+
uint64_t slen = len;
65+
uint8_t *send = buf + len;
66+
do {
67+
obj = JS_ReadObject2(ctx, sbuf, slen, flags, &rest);
68+
sbuf = send - rest;
69+
slen = rest;
70+
JSValue rv = JS_Call(ctx, cb, JS_UNDEFINED, 1, &obj);
71+
JS_FreeValue(ctx, obj);
72+
if (JS_IsException(rv)) {
73+
JS_FreeValue(ctx, cb);
74+
return rv;
75+
}
76+
if (rv == JS_FALSE)
77+
break;
78+
} while (rest);
79+
JS_FreeValue(ctx, cb);
80+
return JS_NewInt64(ctx, rest);
81+
}
82+
else
83+
obj = JS_ReadObject(ctx, buf + pos, len, flags);
84+
return obj;
85+
}
86+
87+
static JSValue js_bjson_write(JSContext *ctx, JSValueConst this_val,
88+
int argc, JSValueConst *argv)
89+
{
90+
size_t len;
91+
uint8_t *buf;
92+
JSValue array;
93+
int flags;
94+
95+
flags = 0;
96+
if (JS_ToBool(ctx, argv[1]))
97+
flags |= JS_WRITE_OBJ_REFERENCE;
98+
buf = JS_WriteObject(ctx, &len, argv[0], flags);
99+
if (!buf)
100+
return JS_EXCEPTION;
101+
array = JS_NewArrayBufferCopy(ctx, buf, len);
102+
js_free(ctx, buf);
103+
return array;
104+
}
105+
106+
static const JSCFunctionListEntry js_bjson_funcs[] = {
107+
JS_CFUNC_DEF("read", 4, js_bjson_read),
108+
JS_CFUNC_DEF("write", 2, js_bjson_write),
109+
};
110+
111+
static int js_bjson_init(JSContext *ctx, JSModuleDef *m)
112+
{
113+
return JS_SetModuleExportList(ctx, m, js_bjson_funcs,
114+
countof(js_bjson_funcs));
115+
}
116+
117+
JSModuleDef *js_init_module_bjson(JSContext *ctx, const char *module_name)
118+
{
119+
JSModuleDef *m;
120+
m = JS_NewCModule(ctx, module_name, js_bjson_init);
121+
if (!m) return NULL;
122+
JS_AddModuleExportList(ctx, m, js_bjson_funcs, countof(js_bjson_funcs));
123+
return m;
124+
}

quickjs-jsx.h

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
4040
JSValue tag = JS_UNINITIALIZED;
4141
JSAtom attr_name = JS_ATOM_NULL;
4242
JSValue attr_value = JS_UNINITIALIZED;
43+
int token_read = 0;
44+
int is_bodyless = 0;
4345

4446
const char* errmsg = "invalid JSX expression";
47+
char msg_buffer[512] = { 0 };
4548
#if defined(CONFIG_JSX_SCITER) // HTML shortcuts used by Sciter
4649
char class_buffer[512] = { 0 };
4750
#endif
@@ -107,7 +110,9 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
107110
errmsg = "expecting '>'";
108111
goto fail;
109112
}
110-
goto GENERATE_KIDS;
113+
//goto GENERATE_KIDS;
114+
is_bodyless = 1;
115+
break;
111116
}
112117
#if defined(CONFIG_JSX_SCITER) // HTML shortcuts used by Sciter
113118
if (s->token.val == '#') { // <div #some> -> <div id="some">
@@ -192,16 +197,29 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
192197
goto fail;
193198
}
194199

195-
if (js_parse_expect(s, '='))
200+
if (s->token.val != '=') {
201+
token_read = 1;
202+
attr_value = JS_AtomToString(s->ctx, JS_ATOM_empty_string);
203+
goto PUSH_ATTR_VALUE;
204+
}
205+
else {
206+
token_read = 0;
207+
if (next_token(s)) // eat `=`
196208
goto fail;
209+
}
197210

198211
if (s->token.val == TOK_STRING) {
199-
attr_value = JS_DupValue(s->ctx,s->token.u.str.str);
212+
attr_value = JS_DupValue(s->ctx, s->token.u.str.str);
200213
PUSH_ATTR_VALUE:
201214
if (emit_push_const(s, attr_value, 0))
202215
goto fail;
203216
JS_FreeValue(s->ctx, attr_value);
204217
}
218+
else if (s->token.val == TOK_TEMPLATE) {
219+
if (js_parse_template(s, 0, NULL))
220+
goto fail;
221+
token_read = 1;
222+
}
205223
else if (s->token.val == '{')
206224
{
207225
if (next_token(s))
@@ -213,15 +231,34 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
213231
goto fail;
214232
}
215233
}
234+
else if(s->token.val == TOK_NUMBER) {
235+
attr_value = JS_DupValue(s->ctx,s->token.u.num.val);
236+
goto PUSH_ATTR_VALUE;
237+
}
238+
else if (s->token.val == TOK_FALSE) {
239+
emit_op(s, OP_push_false);
240+
}
241+
else if (s->token.val == TOK_TRUE) {
242+
emit_op(s, OP_push_true);
243+
}
244+
else if (s->token.val == TOK_NULL) {
245+
emit_op(s, OP_null);
246+
}
247+
else {
248+
errmsg = "bad attribute value";
249+
goto fail;
250+
}
216251

217252
set_object_name(s, attr_name);
218253
emit_op(s, OP_define_field);
219254
emit_atom(s, attr_name);
220255
JS_FreeAtom(s->ctx, attr_name);
221256

257+
if (!token_read) {
222258
if (next_web_token(s))
223259
goto fail;
224260
}
261+
}
225262

226263
#if defined(CONFIG_JSX_SCITER) // HTML shortcuts used by Sciter
227264
if (class_buffer[0]) { // add remaining classes
@@ -239,7 +276,8 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
239276

240277
// parse content of the element
241278

242-
for (;;) {
279+
while(!is_bodyless)
280+
{
243281
const uint8_t *p;
244282
p = s->last_ptr = s->buf_ptr;
245283
s->last_line_num = s->token.line_num;
@@ -271,7 +309,12 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
271309
goto fail;
272310
if (token_is_ident(s->token.val)) { /* keywords and reserved words have a valid atom */
273311
if (s->token.u.ident.atom != tag_atom) {
274-
errmsg = "head and tail tags do not match";
312+
char atail[64];
313+
char ahead[64];
314+
snprintf(msg_buffer, countof(msg_buffer), "head <%s> and tail </%s> tags do not match",
315+
JS_AtomGetStr(s->ctx, ahead, countof(ahead), tag_atom),
316+
JS_AtomGetStr(s->ctx, atail, countof(atail), s->token.u.ident.atom));
317+
errmsg = msg_buffer;
275318
goto fail;
276319
}
277320
if (next_token(s))
@@ -301,7 +344,7 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
301344
}
302345
}
303346

304-
GENERATE_KIDS:
347+
//GENERATE_KIDS:
305348
emit_op(s, OP_array_from);
306349
emit_u16(s, kids_count);
307350

@@ -315,15 +358,11 @@ static int js_parse_jsx_expr(JSParseState *s, int level)
315358

316359
JS_FreeValue(s->ctx, tag);
317360
JS_FreeAtom(s->ctx, tag_atom);
318-
//JS_FreeAtom(s->ctx, attr_name);
319-
//JS_FreeValue(s->ctx, attr_value);
320361

321362
return 0;
322363
fail:
323364
JS_FreeValue(s->ctx, tag);
324365
JS_FreeAtom(s->ctx, tag_atom);
325-
JS_FreeAtom(s->ctx, attr_name);
326-
//JS_FreeValue(s->ctx, attr_value);
327366
return js_parse_error(s, errmsg);
328367
}
329368

0 commit comments

Comments
 (0)