Index: src/runtime/runtime-classes.cc |
diff --git a/src/runtime/runtime-classes.cc b/src/runtime/runtime-classes.cc |
index aafd7779870f74cbd9bc2e5380b79f4842e8638b..5789a848c4f9c9c14c4d7162582de6d032aff686 100644 |
--- a/src/runtime/runtime-classes.cc |
+++ b/src/runtime/runtime-classes.cc |
@@ -388,5 +388,57 @@ RUNTIME_FUNCTION(Runtime_GetSuperConstructor) { |
return active_function->map()->prototype(); |
} |
+RUNTIME_FUNCTION(Runtime_ConstructWithSpread) { |
+ HandleScope scope(isolate); |
+ DCHECK_LE(3, args.length()); |
+ CONVERT_ARG_HANDLE_CHECKED(JSReceiver, constructor, 0); |
+ CONVERT_ARG_HANDLE_CHECKED(Object, new_target, 1); |
+ |
+ int constructor_argc = args.length() - 2; |
+ CONVERT_ARG_HANDLE_CHECKED(Object, spread, args.length() - 1); |
+ |
+ // Iterate over the spread if we need to. |
+ if (spread->MustIterate()) { |
+ Handle<JSFunction> spread_iterable_function = isolate->spread_iterable(); |
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
+ isolate, spread, |
+ Execution::Call(isolate, spread_iterable_function, |
+ isolate->factory()->undefined_value(), 1, &spread)); |
+ } |
+ |
+ Handle<JSArray> spread_array = Handle<JSArray>::cast(spread); |
+ if (constructor_argc > 1) { |
+ uint32_t spread_length; |
+ CHECK(spread_array->length()->ToArrayIndex(&spread_length)); |
+ |
+ // Append each of the individual args to the result. |
+ int result_length = constructor_argc - 1 + spread_length; |
+ Handle<FixedArray> args_array = |
+ isolate->factory()->NewFixedArray(result_length); |
+ for (int i = 0; i < constructor_argc - 1; i++) { |
+ args_array->set(i, *args.at<Object>(2 + i)); |
+ } |
+ |
+ // Append element of the spread to the result. |
+ for (uint32_t i = 0; i < spread_length; i++) { |
+ LookupIterator it(isolate, spread, i); |
+ Handle<Object> element = spread_array->GetDataProperty(&it); |
+ args_array->set(constructor_argc - 1 + i, *element); |
+ } |
+ |
+ spread_array = isolate->factory()->NewJSArrayWithElements( |
+ args_array, FAST_ELEMENTS, result_length); |
+ } |
+ |
+ // Call ReflectConstruct to do the actual super constructor call. |
Benedikt Meurer
2016/12/05 11:42:10
It'd be nice to use Execution::New directly here,
petermarshall
2016/12/05 13:30:46
That is nice, I think we avoid allocating that JS
|
+ Handle<Object> construct_args[] = {constructor, spread_array, new_target}; |
+ Handle<Object> result; |
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
+ isolate, result, Execution::Call(isolate, isolate->reflect_construct(), |
+ isolate->factory()->undefined_value(), 3, |
+ construct_args)); |
+ return *result; |
+} |
+ |
} // namespace internal |
} // namespace v8 |