Skip to content

Commit b70c988

Browse files
authored
Simplify and optimize OP_rest (#869)
Implement it in terms of js_create_array. js_build_rest was basically duplicating its functionality.
1 parent 9712f77 commit b70c988

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

quickjs.c

+5-22
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ static __exception int js_set_length64(JSContext *ctx, JSValue obj,
12811281
static void free_arg_list(JSContext *ctx, JSValue *tab, uint32_t len);
12821282
static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen,
12831283
JSValue array_arg);
1284+
static JSValue js_create_array(JSContext *ctx, int len, JSValue *tab);
12841285
static bool js_get_fast_array(JSContext *ctx, JSValue obj,
12851286
JSValue **arrpp, uint32_t *countp);
12861287
static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
@@ -13713,26 +13714,6 @@ static JSValue js_build_mapped_arguments(JSContext *ctx, int argc,
1371313714
return JS_EXCEPTION;
1371413715
}
1371513716

13716-
static JSValue js_build_rest(JSContext *ctx, int first, int argc, JSValue *argv)
13717-
{
13718-
JSValue val;
13719-
int i, ret;
13720-
13721-
val = JS_NewArray(ctx);
13722-
if (JS_IsException(val))
13723-
return val;
13724-
for (i = first; i < argc; i++) {
13725-
ret = JS_DefinePropertyValueUint32(ctx, val, i - first,
13726-
js_dup(argv[i]),
13727-
JS_PROP_C_W_E);
13728-
if (ret < 0) {
13729-
JS_FreeValue(ctx, val);
13730-
return JS_EXCEPTION;
13731-
}
13732-
}
13733-
return val;
13734-
}
13735-
1373613717
static JSValue build_for_in_iterator(JSContext *ctx, JSValue obj)
1373713718
{
1373813719
JSObject *p;
@@ -15148,9 +15129,11 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
1514815129
BREAK;
1514915130
CASE(OP_rest):
1515015131
{
15151-
int first = get_u16(pc);
15132+
int i, n, first = get_u16(pc);
1515215133
pc += 2;
15153-
*sp++ = js_build_rest(ctx, first, argc, argv);
15134+
i = min_int(first, argc);
15135+
n = argc - i;
15136+
*sp++ = js_create_array(ctx, n, &argv[i]);
1515415137
if (unlikely(JS_IsException(sp[-1])))
1515515138
goto exception;
1515615139
}

0 commit comments

Comments
 (0)