OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
9 #include "src/debug/debug.h" | 9 #include "src/debug/debug.h" |
10 #include "src/isolate-inl.h" | 10 #include "src/isolate-inl.h" |
(...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 // Check that function is a constructor. | 985 // Check that function is a constructor. |
986 if (!function->IsConstructor()) { | 986 if (!function->IsConstructor()) { |
987 THROW_NEW_ERROR_RETURN_FAILURE( | 987 THROW_NEW_ERROR_RETURN_FAILURE( |
988 isolate, NewTypeError(MessageTemplate::kNotConstructor, constructor)); | 988 isolate, NewTypeError(MessageTemplate::kNotConstructor, constructor)); |
989 } | 989 } |
990 | 990 |
991 Debug* debug = isolate->debug(); | 991 Debug* debug = isolate->debug(); |
992 // Handle stepping into constructors if step into is active. | 992 // Handle stepping into constructors if step into is active. |
993 if (debug->StepInActive()) debug->HandleStepIn(function, true); | 993 if (debug->StepInActive()) debug->HandleStepIn(function, true); |
994 | 994 |
995 if (function->has_initial_map()) { | |
996 if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { | |
997 // The 'Function' function ignores the receiver object when | |
998 // called using 'new' and creates a new JSFunction object that | |
999 // is returned. The receiver object is only used for error | |
1000 // reporting if an error occurs when constructing the new | |
1001 // JSFunction. Factory::NewJSObject() should not be used to | |
1002 // allocate JSFunctions since it does not properly initialize | |
1003 // the shared part of the function. Since the receiver is | |
1004 // ignored anyway, we use the global object as the receiver | |
1005 // instead of a new JSFunction object. This way, errors are | |
1006 // reported the same way whether or not 'Function' is called | |
1007 // using 'new'. | |
1008 return isolate->global_proxy(); | |
1009 } | |
1010 } | |
1011 | |
1012 // The function should be compiled for the optimization hints to be | 995 // The function should be compiled for the optimization hints to be |
1013 // available. | 996 // available. |
1014 Compiler::Compile(function, CLEAR_EXCEPTION); | 997 Compiler::Compile(function, CLEAR_EXCEPTION); |
1015 | 998 |
1016 Handle<JSObject> result; | 999 JSFunction::EnsureHasInitialMap(function); |
1017 if (site.is_null()) { | 1000 Handle<Map> initial_map = |
1018 result = isolate->factory()->NewJSObject(function); | 1001 JSFunction::EnsureDerivedHasInitialMap(original_function, function); |
1019 } else { | 1002 |
1020 result = isolate->factory()->NewJSObjectWithMemento(function, site); | 1003 if (initial_map->instance_type() == JS_FUNCTION_TYPE) { |
| 1004 // The 'Function' function ignores the receiver object when |
| 1005 // called using 'new' and creates a new JSFunction object that |
| 1006 // is returned. The receiver object is only used for error |
| 1007 // reporting if an error occurs when constructing the new |
| 1008 // JSFunction. Factory::NewJSObject() should not be used to |
| 1009 // allocate JSFunctions since it does not properly initialize |
| 1010 // the shared part of the function. Since the receiver is |
| 1011 // ignored anyway, we use the global object as the receiver |
| 1012 // instead of a new JSFunction object. This way, errors are |
| 1013 // reported the same way whether or not 'Function' is called |
| 1014 // using 'new'. |
| 1015 return isolate->global_proxy(); |
1021 } | 1016 } |
1022 | 1017 |
1023 // Set up the prototoype using original function. | 1018 Handle<JSObject> result = |
1024 // TODO(dslomov): instead of setting the __proto__, | 1019 isolate->factory()->NewJSObjectFromMap(initial_map, NOT_TENURED, site); |
1025 // use and cache the correct map. | |
1026 if (*original_function != *function) { | |
1027 if (original_function->has_instance_prototype()) { | |
1028 Handle<Object> prototype = | |
1029 handle(original_function->instance_prototype(), isolate); | |
1030 MAYBE_RETURN(JSObject::SetPrototype(result, prototype, false, | |
1031 Object::THROW_ON_ERROR), | |
1032 isolate->heap()->exception()); | |
1033 } | |
1034 } | |
1035 | 1020 |
1036 isolate->counters()->constructed_objects()->Increment(); | 1021 isolate->counters()->constructed_objects()->Increment(); |
1037 isolate->counters()->constructed_objects_runtime()->Increment(); | 1022 isolate->counters()->constructed_objects_runtime()->Increment(); |
1038 | 1023 |
1039 return *result; | 1024 return *result; |
1040 } | 1025 } |
1041 | 1026 |
1042 | 1027 |
1043 RUNTIME_FUNCTION(Runtime_NewObject) { | 1028 RUNTIME_FUNCTION(Runtime_NewObject) { |
1044 HandleScope scope(isolate); | 1029 HandleScope scope(isolate); |
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1574 | 1559 |
1575 RUNTIME_FUNCTION(Runtime_ObjectDefineProperties) { | 1560 RUNTIME_FUNCTION(Runtime_ObjectDefineProperties) { |
1576 HandleScope scope(isolate); | 1561 HandleScope scope(isolate); |
1577 DCHECK(args.length() == 2); | 1562 DCHECK(args.length() == 2); |
1578 CONVERT_ARG_HANDLE_CHECKED(Object, o, 0); | 1563 CONVERT_ARG_HANDLE_CHECKED(Object, o, 0); |
1579 CONVERT_ARG_HANDLE_CHECKED(Object, properties, 1); | 1564 CONVERT_ARG_HANDLE_CHECKED(Object, properties, 1); |
1580 return JSReceiver::DefineProperties(isolate, o, properties); | 1565 return JSReceiver::DefineProperties(isolate, o, properties); |
1581 } | 1566 } |
1582 } // namespace internal | 1567 } // namespace internal |
1583 } // namespace v8 | 1568 } // namespace v8 |
OLD | NEW |