Skip to content

Commit 36ad181

Browse files
committed
* string allocation default increased from 32 to 48
* free_token is general Javascript deallocation, json_free_token creates a special case with reduced evaluation
1 parent e0746cc commit 36ad181

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

quickjs.c

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4360,6 +4360,14 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
43604360
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, (JSString *)ptr - 1));
43614361
}
43624362

4363+
void JS_FreeCStringRT(JSRuntime *rt, const char *ptr)
4364+
{
4365+
if (!ptr)
4366+
return;
4367+
/* purposely removing constness */
4368+
JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_STRING, (JSString *)ptr - 1));
4369+
}
4370+
43634371
static int memcmp16_8(const uint16_t *src1, const uint8_t *src2, int len)
43644372
{
43654373
int c, i;
@@ -20177,7 +20185,7 @@ typedef struct JSFunctionDef {
2017720185
bool need_home_object : 1;
2017820186
bool use_short_opcodes : 1; /* true if short opcodes are used in byte_code */
2017920187
bool has_await : 1; /* true if await is used (used in module eval) */
20180-
20188+
2018120189
JSFunctionKindEnum func_kind : 8;
2018220190
JSParseFunctionEnum func_type : 7;
2018320191
uint8_t is_strict_mode : 1;
@@ -20336,6 +20344,21 @@ static const JSOpCode opcode_info[OP_COUNT + (OP_TEMP_END - OP_TEMP_START)] = {
2033620344
opcode_info[(op) >= OP_TEMP_START ? \
2033720345
(op) + (OP_TEMP_END - OP_TEMP_START) : (op)]
2033820346

20347+
static void json_free_token(JSParseState *s, JSToken *token) {
20348+
// Only free actual allocated values
20349+
switch(token->val) {
20350+
case TOK_NUMBER:
20351+
JS_FreeValue(s->ctx, token->u.num.val);
20352+
break;
20353+
case TOK_STRING:
20354+
JS_FreeValue(s->ctx, token->u.str.str);
20355+
break;
20356+
case TOK_IDENT:
20357+
JS_FreeAtom(s->ctx, token->u.ident.atom);
20358+
break;
20359+
}
20360+
}
20361+
2033920362
static void free_token(JSParseState *s, JSToken *token)
2034020363
{
2034120364
switch(token->val) {
@@ -21417,14 +21440,31 @@ static int json_parse_string(JSParseState *s, const uint8_t **pp)
2141721440
uint32_t c;
2141821441
StringBuffer b_s, *b = &b_s;
2141921442

21420-
if (string_buffer_init(s->ctx, b, 32))
21443+
if (string_buffer_init(s->ctx, b, 48))
2142121444
goto fail;
2142221445

2142321446
p = *pp;
2142421447
for(;;) {
2142521448
if (p >= s->buf_end) {
2142621449
goto end_of_input;
2142721450
}
21451+
21452+
// Fast path: batch consecutive ASCII characters
21453+
const uint8_t *p_start = p;
21454+
while (p < s->buf_end && *p != '"' && *p != '\\' && *p >= 0x20 && *p < 0x80) {
21455+
p++;
21456+
}
21457+
21458+
// Write batched ASCII in one call
21459+
if (p > p_start) {
21460+
if (string_buffer_write8(b, p_start, p - p_start))
21461+
goto fail;
21462+
}
21463+
21464+
if (p >= s->buf_end) {
21465+
goto end_of_input;
21466+
}
21467+
2142821468
c = *p++;
2142921469
if (c == '"')
2143021470
break;
@@ -21570,7 +21610,7 @@ static __exception int json_next_token(JSParseState *s)
2157021610
return -1;
2157121611
}
2157221612

21573-
free_token(s, &s->token);
21613+
json_free_token(s, &s->token);
2157421614

2157521615
p = s->last_ptr = s->buf_ptr;
2157621616
s->last_line_num = s->token.line_num;
@@ -48695,23 +48735,19 @@ static const JSClassExoticMethods js_proxy_exotic_methods = {
4869548735
.set_property = js_proxy_set,
4869648736
};
4869748737

48698-
static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
48699-
int argc, JSValueConst *argv)
48738+
JSValue JS_NewProxy(JSContext *ctx, JSValueConst target, JSValueConst handler)
4870048739
{
48701-
JSValueConst target, handler;
48702-
JSValue obj;
4870348740
JSProxyData *s;
48741+
JSValue obj;
4870448742

48705-
target = argv[0];
48706-
handler = argv[1];
4870748743
if (JS_VALUE_GET_TAG(target) != JS_TAG_OBJECT ||
48708-
JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT)
48744+
JS_VALUE_GET_TAG(handler) != JS_TAG_OBJECT) {
4870948745
return JS_ThrowTypeErrorNotAnObject(ctx);
48710-
48746+
}
4871148747
obj = JS_NewObjectProtoClass(ctx, JS_NULL, JS_CLASS_PROXY);
4871248748
if (JS_IsException(obj))
4871348749
return obj;
48714-
s = js_malloc(ctx, sizeof(JSProxyData));
48750+
s = js_malloc(ctx, sizeof(*s));
4871548751
if (!s) {
4871648752
JS_FreeValue(ctx, obj);
4871748753
return JS_EXCEPTION;
@@ -48725,6 +48761,12 @@ static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
4872548761
return obj;
4872648762
}
4872748763

48764+
static JSValue js_proxy_constructor(JSContext *ctx, JSValueConst this_val,
48765+
int argc, JSValueConst *argv)
48766+
{
48767+
return JS_NewProxy(ctx, argv[0], argv[1]);
48768+
}
48769+
4872848770
static JSValue js_proxy_revoke(JSContext *ctx, JSValueConst this_val,
4872948771
int argc, JSValueConst *argv, int magic,
4873048772
JSValueConst *func_data)
@@ -50548,7 +50590,7 @@ JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValueConst promise)
5054850590
{
5054950591
JSPromiseData *s = JS_GetOpaque(promise, JS_CLASS_PROMISE);
5055050592
if (!s)
50551-
return -1;
50593+
return JS_PROMISE_NOT_A_PROMISE;
5055250594
return s->promise_state;
5055350595
}
5055450596

0 commit comments

Comments
 (0)