Compare commits

..

1 Commits

4 changed files with 106 additions and 132 deletions

View File

@ -1,5 +1,3 @@
2025-09-13:
- added JSON modules and import attributes - added JSON modules and import attributes
- added JS_PrintValue() API - added JS_PrintValue() API
- qjs: pretty print objects in print() and console.log() - qjs: pretty print objects in print() and console.log()
@ -14,7 +12,6 @@
accept JSON5 modules accept JSON5 modules
- added JS_FreePropertyEnum() and JS_AtomToCStringLen() API - added JS_FreePropertyEnum() and JS_AtomToCStringLen() API
- added Error.isError() - added Error.isError()
- misc bug fixes
2025-04-26: 2025-04-26:

View File

@ -1 +1 @@
2025-09-13 2025-04-26

231
quickjs.c
View File

@ -13002,6 +13002,73 @@ static JSValue JS_ToStringCheckObject(JSContext *ctx, JSValueConst val)
return JS_ToString(ctx, 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 #define JS_PRINT_MAX_DEPTH 8
typedef struct { 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_dump_char(JSPrintValueState *s, int c, int sep)
static void js_print_string1(JSPrintValueState *s, JSString *p, int len, int sep)
{ {
uint8_t buf[UTF8_CHAR_LEN_MAX]; if (c == sep || c == '\\') {
int l, i, c, c1; js_putc(s, '\\');
js_putc(s, c);
for(i = 0; i < len; i++) { } else if (c >= ' ' && c <= 126) {
c = string_get(p, i); js_putc(s, c);
switch(c) { } else if (c == '\n') {
case '\t': js_putc(s, '\\');
c = 't'; js_putc(s, 'n');
goto quote; } else {
case '\r': js_printf(s, "\\u%04x", c);
c = 'r';
goto quote;
case '\n':
c = 'n';
goto quote;
case '\b':
c = 'b';
goto quote;
case '\f':
c = 'f';
goto quote;
case '\\':
quote:
js_putc(s, '\\');
js_putc(s, c);
break;
default:
if (c == sep)
goto quote;
if (c >= 32 && c <= 126) {
js_putc(s, c);
} else if (c < 32 ||
(c >= 0x7f && c <= 0x9f)) {
escape:
js_printf(s, "\\u%04x", c);
} 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;
}
} }
} }
@ -13122,10 +13146,12 @@ static void js_print_string_rec(JSPrintValueState *s, JSValueConst val,
{ {
if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) { if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING) {
JSString *p = JS_VALUE_GET_STRING(val); JSString *p = JS_VALUE_GET_STRING(val);
uint32_t len; uint32_t i, len;
if (pos < s->options.max_string_length) { if (pos < s->options.max_string_length) {
len = min_uint32(p->len, s->options.max_string_length - pos); 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) { } else if (JS_VALUE_GET_TAG(val) == JS_TAG_STRING_ROPE) {
JSStringRope *r = JS_VALUE_GET_PTR(val); JSStringRope *r = JS_VALUE_GET_PTR(val);
@ -13198,7 +13224,9 @@ static void js_print_atom(JSPrintValueState *s, JSAtom atom)
} }
} else { } else {
js_putc(s, '"'); 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, '"'); js_putc(s, '"');
} }
} }
@ -34924,8 +34952,7 @@ static void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b)
JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name)); JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
} }
#endif #endif
if (b->byte_code_buf) free_bytecode_atoms(rt, b->byte_code_buf, b->byte_code_len, TRUE);
free_bytecode_atoms(rt, b->byte_code_buf, b->byte_code_len, TRUE);
if (b->vardefs) { if (b->vardefs) {
for(i = 0; i < b->arg_count + b->var_count; i++) { for(i = 0; i < b->arg_count + b->var_count; i++) {
@ -43832,6 +43859,12 @@ static JSValue js_string_trim(JSContext *ctx, JSValueConst this_val,
return ret; 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 */ /* return 0 if before the first char */
static int string_prevc(JSString *p, int *pidx) 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_ALIAS_DEF("trimLeft", "trimStart" ),
JS_CFUNC_DEF("toString", 0, js_string_toString ), JS_CFUNC_DEF("toString", 0, js_string_toString ),
JS_CFUNC_DEF("valueOf", 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("toLowerCase", 0, js_string_toLowerCase, 1 ),
JS_CFUNC_MAGIC_DEF("toUpperCase", 0, js_string_toLowerCase, 0 ), JS_CFUNC_MAGIC_DEF("toUpperCase", 0, js_string_toLowerCase, 0 ),
JS_CFUNC_MAGIC_DEF("toLocaleLowerCase", 0, js_string_toLowerCase, 1 ), JS_CFUNC_MAGIC_DEF("toLocaleLowerCase", 0, js_string_toLowerCase, 1 ),
@ -46633,72 +46667,10 @@ typedef struct JSONStringifyContext {
StringBuffer *b; StringBuffer *b;
} JSONStringifyContext; } JSONStringifyContext;
static int JS_ToQuotedString(JSContext *ctx, StringBuffer *b, JSValueConst val1) static JSValue JS_ToQuotedStringFree(JSContext *ctx, JSValue val) {
{ JSValue r = JS_ToQuotedString(ctx, val);
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;
JS_FreeValue(ctx, val); JS_FreeValue(ctx, val);
return 0; return r;
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;
} }
static JSValue js_json_check(JSContext *ctx, JSONStringifyContext *jsc, 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 (!JS_IsUndefined(v)) {
if (has_content) if (has_content)
string_buffer_putc8(jsc->b, ','); string_buffer_putc8(jsc->b, ',');
string_buffer_concat_value(jsc->b, sep); prop = JS_ToQuotedStringFree(ctx, prop);
if (JS_ToQuotedString(ctx, jsc->b, prop)) { if (JS_IsException(prop)) {
JS_FreeValue(ctx, v); JS_FreeValue(ctx, v);
goto exception; goto exception;
} }
string_buffer_concat_value(jsc->b, sep);
string_buffer_concat_value(jsc->b, prop);
string_buffer_putc8(jsc->b, ':'); string_buffer_putc8(jsc->b, ':');
string_buffer_concat_value(jsc->b, sep1); string_buffer_concat_value(jsc->b, sep1);
if (js_json_to_str(ctx, jsc, val, v, indent1)) 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)) { switch (JS_VALUE_GET_NORM_TAG(val)) {
case JS_TAG_STRING: case JS_TAG_STRING:
case JS_TAG_STRING_ROPE: 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: case JS_TAG_FLOAT64:
if (!isfinite(JS_VALUE_GET_FLOAT64(val))) { if (!isfinite(JS_VALUE_GET_FLOAT64(val))) {
val = JS_NULL; val = JS_NULL;

View File

@ -86,7 +86,7 @@ function toStr(a)
case "undefined": case "undefined":
return "undefined"; return "undefined";
case "string": case "string":
return JSON.stringify(a); return a.__quote();
case "number": case "number":
if (a == 0 && 1 / a < 0) if (a == 0 && 1 / a < 0)
return "-0"; return "-0";