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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 ? index < static_cast<uint32_t>(String::cast(*object)->length()) | 200 ? index < static_cast<uint32_t>(String::cast(*object)->length()) |
201 : key->Equals(isolate->heap()->length_string())); | 201 : key->Equals(isolate->heap()->length_string())); |
202 } else if (object->IsNull(isolate) || object->IsUndefined(isolate)) { | 202 } else if (object->IsNull(isolate) || object->IsUndefined(isolate)) { |
203 THROW_NEW_ERROR_RETURN_FAILURE( | 203 THROW_NEW_ERROR_RETURN_FAILURE( |
204 isolate, NewTypeError(MessageTemplate::kUndefinedOrNullToObject)); | 204 isolate, NewTypeError(MessageTemplate::kUndefinedOrNullToObject)); |
205 } | 205 } |
206 | 206 |
207 return isolate->heap()->false_value(); | 207 return isolate->heap()->false_value(); |
208 } | 208 } |
209 | 209 |
| 210 // ES6 section 19.1.2.2 Object.create ( O [ , Properties ] ) |
| 211 // TODO(verwaest): Support the common cases with precached map directly in |
| 212 // an Object.create stub. |
| 213 RUNTIME_FUNCTION(Runtime_ObjectCreate) { |
| 214 HandleScope scope(isolate); |
| 215 Handle<Object> prototype = args.at<Object>(0); |
| 216 if (!prototype->IsNull(isolate) && !prototype->IsJSReceiver()) { |
| 217 THROW_NEW_ERROR_RETURN_FAILURE( |
| 218 isolate, NewTypeError(MessageTemplate::kProtoObjectOrNull, prototype)); |
| 219 } |
| 220 |
| 221 // Generate the map with the specified {prototype} based on the Object |
| 222 // function's initial map from the current native context. |
| 223 // TODO(bmeurer): Use a dedicated cache for Object.create; think about |
| 224 // slack tracking for Object.create. |
| 225 Handle<Map> map(isolate->native_context()->object_function()->initial_map(), |
| 226 isolate); |
| 227 if (map->prototype() != *prototype) { |
| 228 if (prototype->IsNull(isolate)) { |
| 229 map = isolate->object_with_null_prototype_map(); |
| 230 } else if (prototype->IsJSObject()) { |
| 231 Handle<JSObject> js_prototype = Handle<JSObject>::cast(prototype); |
| 232 if (!js_prototype->map()->is_prototype_map()) { |
| 233 JSObject::OptimizeAsPrototype(js_prototype, FAST_PROTOTYPE); |
| 234 } |
| 235 Handle<PrototypeInfo> info = |
| 236 Map::GetOrCreatePrototypeInfo(js_prototype, isolate); |
| 237 // TODO(verwaest): Use inobject slack tracking for this map. |
| 238 if (info->HasObjectCreateMap()) { |
| 239 map = handle(info->ObjectCreateMap(), isolate); |
| 240 } else { |
| 241 map = Map::CopyInitialMap(map); |
| 242 Map::SetPrototype(map, prototype, FAST_PROTOTYPE); |
| 243 PrototypeInfo::SetObjectCreateMap(info, map); |
| 244 } |
| 245 } else { |
| 246 map = Map::TransitionToPrototype(map, prototype, REGULAR_PROTOTYPE); |
| 247 } |
| 248 } |
| 249 |
| 250 // Actually allocate the object. |
| 251 Handle<JSObject> object = isolate->factory()->NewJSObjectFromMap(map); |
| 252 |
| 253 // Define the properties if properties was specified and is not undefined. |
| 254 Handle<Object> properties = args.at<Object>(1); |
| 255 if (!properties->IsUndefined(isolate)) { |
| 256 RETURN_FAILURE_ON_EXCEPTION( |
| 257 isolate, JSReceiver::DefineProperties(isolate, object, properties)); |
| 258 } |
| 259 |
| 260 return *object; |
| 261 } |
| 262 |
210 MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate, | 263 MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate, |
211 Handle<Object> object, | 264 Handle<Object> object, |
212 Handle<Object> key, | 265 Handle<Object> key, |
213 Handle<Object> value, | 266 Handle<Object> value, |
214 LanguageMode language_mode) { | 267 LanguageMode language_mode) { |
215 if (object->IsUndefined(isolate) || object->IsNull(isolate)) { | 268 if (object->IsUndefined(isolate) || object->IsNull(isolate)) { |
216 THROW_NEW_ERROR( | 269 THROW_NEW_ERROR( |
217 isolate, | 270 isolate, |
218 NewTypeError(MessageTemplate::kNonObjectPropertyStore, key, object), | 271 NewTypeError(MessageTemplate::kNonObjectPropertyStore, key, object), |
219 Object); | 272 Object); |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 if (!success) return isolate->heap()->exception(); | 935 if (!success) return isolate->heap()->exception(); |
883 MAYBE_RETURN( | 936 MAYBE_RETURN( |
884 JSReceiver::CreateDataProperty(&it, value, Object::THROW_ON_ERROR), | 937 JSReceiver::CreateDataProperty(&it, value, Object::THROW_ON_ERROR), |
885 isolate->heap()->exception()); | 938 isolate->heap()->exception()); |
886 return *value; | 939 return *value; |
887 } | 940 } |
888 | 941 |
889 | 942 |
890 } // namespace internal | 943 } // namespace internal |
891 } // namespace v8 | 944 } // namespace v8 |
OLD | NEW |