| 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 1185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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<String> name, | 1205 Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 1206 MaybeHandle<Object> maybe_prototype, | 1206 MaybeHandle<Code> maybe_code, |
| 1207 MaybeHandle<Code> maybe_code) { | 1207 MaybeHandle<Object> maybe_prototype) { |
| 1208 Handle<SharedFunctionInfo> info = NewSharedFunctionInfo(name); | 1208 Handle<SharedFunctionInfo> info = NewSharedFunctionInfo(name); |
| 1209 ASSERT(info->strict_mode() == SLOPPY); | 1209 ASSERT(info->strict_mode() == SLOPPY); |
| 1210 Handle<Code> code; | 1210 Handle<Code> code; |
| 1211 if (maybe_code.ToHandle(&code)) { | 1211 if (maybe_code.ToHandle(&code)) { |
| 1212 info->set_code(*code); | 1212 info->set_code(*code); |
| 1213 } | 1213 } |
| 1214 Handle<Context> context(isolate()->context()->native_context()); | 1214 Handle<Context> context(isolate()->context()->native_context()); |
| 1215 Handle<Map> map = maybe_prototype.is_null() | 1215 Handle<Map> map = maybe_prototype.is_null() |
| 1216 ? isolate()->sloppy_function_without_prototype_map() | 1216 ? isolate()->sloppy_function_without_prototype_map() |
| 1217 : isolate()->sloppy_function_map(); | 1217 : isolate()->sloppy_function_map(); |
| 1218 Handle<JSFunction> result = NewFunction(map, info, context); | 1218 Handle<JSFunction> result = NewFunction(map, info, context); |
| 1219 Handle<Object> prototype; | 1219 Handle<Object> prototype; |
| 1220 if (maybe_prototype.ToHandle(&prototype)) { | 1220 if (maybe_prototype.ToHandle(&prototype)) { |
| 1221 result->set_prototype_or_initial_map(*prototype); | 1221 result->set_prototype_or_initial_map(*prototype); |
| 1222 } | 1222 } |
| 1223 return result; | 1223 return result; |
| 1224 } | 1224 } |
| 1225 | 1225 |
| 1226 | 1226 |
| 1227 Handle<JSFunction> Factory::NewFunction(Handle<String> name) { | 1227 Handle<JSFunction> Factory::NewFunction(Handle<String> name) { |
| 1228 return NewFunction(name, the_hole_value(), MaybeHandle<Code>()); | 1228 return NewFunction(name, MaybeHandle<Code>(), the_hole_value()); |
| 1229 } | 1229 } |
| 1230 | 1230 |
| 1231 | 1231 |
| 1232 Handle<JSFunction> Factory::NewFunction(MaybeHandle<Object> maybe_prototype, | 1232 Handle<JSFunction> Factory::NewFunction(Handle<Object> prototype, |
| 1233 Handle<String> name, | 1233 Handle<String> name, |
| 1234 InstanceType type, | 1234 InstanceType type, |
| 1235 int instance_size, | 1235 int instance_size, |
| 1236 Handle<Code> code) { | 1236 Handle<Code> code) { |
| 1237 // Allocate the function | 1237 // Allocate the function |
| 1238 Handle<JSFunction> function = NewFunction(name, maybe_prototype, code); | 1238 Handle<JSFunction> function = NewFunction(name, code, prototype); |
| 1239 | 1239 |
| 1240 if (!maybe_prototype.is_null() || | 1240 Handle<Map> initial_map = NewMap( |
| 1241 type != JS_OBJECT_TYPE || | 1241 type, instance_size, GetInitialFastElementsKind()); |
| 1242 instance_size != JSObject::kHeaderSize) { | 1242 if (prototype->IsTheHole() && !function->shared()->is_generator()) { |
| 1243 Handle<Object> prototype = maybe_prototype.ToHandleChecked(); | 1243 prototype = NewFunctionPrototype(function); |
| 1244 Handle<Map> initial_map = NewMap( | |
| 1245 type, instance_size, GetInitialFastElementsKind()); | |
| 1246 if (prototype->IsTheHole() && !function->shared()->is_generator()) { | |
| 1247 prototype = NewFunctionPrototype(function); | |
| 1248 } | |
| 1249 initial_map->set_prototype(*prototype); | |
| 1250 function->set_initial_map(*initial_map); | |
| 1251 initial_map->set_constructor(*function); | |
| 1252 } else { | |
| 1253 ASSERT(!function->has_initial_map()); | |
| 1254 ASSERT(!function->has_prototype()); | |
| 1255 } | 1244 } |
| 1245 initial_map->set_prototype(*prototype); |
| 1246 function->set_initial_map(*initial_map); |
| 1247 initial_map->set_constructor(*function); |
| 1256 | 1248 |
| 1257 return function; | 1249 return function; |
| 1258 } | 1250 } |
| 1259 | 1251 |
| 1260 | 1252 |
| 1261 Handle<JSFunction> Factory::NewFunction(Handle<String> name, | 1253 Handle<JSFunction> Factory::NewFunction(Handle<String> name, |
| 1262 InstanceType type, | 1254 InstanceType type, |
| 1263 int instance_size, | 1255 int instance_size, |
| 1264 Handle<Code> code) { | 1256 Handle<Code> code) { |
| 1265 return NewFunction(the_hole_value(), name, type, instance_size, code); | 1257 return NewFunction(the_hole_value(), name, type, instance_size, code); |
| (...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2053 } | 2045 } |
| 2054 | 2046 |
| 2055 | 2047 |
| 2056 Handle<JSFunction> Factory::CreateApiFunction( | 2048 Handle<JSFunction> Factory::CreateApiFunction( |
| 2057 Handle<FunctionTemplateInfo> obj, | 2049 Handle<FunctionTemplateInfo> obj, |
| 2058 Handle<Object> prototype, | 2050 Handle<Object> prototype, |
| 2059 ApiInstanceType instance_type) { | 2051 ApiInstanceType instance_type) { |
| 2060 Handle<Code> code = isolate()->builtins()->HandleApiCall(); | 2052 Handle<Code> code = isolate()->builtins()->HandleApiCall(); |
| 2061 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); | 2053 Handle<Code> construct_stub = isolate()->builtins()->JSConstructStubApi(); |
| 2062 | 2054 |
| 2063 int internal_field_count = 0; | 2055 Handle<JSFunction> result; |
| 2064 if (!obj->instance_template()->IsUndefined()) { | 2056 if (obj->remove_prototype()) { |
| 2065 Handle<ObjectTemplateInfo> instance_template = | 2057 result = NewFunction(empty_string(), code); |
| 2066 Handle<ObjectTemplateInfo>( | 2058 } else { |
| 2067 ObjectTemplateInfo::cast(obj->instance_template())); | 2059 int internal_field_count = 0; |
| 2068 internal_field_count = | 2060 if (!obj->instance_template()->IsUndefined()) { |
| 2069 Smi::cast(instance_template->internal_field_count())->value(); | 2061 Handle<ObjectTemplateInfo> instance_template = |
| 2062 Handle<ObjectTemplateInfo>( |
| 2063 ObjectTemplateInfo::cast(obj->instance_template())); |
| 2064 internal_field_count = |
| 2065 Smi::cast(instance_template->internal_field_count())->value(); |
| 2066 } |
| 2067 |
| 2068 // TODO(svenpanne) Kill ApiInstanceType and refactor things by generalizing |
| 2069 // JSObject::GetHeaderSize. |
| 2070 int instance_size = kPointerSize * internal_field_count; |
| 2071 InstanceType type; |
| 2072 switch (instance_type) { |
| 2073 case JavaScriptObject: |
| 2074 type = JS_OBJECT_TYPE; |
| 2075 instance_size += JSObject::kHeaderSize; |
| 2076 break; |
| 2077 case InnerGlobalObject: |
| 2078 type = JS_GLOBAL_OBJECT_TYPE; |
| 2079 instance_size += JSGlobalObject::kSize; |
| 2080 break; |
| 2081 case OuterGlobalObject: |
| 2082 type = JS_GLOBAL_PROXY_TYPE; |
| 2083 instance_size += JSGlobalProxy::kSize; |
| 2084 break; |
| 2085 default: |
| 2086 UNREACHABLE(); |
| 2087 type = JS_OBJECT_TYPE; // Keep the compiler happy. |
| 2088 break; |
| 2089 } |
| 2090 |
| 2091 result = NewFunction(prototype, empty_string(), type, instance_size, code); |
| 2070 } | 2092 } |
| 2071 | 2093 |
| 2072 // TODO(svenpanne) Kill ApiInstanceType and refactor things by generalizing | |
| 2073 // JSObject::GetHeaderSize. | |
| 2074 int instance_size = kPointerSize * internal_field_count; | |
| 2075 InstanceType type; | |
| 2076 switch (instance_type) { | |
| 2077 case JavaScriptObject: | |
| 2078 type = JS_OBJECT_TYPE; | |
| 2079 instance_size += JSObject::kHeaderSize; | |
| 2080 break; | |
| 2081 case InnerGlobalObject: | |
| 2082 type = JS_GLOBAL_OBJECT_TYPE; | |
| 2083 instance_size += JSGlobalObject::kSize; | |
| 2084 break; | |
| 2085 case OuterGlobalObject: | |
| 2086 type = JS_GLOBAL_PROXY_TYPE; | |
| 2087 instance_size += JSGlobalProxy::kSize; | |
| 2088 break; | |
| 2089 default: | |
| 2090 UNREACHABLE(); | |
| 2091 type = JS_OBJECT_TYPE; // Keep the compiler happy. | |
| 2092 break; | |
| 2093 } | |
| 2094 | |
| 2095 MaybeHandle<Object> maybe_prototype = prototype; | |
| 2096 if (obj->remove_prototype()) maybe_prototype = MaybeHandle<Object>(); | |
| 2097 | |
| 2098 Handle<JSFunction> result = NewFunction( | |
| 2099 maybe_prototype, Factory::empty_string(), type, instance_size, code); | |
| 2100 | |
| 2101 result->shared()->set_length(obj->length()); | 2094 result->shared()->set_length(obj->length()); |
| 2102 Handle<Object> class_name(obj->class_name(), isolate()); | 2095 Handle<Object> class_name(obj->class_name(), isolate()); |
| 2103 if (class_name->IsString()) { | 2096 if (class_name->IsString()) { |
| 2104 result->shared()->set_instance_class_name(*class_name); | 2097 result->shared()->set_instance_class_name(*class_name); |
| 2105 result->shared()->set_name(*class_name); | 2098 result->shared()->set_name(*class_name); |
| 2106 } | 2099 } |
| 2107 result->shared()->set_function_data(*obj); | 2100 result->shared()->set_function_data(*obj); |
| 2108 result->shared()->set_construct_stub(*construct_stub); | 2101 result->shared()->set_construct_stub(*construct_stub); |
| 2109 result->shared()->DontAdaptArguments(); | 2102 result->shared()->DontAdaptArguments(); |
| 2110 | 2103 |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2316 return Handle<Object>::null(); | 2309 return Handle<Object>::null(); |
| 2317 } | 2310 } |
| 2318 | 2311 |
| 2319 | 2312 |
| 2320 Handle<Object> Factory::ToBoolean(bool value) { | 2313 Handle<Object> Factory::ToBoolean(bool value) { |
| 2321 return value ? true_value() : false_value(); | 2314 return value ? true_value() : false_value(); |
| 2322 } | 2315 } |
| 2323 | 2316 |
| 2324 | 2317 |
| 2325 } } // namespace v8::internal | 2318 } } // namespace v8::internal |
| OLD | NEW |