Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: src/builtins/builtins-function.cc

Issue 2156303002: Implement new Function.prototype.toString and fix CreateDynamicFunction parsing (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 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 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 #include "src/builtins/builtins-utils.h" 6 #include "src/builtins/builtins-utils.h"
7 7
8 #include "src/compiler.h" 8 #include "src/compiler.h"
9 #include "src/string-builder.h" 9 #include "src/string-builder.h"
10 10
(...skipping 13 matching lines...) Expand all
24 Handle<JSFunction> target = args.target(); 24 Handle<JSFunction> target = args.target();
25 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); 25 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate);
26 26
27 if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) { 27 if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) {
28 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined); 28 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined);
29 return isolate->factory()->undefined_value(); 29 return isolate->factory()->undefined_value();
30 } 30 }
31 31
32 // Build the source string. 32 // Build the source string.
33 Handle<String> source; 33 Handle<String> source;
34 int parameters_end_pos = kNoSourcePosition;
34 { 35 {
35 IncrementalStringBuilder builder(isolate); 36 IncrementalStringBuilder builder(isolate);
36 builder.AppendCharacter('('); 37 builder.AppendCharacter('(');
37 builder.AppendCString(token); 38 builder.AppendCString(token);
38 builder.AppendCharacter('('); 39 if (FLAG_harmony_function_tostring) {
40 builder.AppendCString(" anonymous(");
41 } else {
42 builder.AppendCharacter('(');
43 }
39 bool parenthesis_in_arg_string = false; 44 bool parenthesis_in_arg_string = false;
40 if (argc > 1) { 45 if (argc > 1) {
41 for (int i = 1; i < argc; ++i) { 46 for (int i = 1; i < argc; ++i) {
42 if (i > 1) builder.AppendCharacter(','); 47 if (i > 1) builder.AppendCharacter(',');
43 Handle<String> param; 48 Handle<String> param;
44 ASSIGN_RETURN_ON_EXCEPTION( 49 ASSIGN_RETURN_ON_EXCEPTION(
45 isolate, param, Object::ToString(isolate, args.at<Object>(i)), 50 isolate, param, Object::ToString(isolate, args.at<Object>(i)),
46 Object); 51 Object);
47 param = String::Flatten(param); 52 param = String::Flatten(param);
48 builder.AppendString(param); 53 builder.AppendString(param);
49 // If the formal parameters string include ) - an illegal 54 if (!FLAG_harmony_function_tostring) {
50 // character - it may make the combined function expression 55 // If the formal parameters string include ) - an illegal
51 // compile. We avoid this problem by checking for this early on. 56 // character - it may make the combined function expression
52 DisallowHeapAllocation no_gc; // Ensure vectors stay valid. 57 // compile. We avoid this problem by checking for this early on.
53 String::FlatContent param_content = param->GetFlatContent(); 58 DisallowHeapAllocation no_gc; // Ensure vectors stay valid.
54 for (int i = 0, length = param->length(); i < length; ++i) { 59 String::FlatContent param_content = param->GetFlatContent();
55 if (param_content.Get(i) == ')') { 60 for (int i = 0, length = param->length(); i < length; ++i) {
56 parenthesis_in_arg_string = true; 61 if (param_content.Get(i) == ')') {
57 break; 62 parenthesis_in_arg_string = true;
63 break;
64 }
58 } 65 }
59 } 66 }
60 } 67 }
61 // If the formal parameters include an unbalanced block comment, the 68 if (!FLAG_harmony_function_tostring) {
62 // function must be rejected. Since JavaScript does not allow nested 69 // If the formal parameters include an unbalanced block comment, the
63 // comments we can include a trailing block comment to catch this. 70 // function must be rejected. Since JavaScript does not allow nested
64 builder.AppendCString("\n/*``*/"); 71 // comments we can include a trailing block comment to catch this.
72 builder.AppendCString("\n/*``*/");
73 }
65 } 74 }
66 builder.AppendCString(") {\n"); 75 if (FLAG_harmony_function_tostring) {
76 parameters_end_pos = builder.Length() + 1;
77 builder.AppendCString("\n) {");
78 } else {
79 builder.AppendCString(") {\n");
80 }
67 if (argc > 0) { 81 if (argc > 0) {
68 Handle<String> body; 82 Handle<String> body;
69 ASSIGN_RETURN_ON_EXCEPTION( 83 ASSIGN_RETURN_ON_EXCEPTION(
70 isolate, body, Object::ToString(isolate, args.at<Object>(argc)), 84 isolate, body, Object::ToString(isolate, args.at<Object>(argc)),
71 Object); 85 Object);
72 builder.AppendString(body); 86 builder.AppendString(body);
73 } 87 }
74 builder.AppendCString("\n})"); 88 builder.AppendCString("\n})");
75 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object); 89 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object);
76 90
77 // The SyntaxError must be thrown after all the (observable) ToString 91 // The SyntaxError must be thrown after all the (observable) ToString
78 // conversions are done. 92 // conversions are done.
79 if (parenthesis_in_arg_string) { 93 if (parenthesis_in_arg_string) {
80 THROW_NEW_ERROR(isolate, 94 THROW_NEW_ERROR(isolate,
81 NewSyntaxError(MessageTemplate::kParenthesisInArgString), 95 NewSyntaxError(MessageTemplate::kParenthesisInArgString),
82 Object); 96 Object);
83 } 97 }
84 } 98 }
85 99
86 // Compile the string in the constructor and not a helper so that errors to 100 // Compile the string in the constructor and not a helper so that errors to
87 // come from here. 101 // come from here.
88 Handle<JSFunction> function; 102 Handle<JSFunction> function;
89 { 103 {
90 ASSIGN_RETURN_ON_EXCEPTION(isolate, function, 104 ASSIGN_RETURN_ON_EXCEPTION(
91 Compiler::GetFunctionFromString( 105 isolate, function,
92 handle(target->native_context(), isolate), 106 Compiler::GetFunctionFromString(
93 source, ONLY_SINGLE_FUNCTION_LITERAL), 107 handle(target->native_context(), isolate), source,
94 Object); 108 ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos),
109 Object);
95 Handle<Object> result; 110 Handle<Object> result;
96 ASSIGN_RETURN_ON_EXCEPTION( 111 ASSIGN_RETURN_ON_EXCEPTION(
97 isolate, result, 112 isolate, result,
98 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), 113 Execution::Call(isolate, function, target_global_proxy, 0, nullptr),
99 Object); 114 Object);
100 function = Handle<JSFunction>::cast(result); 115 function = Handle<JSFunction>::cast(result);
101 function->shared()->set_name_should_print_as_anonymous(true); 116 function->shared()->set_name_should_print_as_anonymous(true);
102 } 117 }
103 118
104 // If new.target is equal to target then the function created 119 // If new.target is equal to target then the function created
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 304
290 Node* f = assembler.Parameter(0); 305 Node* f = assembler.Parameter(0);
291 Node* v = assembler.Parameter(1); 306 Node* v = assembler.Parameter(1);
292 Node* context = assembler.Parameter(4); 307 Node* context = assembler.Parameter(4);
293 Node* result = assembler.OrdinaryHasInstance(context, f, v); 308 Node* result = assembler.OrdinaryHasInstance(context, f, v);
294 assembler.Return(result); 309 assembler.Return(result);
295 } 310 }
296 311
297 } // namespace internal 312 } // namespace internal
298 } // namespace v8 313 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698