OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/builtins/builtins.h" | 5 #include "src/builtins/builtins.h" |
6 | 6 |
7 #include "src/api-arguments.h" | 7 #include "src/api-arguments.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/base/ieee754.h" | 10 #include "src/base/ieee754.h" |
(...skipping 4597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4608 } | 4608 } |
4609 | 4609 |
4610 // static | 4610 // static |
4611 void Builtins::Generate_DatePrototypeGetUTCSeconds(MacroAssembler* masm) { | 4611 void Builtins::Generate_DatePrototypeGetUTCSeconds(MacroAssembler* masm) { |
4612 Generate_DatePrototype_GetField(masm, JSDate::kSecondUTC); | 4612 Generate_DatePrototype_GetField(masm, JSDate::kSecondUTC); |
4613 } | 4613 } |
4614 | 4614 |
4615 namespace { | 4615 namespace { |
4616 | 4616 |
4617 // ES6 section 19.2.1.1.1 CreateDynamicFunction | 4617 // ES6 section 19.2.1.1.1 CreateDynamicFunction |
4618 MaybeHandle<JSFunction> CreateDynamicFunction(Isolate* isolate, | 4618 MaybeHandle<Object> CreateDynamicFunction(Isolate* isolate, |
4619 BuiltinArguments args, | 4619 BuiltinArguments args, |
4620 const char* token) { | 4620 const char* token) { |
4621 // Compute number of arguments, ignoring the receiver. | 4621 // Compute number of arguments, ignoring the receiver. |
4622 DCHECK_LE(1, args.length()); | 4622 DCHECK_LE(1, args.length()); |
4623 int const argc = args.length() - 1; | 4623 int const argc = args.length() - 1; |
4624 | 4624 |
| 4625 Handle<JSFunction> target = args.target<JSFunction>(); |
| 4626 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); |
| 4627 |
| 4628 HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 4629 if (!FLAG_allow_unsafe_function_constructor && |
| 4630 !impl->LastEnteredContext().is_null() && |
| 4631 *impl->LastEnteredContext() != target->context() && |
| 4632 !isolate->MayAccess(impl->LastEnteredContext(), target_global_proxy)) { |
| 4633 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined); |
| 4634 return isolate->factory()->undefined_value(); |
| 4635 } |
| 4636 |
4625 // Build the source string. | 4637 // Build the source string. |
4626 Handle<String> source; | 4638 Handle<String> source; |
4627 { | 4639 { |
4628 IncrementalStringBuilder builder(isolate); | 4640 IncrementalStringBuilder builder(isolate); |
4629 builder.AppendCharacter('('); | 4641 builder.AppendCharacter('('); |
4630 builder.AppendCString(token); | 4642 builder.AppendCString(token); |
4631 builder.AppendCharacter('('); | 4643 builder.AppendCharacter('('); |
4632 bool parenthesis_in_arg_string = false; | 4644 bool parenthesis_in_arg_string = false; |
4633 if (argc > 1) { | 4645 if (argc > 1) { |
4634 for (int i = 1; i < argc; ++i) { | 4646 for (int i = 1; i < argc; ++i) { |
4635 if (i > 1) builder.AppendCharacter(','); | 4647 if (i > 1) builder.AppendCharacter(','); |
4636 Handle<String> param; | 4648 Handle<String> param; |
4637 ASSIGN_RETURN_ON_EXCEPTION( | 4649 ASSIGN_RETURN_ON_EXCEPTION( |
4638 isolate, param, Object::ToString(isolate, args.at<Object>(i)), | 4650 isolate, param, Object::ToString(isolate, args.at<Object>(i)), |
4639 JSFunction); | 4651 Object); |
4640 param = String::Flatten(param); | 4652 param = String::Flatten(param); |
4641 builder.AppendString(param); | 4653 builder.AppendString(param); |
4642 // If the formal parameters string include ) - an illegal | 4654 // If the formal parameters string include ) - an illegal |
4643 // character - it may make the combined function expression | 4655 // character - it may make the combined function expression |
4644 // compile. We avoid this problem by checking for this early on. | 4656 // compile. We avoid this problem by checking for this early on. |
4645 DisallowHeapAllocation no_gc; // Ensure vectors stay valid. | 4657 DisallowHeapAllocation no_gc; // Ensure vectors stay valid. |
4646 String::FlatContent param_content = param->GetFlatContent(); | 4658 String::FlatContent param_content = param->GetFlatContent(); |
4647 for (int i = 0, length = param->length(); i < length; ++i) { | 4659 for (int i = 0, length = param->length(); i < length; ++i) { |
4648 if (param_content.Get(i) == ')') { | 4660 if (param_content.Get(i) == ')') { |
4649 parenthesis_in_arg_string = true; | 4661 parenthesis_in_arg_string = true; |
4650 break; | 4662 break; |
4651 } | 4663 } |
4652 } | 4664 } |
4653 } | 4665 } |
4654 // If the formal parameters include an unbalanced block comment, the | 4666 // If the formal parameters include an unbalanced block comment, the |
4655 // function must be rejected. Since JavaScript does not allow nested | 4667 // function must be rejected. Since JavaScript does not allow nested |
4656 // comments we can include a trailing block comment to catch this. | 4668 // comments we can include a trailing block comment to catch this. |
4657 builder.AppendCString("\n/**/"); | 4669 builder.AppendCString("\n/**/"); |
4658 } | 4670 } |
4659 builder.AppendCString(") {\n"); | 4671 builder.AppendCString(") {\n"); |
4660 if (argc > 0) { | 4672 if (argc > 0) { |
4661 Handle<String> body; | 4673 Handle<String> body; |
4662 ASSIGN_RETURN_ON_EXCEPTION( | 4674 ASSIGN_RETURN_ON_EXCEPTION( |
4663 isolate, body, Object::ToString(isolate, args.at<Object>(argc)), | 4675 isolate, body, Object::ToString(isolate, args.at<Object>(argc)), |
4664 JSFunction); | 4676 Object); |
4665 builder.AppendString(body); | 4677 builder.AppendString(body); |
4666 } | 4678 } |
4667 builder.AppendCString("\n})"); | 4679 builder.AppendCString("\n})"); |
4668 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), JSFunction); | 4680 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object); |
4669 | 4681 |
4670 // The SyntaxError must be thrown after all the (observable) ToString | 4682 // The SyntaxError must be thrown after all the (observable) ToString |
4671 // conversions are done. | 4683 // conversions are done. |
4672 if (parenthesis_in_arg_string) { | 4684 if (parenthesis_in_arg_string) { |
4673 THROW_NEW_ERROR(isolate, | 4685 THROW_NEW_ERROR(isolate, |
4674 NewSyntaxError(MessageTemplate::kParenthesisInArgString), | 4686 NewSyntaxError(MessageTemplate::kParenthesisInArgString), |
4675 JSFunction); | 4687 Object); |
4676 } | 4688 } |
4677 } | 4689 } |
4678 | 4690 |
4679 // Compile the string in the constructor and not a helper so that errors to | 4691 // Compile the string in the constructor and not a helper so that errors to |
4680 // come from here. | 4692 // come from here. |
4681 Handle<JSFunction> target = args.target<JSFunction>(); | |
4682 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); | |
4683 Handle<JSFunction> function; | 4693 Handle<JSFunction> function; |
4684 { | 4694 { |
4685 ASSIGN_RETURN_ON_EXCEPTION( | 4695 ASSIGN_RETURN_ON_EXCEPTION( |
4686 isolate, function, | 4696 isolate, function, |
4687 CompileString(handle(target->native_context(), isolate), source, | 4697 CompileString(handle(target->native_context(), isolate), source, |
4688 ONLY_SINGLE_FUNCTION_LITERAL), | 4698 ONLY_SINGLE_FUNCTION_LITERAL), |
4689 JSFunction); | 4699 Object); |
4690 Handle<Object> result; | 4700 Handle<Object> result; |
4691 ASSIGN_RETURN_ON_EXCEPTION( | 4701 ASSIGN_RETURN_ON_EXCEPTION( |
4692 isolate, result, | 4702 isolate, result, |
4693 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), | 4703 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), |
4694 JSFunction); | 4704 Object); |
4695 function = Handle<JSFunction>::cast(result); | 4705 function = Handle<JSFunction>::cast(result); |
4696 function->shared()->set_name_should_print_as_anonymous(true); | 4706 function->shared()->set_name_should_print_as_anonymous(true); |
4697 } | 4707 } |
4698 | 4708 |
4699 // If new.target is equal to target then the function created | 4709 // If new.target is equal to target then the function created |
4700 // is already correctly setup and nothing else should be done | 4710 // is already correctly setup and nothing else should be done |
4701 // here. But if new.target is not equal to target then we are | 4711 // here. But if new.target is not equal to target then we are |
4702 // have a Function builtin subclassing case and therefore the | 4712 // have a Function builtin subclassing case and therefore the |
4703 // function has wrong initial map. To fix that we create a new | 4713 // function has wrong initial map. To fix that we create a new |
4704 // function object with correct initial map. | 4714 // function object with correct initial map. |
4705 Handle<Object> unchecked_new_target = args.new_target(); | 4715 Handle<Object> unchecked_new_target = args.new_target(); |
4706 if (!unchecked_new_target->IsUndefined(isolate) && | 4716 if (!unchecked_new_target->IsUndefined(isolate) && |
4707 !unchecked_new_target.is_identical_to(target)) { | 4717 !unchecked_new_target.is_identical_to(target)) { |
4708 Handle<JSReceiver> new_target = | 4718 Handle<JSReceiver> new_target = |
4709 Handle<JSReceiver>::cast(unchecked_new_target); | 4719 Handle<JSReceiver>::cast(unchecked_new_target); |
4710 Handle<Map> initial_map; | 4720 Handle<Map> initial_map; |
4711 ASSIGN_RETURN_ON_EXCEPTION( | 4721 ASSIGN_RETURN_ON_EXCEPTION( |
4712 isolate, initial_map, | 4722 isolate, initial_map, |
4713 JSFunction::GetDerivedMap(isolate, target, new_target), JSFunction); | 4723 JSFunction::GetDerivedMap(isolate, target, new_target), Object); |
4714 | 4724 |
4715 Handle<SharedFunctionInfo> shared_info(function->shared(), isolate); | 4725 Handle<SharedFunctionInfo> shared_info(function->shared(), isolate); |
4716 Handle<Map> map = Map::AsLanguageMode( | 4726 Handle<Map> map = Map::AsLanguageMode( |
4717 initial_map, shared_info->language_mode(), shared_info->kind()); | 4727 initial_map, shared_info->language_mode(), shared_info->kind()); |
4718 | 4728 |
4719 Handle<Context> context(function->context(), isolate); | 4729 Handle<Context> context(function->context(), isolate); |
4720 function = isolate->factory()->NewFunctionFromSharedFunctionInfo( | 4730 function = isolate->factory()->NewFunctionFromSharedFunctionInfo( |
4721 map, shared_info, context, NOT_TENURED); | 4731 map, shared_info, context, NOT_TENURED); |
4722 } | 4732 } |
4723 return function; | 4733 return function; |
4724 } | 4734 } |
4725 | 4735 |
4726 } // namespace | 4736 } // namespace |
4727 | 4737 |
4728 // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) | 4738 // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) |
4729 BUILTIN(FunctionConstructor) { | 4739 BUILTIN(FunctionConstructor) { |
4730 HandleScope scope(isolate); | 4740 HandleScope scope(isolate); |
4731 Handle<JSFunction> result; | 4741 Handle<Object> result; |
4732 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 4742 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
4733 isolate, result, CreateDynamicFunction(isolate, args, "function")); | 4743 isolate, result, CreateDynamicFunction(isolate, args, "function")); |
4734 return *result; | 4744 return *result; |
4735 } | 4745 } |
4736 | 4746 |
4737 namespace { | 4747 namespace { |
4738 | 4748 |
4739 Object* DoFunctionBind(Isolate* isolate, BuiltinArguments args) { | 4749 Object* DoFunctionBind(Isolate* isolate, BuiltinArguments args) { |
4740 HandleScope scope(isolate); | 4750 HandleScope scope(isolate); |
4741 DCHECK_LE(1, args.length()); | 4751 DCHECK_LE(1, args.length()); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4853 | 4863 |
4854 // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) | 4864 // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) |
4855 BUILTIN(GeneratorFunctionConstructor) { | 4865 BUILTIN(GeneratorFunctionConstructor) { |
4856 HandleScope scope(isolate); | 4866 HandleScope scope(isolate); |
4857 RETURN_RESULT_OR_FAILURE(isolate, | 4867 RETURN_RESULT_OR_FAILURE(isolate, |
4858 CreateDynamicFunction(isolate, args, "function*")); | 4868 CreateDynamicFunction(isolate, args, "function*")); |
4859 } | 4869 } |
4860 | 4870 |
4861 BUILTIN(AsyncFunctionConstructor) { | 4871 BUILTIN(AsyncFunctionConstructor) { |
4862 HandleScope scope(isolate); | 4872 HandleScope scope(isolate); |
4863 Handle<JSFunction> func; | 4873 Handle<Object> maybe_func; |
4864 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 4874 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
4865 isolate, func, CreateDynamicFunction(isolate, args, "async function")); | 4875 isolate, maybe_func, |
| 4876 CreateDynamicFunction(isolate, args, "async function")); |
| 4877 if (!maybe_func->IsJSFunction()) return *maybe_func; |
4866 | 4878 |
4867 // Do not lazily compute eval position for AsyncFunction, as they may not be | 4879 // Do not lazily compute eval position for AsyncFunction, as they may not be |
4868 // determined after the function is resumed. | 4880 // determined after the function is resumed. |
| 4881 Handle<JSFunction> func = Handle<JSFunction>::cast(maybe_func); |
4869 Handle<Script> script = handle(Script::cast(func->shared()->script())); | 4882 Handle<Script> script = handle(Script::cast(func->shared()->script())); |
4870 int position = script->GetEvalPosition(); | 4883 int position = script->GetEvalPosition(); |
4871 USE(position); | 4884 USE(position); |
4872 | 4885 |
4873 return *func; | 4886 return *func; |
4874 } | 4887 } |
4875 | 4888 |
4876 // ----------------------------------------------------------------------------- | 4889 // ----------------------------------------------------------------------------- |
4877 // ES6 section 19.1 Object Objects | 4890 // ES6 section 19.1 Object Objects |
4878 | 4891 |
(...skipping 1783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6662 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 6675 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
6663 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 6676 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
6664 #undef DEFINE_BUILTIN_ACCESSOR_C | 6677 #undef DEFINE_BUILTIN_ACCESSOR_C |
6665 #undef DEFINE_BUILTIN_ACCESSOR_A | 6678 #undef DEFINE_BUILTIN_ACCESSOR_A |
6666 #undef DEFINE_BUILTIN_ACCESSOR_T | 6679 #undef DEFINE_BUILTIN_ACCESSOR_T |
6667 #undef DEFINE_BUILTIN_ACCESSOR_S | 6680 #undef DEFINE_BUILTIN_ACCESSOR_S |
6668 #undef DEFINE_BUILTIN_ACCESSOR_H | 6681 #undef DEFINE_BUILTIN_ACCESSOR_H |
6669 | 6682 |
6670 } // namespace internal | 6683 } // namespace internal |
6671 } // namespace v8 | 6684 } // namespace v8 |
OLD | NEW |