OLD | NEW |
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-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
7 #include "src/code-factory.h" | 7 #include "src/code-factory.h" |
8 #include "src/code-stub-assembler.h" | 8 #include "src/code-stub-assembler.h" |
9 #include "src/compiler.h" | 9 #include "src/compiler.h" |
10 #include "src/string-builder.h" | 10 #include "src/string-builder.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 Handle<JSFunction> target = args.target(); | 25 Handle<JSFunction> target = args.target(); |
26 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); | 26 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate); |
27 | 27 |
28 if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) { | 28 if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) { |
29 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined); | 29 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined); |
30 return isolate->factory()->undefined_value(); | 30 return isolate->factory()->undefined_value(); |
31 } | 31 } |
32 | 32 |
33 // Build the source string. | 33 // Build the source string. |
34 Handle<String> source; | 34 Handle<String> source; |
| 35 int parameters_end_pos = kNoSourcePosition; |
35 { | 36 { |
36 IncrementalStringBuilder builder(isolate); | 37 IncrementalStringBuilder builder(isolate); |
37 builder.AppendCharacter('('); | 38 builder.AppendCharacter('('); |
38 builder.AppendCString(token); | 39 builder.AppendCString(token); |
39 builder.AppendCharacter('('); | 40 if (FLAG_harmony_function_tostring) { |
| 41 builder.AppendCString(" anonymous("); |
| 42 } else { |
| 43 builder.AppendCharacter('('); |
| 44 } |
40 bool parenthesis_in_arg_string = false; | 45 bool parenthesis_in_arg_string = false; |
41 if (argc > 1) { | 46 if (argc > 1) { |
42 for (int i = 1; i < argc; ++i) { | 47 for (int i = 1; i < argc; ++i) { |
43 if (i > 1) builder.AppendCharacter(','); | 48 if (i > 1) builder.AppendCharacter(','); |
44 Handle<String> param; | 49 Handle<String> param; |
45 ASSIGN_RETURN_ON_EXCEPTION( | 50 ASSIGN_RETURN_ON_EXCEPTION( |
46 isolate, param, Object::ToString(isolate, args.at(i)), Object); | 51 isolate, param, Object::ToString(isolate, args.at(i)), 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 } |
| 74 } |
| 75 if (FLAG_harmony_function_tostring) { |
| 76 builder.AppendCharacter('\n'); |
| 77 parameters_end_pos = builder.Length(); |
65 } | 78 } |
66 builder.AppendCString(") {\n"); | 79 builder.AppendCString(") {\n"); |
67 if (argc > 0) { | 80 if (argc > 0) { |
68 Handle<String> body; | 81 Handle<String> body; |
69 ASSIGN_RETURN_ON_EXCEPTION( | 82 ASSIGN_RETURN_ON_EXCEPTION( |
70 isolate, body, Object::ToString(isolate, args.at(argc)), Object); | 83 isolate, body, Object::ToString(isolate, args.at(argc)), Object); |
71 builder.AppendString(body); | 84 builder.AppendString(body); |
72 } | 85 } |
73 builder.AppendCString("\n})"); | 86 builder.AppendCString("\n})"); |
74 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object); | 87 ASSIGN_RETURN_ON_EXCEPTION(isolate, source, builder.Finish(), Object); |
75 | 88 |
76 // The SyntaxError must be thrown after all the (observable) ToString | 89 // The SyntaxError must be thrown after all the (observable) ToString |
77 // conversions are done. | 90 // conversions are done. |
78 if (parenthesis_in_arg_string) { | 91 if (parenthesis_in_arg_string) { |
79 THROW_NEW_ERROR(isolate, | 92 THROW_NEW_ERROR(isolate, |
80 NewSyntaxError(MessageTemplate::kParenthesisInArgString), | 93 NewSyntaxError(MessageTemplate::kParenthesisInArgString), |
81 Object); | 94 Object); |
82 } | 95 } |
83 } | 96 } |
84 | 97 |
85 // Compile the string in the constructor and not a helper so that errors to | 98 // Compile the string in the constructor and not a helper so that errors to |
86 // come from here. | 99 // come from here. |
87 Handle<JSFunction> function; | 100 Handle<JSFunction> function; |
88 { | 101 { |
89 ASSIGN_RETURN_ON_EXCEPTION(isolate, function, | 102 ASSIGN_RETURN_ON_EXCEPTION( |
90 Compiler::GetFunctionFromString( | 103 isolate, function, |
91 handle(target->native_context(), isolate), | 104 Compiler::GetFunctionFromString( |
92 source, ONLY_SINGLE_FUNCTION_LITERAL), | 105 handle(target->native_context(), isolate), source, |
93 Object); | 106 ONLY_SINGLE_FUNCTION_LITERAL, parameters_end_pos), |
| 107 Object); |
94 Handle<Object> result; | 108 Handle<Object> result; |
95 ASSIGN_RETURN_ON_EXCEPTION( | 109 ASSIGN_RETURN_ON_EXCEPTION( |
96 isolate, result, | 110 isolate, result, |
97 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), | 111 Execution::Call(isolate, function, target_global_proxy, 0, nullptr), |
98 Object); | 112 Object); |
99 function = Handle<JSFunction>::cast(result); | 113 function = Handle<JSFunction>::cast(result); |
100 function->shared()->set_name_should_print_as_anonymous(true); | 114 function->shared()->set_name_should_print_as_anonymous(true); |
101 } | 115 } |
102 | 116 |
103 // If new.target is equal to target then the function created | 117 // If new.target is equal to target then the function created |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 | 480 |
467 Node* f = assembler.Parameter(0); | 481 Node* f = assembler.Parameter(0); |
468 Node* v = assembler.Parameter(1); | 482 Node* v = assembler.Parameter(1); |
469 Node* context = assembler.Parameter(4); | 483 Node* context = assembler.Parameter(4); |
470 Node* result = assembler.OrdinaryHasInstance(context, f, v); | 484 Node* result = assembler.OrdinaryHasInstance(context, f, v); |
471 assembler.Return(result); | 485 assembler.Return(result); |
472 } | 486 } |
473 | 487 |
474 } // namespace internal | 488 } // namespace internal |
475 } // namespace v8 | 489 } // namespace v8 |
OLD | NEW |