Skip to content

Commit 8f671bc

Browse files
authored
Add JS_NewArrayFrom (#870)
Add a version of JS_NewArray that takes the initial elements as its argument. Change the OP_array_from bytecode handler to use it, to show that it works. Refs: #868
1 parent b70c988 commit 8f671bc

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

quickjs.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen,
12841284
static JSValue js_create_array(JSContext *ctx, int len, JSValue *tab);
12851285
static bool js_get_fast_array(JSContext *ctx, JSValue obj,
12861286
JSValue **arrpp, uint32_t *countp);
1287+
static int expand_fast_array(JSContext *ctx, JSObject *p, uint32_t new_len);
12871288
static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
12881289
JSValue sync_iter);
12891290
static void js_c_function_data_finalizer(JSRuntime *rt, JSValue val);
@@ -5043,6 +5044,28 @@ JSValue JS_NewArray(JSContext *ctx)
50435044
JS_CLASS_ARRAY);
50445045
}
50455046

5047+
// note: takes ownership of |values|, unlike js_create_array
5048+
JSValue JS_NewArrayFrom(JSContext *ctx, int count, const JSValue *values)
5049+
{
5050+
JSObject *p;
5051+
JSValue obj;
5052+
5053+
obj = JS_NewArray(ctx);
5054+
if (JS_IsException(obj))
5055+
return JS_EXCEPTION;
5056+
if (count > 0) {
5057+
p = JS_VALUE_GET_OBJ(obj);
5058+
if (expand_fast_array(ctx, p, count)) {
5059+
JS_FreeValue(ctx, obj);
5060+
return JS_EXCEPTION;
5061+
}
5062+
p->u.array.count = count;
5063+
p->prop[0].u.value = js_int32(count);
5064+
memcpy(p->u.array.u.values, values, count * sizeof(*values));
5065+
}
5066+
return obj;
5067+
}
5068+
50465069
JSValue JS_NewObject(JSContext *ctx)
50475070
{
50485071
/* inline JS_NewObjectClass(ctx, JS_CLASS_OBJECT); */
@@ -15355,23 +15378,12 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
1535515378
BREAK;
1535615379
CASE(OP_array_from):
1535715380
{
15358-
int i, ret;
15359-
1536015381
call_argc = get_u16(pc);
1536115382
pc += 2;
15362-
ret_val = JS_NewArray(ctx);
15383+
call_argv = sp - call_argc;
15384+
ret_val = JS_NewArrayFrom(ctx, call_argc, call_argv);
1536315385
if (unlikely(JS_IsException(ret_val)))
1536415386
goto exception;
15365-
call_argv = sp - call_argc;
15366-
for(i = 0; i < call_argc; i++) {
15367-
ret = JS_DefinePropertyValue(ctx, ret_val, __JS_AtomFromUInt32(i), call_argv[i],
15368-
JS_PROP_C_W_E | JS_PROP_THROW);
15369-
call_argv[i] = JS_UNDEFINED;
15370-
if (ret < 0) {
15371-
JS_FreeValue(ctx, ret_val);
15372-
goto exception;
15373-
}
15374-
}
1537515387
sp -= call_argc;
1537615388
*sp++ = ret_val;
1537715389
}

quickjs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ JS_EXTERN bool JS_IsRegExp(JSValue val);
728728
JS_EXTERN bool JS_IsMap(JSValue val);
729729

730730
JS_EXTERN JSValue JS_NewArray(JSContext *ctx);
731+
// takes ownership of the values
732+
JS_EXTERN JSValue JS_NewArrayFrom(JSContext *ctx, int count,
733+
const JSValue *values);
731734
JS_EXTERN int JS_IsArray(JSContext *ctx, JSValue val);
732735

733736
JS_EXTERN JSValue JS_NewDate(JSContext *ctx, double epoch_ms);

0 commit comments

Comments
 (0)