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.h" | 7 #include "src/api.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/arguments.h" | 9 #include "src/arguments.h" |
10 #include "src/base/once.h" | 10 #include "src/base/once.h" |
(...skipping 1953 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1964 // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) | 1964 // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body ) |
1965 BUILTIN(FunctionConstructor) { | 1965 BUILTIN(FunctionConstructor) { |
1966 HandleScope scope(isolate); | 1966 HandleScope scope(isolate); |
1967 Handle<JSFunction> result; | 1967 Handle<JSFunction> result; |
1968 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 1968 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
1969 isolate, result, CreateDynamicFunction(isolate, args, "function")); | 1969 isolate, result, CreateDynamicFunction(isolate, args, "function")); |
1970 return *result; | 1970 return *result; |
1971 } | 1971 } |
1972 | 1972 |
1973 | 1973 |
| 1974 // ES6 section 19.2.3.2 Function.prototype.bind ( thisArg, ...args ) |
| 1975 BUILTIN(FunctionPrototypeBind) { |
| 1976 HandleScope scope(isolate); |
| 1977 DCHECK_LE(1, args.length()); |
| 1978 if (!args.receiver()->IsCallable()) { |
| 1979 THROW_NEW_ERROR_RETURN_FAILURE( |
| 1980 isolate, NewTypeError(MessageTemplate::kFunctionBind)); |
| 1981 } |
| 1982 |
| 1983 // Allocate the bound function with the given {this_arg} and {args}. |
| 1984 Handle<JSReceiver> target = args.at<JSReceiver>(0); |
| 1985 Handle<Object> this_arg = isolate->factory()->undefined_value(); |
| 1986 ScopedVector<Handle<Object>> argv(std::max(0, args.length() - 2)); |
| 1987 if (args.length() > 1) { |
| 1988 this_arg = args.at<Object>(1); |
| 1989 for (int i = 2; i < args.length(); ++i) { |
| 1990 argv[i - 2] = args.at<Object>(i); |
| 1991 } |
| 1992 } |
| 1993 Handle<JSBoundFunction> function; |
| 1994 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 1995 isolate, function, |
| 1996 isolate->factory()->NewJSBoundFunction(target, this_arg, argv)); |
| 1997 |
| 1998 // TODO(bmeurer): Optimize the rest for the common cases where {target} is |
| 1999 // a function with some initial map or even a bound function. |
| 2000 // Setup the "length" property based on the "length" of the {target}. |
| 2001 Handle<Object> length(Smi::FromInt(0), isolate); |
| 2002 Maybe<bool> target_has_length = |
| 2003 JSReceiver::HasOwnProperty(target, isolate->factory()->length_string()); |
| 2004 if (!target_has_length.IsJust()) { |
| 2005 return isolate->heap()->exception(); |
| 2006 } else if (target_has_length.FromJust()) { |
| 2007 Handle<Object> target_length; |
| 2008 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 2009 isolate, target_length, |
| 2010 JSReceiver::GetProperty(target, isolate->factory()->length_string())); |
| 2011 if (target_length->IsNumber()) { |
| 2012 length = isolate->factory()->NewNumber(std::max( |
| 2013 0.0, DoubleToInteger(target_length->Number()) - argv.length())); |
| 2014 } |
| 2015 } |
| 2016 function->set_length(*length); |
| 2017 |
| 2018 // Setup the "name" property based on the "name" of the {target}. |
| 2019 Handle<Object> target_name; |
| 2020 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 2021 isolate, target_name, |
| 2022 JSReceiver::GetProperty(target, isolate->factory()->name_string())); |
| 2023 Handle<String> name; |
| 2024 if (!target_name->IsString()) { |
| 2025 name = isolate->factory()->bound__string(); |
| 2026 } else { |
| 2027 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 2028 isolate, name, Name::ToFunctionName(Handle<String>::cast(target_name))); |
| 2029 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 2030 isolate, name, isolate->factory()->NewConsString( |
| 2031 isolate->factory()->bound__string(), name)); |
| 2032 } |
| 2033 function->set_name(*name); |
| 2034 return *function; |
| 2035 } |
| 2036 |
| 2037 |
1974 // ES6 section 19.2.3.5 Function.prototype.toString ( ) | 2038 // ES6 section 19.2.3.5 Function.prototype.toString ( ) |
1975 BUILTIN(FunctionPrototypeToString) { | 2039 BUILTIN(FunctionPrototypeToString) { |
1976 HandleScope scope(isolate); | 2040 HandleScope scope(isolate); |
1977 Handle<Object> receiver = args.receiver(); | 2041 Handle<Object> receiver = args.receiver(); |
1978 | 2042 |
1979 if (receiver->IsJSFunction()) { | 2043 if (receiver->IsJSBoundFunction()) { |
| 2044 return *JSBoundFunction::ToString(Handle<JSBoundFunction>::cast(receiver)); |
| 2045 } else if (receiver->IsJSFunction()) { |
1980 return *JSFunction::ToString(Handle<JSFunction>::cast(receiver)); | 2046 return *JSFunction::ToString(Handle<JSFunction>::cast(receiver)); |
1981 } | 2047 } |
1982 THROW_NEW_ERROR_RETURN_FAILURE( | 2048 THROW_NEW_ERROR_RETURN_FAILURE( |
1983 isolate, NewTypeError(MessageTemplate::kNotGeneric, | 2049 isolate, NewTypeError(MessageTemplate::kNotGeneric, |
1984 isolate->factory()->NewStringFromAsciiChecked( | 2050 isolate->factory()->NewStringFromAsciiChecked( |
1985 "Function.prototype.toString"))); | 2051 "Function.prototype.toString"))); |
1986 } | 2052 } |
1987 | 2053 |
1988 | 2054 |
1989 // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) | 2055 // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body) |
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2700 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) | 2766 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) |
2701 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) | 2767 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) |
2702 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 2768 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
2703 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 2769 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
2704 #undef DEFINE_BUILTIN_ACCESSOR_C | 2770 #undef DEFINE_BUILTIN_ACCESSOR_C |
2705 #undef DEFINE_BUILTIN_ACCESSOR_A | 2771 #undef DEFINE_BUILTIN_ACCESSOR_A |
2706 | 2772 |
2707 | 2773 |
2708 } // namespace internal | 2774 } // namespace internal |
2709 } // namespace v8 | 2775 } // namespace v8 |
OLD | NEW |