mirror of
https://github.com/bellard/quickjs.git
synced 2025-09-27 21:58:45 +03:00
Compare commits
1 Commits
8b43bae10b
...
3c38f29ab4
Author | SHA1 | Date | |
---|---|---|---|
|
3c38f29ab4 |
@ -1,5 +1,3 @@
|
||||
2025-09-13:
|
||||
|
||||
- added JSON modules and import attributes
|
||||
- added JS_PrintValue() API
|
||||
- qjs: pretty print objects in print() and console.log()
|
||||
@ -14,7 +12,6 @@
|
||||
accept JSON5 modules
|
||||
- added JS_FreePropertyEnum() and JS_AtomToCStringLen() API
|
||||
- added Error.isError()
|
||||
- misc bug fixes
|
||||
|
||||
2025-04-26:
|
||||
|
||||
|
4
Makefile
4
Makefile
@ -55,8 +55,8 @@ PREFIX?=/usr/local
|
||||
#CONFIG_UBSAN=y
|
||||
|
||||
# TEST262 bootstrap config: commit id and shallow "since" parameter
|
||||
TEST262_COMMIT?=af3d908437b0912513a594e7167f17658e72d88b
|
||||
TEST262_SINCE?=2025-09-14
|
||||
TEST262_COMMIT?=3fd4ec27f1798ebecafc73b354a45dcdda9bde29
|
||||
TEST262_SINCE?=2025-08-25
|
||||
|
||||
OBJDIR=.obj
|
||||
|
||||
|
221
quickjs.c
221
quickjs.c
@ -13002,6 +13002,73 @@ static JSValue JS_ToStringCheckObject(JSContext *ctx, JSValueConst val)
|
||||
return JS_ToString(ctx, val);
|
||||
}
|
||||
|
||||
static JSValue JS_ToQuotedString(JSContext *ctx, JSValueConst val1)
|
||||
{
|
||||
JSValue val;
|
||||
JSString *p;
|
||||
int i;
|
||||
uint32_t c;
|
||||
StringBuffer b_s, *b = &b_s;
|
||||
char buf[16];
|
||||
|
||||
val = JS_ToStringCheckObject(ctx, val1);
|
||||
if (JS_IsException(val))
|
||||
return val;
|
||||
p = JS_VALUE_GET_STRING(val);
|
||||
|
||||
if (string_buffer_init(ctx, b, p->len + 2))
|
||||
goto fail;
|
||||
|
||||
if (string_buffer_putc8(b, '\"'))
|
||||
goto fail;
|
||||
for(i = 0; i < p->len; ) {
|
||||
c = string_getc(p, &i);
|
||||
switch(c) {
|
||||
case '\t':
|
||||
c = 't';
|
||||
goto quote;
|
||||
case '\r':
|
||||
c = 'r';
|
||||
goto quote;
|
||||
case '\n':
|
||||
c = 'n';
|
||||
goto quote;
|
||||
case '\b':
|
||||
c = 'b';
|
||||
goto quote;
|
||||
case '\f':
|
||||
c = 'f';
|
||||
goto quote;
|
||||
case '\"':
|
||||
case '\\':
|
||||
quote:
|
||||
if (string_buffer_putc8(b, '\\'))
|
||||
goto fail;
|
||||
if (string_buffer_putc8(b, c))
|
||||
goto fail;
|
||||
break;
|
||||
default:
|
||||
if (c < 32 || is_surrogate(c)) {
|
||||
snprintf(buf, sizeof(buf), "\\u%04x", c);
|
||||
if (string_buffer_puts8(b, buf))
|
||||
goto fail;
|
||||
} else {
|
||||
if (string_buffer_putc(b, c))
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (string_buffer_putc8(b, '\"'))
|
||||
goto fail;
|
||||
JS_FreeValue(ctx, val);
|
||||
return string_buffer_end(b);
|
||||
fail:
|
||||
JS_FreeValue(ctx, val);
|
||||
string_buffer_free(b);
|
||||
return JS_EXCEPTION;
|
||||
}
|
||||
|
||||
#define JS_PRINT_MAX_DEPTH 8
|
||||
|
||||
typedef struct {
|
||||
@ -13059,61 +13126,18 @@ static uint32_t js_string_get_length(JSValueConst val)
|
||||
}
|
||||
}
|
||||
|
||||
/* pretty print the first 'len' characters of 'p' */
|
||||
static void js_print_string1(JSPrintValueState *s, JSString *p, int len, int sep)
|
||||
static void js_dump_char(JSPrintValueState *s, int c, int sep)
|
||||
{
|
||||
uint8_t buf[UTF8_CHAR_LEN_MAX];
|
||||
int l, i, c, c1;
|
||||
|
||||
for(i = 0; i < len; i++) {
|
||||
c = string_get(p, i);
|
||||
switch(c) {
|
||||
case '\t':
|
||||
c = 't';
|
||||
goto quote;
|
||||
case '\r':
|
||||
c = 'r';
|
||||
goto quote;
|
||||
case '\n':
|
||||
c = 'n';
|
||||
goto quote;
|
||||
case '\b':
|
||||
c = 'b';
|
||||
goto quote;
|
||||
case '\f':
|
||||
c = 'f';
|
||||
goto quote;
|
||||
case '\\':
|
||||
quote:
|
||||
if (c == sep || c == '\\') {
|
||||
js_putc(s, '\\');
|
||||
js_putc(s, c);
|
||||
break;
|
||||
default:
|
||||
if (c == sep)
|
||||
goto quote;
|
||||
if (c >= 32 && c <= 126) {
|
||||
} else if (c >= ' ' && c <= 126) {
|
||||
js_putc(s, c);
|
||||
} else if (c < 32 ||
|
||||
(c >= 0x7f && c <= 0x9f)) {
|
||||
escape:
|
||||
js_printf(s, "\\u%04x", c);
|
||||
} else if (c == '\n') {
|
||||
js_putc(s, '\\');
|
||||
js_putc(s, 'n');
|
||||
} else {
|
||||
if (is_hi_surrogate(c)) {
|
||||
if ((i + 1) >= len)
|
||||
goto escape;
|
||||
c1 = string_get(p, i + 1);
|
||||
if (!is_lo_surrogate(c1))
|
||||
goto escape;
|
||||
i++;
|
||||
c = from_surrogate(c, c1);
|
||||
} else if (is_lo_surrogate(c)) {
|
||||
goto escape;
|
||||
}
|
||||
l = unicode_to_utf8(buf, c);
|
||||
s->write_func(s->write_opaque, (char *)buf, l);
|
||||
}
|
||||
break;
|
||||
}
|
||||
js_printf(s, "\\u%04x", c);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13122,10 +13146,12 @@ static void js_print_string_rec(JSPrintValueState *s, JSValueConst val,
|
||||
{
|
||||
if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
|
||||
JSString *p = JS_VALUE_GET_STRING(val);
|
||||
uint32_t len;
|
||||
uint32_t i, len;
|
||||
if (pos < s->options.max_string_length) {
|
||||
len = min_uint32(p->len, s->options.max_string_length - pos);
|
||||
js_print_string1(s, p, len, sep);
|
||||
for(i = 0; i < len; i++) {
|
||||
js_dump_char(s, string_get(p, i), sep);
|
||||
}
|
||||
}
|
||||
} else if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING_ROPE) {
|
||||
JSStringRope *r = JS_VALUE_GET_PTR(val);
|
||||
@ -13198,7 +13224,9 @@ static void js_print_atom(JSPrintValueState *s, JSAtom atom)
|
||||
}
|
||||
} else {
|
||||
js_putc(s, '"');
|
||||
js_print_string1(s, p, p->len, '\"');
|
||||
for(i = 0; i < p->len; i++) {
|
||||
js_dump_char(s, string_get(p, i), '\"');
|
||||
}
|
||||
js_putc(s, '"');
|
||||
}
|
||||
}
|
||||
@ -34924,7 +34952,6 @@ static void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b)
|
||||
JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
|
||||
}
|
||||
#endif
|
||||
if (b->byte_code_buf)
|
||||
free_bytecode_atoms(rt, b->byte_code_buf, b->byte_code_len, TRUE);
|
||||
|
||||
if (b->vardefs) {
|
||||
@ -43832,6 +43859,12 @@ static JSValue js_string_trim(JSContext *ctx, JSValueConst this_val,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static JSValue js_string___quote(JSContext *ctx, JSValueConst this_val,
|
||||
int argc, JSValueConst *argv)
|
||||
{
|
||||
return JS_ToQuotedString(ctx, this_val);
|
||||
}
|
||||
|
||||
/* return 0 if before the first char */
|
||||
static int string_prevc(JSString *p, int *pidx)
|
||||
{
|
||||
@ -44319,6 +44352,7 @@ static const JSCFunctionListEntry js_string_proto_funcs[] = {
|
||||
JS_ALIAS_DEF("trimLeft", "trimStart" ),
|
||||
JS_CFUNC_DEF("toString", 0, js_string_toString ),
|
||||
JS_CFUNC_DEF("valueOf", 0, js_string_toString ),
|
||||
JS_CFUNC_DEF("__quote", 1, js_string___quote ),
|
||||
JS_CFUNC_MAGIC_DEF("toLowerCase", 0, js_string_toLowerCase, 1 ),
|
||||
JS_CFUNC_MAGIC_DEF("toUpperCase", 0, js_string_toLowerCase, 0 ),
|
||||
JS_CFUNC_MAGIC_DEF("toLocaleLowerCase", 0, js_string_toLowerCase, 1 ),
|
||||
@ -46633,72 +46667,10 @@ typedef struct JSONStringifyContext {
|
||||
StringBuffer *b;
|
||||
} JSONStringifyContext;
|
||||
|
||||
static int JS_ToQuotedString(JSContext *ctx, StringBuffer *b, JSValueConst val1)
|
||||
{
|
||||
JSValue val;
|
||||
JSString *p;
|
||||
int i;
|
||||
uint32_t c;
|
||||
char buf[16];
|
||||
|
||||
val = JS_ToStringCheckObject(ctx, val1);
|
||||
if (JS_IsException(val))
|
||||
return -1;
|
||||
p = JS_VALUE_GET_STRING(val);
|
||||
|
||||
if (string_buffer_putc8(b, '\"'))
|
||||
goto fail;
|
||||
for(i = 0; i < p->len; ) {
|
||||
c = string_getc(p, &i);
|
||||
switch(c) {
|
||||
case '\t':
|
||||
c = 't';
|
||||
goto quote;
|
||||
case '\r':
|
||||
c = 'r';
|
||||
goto quote;
|
||||
case '\n':
|
||||
c = 'n';
|
||||
goto quote;
|
||||
case '\b':
|
||||
c = 'b';
|
||||
goto quote;
|
||||
case '\f':
|
||||
c = 'f';
|
||||
goto quote;
|
||||
case '\"':
|
||||
case '\\':
|
||||
quote:
|
||||
if (string_buffer_putc8(b, '\\'))
|
||||
goto fail;
|
||||
if (string_buffer_putc8(b, c))
|
||||
goto fail;
|
||||
break;
|
||||
default:
|
||||
if (c < 32 || is_surrogate(c)) {
|
||||
snprintf(buf, sizeof(buf), "\\u%04x", c);
|
||||
if (string_buffer_puts8(b, buf))
|
||||
goto fail;
|
||||
} else {
|
||||
if (string_buffer_putc(b, c))
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (string_buffer_putc8(b, '\"'))
|
||||
goto fail;
|
||||
static JSValue JS_ToQuotedStringFree(JSContext *ctx, JSValue val) {
|
||||
JSValue r = JS_ToQuotedString(ctx, val);
|
||||
JS_FreeValue(ctx, val);
|
||||
return 0;
|
||||
fail:
|
||||
JS_FreeValue(ctx, val);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int JS_ToQuotedStringFree(JSContext *ctx, StringBuffer *b, JSValue val) {
|
||||
int ret = JS_ToQuotedString(ctx, b, val);
|
||||
JS_FreeValue(ctx, val);
|
||||
return ret;
|
||||
return r;
|
||||
}
|
||||
|
||||
static JSValue js_json_check(JSContext *ctx, JSONStringifyContext *jsc,
|
||||
@ -46881,11 +46853,13 @@ static int js_json_to_str(JSContext *ctx, JSONStringifyContext *jsc,
|
||||
if (!JS_IsUndefined(v)) {
|
||||
if (has_content)
|
||||
string_buffer_putc8(jsc->b, ',');
|
||||
string_buffer_concat_value(jsc->b, sep);
|
||||
if (JS_ToQuotedString(ctx, jsc->b, prop)) {
|
||||
prop = JS_ToQuotedStringFree(ctx, prop);
|
||||
if (JS_IsException(prop)) {
|
||||
JS_FreeValue(ctx, v);
|
||||
goto exception;
|
||||
}
|
||||
string_buffer_concat_value(jsc->b, sep);
|
||||
string_buffer_concat_value(jsc->b, prop);
|
||||
string_buffer_putc8(jsc->b, ':');
|
||||
string_buffer_concat_value(jsc->b, sep1);
|
||||
if (js_json_to_str(ctx, jsc, val, v, indent1))
|
||||
@ -46913,7 +46887,10 @@ static int js_json_to_str(JSContext *ctx, JSONStringifyContext *jsc,
|
||||
switch (JS_VALUE_GET_NORM_TAG(val)) {
|
||||
case JS_TAG_STRING:
|
||||
case JS_TAG_STRING_ROPE:
|
||||
return JS_ToQuotedStringFree(ctx, jsc->b, val);
|
||||
val = JS_ToQuotedStringFree(ctx, val);
|
||||
if (JS_IsException(val))
|
||||
goto exception;
|
||||
goto concat_value;
|
||||
case JS_TAG_FLOAT64:
|
||||
if (!isfinite(JS_VALUE_GET_FLOAT64(val))) {
|
||||
val = JS_NULL;
|
||||
|
@ -86,7 +86,7 @@ function toStr(a)
|
||||
case "undefined":
|
||||
return "undefined";
|
||||
case "string":
|
||||
return JSON.stringify(a);
|
||||
return a.__quote();
|
||||
case "number":
|
||||
if (a == 0 && 1 / a < 0)
|
||||
return "-0";
|
||||
|
Loading…
x
Reference in New Issue
Block a user