@@ -1284,6 +1284,7 @@ static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen,
1284
1284
static JSValue js_create_array(JSContext *ctx, int len, JSValue *tab);
1285
1285
static bool js_get_fast_array(JSContext *ctx, JSValue obj,
1286
1286
JSValue **arrpp, uint32_t *countp);
1287
+ static int expand_fast_array(JSContext *ctx, JSObject *p, uint32_t new_len);
1287
1288
static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
1288
1289
JSValue sync_iter);
1289
1290
static void js_c_function_data_finalizer(JSRuntime *rt, JSValue val);
@@ -5043,6 +5044,28 @@ JSValue JS_NewArray(JSContext *ctx)
5043
5044
JS_CLASS_ARRAY);
5044
5045
}
5045
5046
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
+
5046
5069
JSValue JS_NewObject(JSContext *ctx)
5047
5070
{
5048
5071
/* inline JS_NewObjectClass(ctx, JS_CLASS_OBJECT); */
@@ -15355,23 +15378,12 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValue func_obj,
15355
15378
BREAK;
15356
15379
CASE(OP_array_from):
15357
15380
{
15358
- int i, ret;
15359
-
15360
15381
call_argc = get_u16(pc);
15361
15382
pc += 2;
15362
- ret_val = JS_NewArray(ctx);
15383
+ call_argv = sp - call_argc;
15384
+ ret_val = JS_NewArrayFrom(ctx, call_argc, call_argv);
15363
15385
if (unlikely(JS_IsException(ret_val)))
15364
15386
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
- }
15375
15387
sp -= call_argc;
15376
15388
*sp++ = ret_val;
15377
15389
}
0 commit comments