// Copyright 2016 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/builtins/builtins-utils.h" #include "src/builtins/builtins.h" #include "src/code-factory.h" #include "src/code-stub-assembler.h" #include "src/compiler.h" #include "src/conversions.h" #include "src/counters.h" #include "src/lookup.h" #include "src/objects-inl.h" #include "src/string-builder.h" namespace v8 { namespace internal { namespace { // ES6 section 19.2.1.1.1 CreateDynamicFunction MaybeHandle CreateDynamicFunction(Isolate* isolate, BuiltinArguments args, const char* token) { // Compute number of arguments, ignoring the receiver. DCHECK_LE(1, args.length()); int const argc = args.length() - 1; Handle target = args.target(); Handle target_global_proxy(target->global_proxy(), isolate); if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) { isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined); return isolate->factory()->undefined_value(); } // Build the source string. Handle source; int parameters_end_pos = kNoSourcePosition; { IncrementalStringBuilder builder(isolate); builder.AppendCharacter('('); builder.AppendCString(token); if (FLAG_harmony_function_tostring) { builder.AppendCString(" anonymous("); } else { builder.AppendCharacter('('); } bool parenthesis_in_arg_string = false; if (argc > 1) { for (int i = 1; i < argc; ++i) { if (i > 1) builder.AppendCharacter(','); Handle param; ASSIGN_RETURN_ON_EXCEPTION( isolate, param, Object::ToString(isolate, args.at(i)), Object); param = String::Flatten(param); builder.AppendString(param); if (!FLAG_harmony_function_tostring) { // If the formal parameters string include ) - an illegal // character - it may make the combined function expression // compile. We avoid this problem by checking for this early on. DisallowHeapAllocation no_gc; // Ensure vectors stay valid. String::FlatContent param_content = param->GetFlatContent(); for (int i = 0, length = param->length(); i < length; ++i) { if (param_content.Get(i) == ')') { parenthesis_in_arg_string = true; break; } } } } if (!FLAG_harmony_function_tostring) { // If the formal parameters include an unbalanced block comment, the // function must be rejected. Since JavaScript does not allow nested // comments we can include a trailing block comment to catch this. builder.AppendCString("\n/*``*/"); } } if (FLAG_harmony_function_tostring) { builder.AppendCharacter('\n'); parameters_end_pos = builder.Length(); } builder.AppendCString(") {\n"); if (argc > 0) { Handle body; ASSIGN_RETURN_ON_EXCEPTION( isolate, body, Object::ToString(isolate, args.at(argc)), Object); builder.AppendString(body); } builder.AppendCString("\n})"); ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object); // The SyntaxError must be thrown after all the (observable) ToString // conversions are done. if (parenthesis_in_arg_string) { THROW_NEW_ERROR(isolate, NewSyntaxError(MessageTemplate::kParenthesisInArgString), Object); } } // Compile the string in the constructor and not a helper so that errors to // come from here. Handle function; { ASSIGN_RETURN_ON_EXCEPTION( isolate, function, Compiler::GetFunctionFromString( handle(target->native_context(), isolate), source, ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos), Object); Handle result; ASSIGN_RETURN_ON_EXCEPTION( isolate, result, Execution::Call(isolate, function, target_global_proxy, 0, nullptr), Object); function = Handle::cast(result); function->shared()->set_name_should_print_as_anonymous(true); } // If new.target is equal to target then the function created // is already correctly setup and nothing else should be done // here. But if new.target is not equal to target then we are // have a Function builtin subclassing case and therefore the // function has wrong initial map. To fix that we create a new // function object with correct initial map. Handle unchecked_new_target = args.new_target(); if (!unchecked_new_target->IsUndefined(isolate) && !unchecked_new_target.is_identical_to(target)) { Handle new_target = Handle::cast(unchecked_new_target); Handle initial_map; ASSIGN_RETURN_ON_EXCEPTION( isolate, initial_map, JSFunction::GetDerivedMap(isolate, target, new_target), Object); Handle shared_info(function->shared(), isolate); Handle map = Map::AsLanguageMode( initial_map, shared_info->language_mode(), shared_info->kind()); Handle context(function->context(), isolate); function = isolate->factory()->NewFunctionFromSharedFunctionInfo( map, shared_info, context, NOT_TENURED); } return function; } } // namespace // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) BUILTIN(FunctionConstructor) { HandleScope scope(isolate); Handle result; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, result, CreateDynamicFunction(isolate, args, "function")); return *result; } // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) BUILTIN(GeneratorFunctionConstructor) { HandleScope scope(isolate); RETURN_RESULT_OR_FAILURE(isolate, CreateDynamicFunction(isolate, args, "function*")); } BUILTIN(AsyncFunctionConstructor) { HandleScope scope(isolate); Handle maybe_func; ASSIGN_RETURN_FAILURE_ON_EXCEPTION( isolate, maybe_func, CreateDynamicFunction(isolate, args, "async function")); if (!maybe_func->IsJSFunction()) return *maybe_func; // Do not lazily compute eval position for AsyncFunction, as they may not be // determined after the function is resumed. Handle func = Handle::cast(maybe_func); Handle