Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/builtins/builtins-utils.h"
6 : #include "src/builtins/builtins.h"
7 : #include "src/code-factory.h"
8 : #include "src/compiler.h"
9 : #include "src/conversions.h"
10 : #include "src/counters.h"
11 : #include "src/lookup.h"
12 : #include "src/objects-inl.h"
13 : #include "src/string-builder.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 : namespace {
19 :
20 : // ES6 section 19.2.1.1.1 CreateDynamicFunction
21 1196996 : MaybeHandle<Object> CreateDynamicFunction(Isolate* isolate,
22 : BuiltinArguments args,
23 : const char* token) {
24 : // Compute number of arguments, ignoring the receiver.
25 : DCHECK_LE(1, args.length());
26 1196996 : int const argc = args.length() - 1;
27 :
28 : Handle<JSFunction> target = args.target();
29 : Handle<JSObject> target_global_proxy(target->global_proxy(), isolate);
30 :
31 1196996 : if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) {
32 200 : isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined);
33 200 : return isolate->factory()->undefined_value();
34 : }
35 :
36 : // Build the source string.
37 : Handle<String> source;
38 : int parameters_end_pos = kNoSourcePosition;
39 : {
40 1196796 : IncrementalStringBuilder builder(isolate);
41 : builder.AppendCharacter('(');
42 : builder.AppendCString(token);
43 1196796 : if (FLAG_harmony_function_tostring) {
44 : builder.AppendCString(" anonymous(");
45 : } else {
46 : builder.AppendCharacter('(');
47 : }
48 : bool parenthesis_in_arg_string = false;
49 1196796 : if (argc > 1) {
50 1128689 : for (int i = 1; i < argc; ++i) {
51 1128689 : if (i > 1) builder.AppendCharacter(',');
52 : Handle<String> param;
53 2257378 : ASSIGN_RETURN_ON_EXCEPTION(
54 : isolate, param, Object::ToString(isolate, args.at(i)), Object);
55 1128689 : param = String::Flatten(param);
56 1128689 : builder.AppendString(param);
57 1128689 : if (!FLAG_harmony_function_tostring) {
58 : // If the formal parameters string include ) - an illegal
59 : // character - it may make the combined function expression
60 : // compile. We avoid this problem by checking for this early on.
61 : DisallowHeapAllocation no_gc; // Ensure vectors stay valid.
62 1128490 : String::FlatContent param_content = param->GetFlatContent();
63 5524631 : for (int i = 0, length = param->length(); i < length; ++i) {
64 8792322 : if (param_content.Get(i) == ')') {
65 : parenthesis_in_arg_string = true;
66 : break;
67 : }
68 : }
69 : }
70 : }
71 1126552 : if (!FLAG_harmony_function_tostring) {
72 : // If the formal parameters include an unbalanced block comment, the
73 : // function must be rejected. Since JavaScript does not allow nested
74 : // comments we can include a trailing block comment to catch this.
75 : builder.AppendCString("\n/*``*/");
76 : }
77 : }
78 1196796 : if (FLAG_harmony_function_tostring) {
79 : builder.AppendCharacter('\n');
80 : parameters_end_pos = builder.Length();
81 : }
82 : builder.AppendCString(") {\n");
83 1196796 : if (argc > 0) {
84 : Handle<String> body;
85 2381850 : ASSIGN_RETURN_ON_EXCEPTION(
86 : isolate, body, Object::ToString(isolate, args.at(argc)), Object);
87 1190925 : builder.AppendString(body);
88 : }
89 : builder.AppendCString("\n})");
90 2393592 : ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object);
91 :
92 : // The SyntaxError must be thrown after all the (observable) ToString
93 : // conversions are done.
94 1196796 : if (parenthesis_in_arg_string) {
95 40 : THROW_NEW_ERROR(isolate,
96 : NewSyntaxError(MessageTemplate::kParenthesisInArgString),
97 : Object);
98 : }
99 : }
100 :
101 : // Compile the string in the constructor and not a helper so that errors to
102 : // come from here.
103 : Handle<JSFunction> function;
104 : {
105 2393552 : ASSIGN_RETURN_ON_EXCEPTION(
106 : isolate, function,
107 : Compiler::GetFunctionFromString(
108 : handle(target->native_context(), isolate), source,
109 : ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos),
110 : Object);
111 : Handle<Object> result;
112 2385426 : ASSIGN_RETURN_ON_EXCEPTION(
113 : isolate, result,
114 : Execution::Call(isolate, function, target_global_proxy, 0, nullptr),
115 : Object);
116 : function = Handle<JSFunction>::cast(result);
117 1191226 : function->shared()->set_name_should_print_as_anonymous(true);
118 : }
119 :
120 : // If new.target is equal to target then the function created
121 : // is already correctly setup and nothing else should be done
122 : // here. But if new.target is not equal to target then we are
123 : // have a Function builtin subclassing case and therefore the
124 : // function has wrong initial map. To fix that we create a new
125 : // function object with correct initial map.
126 : Handle<Object> unchecked_new_target = args.new_target();
127 1316028 : if (!unchecked_new_target->IsUndefined(isolate) &&
128 : !unchecked_new_target.is_identical_to(target)) {
129 : Handle<JSReceiver> new_target =
130 687 : Handle<JSReceiver>::cast(unchecked_new_target);
131 : Handle<Map> initial_map;
132 1374 : ASSIGN_RETURN_ON_EXCEPTION(
133 : isolate, initial_map,
134 : JSFunction::GetDerivedMap(isolate, target, new_target), Object);
135 :
136 : Handle<SharedFunctionInfo> shared_info(function->shared(), isolate);
137 687 : Handle<Map> map = Map::AsLanguageMode(initial_map, shared_info);
138 :
139 : Handle<Context> context(function->context(), isolate);
140 : function = isolate->factory()->NewFunctionFromSharedFunctionInfo(
141 687 : map, shared_info, context, NOT_TENURED);
142 : }
143 1191226 : return function;
144 : }
145 :
146 : } // namespace
147 :
148 : // ES6 section 19.2.1.1 Function ( p1, p2, ... , pn, body )
149 3576039 : BUILTIN(FunctionConstructor) {
150 : HandleScope scope(isolate);
151 : Handle<Object> result;
152 2384026 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
153 : isolate, result, CreateDynamicFunction(isolate, args, "function"));
154 1189740 : return *result;
155 : }
156 :
157 : // ES6 section 25.2.1.1 GeneratorFunction (p1, p2, ... , pn, body)
158 6159 : BUILTIN(GeneratorFunctionConstructor) {
159 : HandleScope scope(isolate);
160 4602 : RETURN_RESULT_OR_FAILURE(isolate,
161 : CreateDynamicFunction(isolate, args, "function*"));
162 : }
163 :
164 6468 : BUILTIN(AsyncFunctionConstructor) {
165 : HandleScope scope(isolate);
166 : Handle<Object> maybe_func;
167 4312 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
168 : isolate, maybe_func,
169 : CreateDynamicFunction(isolate, args, "async function"));
170 416 : if (!maybe_func->IsJSFunction()) return *maybe_func;
171 :
172 : // Do not lazily compute eval position for AsyncFunction, as they may not be
173 : // determined after the function is resumed.
174 : Handle<JSFunction> func = Handle<JSFunction>::cast(maybe_func);
175 : Handle<Script> script = handle(Script::cast(func->shared()->script()));
176 416 : int position = script->GetEvalPosition();
177 : USE(position);
178 :
179 416 : return *func;
180 : }
181 :
182 2322 : BUILTIN(AsyncGeneratorFunctionConstructor) {
183 : HandleScope scope(isolate);
184 : Handle<Object> maybe_func;
185 1548 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
186 : isolate, maybe_func,
187 : CreateDynamicFunction(isolate, args, "async function*"));
188 774 : if (!maybe_func->IsJSFunction()) return *maybe_func;
189 :
190 : // Do not lazily compute eval position for AsyncFunction, as they may not be
191 : // determined after the function is resumed.
192 : Handle<JSFunction> func = Handle<JSFunction>::cast(maybe_func);
193 : Handle<Script> script = handle(Script::cast(func->shared()->script()));
194 774 : int position = script->GetEvalPosition();
195 : USE(position);
196 :
197 774 : return *func;
198 : }
199 :
200 : namespace {
201 :
202 291 : Object* DoFunctionBind(Isolate* isolate, BuiltinArguments args) {
203 : HandleScope scope(isolate);
204 : DCHECK_LE(1, args.length());
205 291 : if (!args.receiver()->IsCallable()) {
206 56 : THROW_NEW_ERROR_RETURN_FAILURE(
207 : isolate, NewTypeError(MessageTemplate::kFunctionBind));
208 : }
209 :
210 : // Allocate the bound function with the given {this_arg} and {args}.
211 : Handle<JSReceiver> target = args.at<JSReceiver>(0);
212 : Handle<Object> this_arg = isolate->factory()->undefined_value();
213 789 : ScopedVector<Handle<Object>> argv(std::max(0, args.length() - 2));
214 263 : if (args.length() > 1) {
215 253 : this_arg = args.at(1);
216 451 : for (int i = 2; i < args.length(); ++i) {
217 396 : argv[i - 2] = args.at(i);
218 : }
219 : }
220 : Handle<JSBoundFunction> function;
221 526 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
222 : isolate, function,
223 : isolate->factory()->NewJSBoundFunction(target, this_arg, argv));
224 :
225 : LookupIterator length_lookup(target, isolate->factory()->length_string(),
226 263 : target, LookupIterator::OWN);
227 : // Setup the "length" property based on the "length" of the {target}.
228 : // If the targets length is the default JSFunction accessor, we can keep the
229 : // accessor that's installed by default on the JSBoundFunction. It lazily
230 : // computes the value from the underlying internal length.
231 526 : if (!target->IsJSFunction() ||
232 355 : length_lookup.state() != LookupIterator::ACCESSOR ||
233 447 : !length_lookup.GetAccessors()->IsAccessorInfo()) {
234 : Handle<Object> length(Smi::kZero, isolate);
235 : Maybe<PropertyAttributes> attributes =
236 171 : JSReceiver::GetPropertyAttributes(&length_lookup);
237 171 : if (!attributes.IsJust()) return isolate->heap()->exception();
238 171 : if (attributes.FromJust() != ABSENT) {
239 : Handle<Object> target_length;
240 342 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, target_length,
241 : Object::GetProperty(&length_lookup));
242 171 : if (target_length->IsNumber()) {
243 : length = isolate->factory()->NewNumber(std::max(
244 324 : 0.0, DoubleToInteger(target_length->Number()) - argv.length()));
245 : }
246 : }
247 171 : LookupIterator it(function, isolate->factory()->length_string(), function);
248 : DCHECK_EQ(LookupIterator::ACCESSOR, it.state());
249 342 : RETURN_FAILURE_ON_EXCEPTION(isolate,
250 : JSObject::DefineOwnPropertyIgnoreAttributes(
251 : &it, length, it.property_attributes()));
252 : }
253 :
254 : // Setup the "name" property based on the "name" of the {target}.
255 : // If the target's name is the default JSFunction accessor, we can keep the
256 : // accessor that's installed by default on the JSBoundFunction. It lazily
257 : // computes the value from the underlying internal name.
258 263 : LookupIterator name_lookup(target, isolate->factory()->name_string(), target);
259 536 : if (!target->IsJSFunction() ||
260 174 : name_lookup.state() != LookupIterator::ACCESSOR ||
261 762 : !name_lookup.GetAccessors()->IsAccessorInfo() ||
262 144 : (name_lookup.IsFound() && !name_lookup.HolderIsReceiver())) {
263 : Handle<Object> target_name;
264 382 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, target_name,
265 : Object::GetProperty(&name_lookup));
266 : Handle<String> name;
267 191 : if (target_name->IsString()) {
268 272 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
269 : isolate, name,
270 : Name::ToFunctionName(Handle<String>::cast(target_name)));
271 272 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
272 : isolate, name, isolate->factory()->NewConsString(
273 : isolate->factory()->bound__string(), name));
274 : } else {
275 : name = isolate->factory()->bound__string();
276 : }
277 191 : LookupIterator it(function, isolate->factory()->name_string());
278 : DCHECK_EQ(LookupIterator::ACCESSOR, it.state());
279 382 : RETURN_FAILURE_ON_EXCEPTION(isolate,
280 : JSObject::DefineOwnPropertyIgnoreAttributes(
281 : &it, name, it.property_attributes()));
282 : }
283 263 : return *function;
284 : }
285 :
286 : } // namespace
287 :
288 : // ES6 section 19.2.3.2 Function.prototype.bind ( thisArg, ...args )
289 582 : BUILTIN(FunctionPrototypeBind) { return DoFunctionBind(isolate, args); }
290 :
291 : // TODO(verwaest): This is a temporary helper until the FastFunctionBind stub
292 : // can tailcall to the builtin directly.
293 0 : RUNTIME_FUNCTION(Runtime_FunctionBind) {
294 : DCHECK_EQ(2, args.length());
295 0 : Arguments* incoming = reinterpret_cast<Arguments*>(args[0]);
296 : // Rewrap the arguments as builtins arguments.
297 0 : int argc = incoming->length() + BuiltinArguments::kNumExtraArgsWithReceiver;
298 0 : BuiltinArguments caller_args(argc, incoming->arguments() + 1);
299 0 : return DoFunctionBind(isolate, caller_args);
300 : }
301 :
302 : // ES6 section 19.2.3.5 Function.prototype.toString ( )
303 5302917 : BUILTIN(FunctionPrototypeToString) {
304 : HandleScope scope(isolate);
305 : Handle<Object> receiver = args.receiver();
306 1767639 : if (receiver->IsJSBoundFunction()) {
307 110 : return *JSBoundFunction::ToString(Handle<JSBoundFunction>::cast(receiver));
308 1767584 : } else if (receiver->IsJSFunction()) {
309 3534734 : return *JSFunction::ToString(Handle<JSFunction>::cast(receiver));
310 : }
311 651 : THROW_NEW_ERROR_RETURN_FAILURE(
312 : isolate, NewTypeError(MessageTemplate::kNotGeneric,
313 : isolate->factory()->NewStringFromAsciiChecked(
314 : "Function.prototype.toString"),
315 : isolate->factory()->Function_string()));
316 : }
317 :
318 : } // namespace internal
319 : } // namespace v8
|