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 "factory.h" | 5 #include "factory.h" |
6 | 6 |
7 #include "conversions.h" | 7 #include "conversions.h" |
8 #include "isolate-inl.h" | 8 #include "isolate-inl.h" |
9 #include "macro-assembler.h" | 9 #include "macro-assembler.h" |
10 | 10 |
(...skipping 1184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1195 Handle<SharedFunctionInfo> info, | 1195 Handle<SharedFunctionInfo> info, |
1196 Handle<Context> context, | 1196 Handle<Context> context, |
1197 PretenureFlag pretenure) { | 1197 PretenureFlag pretenure) { |
1198 AllocationSpace space = pretenure == TENURED ? OLD_POINTER_SPACE : NEW_SPACE; | 1198 AllocationSpace space = pretenure == TENURED ? OLD_POINTER_SPACE : NEW_SPACE; |
1199 Handle<JSFunction> result = New<JSFunction>(map, space); | 1199 Handle<JSFunction> result = New<JSFunction>(map, space); |
1200 InitializeFunction(result, info, context); | 1200 InitializeFunction(result, info, context); |
1201 return result; | 1201 return result; |
1202 } | 1202 } |
1203 | 1203 |
1204 | 1204 |
| 1205 Handle<JSFunction> Factory::NewFunction(Handle<Map> map, |
| 1206 Handle<String> name, |
| 1207 MaybeHandle<Code> code) { |
| 1208 Handle<Context> context(isolate()->context()->native_context()); |
| 1209 Handle<SharedFunctionInfo> info = NewSharedFunctionInfo(name, code); |
| 1210 ASSERT((info->strict_mode() == SLOPPY) && |
| 1211 (map.is_identical_to(isolate()->sloppy_function_map()) || |
| 1212 map.is_identical_to( |
| 1213 isolate()->sloppy_function_without_prototype_map()))); |
| 1214 return NewFunction(map, info, context); |
| 1215 } |
| 1216 |
| 1217 |
| 1218 Handle<JSFunction> Factory::NewFunction(Handle<String> name) { |
| 1219 return NewFunction( |
| 1220 isolate()->sloppy_function_map(), name, MaybeHandle<Code>()); |
| 1221 } |
| 1222 |
| 1223 |
| 1224 Handle<JSFunction> Factory::NewFunctionWithoutPrototype(Handle<String> name, |
| 1225 Handle<Code> code) { |
| 1226 return NewFunction( |
| 1227 isolate()->sloppy_function_without_prototype_map(), name, code); |
| 1228 } |
| 1229 |
| 1230 |
1205 Handle<JSFunction> Factory::NewFunction(Handle<String> name, | 1231 Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
1206 MaybeHandle<Code> maybe_code, | 1232 Handle<Code> code, |
1207 MaybeHandle<Object> maybe_prototype) { | 1233 Handle<Object> prototype) { |
1208 Handle<SharedFunctionInfo> info = NewSharedFunctionInfo(name); | 1234 Handle<JSFunction> result = NewFunction( |
1209 ASSERT(info->strict_mode() == SLOPPY); | 1235 isolate()->sloppy_function_map(), name, code); |
1210 Handle<Code> code; | 1236 result->set_prototype_or_initial_map(*prototype); |
1211 if (maybe_code.ToHandle(&code)) { | |
1212 info->set_code(*code); | |
1213 } | |
1214 Handle<Context> context(isolate()->context()->native_context()); | |
1215 Handle<Map> map = maybe_prototype.is_null() | |
1216 ? isolate()->sloppy_function_without_prototype_map() | |
1217 : isolate()->sloppy_function_map(); | |
1218 Handle<JSFunction> result = NewFunction(map, info, context); | |
1219 Handle<Object> prototype; | |
1220 if (maybe_prototype.ToHandle(&prototype)) { | |
1221 result->set_prototype_or_initial_map(*prototype); | |
1222 } | |
1223 return result; | 1237 return result; |
1224 } | 1238 } |
1225 | 1239 |
1226 | 1240 |
1227 Handle<JSFunction> Factory::NewFunction(Handle<String> name) { | 1241 Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
1228 return NewFunction(name, MaybeHandle<Code>(), the_hole_value()); | 1242 Handle<Code> code, |
1229 } | 1243 Handle<Object> prototype, |
1230 | |
1231 | |
1232 Handle<JSFunction> Factory::NewFunction(Handle<Object> prototype, | |
1233 Handle<String> name, | |
1234 InstanceType type, | 1244 InstanceType type, |
1235 int instance_size, | 1245 int instance_size) { |
1236 Handle<Code> code) { | |
1237 // Allocate the function | 1246 // Allocate the function |
1238 Handle<JSFunction> function = NewFunction(name, code, prototype); | 1247 Handle<JSFunction> function = NewFunction(name, code, prototype); |
1239 | 1248 |
1240 Handle<Map> initial_map = NewMap( | 1249 Handle<Map> initial_map = NewMap( |
1241 type, instance_size, GetInitialFastElementsKind()); | 1250 type, instance_size, GetInitialFastElementsKind()); |
1242 if (prototype->IsTheHole() && !function->shared()->is_generator()) { | 1251 if (prototype->IsTheHole() && !function->shared()->is_generator()) { |
1243 prototype = NewFunctionPrototype(function); | 1252 prototype = NewFunctionPrototype(function); |
1244 } | 1253 } |
1245 initial_map->set_prototype(*prototype); | 1254 initial_map->set_prototype(*prototype); |
1246 function->set_initial_map(*initial_map); | 1255 function->set_initial_map(*initial_map); |
1247 initial_map->set_constructor(*function); | 1256 initial_map->set_constructor(*function); |
1248 | 1257 |
1249 return function; | 1258 return function; |
1250 } | 1259 } |
1251 | 1260 |
1252 | 1261 |
1253 Handle<JSFunction> Factory::NewFunction(Handle<String> name, | 1262 Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 1263 Handle<Code> code, |
1254 InstanceType type, | 1264 InstanceType type, |
1255 int instance_size, | 1265 int instance_size) { |
1256 Handle<Code> code) { | 1266 return NewFunction(name, code, the_hole_value(), type, instance_size); |
1257 return NewFunction(the_hole_value(), name, type, instance_size, code); | |
1258 } | 1267 } |
1259 | 1268 |
1260 | 1269 |
1261 Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { | 1270 Handle<JSObject> Factory::NewFunctionPrototype(Handle<JSFunction> function) { |
1262 // Make sure to use globals from the function's context, since the function | 1271 // Make sure to use globals from the function's context, since the function |
1263 // can be from a different context. | 1272 // can be from a different context. |
1264 Handle<Context> native_context(function->context()->native_context()); | 1273 Handle<Context> native_context(function->context()->native_context()); |
1265 Handle<Map> new_map; | 1274 Handle<Map> new_map; |
1266 if (function->shared()->is_generator()) { | 1275 if (function->shared()->is_generator()) { |
1267 // Generator prototypes can share maps since they don't have "constructor" | 1276 // Generator prototypes can share maps since they don't have "constructor" |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1746 // Allocate the backing storage for the properties. | 1755 // Allocate the backing storage for the properties. |
1747 int prop_size = map->InitialPropertiesLength(); | 1756 int prop_size = map->InitialPropertiesLength(); |
1748 Handle<FixedArray> properties = NewFixedArray(prop_size, TENURED); | 1757 Handle<FixedArray> properties = NewFixedArray(prop_size, TENURED); |
1749 | 1758 |
1750 Heap* heap = isolate()->heap(); | 1759 Heap* heap = isolate()->heap(); |
1751 MaybeHandle<SharedFunctionInfo> shared; | 1760 MaybeHandle<SharedFunctionInfo> shared; |
1752 if (type == JS_FUNCTION_TYPE) { | 1761 if (type == JS_FUNCTION_TYPE) { |
1753 OneByteStringKey key(STATIC_ASCII_VECTOR("<freezing call trap>"), | 1762 OneByteStringKey key(STATIC_ASCII_VECTOR("<freezing call trap>"), |
1754 heap->HashSeed()); | 1763 heap->HashSeed()); |
1755 Handle<String> name = InternalizeStringWithKey(&key); | 1764 Handle<String> name = InternalizeStringWithKey(&key); |
1756 shared = NewSharedFunctionInfo(name); | 1765 shared = NewSharedFunctionInfo(name, MaybeHandle<Code>()); |
1757 } | 1766 } |
1758 | 1767 |
1759 // In order to keep heap in consistent state there must be no allocations | 1768 // In order to keep heap in consistent state there must be no allocations |
1760 // before object re-initialization is finished and filler object is installed. | 1769 // before object re-initialization is finished and filler object is installed. |
1761 DisallowHeapAllocation no_allocation; | 1770 DisallowHeapAllocation no_allocation; |
1762 | 1771 |
1763 // Reset the map for the object. | 1772 // Reset the map for the object. |
1764 object->set_map(*map); | 1773 object->set_map(*map); |
1765 Handle<JSObject> jsobj = Handle<JSObject>::cast(object); | 1774 Handle<JSObject> jsobj = Handle<JSObject>::cast(object); |
1766 | 1775 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1841 } | 1850 } |
1842 | 1851 |
1843 | 1852 |
1844 Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( | 1853 Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
1845 Handle<String> name, | 1854 Handle<String> name, |
1846 int number_of_literals, | 1855 int number_of_literals, |
1847 bool is_generator, | 1856 bool is_generator, |
1848 Handle<Code> code, | 1857 Handle<Code> code, |
1849 Handle<ScopeInfo> scope_info, | 1858 Handle<ScopeInfo> scope_info, |
1850 Handle<FixedArray> feedback_vector) { | 1859 Handle<FixedArray> feedback_vector) { |
1851 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name); | 1860 Handle<SharedFunctionInfo> shared = NewSharedFunctionInfo(name, code); |
1852 shared->set_code(*code); | |
1853 shared->set_scope_info(*scope_info); | 1861 shared->set_scope_info(*scope_info); |
1854 shared->set_feedback_vector(*feedback_vector); | 1862 shared->set_feedback_vector(*feedback_vector); |
1855 int literals_array_size = number_of_literals; | 1863 int literals_array_size = number_of_literals; |
1856 // If the function contains object, regexp or array literals, | 1864 // If the function contains object, regexp or array literals, |
1857 // allocate extra space for a literals array prefix containing the | 1865 // allocate extra space for a literals array prefix containing the |
1858 // context. | 1866 // context. |
1859 if (number_of_literals > 0) { | 1867 if (number_of_literals > 0) { |
1860 literals_array_size += JSFunction::kLiteralsPrefixSize; | 1868 literals_array_size += JSFunction::kLiteralsPrefixSize; |
1861 } | 1869 } |
1862 shared->set_num_literals(literals_array_size); | 1870 shared->set_num_literals(literals_array_size); |
(...skipping 20 matching lines...) Expand all Loading... |
1883 message->set_type(*type); | 1891 message->set_type(*type); |
1884 message->set_arguments(*arguments); | 1892 message->set_arguments(*arguments); |
1885 message->set_start_position(start_position); | 1893 message->set_start_position(start_position); |
1886 message->set_end_position(end_position); | 1894 message->set_end_position(end_position); |
1887 message->set_script(*script); | 1895 message->set_script(*script); |
1888 message->set_stack_frames(*stack_frames); | 1896 message->set_stack_frames(*stack_frames); |
1889 return message; | 1897 return message; |
1890 } | 1898 } |
1891 | 1899 |
1892 | 1900 |
1893 Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo(Handle<String> name) { | 1901 Handle<SharedFunctionInfo> Factory::NewSharedFunctionInfo( |
| 1902 Handle<String> name, |
| 1903 MaybeHandle<Code> maybe_code) { |
1894 Handle<Map> map = shared_function_info_map(); | 1904 Handle<Map> map = shared_function_info_map(); |
1895 Handle<SharedFunctionInfo> share = New<SharedFunctionInfo>(map, | 1905 Handle<SharedFunctionInfo> share = New<SharedFunctionInfo>(map, |
1896 OLD_POINTER_SPACE); | 1906 OLD_POINTER_SPACE); |
1897 | 1907 |
1898 // Set pointer fields. | 1908 // Set pointer fields. |
1899 share->set_name(*name); | 1909 share->set_name(*name); |
1900 Code* illegal = isolate()->builtins()->builtin(Builtins::kIllegal); | 1910 Handle<Code> code; |
1901 share->set_code(illegal); | 1911 if (!maybe_code.ToHandle(&code)) { |
| 1912 code = handle(isolate()->builtins()->builtin(Builtins::kIllegal)); |
| 1913 } |
| 1914 share->set_code(*code); |
1902 share->set_optimized_code_map(Smi::FromInt(0)); | 1915 share->set_optimized_code_map(Smi::FromInt(0)); |
1903 share->set_scope_info(ScopeInfo::Empty(isolate())); | 1916 share->set_scope_info(ScopeInfo::Empty(isolate())); |
1904 Code* construct_stub = | 1917 Code* construct_stub = |
1905 isolate()->builtins()->builtin(Builtins::kJSConstructStubGeneric); | 1918 isolate()->builtins()->builtin(Builtins::kJSConstructStubGeneric); |
1906 share->set_construct_stub(construct_stub); | 1919 share->set_construct_stub(construct_stub); |
1907 share->set_instance_class_name(*Object_string()); | 1920 share->set_instance_class_name(*Object_string()); |
1908 share->set_function_data(*undefined_value(), SKIP_WRITE_BARRIER); | 1921 share->set_function_data(*undefined_value(), SKIP_WRITE_BARRIER); |
1909 share->set_script(*undefined_value(), SKIP_WRITE_BARRIER); | 1922 share->set_script(*undefined_value(), SKIP_WRITE_BARRIER); |
1910 share->set_debug_info(*undefined_value(), SKIP_WRITE_BARRIER); | 1923 share->set_debug_info(*undefined_value(), SKIP_WRITE_BARRIER); |
1911 share->set_inferred_name(*empty_string(), SKIP_WRITE_BARRIER); | 1924 share->set_inferred_name(*empty_string(), SKIP_WRITE_BARRIER); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2047 | 2060 |
2048 Handle<JSFunction> Factory::CreateApiFunction( | 2061 Handle<JSFunction> Factory::CreateApiFunction( |
2049 Handle<FunctionTemplateInfo> obj, | 2062 Handle<FunctionTemplateInfo> obj, |
2050 Handle<Object> prototype, | 2063 Handle<Object> prototype, |
2051 ApiInstanceType instance_type) { | 2064 ApiInstanceType instance_type) { |
2052 Handle<Code> code = isolate()->builtins()->HandleApiCall(); | 2065 Handle<Code> code = isolate()->builtins()->HandleApiCall(); |
2053 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); | 2066 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); |
2054 | 2067 |
2055 Handle<JSFunction> result; | 2068 Handle<JSFunction> result; |
2056 if (obj->remove_prototype()) { | 2069 if (obj->remove_prototype()) { |
2057 result = NewFunction(empty_string(), code); | 2070 result = NewFunctionWithoutPrototype(empty_string(), code); |
2058 } else { | 2071 } else { |
2059 int internal_field_count = 0; | 2072 int internal_field_count = 0; |
2060 if (!obj->instance_template()->IsUndefined()) { | 2073 if (!obj->instance_template()->IsUndefined()) { |
2061 Handle<ObjectTemplateInfo> instance_template = | 2074 Handle<ObjectTemplateInfo> instance_template = |
2062 Handle<ObjectTemplateInfo>( | 2075 Handle<ObjectTemplateInfo>( |
2063 ObjectTemplateInfo::cast(obj->instance_template())); | 2076 ObjectTemplateInfo::cast(obj->instance_template())); |
2064 internal_field_count = | 2077 internal_field_count = |
2065 Smi::cast(instance_template->internal_field_count())->value(); | 2078 Smi::cast(instance_template->internal_field_count())->value(); |
2066 } | 2079 } |
2067 | 2080 |
(...skipping 13 matching lines...) Expand all Loading... |
2081 case OuterGlobalObject: | 2094 case OuterGlobalObject: |
2082 type = JS_GLOBAL_PROXY_TYPE; | 2095 type = JS_GLOBAL_PROXY_TYPE; |
2083 instance_size += JSGlobalProxy::kSize; | 2096 instance_size += JSGlobalProxy::kSize; |
2084 break; | 2097 break; |
2085 default: | 2098 default: |
2086 UNREACHABLE(); | 2099 UNREACHABLE(); |
2087 type = JS_OBJECT_TYPE; // Keep the compiler happy. | 2100 type = JS_OBJECT_TYPE; // Keep the compiler happy. |
2088 break; | 2101 break; |
2089 } | 2102 } |
2090 | 2103 |
2091 result = NewFunction(prototype, empty_string(), type, instance_size, code); | 2104 result = NewFunction(empty_string(), code, prototype, type, instance_size); |
2092 } | 2105 } |
2093 | 2106 |
2094 result->shared()->set_length(obj->length()); | 2107 result->shared()->set_length(obj->length()); |
2095 Handle<Object> class_name(obj->class_name(), isolate()); | 2108 Handle<Object> class_name(obj->class_name(), isolate()); |
2096 if (class_name->IsString()) { | 2109 if (class_name->IsString()) { |
2097 result->shared()->set_instance_class_name(*class_name); | 2110 result->shared()->set_instance_class_name(*class_name); |
2098 result->shared()->set_name(*class_name); | 2111 result->shared()->set_name(*class_name); |
2099 } | 2112 } |
2100 result->shared()->set_function_data(*obj); | 2113 result->shared()->set_function_data(*obj); |
2101 result->shared()->set_construct_stub(*construct_stub); | 2114 result->shared()->set_construct_stub(*construct_stub); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2309 return Handle<Object>::null(); | 2322 return Handle<Object>::null(); |
2310 } | 2323 } |
2311 | 2324 |
2312 | 2325 |
2313 Handle<Object> Factory::ToBoolean(bool value) { | 2326 Handle<Object> Factory::ToBoolean(bool value) { |
2314 return value ? true_value() : false_value(); | 2327 return value ? true_value() : false_value(); |
2315 } | 2328 } |
2316 | 2329 |
2317 | 2330 |
2318 } } // namespace v8::internal | 2331 } } // namespace v8::internal |
OLD | NEW |