@@ -1284,6 +1284,7 @@ static JSValue *build_arg_list(JSContext *ctx, uint32_t *plen,
12841284static JSValue js_create_array(JSContext *ctx, int len, JSValue *tab);
12851285static 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);
12871288static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
12881289 JSValue sync_iter);
12891290static 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+
50465069JSValue 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 }
0 commit comments