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.h" | 5 #include "src/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 4705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4716 | 4716 |
4717 // static | 4717 // static |
4718 void Builtins::Generate_DatePrototypeGetUTCSeconds(MacroAssembler* masm) { | 4718 void Builtins::Generate_DatePrototypeGetUTCSeconds(MacroAssembler* masm) { |
4719 Generate_DatePrototype_GetField(masm, JSDate::kSecondUTC); | 4719 Generate_DatePrototype_GetField(masm, JSDate::kSecondUTC); |
4720 } | 4720 } |
4721 | 4721 |
4722 | 4722 |
4723 namespace { | 4723 namespace { |
4724 | 4724 |
4725 // ES6 section 19.2.1.1.1 CreateDynamicFunction | 4725 // ES6 section 19.2.1.1.1 CreateDynamicFunction |
4726 MaybeHandle<JSFunction> CreateDynamicFunction(Isolate* isolate, | 4726 MaybeHandle<Object> CreateDynamicFunction(Isolate* isolate, |
4727 BuiltinArguments args, | 4727 BuiltinArguments args, |
4728 const char* token) { | 4728 const char* token) { |
4729 // Compute number of arguments, ignoring the receiver. | 4729 // Compute number of arguments, ignoring the receiver. |
4730 DCHECK_LE(1, args.length()); | 4730 DCHECK_LE(1, args.length()); |
4731 int const argc = args.length() - 1; | 4731 int const argc = args.length() - 1; |
4732 | 4732 |
| 4733 Handle<JSFunction> target = args.target<JSFunction>(); |
| 4734 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); |
| 4735 |
| 4736 HandleScopeImplementer* impl = isolate->handle_scope_implementer(); |
| 4737 if (!FLAG_allow_unsafe_function_constructor && |
| 4738 !isolate->MayAccess(impl->LastEnteredContext(), target_global_proxy)) { |
| 4739 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined); |
| 4740 return isolate->factory()->undefined_value(); |
| 4741 } |
| 4742 |
4733 // Build the source string. | 4743 // Build the source string. |
4734 Handle<String> source; | 4744 Handle<String> source; |
4735 { | 4745 { |
4736 IncrementalStringBuilder builder(isolate); | 4746 IncrementalStringBuilder builder(isolate); |
4737 builder.AppendCharacter('('); | 4747 builder.AppendCharacter('('); |
4738 builder.AppendCString(token); | 4748 builder.AppendCString(token); |
4739 builder.AppendCharacter('('); | 4749 builder.AppendCharacter('('); |
4740 bool parenthesis_in_arg_string = false; | 4750 bool parenthesis_in_arg_string = false; |
4741 if (argc > 1) { | 4751 if (argc > 1) { |
4742 for (int i = 1; i < argc; ++i) { | 4752 for (int i = 1; i < argc; ++i) { |
4743 if (i > 1) builder.AppendCharacter(','); | 4753 if (i > 1) builder.AppendCharacter(','); |
4744 Handle<String> param; | 4754 Handle<String> param; |
4745 ASSIGN_RETURN_ON_EXCEPTION( | 4755 ASSIGN_RETURN_ON_EXCEPTION( |
4746 isolate, param, Object::ToString(isolate, args.at<Object>(i)), | 4756 isolate, param, Object::ToString(isolate, args.at<Object>(i)), |
4747 JSFunction); | 4757 Object); |
4748 param = String::Flatten(param); | 4758 param = String::Flatten(param); |
4749 builder.AppendString(param); | 4759 builder.AppendString(param); |
4750 // If the formal parameters string include ) - an illegal | 4760 // If the formal parameters string include ) - an illegal |
4751 // character - it may make the combined function expression | 4761 // character - it may make the combined function expression |
4752 // compile. We avoid this problem by checking for this early on. | 4762 // compile. We avoid this problem by checking for this early on. |
4753 DisallowHeapAllocation no_gc; // Ensure vectors stay valid. | 4763 DisallowHeapAllocation no_gc; // Ensure vectors stay valid. |
4754 String::FlatContent param_content = param->GetFlatContent(); | 4764 String::FlatContent param_content = param->GetFlatContent(); |
4755 for (int i = 0, length = param->length(); i < length; ++i) { | 4765 for (int i = 0, length = param->length(); i < length; ++i) { |
4756 if (param_content.Get(i) == ')') { | 4766 if (param_content.Get(i) == ')') { |
4757 parenthesis_in_arg_string = true; | 4767 parenthesis_in_arg_string = true; |
4758 break; | 4768 break; |
4759 } | 4769 } |
4760 } | 4770 } |
4761 } | 4771 } |
4762 // If the formal parameters include an unbalanced block comment, the | 4772 // If the formal parameters include an unbalanced block comment, the |
4763 // function must be rejected. Since JavaScript does not allow nested | 4773 // function must be rejected. Since JavaScript does not allow nested |
4764 // comments we can include a trailing block comment to catch this. | 4774 // comments we can include a trailing block comment to catch this. |
4765 builder.AppendCString("\n/**/"); | 4775 builder.AppendCString("\n/**/"); |
4766 } | 4776 } |
4767 builder.AppendCString(") {\n"); | 4777 builder.AppendCString(") {\n"); |
4768 if (argc > 0) { | 4778 if (argc > 0) { |
4769 Handle<String> body; | 4779 Handle<String> body; |
4770 ASSIGN_RETURN_ON_EXCEPTION( | 4780 ASSIGN_RETURN_ON_EXCEPTION( |
4771 isolate, body, Object::ToString(isolate, args.at<Object>(argc)), | 4781 isolate, body, Object::ToString(isolate, args.at<Object>(argc)), |
4772 JSFunction); | 4782 Object); |
4773 builder.AppendString(body); | 4783 builder.AppendString(body); |
4774 } | 4784 } |
4775 builder.AppendCString("\n})"); | 4785 builder.AppendCString("\n})"); |
4776 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), JSFunction); | 4786 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object); |
4777 | 4787 |
4778 // The SyntaxError must be thrown after all the (observable) ToString | 4788 // The SyntaxError must be thrown after all the (observable) ToString |
4779 // conversions are done. | 4789 // conversions are done. |
4780 if (parenthesis_in_arg_string) { | 4790 if (parenthesis_in_arg_string) { |
4781 THROW_NEW_ERROR(isolate, | 4791 THROW_NEW_ERROR(isolate, |
4782 NewSyntaxError(MessageTemplate::kParenthesisInArgString), | 4792 NewSyntaxError(MessageTemplate::kParenthesisInArgString), |
4783 JSFunction); | 4793 Object); |
4784 } | 4794 } |
4785 } | 4795 } |
4786 | 4796 |
4787 // Compile the string in the constructor and not a helper so that errors to | 4797 // Compile the string in the constructor and not a helper so that errors to |
4788 // come from here. | 4798 // come from here. |
4789 Handle<JSFunction> target = args.target<JSFunction>(); | |
4790 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); | |
4791 Handle<JSFunction> function; | 4799 Handle<JSFunction> function; |
4792 { | 4800 { |
4793 ASSIGN_RETURN_ON_EXCEPTION( | 4801 ASSIGN_RETURN_ON_EXCEPTION( |
4794 isolate, function, | 4802 isolate, function, |
4795 CompileString(handle(target->native_context(), isolate), source, | 4803 CompileString(handle(target->native_context(), isolate), source, |
4796 ONLY_SINGLE_FUNCTION_LITERAL), | 4804 ONLY_SINGLE_FUNCTION_LITERAL), |
4797 JSFunction); | 4805 Object); |
4798 Handle<Object> result; | 4806 Handle<Object> result; |
4799 ASSIGN_RETURN_ON_EXCEPTION( | 4807 ASSIGN_RETURN_ON_EXCEPTION( |
4800 isolate, result, | 4808 isolate, result, |
4801 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), | 4809 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), |
4802 JSFunction); | 4810 Object); |
4803 function = Handle<JSFunction>::cast(result); | 4811 function = Handle<JSFunction>::cast(result); |
4804 function->shared()->set_name_should_print_as_anonymous(true); | 4812 function->shared()->set_name_should_print_as_anonymous(true); |
4805 } | 4813 } |
4806 | 4814 |
4807 // If new.target is equal to target then the function created | 4815 // If new.target is equal to target then the function created |
4808 // is already correctly setup and nothing else should be done | 4816 // is already correctly setup and nothing else should be done |
4809 // here. But if new.target is not equal to target then we are | 4817 // here. But if new.target is not equal to target then we are |
4810 // have a Function builtin subclassing case and therefore the | 4818 // have a Function builtin subclassing case and therefore the |
4811 // function has wrong initial map. To fix that we create a new | 4819 // function has wrong initial map. To fix that we create a new |
4812 // function object with correct initial map. | 4820 // function object with correct initial map. |
4813 Handle<Object> unchecked_new_target = args.new_target(); | 4821 Handle<Object> unchecked_new_target = args.new_target(); |
4814 if (!unchecked_new_target->IsUndefined(isolate) && | 4822 if (!unchecked_new_target->IsUndefined(isolate) && |
4815 !unchecked_new_target.is_identical_to(target)) { | 4823 !unchecked_new_target.is_identical_to(target)) { |
4816 Handle<JSReceiver> new_target = | 4824 Handle<JSReceiver> new_target = |
4817 Handle<JSReceiver>::cast(unchecked_new_target); | 4825 Handle<JSReceiver>::cast(unchecked_new_target); |
4818 Handle<Map> initial_map; | 4826 Handle<Map> initial_map; |
4819 ASSIGN_RETURN_ON_EXCEPTION( | 4827 ASSIGN_RETURN_ON_EXCEPTION( |
4820 isolate, initial_map, | 4828 isolate, initial_map, |
4821 JSFunction::GetDerivedMap(isolate, target, new_target), JSFunction); | 4829 JSFunction::GetDerivedMap(isolate, target, new_target), Object); |
4822 | 4830 |
4823 Handle<SharedFunctionInfo> shared_info(function->shared(), isolate); | 4831 Handle<SharedFunctionInfo> shared_info(function->shared(), isolate); |
4824 Handle<Map> map = Map::AsLanguageMode( | 4832 Handle<Map> map = Map::AsLanguageMode( |
4825 initial_map, shared_info->language_mode(), shared_info->kind()); | 4833 initial_map, shared_info->language_mode(), shared_info->kind()); |
4826 | 4834 |
4827 Handle<Context> context(function->context(), isolate); | 4835 Handle<Context> context(function->context(), isolate); |
4828 function = isolate->factory()->NewFunctionFromSharedFunctionInfo( | 4836 function = isolate->factory()->NewFunctionFromSharedFunctionInfo( |
4829 map, shared_info, context, NOT_TENURED); | 4837 map, shared_info, context, NOT_TENURED); |
4830 } | 4838 } |
4831 return function; | 4839 return function; |
4832 } | 4840 } |
4833 | 4841 |
4834 } // namespace | 4842 } // namespace |
4835 | 4843 |
4836 | 4844 |
4837 // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) | 4845 // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) |
4838 BUILTIN(FunctionConstructor) { | 4846 BUILTIN(FunctionConstructor) { |
4839 HandleScope scope(isolate); | 4847 HandleScope scope(isolate); |
4840 Handle<JSFunction> result; | 4848 Handle<Object> result; |
4841 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 4849 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
4842 isolate, result, CreateDynamicFunction(isolate, args, "function")); | 4850 isolate, result, CreateDynamicFunction(isolate, args, "function")); |
4843 return *result; | 4851 return *result; |
4844 } | 4852 } |
4845 | 4853 |
4846 namespace { | 4854 namespace { |
4847 | 4855 |
4848 Object* DoFunctionBind(Isolate* isolate, BuiltinArguments args) { | 4856 Object* DoFunctionBind(Isolate* isolate, BuiltinArguments args) { |
4849 HandleScope scope(isolate); | 4857 HandleScope scope(isolate); |
4850 DCHECK_LE(1, args.length()); | 4858 DCHECK_LE(1, args.length()); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4963 | 4971 |
4964 // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) | 4972 // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) |
4965 BUILTIN(GeneratorFunctionConstructor) { | 4973 BUILTIN(GeneratorFunctionConstructor) { |
4966 HandleScope scope(isolate); | 4974 HandleScope scope(isolate); |
4967 RETURN_RESULT_OR_FAILURE(isolate, | 4975 RETURN_RESULT_OR_FAILURE(isolate, |
4968 CreateDynamicFunction(isolate, args, "function*")); | 4976 CreateDynamicFunction(isolate, args, "function*")); |
4969 } | 4977 } |
4970 | 4978 |
4971 BUILTIN(AsyncFunctionConstructor) { | 4979 BUILTIN(AsyncFunctionConstructor) { |
4972 HandleScope scope(isolate); | 4980 HandleScope scope(isolate); |
4973 Handle<JSFunction> func; | 4981 Handle<Object> maybe_func; |
4974 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 4982 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
4975 isolate, func, CreateDynamicFunction(isolate, args, "async function")); | 4983 isolate, maybe_func, |
| 4984 CreateDynamicFunction(isolate, args, "async function")); |
| 4985 if (!maybe_func->IsJSFunction()) return *maybe_func; |
4976 | 4986 |
4977 // Do not lazily compute eval position for AsyncFunction, as they may not be | 4987 // Do not lazily compute eval position for AsyncFunction, as they may not be |
4978 // determined after the function is resumed. | 4988 // determined after the function is resumed. |
| 4989 Handle<JSFunction> func = Handle<JSFunction>::cast(maybe_func); |
4979 Handle<Script> script = handle(Script::cast(func->shared()->script())); | 4990 Handle<Script> script = handle(Script::cast(func->shared()->script())); |
4980 int position = script->GetEvalPosition(); | 4991 int position = script->GetEvalPosition(); |
4981 USE(position); | 4992 USE(position); |
4982 | 4993 |
4983 return *func; | 4994 return *func; |
4984 } | 4995 } |
4985 | 4996 |
4986 // ES6 19.1.3.6 Object.prototype.toString | 4997 // ES6 19.1.3.6 Object.prototype.toString |
4987 BUILTIN(ObjectProtoToString) { | 4998 BUILTIN(ObjectProtoToString) { |
4988 HandleScope scope(isolate); | 4999 HandleScope scope(isolate); |
(...skipping 1748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6737 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 6748 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
6738 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 6749 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
6739 #undef DEFINE_BUILTIN_ACCESSOR_C | 6750 #undef DEFINE_BUILTIN_ACCESSOR_C |
6740 #undef DEFINE_BUILTIN_ACCESSOR_A | 6751 #undef DEFINE_BUILTIN_ACCESSOR_A |
6741 #undef DEFINE_BUILTIN_ACCESSOR_T | 6752 #undef DEFINE_BUILTIN_ACCESSOR_T |
6742 #undef DEFINE_BUILTIN_ACCESSOR_S | 6753 #undef DEFINE_BUILTIN_ACCESSOR_S |
6743 #undef DEFINE_BUILTIN_ACCESSOR_H | 6754 #undef DEFINE_BUILTIN_ACCESSOR_H |
6744 | 6755 |
6745 } // namespace internal | 6756 } // namespace internal |
6746 } // namespace v8 | 6757 } // namespace v8 |
OLD | NEW |