deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / runtime / runtime-object.cc
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
3 // found in the LICENSE file.
4
5 #include "src/v8.h"
6
7 #include "src/arguments.h"
8 #include "src/bootstrapper.h"
9 #include "src/debug.h"
10 #include "src/runtime/runtime.h"
11 #include "src/runtime/runtime-utils.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // Returns a single character string where first character equals
17 // string->Get(index).
18 static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) {
19   if (index < static_cast<uint32_t>(string->length())) {
20     Factory* factory = string->GetIsolate()->factory();
21     return factory->LookupSingleCharacterStringFromCode(
22         String::Flatten(string)->Get(index));
23   }
24   return Execution::CharAt(string, index);
25 }
26
27
28 MaybeHandle<Object> Runtime::GetElementOrCharAt(Isolate* isolate,
29                                                 Handle<Object> object,
30                                                 uint32_t index) {
31   // Handle [] indexing on Strings
32   if (object->IsString()) {
33     Handle<Object> result = GetCharAt(Handle<String>::cast(object), index);
34     if (!result->IsUndefined()) return result;
35   }
36
37   // Handle [] indexing on String objects
38   if (object->IsStringObjectWithCharacterAt(index)) {
39     Handle<JSValue> js_value = Handle<JSValue>::cast(object);
40     Handle<Object> result =
41         GetCharAt(Handle<String>(String::cast(js_value->value())), index);
42     if (!result->IsUndefined()) return result;
43   }
44
45   Handle<Object> result;
46   if (object->IsString() || object->IsNumber() || object->IsBoolean()) {
47     PrototypeIterator iter(isolate, object);
48     return Object::GetElement(isolate, PrototypeIterator::GetCurrent(iter),
49                               index);
50   } else {
51     return Object::GetElement(isolate, object, index);
52   }
53 }
54
55
56 MaybeHandle<Name> Runtime::ToName(Isolate* isolate, Handle<Object> key) {
57   if (key->IsName()) {
58     return Handle<Name>::cast(key);
59   } else {
60     Handle<Object> converted;
61     ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
62                                Execution::ToString(isolate, key), Name);
63     return Handle<Name>::cast(converted);
64   }
65 }
66
67
68 MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate,
69                                                Handle<Object> object,
70                                                Handle<Object> key) {
71   if (object->IsUndefined() || object->IsNull()) {
72     Handle<Object> args[2] = {key, object};
73     THROW_NEW_ERROR(isolate, NewTypeError("non_object_property_load",
74                                           HandleVector(args, 2)),
75                     Object);
76   }
77
78   // Check if the given key is an array index.
79   uint32_t index;
80   if (key->ToArrayIndex(&index)) {
81     return GetElementOrCharAt(isolate, object, index);
82   }
83
84   // Convert the key to a name - possibly by calling back into JavaScript.
85   Handle<Name> name;
86   ASSIGN_RETURN_ON_EXCEPTION(isolate, name, ToName(isolate, key), Object);
87
88   // Check if the name is trivially convertible to an index and get
89   // the element if so.
90   if (name->AsArrayIndex(&index)) {
91     return GetElementOrCharAt(isolate, object, index);
92   } else {
93     return Object::GetProperty(object, name);
94   }
95 }
96
97
98 MaybeHandle<Object> Runtime::SetObjectProperty(Isolate* isolate,
99                                                Handle<Object> object,
100                                                Handle<Object> key,
101                                                Handle<Object> value,
102                                                LanguageMode language_mode) {
103   if (object->IsUndefined() || object->IsNull()) {
104     Handle<Object> args[2] = {key, object};
105     THROW_NEW_ERROR(isolate, NewTypeError("non_object_property_store",
106                                           HandleVector(args, 2)),
107                     Object);
108   }
109
110   if (object->IsJSProxy()) {
111     Handle<Object> name_object;
112     if (key->IsSymbol()) {
113       name_object = key;
114     } else {
115       ASSIGN_RETURN_ON_EXCEPTION(isolate, name_object,
116                                  Execution::ToString(isolate, key), Object);
117     }
118     Handle<Name> name = Handle<Name>::cast(name_object);
119     return Object::SetProperty(Handle<JSProxy>::cast(object), name, value,
120                                language_mode);
121   }
122
123   // Check if the given key is an array index.
124   uint32_t index;
125   if (key->ToArrayIndex(&index)) {
126     // TODO(verwaest): Support non-JSObject receivers.
127     if (!object->IsJSObject()) return value;
128     Handle<JSObject> js_object = Handle<JSObject>::cast(object);
129
130     // In Firefox/SpiderMonkey, Safari and Opera you can access the characters
131     // of a string using [] notation.  We need to support this too in
132     // JavaScript.
133     // In the case of a String object we just need to redirect the assignment to
134     // the underlying string if the index is in range.  Since the underlying
135     // string does nothing with the assignment then we can ignore such
136     // assignments.
137     if (js_object->IsStringObjectWithCharacterAt(index)) {
138       return value;
139     }
140
141     JSObject::ValidateElements(js_object);
142     if (js_object->HasExternalArrayElements() ||
143         js_object->HasFixedTypedArrayElements()) {
144       if (!value->IsNumber() && !value->IsUndefined()) {
145         ASSIGN_RETURN_ON_EXCEPTION(isolate, value,
146                                    Execution::ToNumber(isolate, value), Object);
147       }
148     }
149
150     MaybeHandle<Object> result = JSObject::SetElement(
151         js_object, index, value, NONE, language_mode, true, SET_PROPERTY);
152     JSObject::ValidateElements(js_object);
153
154     return result.is_null() ? result : value;
155   }
156
157   if (key->IsName()) {
158     Handle<Name> name = Handle<Name>::cast(key);
159     if (name->AsArrayIndex(&index)) {
160       // TODO(verwaest): Support non-JSObject receivers.
161       if (!object->IsJSObject()) return value;
162       Handle<JSObject> js_object = Handle<JSObject>::cast(object);
163       if (js_object->HasExternalArrayElements()) {
164         if (!value->IsNumber() && !value->IsUndefined()) {
165           ASSIGN_RETURN_ON_EXCEPTION(
166               isolate, value, Execution::ToNumber(isolate, value), Object);
167         }
168       }
169       return JSObject::SetElement(js_object, index, value, NONE, language_mode,
170                                   true, SET_PROPERTY);
171     } else {
172       if (name->IsString()) name = String::Flatten(Handle<String>::cast(name));
173       return Object::SetProperty(object, name, value, language_mode);
174     }
175   }
176
177   // Call-back into JavaScript to convert the key to a string.
178   Handle<Object> converted;
179   ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
180                              Execution::ToString(isolate, key), Object);
181   Handle<String> name = Handle<String>::cast(converted);
182
183   if (name->AsArrayIndex(&index)) {
184     // TODO(verwaest): Support non-JSObject receivers.
185     if (!object->IsJSObject()) return value;
186     Handle<JSObject> js_object = Handle<JSObject>::cast(object);
187     return JSObject::SetElement(js_object, index, value, NONE, language_mode,
188                                 true, SET_PROPERTY);
189   }
190   return Object::SetProperty(object, name, value, language_mode);
191 }
192
193
194 MaybeHandle<Object> Runtime::DefineObjectProperty(Handle<JSObject> js_object,
195                                                   Handle<Object> key,
196                                                   Handle<Object> value,
197                                                   PropertyAttributes attrs) {
198   Isolate* isolate = js_object->GetIsolate();
199   // Check if the given key is an array index.
200   uint32_t index;
201   if (key->ToArrayIndex(&index)) {
202     // In Firefox/SpiderMonkey, Safari and Opera you can access the characters
203     // of a string using [] notation.  We need to support this too in
204     // JavaScript.
205     // In the case of a String object we just need to redirect the assignment to
206     // the underlying string if the index is in range.  Since the underlying
207     // string does nothing with the assignment then we can ignore such
208     // assignments.
209     if (js_object->IsStringObjectWithCharacterAt(index)) {
210       return value;
211     }
212
213     return JSObject::SetElement(js_object, index, value, attrs, SLOPPY, false,
214                                 DEFINE_PROPERTY);
215   }
216
217   if (key->IsName()) {
218     Handle<Name> name = Handle<Name>::cast(key);
219     if (name->AsArrayIndex(&index)) {
220       return JSObject::SetElement(js_object, index, value, attrs, SLOPPY, false,
221                                   DEFINE_PROPERTY);
222     } else {
223       if (name->IsString()) name = String::Flatten(Handle<String>::cast(name));
224       return JSObject::SetOwnPropertyIgnoreAttributes(js_object, name, value,
225                                                       attrs);
226     }
227   }
228
229   // Call-back into JavaScript to convert the key to a string.
230   Handle<Object> converted;
231   ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
232                              Execution::ToString(isolate, key), Object);
233   Handle<String> name = Handle<String>::cast(converted);
234
235   if (name->AsArrayIndex(&index)) {
236     return JSObject::SetElement(js_object, index, value, attrs, SLOPPY, false,
237                                 DEFINE_PROPERTY);
238   } else {
239     return JSObject::SetOwnPropertyIgnoreAttributes(js_object, name, value,
240                                                     attrs);
241   }
242 }
243
244
245 MaybeHandle<Object> Runtime::GetPrototype(Isolate* isolate,
246                                           Handle<Object> obj) {
247   // We don't expect access checks to be needed on JSProxy objects.
248   DCHECK(!obj->IsAccessCheckNeeded() || obj->IsJSObject());
249   PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER);
250   do {
251     if (PrototypeIterator::GetCurrent(iter)->IsAccessCheckNeeded() &&
252         !isolate->MayAccess(
253             Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)))) {
254       isolate->ReportFailedAccessCheck(
255           Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
256       RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
257       return isolate->factory()->undefined_value();
258     }
259     iter.AdvanceIgnoringProxies();
260     if (PrototypeIterator::GetCurrent(iter)->IsJSProxy()) {
261       return PrototypeIterator::GetCurrent(iter);
262     }
263   } while (!iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN));
264   return PrototypeIterator::GetCurrent(iter);
265 }
266
267
268 RUNTIME_FUNCTION(Runtime_GetPrototype) {
269   HandleScope scope(isolate);
270   DCHECK(args.length() == 1);
271   CONVERT_ARG_HANDLE_CHECKED(Object, obj, 0);
272   Handle<Object> result;
273   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
274                                      Runtime::GetPrototype(isolate, obj));
275   return *result;
276 }
277
278
279 RUNTIME_FUNCTION(Runtime_InternalSetPrototype) {
280   HandleScope scope(isolate);
281   DCHECK(args.length() == 2);
282   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
283   CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1);
284   DCHECK(!obj->IsAccessCheckNeeded());
285   DCHECK(!obj->map()->is_observed());
286   Handle<Object> result;
287   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
288       isolate, result, JSObject::SetPrototype(obj, prototype, false));
289   return *result;
290 }
291
292
293 RUNTIME_FUNCTION(Runtime_SetPrototype) {
294   HandleScope scope(isolate);
295   DCHECK(args.length() == 2);
296   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
297   CONVERT_ARG_HANDLE_CHECKED(Object, prototype, 1);
298   if (obj->IsAccessCheckNeeded() && !isolate->MayAccess(obj)) {
299     isolate->ReportFailedAccessCheck(obj);
300     RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
301     return isolate->heap()->undefined_value();
302   }
303   if (obj->map()->is_observed()) {
304     Handle<Object> old_value =
305         Object::GetPrototypeSkipHiddenPrototypes(isolate, obj);
306     Handle<Object> result;
307     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
308         isolate, result, JSObject::SetPrototype(obj, prototype, true));
309
310     Handle<Object> new_value =
311         Object::GetPrototypeSkipHiddenPrototypes(isolate, obj);
312     if (!new_value->SameValue(*old_value)) {
313       RETURN_FAILURE_ON_EXCEPTION(
314           isolate, JSObject::EnqueueChangeRecord(
315                        obj, "setPrototype", isolate->factory()->proto_string(),
316                        old_value));
317     }
318     return *result;
319   }
320   Handle<Object> result;
321   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
322       isolate, result, JSObject::SetPrototype(obj, prototype, true));
323   return *result;
324 }
325
326
327 RUNTIME_FUNCTION(Runtime_IsInPrototypeChain) {
328   HandleScope shs(isolate);
329   DCHECK(args.length() == 2);
330   // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8).
331   CONVERT_ARG_HANDLE_CHECKED(Object, O, 0);
332   CONVERT_ARG_HANDLE_CHECKED(Object, V, 1);
333   PrototypeIterator iter(isolate, V, PrototypeIterator::START_AT_RECEIVER);
334   while (true) {
335     iter.AdvanceIgnoringProxies();
336     if (iter.IsAtEnd()) return isolate->heap()->false_value();
337     if (iter.IsAtEnd(O)) return isolate->heap()->true_value();
338   }
339 }
340
341
342 // Enumerator used as indices into the array returned from GetOwnProperty
343 enum PropertyDescriptorIndices {
344   IS_ACCESSOR_INDEX,
345   VALUE_INDEX,
346   GETTER_INDEX,
347   SETTER_INDEX,
348   WRITABLE_INDEX,
349   ENUMERABLE_INDEX,
350   CONFIGURABLE_INDEX,
351   DESCRIPTOR_SIZE
352 };
353
354
355 MUST_USE_RESULT static MaybeHandle<Object> GetOwnProperty(Isolate* isolate,
356                                                           Handle<JSObject> obj,
357                                                           Handle<Name> name) {
358   Heap* heap = isolate->heap();
359   Factory* factory = isolate->factory();
360
361   PropertyAttributes attrs;
362   uint32_t index = 0;
363   Handle<Object> value;
364   MaybeHandle<AccessorPair> maybe_accessors;
365   // TODO(verwaest): Unify once indexed properties can be handled by the
366   // LookupIterator.
367   if (name->AsArrayIndex(&index)) {
368     // Get attributes.
369     Maybe<PropertyAttributes> maybe =
370         JSReceiver::GetOwnElementAttribute(obj, index);
371     if (!maybe.IsJust()) return MaybeHandle<Object>();
372     attrs = maybe.FromJust();
373     if (attrs == ABSENT) return factory->undefined_value();
374
375     // Get AccessorPair if present.
376     maybe_accessors = JSObject::GetOwnElementAccessorPair(obj, index);
377
378     // Get value if not an AccessorPair.
379     if (maybe_accessors.is_null()) {
380       ASSIGN_RETURN_ON_EXCEPTION(
381           isolate, value, Runtime::GetElementOrCharAt(isolate, obj, index),
382           Object);
383     }
384   } else {
385     // Get attributes.
386     LookupIterator it(obj, name, LookupIterator::HIDDEN);
387     Maybe<PropertyAttributes> maybe = JSObject::GetPropertyAttributes(&it);
388     if (!maybe.IsJust()) return MaybeHandle<Object>();
389     attrs = maybe.FromJust();
390     if (attrs == ABSENT) return factory->undefined_value();
391
392     // Get AccessorPair if present.
393     if (it.state() == LookupIterator::ACCESSOR &&
394         it.GetAccessors()->IsAccessorPair()) {
395       maybe_accessors = Handle<AccessorPair>::cast(it.GetAccessors());
396     }
397
398     // Get value if not an AccessorPair.
399     if (maybe_accessors.is_null()) {
400       ASSIGN_RETURN_ON_EXCEPTION(isolate, value, Object::GetProperty(&it),
401                                  Object);
402     }
403   }
404   DCHECK(!isolate->has_pending_exception());
405   Handle<FixedArray> elms = factory->NewFixedArray(DESCRIPTOR_SIZE);
406   elms->set(ENUMERABLE_INDEX, heap->ToBoolean((attrs & DONT_ENUM) == 0));
407   elms->set(CONFIGURABLE_INDEX, heap->ToBoolean((attrs & DONT_DELETE) == 0));
408   elms->set(IS_ACCESSOR_INDEX, heap->ToBoolean(!maybe_accessors.is_null()));
409
410   Handle<AccessorPair> accessors;
411   if (maybe_accessors.ToHandle(&accessors)) {
412     Handle<Object> getter(accessors->GetComponent(ACCESSOR_GETTER), isolate);
413     Handle<Object> setter(accessors->GetComponent(ACCESSOR_SETTER), isolate);
414     elms->set(GETTER_INDEX, *getter);
415     elms->set(SETTER_INDEX, *setter);
416   } else {
417     elms->set(WRITABLE_INDEX, heap->ToBoolean((attrs & READ_ONLY) == 0));
418     elms->set(VALUE_INDEX, *value);
419   }
420
421   return factory->NewJSArrayWithElements(elms);
422 }
423
424
425 // Returns an array with the property description:
426 //  if args[1] is not a property on args[0]
427 //          returns undefined
428 //  if args[1] is a data property on args[0]
429 //         [false, value, Writeable, Enumerable, Configurable]
430 //  if args[1] is an accessor on args[0]
431 //         [true, GetFunction, SetFunction, Enumerable, Configurable]
432 RUNTIME_FUNCTION(Runtime_GetOwnProperty) {
433   HandleScope scope(isolate);
434   DCHECK(args.length() == 2);
435   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
436   CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
437   Handle<Object> result;
438   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
439                                      GetOwnProperty(isolate, obj, name));
440   return *result;
441 }
442
443
444 RUNTIME_FUNCTION(Runtime_PreventExtensions) {
445   HandleScope scope(isolate);
446   DCHECK(args.length() == 1);
447   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
448   Handle<Object> result;
449   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
450                                      JSObject::PreventExtensions(obj));
451   return *result;
452 }
453
454
455 RUNTIME_FUNCTION(Runtime_IsExtensible) {
456   SealHandleScope shs(isolate);
457   DCHECK(args.length() == 1);
458   CONVERT_ARG_CHECKED(JSObject, obj, 0);
459   if (obj->IsJSGlobalProxy()) {
460     PrototypeIterator iter(isolate, obj);
461     if (iter.IsAtEnd()) return isolate->heap()->false_value();
462     DCHECK(iter.GetCurrent()->IsJSGlobalObject());
463     obj = JSObject::cast(iter.GetCurrent());
464   }
465   return isolate->heap()->ToBoolean(obj->map()->is_extensible());
466 }
467
468
469 RUNTIME_FUNCTION(Runtime_DisableAccessChecks) {
470   HandleScope scope(isolate);
471   DCHECK(args.length() == 1);
472   CONVERT_ARG_HANDLE_CHECKED(HeapObject, object, 0);
473   Handle<Map> old_map(object->map());
474   bool needs_access_checks = old_map->is_access_check_needed();
475   if (needs_access_checks) {
476     // Copy map so it won't interfere constructor's initial map.
477     Handle<Map> new_map = Map::Copy(old_map, "DisableAccessChecks");
478     new_map->set_is_access_check_needed(false);
479     JSObject::MigrateToMap(Handle<JSObject>::cast(object), new_map);
480   }
481   return isolate->heap()->ToBoolean(needs_access_checks);
482 }
483
484
485 RUNTIME_FUNCTION(Runtime_EnableAccessChecks) {
486   HandleScope scope(isolate);
487   DCHECK(args.length() == 1);
488   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
489   Handle<Map> old_map(object->map());
490   RUNTIME_ASSERT(!old_map->is_access_check_needed());
491   // Copy map so it won't interfere constructor's initial map.
492   Handle<Map> new_map = Map::Copy(old_map, "EnableAccessChecks");
493   new_map->set_is_access_check_needed(true);
494   JSObject::MigrateToMap(object, new_map);
495   return isolate->heap()->undefined_value();
496 }
497
498
499 RUNTIME_FUNCTION(Runtime_OptimizeObjectForAddingMultipleProperties) {
500   HandleScope scope(isolate);
501   DCHECK(args.length() == 2);
502   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
503   CONVERT_SMI_ARG_CHECKED(properties, 1);
504   // Conservative upper limit to prevent fuzz tests from going OOM.
505   RUNTIME_ASSERT(properties <= 100000);
506   if (object->HasFastProperties() && !object->IsJSGlobalProxy()) {
507     JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties,
508                                   "OptimizeForAdding");
509   }
510   return *object;
511 }
512
513
514 RUNTIME_FUNCTION(Runtime_ObjectFreeze) {
515   HandleScope scope(isolate);
516   DCHECK(args.length() == 1);
517   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
518
519   // %ObjectFreeze is a fast path and these cases are handled elsewhere.
520   RUNTIME_ASSERT(!object->HasSloppyArgumentsElements() &&
521                  !object->map()->is_observed() && !object->IsJSProxy());
522
523   Handle<Object> result;
524   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Freeze(object));
525   return *result;
526 }
527
528
529 RUNTIME_FUNCTION(Runtime_ObjectSeal) {
530   HandleScope scope(isolate);
531   DCHECK(args.length() == 1);
532   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
533
534   // %ObjectSeal is a fast path and these cases are handled elsewhere.
535   RUNTIME_ASSERT(!object->HasSloppyArgumentsElements() &&
536                  !object->map()->is_observed() && !object->IsJSProxy());
537
538   Handle<Object> result;
539   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Seal(object));
540   return *result;
541 }
542
543
544 RUNTIME_FUNCTION(Runtime_GetProperty) {
545   HandleScope scope(isolate);
546   DCHECK(args.length() == 2);
547
548   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
549   CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
550   Handle<Object> result;
551   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
552       isolate, result, Runtime::GetObjectProperty(isolate, object, key));
553   return *result;
554 }
555
556
557 MUST_USE_RESULT static MaybeHandle<Object> TransitionElements(
558     Handle<Object> object, ElementsKind to_kind, Isolate* isolate) {
559   HandleScope scope(isolate);
560   if (!object->IsJSObject()) {
561     isolate->ThrowIllegalOperation();
562     return MaybeHandle<Object>();
563   }
564   ElementsKind from_kind =
565       Handle<JSObject>::cast(object)->map()->elements_kind();
566   if (Map::IsValidElementsTransition(from_kind, to_kind)) {
567     JSObject::TransitionElementsKind(Handle<JSObject>::cast(object), to_kind);
568     return object;
569   }
570   isolate->ThrowIllegalOperation();
571   return MaybeHandle<Object>();
572 }
573
574
575 // KeyedGetProperty is called from KeyedLoadIC::GenerateGeneric.
576 RUNTIME_FUNCTION(Runtime_KeyedGetProperty) {
577   HandleScope scope(isolate);
578   DCHECK(args.length() == 2);
579
580   CONVERT_ARG_HANDLE_CHECKED(Object, receiver_obj, 0);
581   CONVERT_ARG_HANDLE_CHECKED(Object, key_obj, 1);
582
583   // Fast cases for getting named properties of the receiver JSObject
584   // itself.
585   //
586   // The global proxy objects has to be excluded since LookupOwn on
587   // the global proxy object can return a valid result even though the
588   // global proxy object never has properties.  This is the case
589   // because the global proxy object forwards everything to its hidden
590   // prototype including own lookups.
591   //
592   // Additionally, we need to make sure that we do not cache results
593   // for objects that require access checks.
594   if (receiver_obj->IsJSObject()) {
595     if (!receiver_obj->IsJSGlobalProxy() &&
596         !receiver_obj->IsAccessCheckNeeded() && key_obj->IsName()) {
597       DisallowHeapAllocation no_allocation;
598       Handle<JSObject> receiver = Handle<JSObject>::cast(receiver_obj);
599       Handle<Name> key = Handle<Name>::cast(key_obj);
600       if (!receiver->HasFastProperties()) {
601         // Attempt dictionary lookup.
602         NameDictionary* dictionary = receiver->property_dictionary();
603         int entry = dictionary->FindEntry(key);
604         if ((entry != NameDictionary::kNotFound) &&
605             (dictionary->DetailsAt(entry).type() == DATA)) {
606           Object* value = dictionary->ValueAt(entry);
607           if (!receiver->IsGlobalObject()) return value;
608           DCHECK(value->IsPropertyCell());
609           value = PropertyCell::cast(value)->value();
610           if (!value->IsTheHole()) return value;
611           // If value is the hole (meaning, absent) do the general lookup.
612         }
613       }
614     } else if (key_obj->IsSmi()) {
615       // JSObject without a name key. If the key is a Smi, check for a
616       // definite out-of-bounds access to elements, which is a strong indicator
617       // that subsequent accesses will also call the runtime. Proactively
618       // transition elements to FAST_*_ELEMENTS to avoid excessive boxing of
619       // doubles for those future calls in the case that the elements would
620       // become FAST_DOUBLE_ELEMENTS.
621       Handle<JSObject> js_object = Handle<JSObject>::cast(receiver_obj);
622       ElementsKind elements_kind = js_object->GetElementsKind();
623       if (IsFastDoubleElementsKind(elements_kind)) {
624         Handle<Smi> key = Handle<Smi>::cast(key_obj);
625         if (key->value() >= js_object->elements()->length()) {
626           if (IsFastHoleyElementsKind(elements_kind)) {
627             elements_kind = FAST_HOLEY_ELEMENTS;
628           } else {
629             elements_kind = FAST_ELEMENTS;
630           }
631           RETURN_FAILURE_ON_EXCEPTION(
632               isolate, TransitionElements(js_object, elements_kind, isolate));
633         }
634       } else {
635         DCHECK(IsFastSmiOrObjectElementsKind(elements_kind) ||
636                !IsFastElementsKind(elements_kind));
637       }
638     }
639   } else if (receiver_obj->IsString() && key_obj->IsSmi()) {
640     // Fast case for string indexing using [] with a smi index.
641     Handle<String> str = Handle<String>::cast(receiver_obj);
642     int index = args.smi_at(1);
643     if (index >= 0 && index < str->length()) {
644       return *GetCharAt(str, index);
645     }
646   }
647
648   // Fall back to GetObjectProperty.
649   Handle<Object> result;
650   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
651       isolate, result,
652       Runtime::GetObjectProperty(isolate, receiver_obj, key_obj));
653   return *result;
654 }
655
656
657 RUNTIME_FUNCTION(Runtime_AddNamedProperty) {
658   HandleScope scope(isolate);
659   RUNTIME_ASSERT(args.length() == 4);
660
661   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
662   CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
663   CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
664   CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
665
666 #ifdef DEBUG
667   uint32_t index = 0;
668   DCHECK(!key->ToArrayIndex(&index));
669   LookupIterator it(object, key, LookupIterator::OWN_SKIP_INTERCEPTOR);
670   Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(&it);
671   if (!maybe.IsJust()) return isolate->heap()->exception();
672   RUNTIME_ASSERT(!it.IsFound());
673 #endif
674
675   Handle<Object> result;
676   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
677       isolate, result,
678       JSObject::SetOwnPropertyIgnoreAttributes(object, key, value, attrs));
679   return *result;
680 }
681
682
683 RUNTIME_FUNCTION(Runtime_SetProperty) {
684   HandleScope scope(isolate);
685   RUNTIME_ASSERT(args.length() == 4);
686
687   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
688   CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
689   CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
690   CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode_arg, 3);
691   LanguageMode language_mode = language_mode_arg;
692
693   Handle<Object> result;
694   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
695       isolate, result,
696       Runtime::SetObjectProperty(isolate, object, key, value, language_mode));
697   return *result;
698 }
699
700
701 // Adds an element to an array.
702 // This is used to create an indexed data property into an array.
703 RUNTIME_FUNCTION(Runtime_AddElement) {
704   HandleScope scope(isolate);
705   RUNTIME_ASSERT(args.length() == 4);
706
707   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
708   CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
709   CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
710   CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
711
712   uint32_t index = 0;
713   key->ToArrayIndex(&index);
714
715   Handle<Object> result;
716   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
717       isolate, result, JSObject::SetElement(object, index, value, attrs, SLOPPY,
718                                             false, DEFINE_PROPERTY));
719   return *result;
720 }
721
722
723 RUNTIME_FUNCTION(Runtime_DeleteProperty) {
724   HandleScope scope(isolate);
725   DCHECK(args.length() == 3);
726   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0);
727   CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
728   CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode, 2);
729   Handle<Object> result;
730   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
731       isolate, result, JSReceiver::DeleteProperty(object, key, language_mode));
732   return *result;
733 }
734
735
736 static Object* HasOwnPropertyImplementation(Isolate* isolate,
737                                             Handle<JSObject> object,
738                                             Handle<Name> key) {
739   Maybe<bool> maybe = JSReceiver::HasOwnProperty(object, key);
740   if (!maybe.IsJust()) return isolate->heap()->exception();
741   if (maybe.FromJust()) return isolate->heap()->true_value();
742   // Handle hidden prototypes.  If there's a hidden prototype above this thing
743   // then we have to check it for properties, because they are supposed to
744   // look like they are on this object.
745   PrototypeIterator iter(isolate, object);
746   if (!iter.IsAtEnd() &&
747       Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter))
748           ->map()
749           ->is_hidden_prototype()) {
750     // TODO(verwaest): The recursion is not necessary for keys that are array
751     // indices. Removing this.
752     return HasOwnPropertyImplementation(
753         isolate, Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)),
754         key);
755   }
756   RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
757   return isolate->heap()->false_value();
758 }
759
760
761 RUNTIME_FUNCTION(Runtime_HasOwnProperty) {
762   HandleScope scope(isolate);
763   DCHECK(args.length() == 2);
764   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0)
765   CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
766
767   uint32_t index;
768   const bool key_is_array_index = key->AsArrayIndex(&index);
769
770   // Only JS objects can have properties.
771   if (object->IsJSObject()) {
772     Handle<JSObject> js_obj = Handle<JSObject>::cast(object);
773     // Fast case: either the key is a real named property or it is not
774     // an array index and there are no interceptors or hidden
775     // prototypes.
776     Maybe<bool> maybe = Nothing<bool>();
777     if (key_is_array_index) {
778       maybe = JSObject::HasOwnElement(js_obj, index);
779     } else {
780       maybe = JSObject::HasRealNamedProperty(js_obj, key);
781     }
782     if (!maybe.IsJust()) return isolate->heap()->exception();
783     DCHECK(!isolate->has_pending_exception());
784     if (maybe.FromJust()) {
785       return isolate->heap()->true_value();
786     }
787     Map* map = js_obj->map();
788     if (!key_is_array_index && !map->has_named_interceptor() &&
789         !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) {
790       return isolate->heap()->false_value();
791     }
792     // Slow case.
793     return HasOwnPropertyImplementation(isolate, Handle<JSObject>(js_obj),
794                                         Handle<Name>(key));
795   } else if (object->IsString() && key_is_array_index) {
796     // Well, there is one exception:  Handle [] on strings.
797     Handle<String> string = Handle<String>::cast(object);
798     if (index < static_cast<uint32_t>(string->length())) {
799       return isolate->heap()->true_value();
800     }
801   }
802   return isolate->heap()->false_value();
803 }
804
805
806 RUNTIME_FUNCTION(Runtime_HasProperty) {
807   HandleScope scope(isolate);
808   DCHECK(args.length() == 2);
809   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0);
810   CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
811
812   Maybe<bool> maybe = JSReceiver::HasProperty(receiver, key);
813   if (!maybe.IsJust()) return isolate->heap()->exception();
814   return isolate->heap()->ToBoolean(maybe.FromJust());
815 }
816
817
818 RUNTIME_FUNCTION(Runtime_HasElement) {
819   HandleScope scope(isolate);
820   DCHECK(args.length() == 2);
821   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0);
822   CONVERT_SMI_ARG_CHECKED(index, 1);
823
824   Maybe<bool> maybe = JSReceiver::HasElement(receiver, index);
825   if (!maybe.IsJust()) return isolate->heap()->exception();
826   return isolate->heap()->ToBoolean(maybe.FromJust());
827 }
828
829
830 RUNTIME_FUNCTION(Runtime_IsPropertyEnumerable) {
831   HandleScope scope(isolate);
832   DCHECK(args.length() == 2);
833
834   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
835   CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
836
837   Maybe<PropertyAttributes> maybe =
838       JSReceiver::GetOwnPropertyAttributes(object, key);
839   if (!maybe.IsJust()) return isolate->heap()->exception();
840   if (maybe.FromJust() == ABSENT) maybe = Just(DONT_ENUM);
841   return isolate->heap()->ToBoolean((maybe.FromJust() & DONT_ENUM) == 0);
842 }
843
844
845 RUNTIME_FUNCTION(Runtime_GetPropertyNames) {
846   HandleScope scope(isolate);
847   DCHECK(args.length() == 1);
848   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0);
849   Handle<JSArray> result;
850
851   isolate->counters()->for_in()->Increment();
852   Handle<FixedArray> elements;
853   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
854       isolate, elements,
855       JSReceiver::GetKeys(object, JSReceiver::INCLUDE_PROTOS));
856   return *isolate->factory()->NewJSArrayWithElements(elements);
857 }
858
859
860 // Returns either a FixedArray as Runtime_GetPropertyNames,
861 // or, if the given object has an enum cache that contains
862 // all enumerable properties of the object and its prototypes
863 // have none, the map of the object. This is used to speed up
864 // the check for deletions during a for-in.
865 RUNTIME_FUNCTION(Runtime_GetPropertyNamesFast) {
866   SealHandleScope shs(isolate);
867   DCHECK(args.length() == 1);
868
869   CONVERT_ARG_CHECKED(JSReceiver, raw_object, 0);
870
871   if (raw_object->IsSimpleEnum()) return raw_object->map();
872
873   HandleScope scope(isolate);
874   Handle<JSReceiver> object(raw_object);
875   Handle<FixedArray> content;
876   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
877       isolate, content,
878       JSReceiver::GetKeys(object, JSReceiver::INCLUDE_PROTOS));
879
880   // Test again, since cache may have been built by preceding call.
881   if (object->IsSimpleEnum()) return object->map();
882
883   return *content;
884 }
885
886
887 // Find the length of the prototype chain that is to be handled as one. If a
888 // prototype object is hidden it is to be viewed as part of the the object it
889 // is prototype for.
890 static int OwnPrototypeChainLength(JSObject* obj) {
891   int count = 1;
892   for (PrototypeIterator iter(obj->GetIsolate(), obj);
893        !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) {
894     count++;
895   }
896   return count;
897 }
898
899
900 // Return the names of the own named properties.
901 // args[0]: object
902 // args[1]: PropertyAttributes as int
903 RUNTIME_FUNCTION(Runtime_GetOwnPropertyNames) {
904   HandleScope scope(isolate);
905   DCHECK(args.length() == 2);
906   if (!args[0]->IsJSObject()) {
907     return isolate->heap()->undefined_value();
908   }
909   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
910   CONVERT_SMI_ARG_CHECKED(filter_value, 1);
911   PropertyAttributes filter = static_cast<PropertyAttributes>(filter_value);
912
913   // Skip the global proxy as it has no properties and always delegates to the
914   // real global object.
915   if (obj->IsJSGlobalProxy()) {
916     // Only collect names if access is permitted.
917     if (obj->IsAccessCheckNeeded() && !isolate->MayAccess(obj)) {
918       isolate->ReportFailedAccessCheck(obj);
919       RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
920       return *isolate->factory()->NewJSArray(0);
921     }
922     PrototypeIterator iter(isolate, obj);
923     obj = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
924   }
925
926   // Find the number of objects making up this.
927   int length = OwnPrototypeChainLength(*obj);
928
929   // Find the number of own properties for each of the objects.
930   ScopedVector<int> own_property_count(length);
931   int total_property_count = 0;
932   {
933     PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER);
934     for (int i = 0; i < length; i++) {
935       DCHECK(!iter.IsAtEnd());
936       Handle<JSObject> jsproto =
937           Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
938       // Only collect names if access is permitted.
939       if (jsproto->IsAccessCheckNeeded() && !isolate->MayAccess(jsproto)) {
940         isolate->ReportFailedAccessCheck(jsproto);
941         RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
942         return *isolate->factory()->NewJSArray(0);
943       }
944       int n;
945       n = jsproto->NumberOfOwnProperties(filter);
946       own_property_count[i] = n;
947       total_property_count += n;
948       iter.Advance();
949     }
950   }
951
952   // Allocate an array with storage for all the property names.
953   Handle<FixedArray> names =
954       isolate->factory()->NewFixedArray(total_property_count);
955
956   // Get the property names.
957   int next_copy_index = 0;
958   int hidden_strings = 0;
959   {
960     PrototypeIterator iter(isolate, obj, PrototypeIterator::START_AT_RECEIVER);
961     for (int i = 0; i < length; i++) {
962       DCHECK(!iter.IsAtEnd());
963       Handle<JSObject> jsproto =
964           Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
965       jsproto->GetOwnPropertyNames(*names, next_copy_index, filter);
966       // Names from hidden prototypes may already have been added
967       // for inherited function template instances. Count the duplicates
968       // and stub them out; the final copy pass at the end ignores holes.
969       for (int j = next_copy_index; j < next_copy_index + own_property_count[i];
970            j++) {
971         Object* name_from_hidden_proto = names->get(j);
972         if (isolate->IsInternallyUsedPropertyName(name_from_hidden_proto)) {
973           hidden_strings++;
974         } else {
975           for (int k = 0; k < next_copy_index; k++) {
976             Object* name = names->get(k);
977             if (name_from_hidden_proto == name) {
978               names->set(j, isolate->heap()->hidden_string());
979               hidden_strings++;
980               break;
981             }
982           }
983         }
984       }
985       next_copy_index += own_property_count[i];
986
987       iter.Advance();
988     }
989   }
990
991   // Filter out name of hidden properties object and
992   // hidden prototype duplicates.
993   if (hidden_strings > 0) {
994     Handle<FixedArray> old_names = names;
995     names = isolate->factory()->NewFixedArray(names->length() - hidden_strings);
996     int dest_pos = 0;
997     for (int i = 0; i < total_property_count; i++) {
998       Object* name = old_names->get(i);
999       if (isolate->IsInternallyUsedPropertyName(name)) {
1000         hidden_strings--;
1001         continue;
1002       }
1003       names->set(dest_pos++, name);
1004     }
1005     DCHECK_EQ(0, hidden_strings);
1006   }
1007
1008   return *isolate->factory()->NewJSArrayWithElements(names);
1009 }
1010
1011
1012 // Return the names of the own indexed properties.
1013 // args[0]: object
1014 RUNTIME_FUNCTION(Runtime_GetOwnElementNames) {
1015   HandleScope scope(isolate);
1016   DCHECK(args.length() == 1);
1017   if (!args[0]->IsJSObject()) {
1018     return isolate->heap()->undefined_value();
1019   }
1020   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1021
1022   int n = obj->NumberOfOwnElements(NONE);
1023   Handle<FixedArray> names = isolate->factory()->NewFixedArray(n);
1024   obj->GetOwnElementKeys(*names, NONE);
1025   return *isolate->factory()->NewJSArrayWithElements(names);
1026 }
1027
1028
1029 // Return information on whether an object has a named or indexed interceptor.
1030 // args[0]: object
1031 RUNTIME_FUNCTION(Runtime_GetInterceptorInfo) {
1032   HandleScope scope(isolate);
1033   DCHECK(args.length() == 1);
1034   if (!args[0]->IsJSObject()) {
1035     return Smi::FromInt(0);
1036   }
1037   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1038
1039   int result = 0;
1040   if (obj->HasNamedInterceptor()) result |= 2;
1041   if (obj->HasIndexedInterceptor()) result |= 1;
1042
1043   return Smi::FromInt(result);
1044 }
1045
1046
1047 // Return property names from named interceptor.
1048 // args[0]: object
1049 RUNTIME_FUNCTION(Runtime_GetNamedInterceptorPropertyNames) {
1050   HandleScope scope(isolate);
1051   DCHECK(args.length() == 1);
1052   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1053
1054   if (obj->HasNamedInterceptor()) {
1055     Handle<JSObject> result;
1056     if (JSObject::GetKeysForNamedInterceptor(obj, obj).ToHandle(&result)) {
1057       return *result;
1058     }
1059   }
1060   return isolate->heap()->undefined_value();
1061 }
1062
1063
1064 // Return element names from indexed interceptor.
1065 // args[0]: object
1066 RUNTIME_FUNCTION(Runtime_GetIndexedInterceptorElementNames) {
1067   HandleScope scope(isolate);
1068   DCHECK(args.length() == 1);
1069   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1070
1071   if (obj->HasIndexedInterceptor()) {
1072     Handle<JSObject> result;
1073     if (JSObject::GetKeysForIndexedInterceptor(obj, obj).ToHandle(&result)) {
1074       return *result;
1075     }
1076   }
1077   return isolate->heap()->undefined_value();
1078 }
1079
1080
1081 RUNTIME_FUNCTION(Runtime_OwnKeys) {
1082   HandleScope scope(isolate);
1083   DCHECK(args.length() == 1);
1084   CONVERT_ARG_CHECKED(JSObject, raw_object, 0);
1085   Handle<JSObject> object(raw_object);
1086
1087   if (object->IsJSGlobalProxy()) {
1088     // Do access checks before going to the global object.
1089     if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
1090       isolate->ReportFailedAccessCheck(object);
1091       RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
1092       return *isolate->factory()->NewJSArray(0);
1093     }
1094
1095     PrototypeIterator iter(isolate, object);
1096     // If proxy is detached we simply return an empty array.
1097     if (iter.IsAtEnd()) return *isolate->factory()->NewJSArray(0);
1098     object = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
1099   }
1100
1101   Handle<FixedArray> contents;
1102   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1103       isolate, contents, JSReceiver::GetKeys(object, JSReceiver::OWN_ONLY));
1104
1105   // Some fast paths through GetKeysInFixedArrayFor reuse a cached
1106   // property array and since the result is mutable we have to create
1107   // a fresh clone on each invocation.
1108   int length = contents->length();
1109   Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length);
1110   for (int i = 0; i < length; i++) {
1111     Object* entry = contents->get(i);
1112     if (entry->IsString()) {
1113       copy->set(i, entry);
1114     } else {
1115       DCHECK(entry->IsNumber());
1116       HandleScope scope(isolate);
1117       Handle<Object> entry_handle(entry, isolate);
1118       Handle<Object> entry_str =
1119           isolate->factory()->NumberToString(entry_handle);
1120       copy->set(i, *entry_str);
1121     }
1122   }
1123   return *isolate->factory()->NewJSArrayWithElements(copy);
1124 }
1125
1126
1127 RUNTIME_FUNCTION(Runtime_ToFastProperties) {
1128   HandleScope scope(isolate);
1129   DCHECK(args.length() == 1);
1130   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
1131   if (object->IsJSObject() && !object->IsGlobalObject()) {
1132     JSObject::MigrateSlowToFast(Handle<JSObject>::cast(object), 0,
1133                                 "RuntimeToFastProperties");
1134   }
1135   return *object;
1136 }
1137
1138
1139 RUNTIME_FUNCTION(Runtime_ToBool) {
1140   SealHandleScope shs(isolate);
1141   DCHECK(args.length() == 1);
1142   CONVERT_ARG_CHECKED(Object, object, 0);
1143
1144   return isolate->heap()->ToBoolean(object->BooleanValue());
1145 }
1146
1147
1148 // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47).
1149 // Possible optimizations: put the type string into the oddballs.
1150 RUNTIME_FUNCTION(Runtime_Typeof) {
1151   SealHandleScope shs(isolate);
1152   DCHECK(args.length() == 1);
1153   CONVERT_ARG_CHECKED(Object, obj, 0);
1154   if (obj->IsNumber()) return isolate->heap()->number_string();
1155   HeapObject* heap_obj = HeapObject::cast(obj);
1156
1157   // typeof an undetectable object is 'undefined'
1158   if (heap_obj->map()->is_undetectable()) {
1159     return isolate->heap()->undefined_string();
1160   }
1161
1162   InstanceType instance_type = heap_obj->map()->instance_type();
1163   if (instance_type < FIRST_NONSTRING_TYPE) {
1164     return isolate->heap()->string_string();
1165   }
1166
1167   switch (instance_type) {
1168     case ODDBALL_TYPE:
1169       if (heap_obj->IsTrue() || heap_obj->IsFalse()) {
1170         return isolate->heap()->boolean_string();
1171       }
1172       if (heap_obj->IsNull()) {
1173         return isolate->heap()->object_string();
1174       }
1175       DCHECK(heap_obj->IsUndefined());
1176       return isolate->heap()->undefined_string();
1177     case SYMBOL_TYPE:
1178       return isolate->heap()->symbol_string();
1179     case JS_FUNCTION_TYPE:
1180     case JS_FUNCTION_PROXY_TYPE:
1181       return isolate->heap()->function_string();
1182     default:
1183       // For any kind of object not handled above, the spec rule for
1184       // host objects gives that it is okay to return "object"
1185       return isolate->heap()->object_string();
1186   }
1187 }
1188
1189
1190 RUNTIME_FUNCTION(Runtime_NewStringWrapper) {
1191   HandleScope scope(isolate);
1192   DCHECK(args.length() == 1);
1193   CONVERT_ARG_HANDLE_CHECKED(String, value, 0);
1194   return *Object::ToObject(isolate, value).ToHandleChecked();
1195 }
1196
1197
1198 RUNTIME_FUNCTION(Runtime_AllocateHeapNumber) {
1199   HandleScope scope(isolate);
1200   DCHECK(args.length() == 0);
1201   return *isolate->factory()->NewHeapNumber(0);
1202 }
1203
1204
1205 static Object* Runtime_NewObjectHelper(Isolate* isolate,
1206                                        Handle<Object> constructor,
1207                                        Handle<Object> original_constructor,
1208                                        Handle<AllocationSite> site) {
1209   // If the constructor isn't a proper function we throw a type error.
1210   if (!constructor->IsJSFunction()) {
1211     Vector<Handle<Object> > arguments = HandleVector(&constructor, 1);
1212     THROW_NEW_ERROR_RETURN_FAILURE(isolate,
1213                                    NewTypeError("not_constructor", arguments));
1214   }
1215
1216   Handle<JSFunction> function = Handle<JSFunction>::cast(constructor);
1217
1218   CHECK(original_constructor->IsJSFunction());
1219   Handle<JSFunction> original_function =
1220       Handle<JSFunction>::cast(original_constructor);
1221
1222
1223   // If function should not have prototype, construction is not allowed. In this
1224   // case generated code bailouts here, since function has no initial_map.
1225   if (!function->should_have_prototype() && !function->shared()->bound()) {
1226     Vector<Handle<Object> > arguments = HandleVector(&constructor, 1);
1227     THROW_NEW_ERROR_RETURN_FAILURE(isolate,
1228                                    NewTypeError("not_constructor", arguments));
1229   }
1230
1231   Debug* debug = isolate->debug();
1232   // Handle stepping into constructors if step into is active.
1233   if (debug->StepInActive()) {
1234     debug->HandleStepIn(function, Handle<Object>::null(), 0, true);
1235   }
1236
1237   if (function->has_initial_map()) {
1238     if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) {
1239       // The 'Function' function ignores the receiver object when
1240       // called using 'new' and creates a new JSFunction object that
1241       // is returned.  The receiver object is only used for error
1242       // reporting if an error occurs when constructing the new
1243       // JSFunction. Factory::NewJSObject() should not be used to
1244       // allocate JSFunctions since it does not properly initialize
1245       // the shared part of the function. Since the receiver is
1246       // ignored anyway, we use the global object as the receiver
1247       // instead of a new JSFunction object. This way, errors are
1248       // reported the same way whether or not 'Function' is called
1249       // using 'new'.
1250       return isolate->global_proxy();
1251     }
1252   }
1253
1254   // The function should be compiled for the optimization hints to be
1255   // available.
1256   Compiler::EnsureCompiled(function, CLEAR_EXCEPTION);
1257
1258   Handle<JSObject> result;
1259   if (site.is_null()) {
1260     result = isolate->factory()->NewJSObject(function);
1261   } else {
1262     result = isolate->factory()->NewJSObjectWithMemento(function, site);
1263   }
1264
1265   // Set up the prototoype using original function.
1266   // TODO(dslomov): instead of setting the __proto__,
1267   // use and cache the correct map.
1268   if (*original_function != *function) {
1269     if (original_function->has_instance_prototype()) {
1270       Handle<Object> prototype =
1271           handle(original_function->instance_prototype(), isolate);
1272       RETURN_FAILURE_ON_EXCEPTION(
1273           isolate, JSObject::SetPrototype(result, prototype, false));
1274     }
1275   }
1276
1277   isolate->counters()->constructed_objects()->Increment();
1278   isolate->counters()->constructed_objects_runtime()->Increment();
1279
1280   return *result;
1281 }
1282
1283
1284 RUNTIME_FUNCTION(Runtime_NewObject) {
1285   HandleScope scope(isolate);
1286   DCHECK(args.length() == 2);
1287   CONVERT_ARG_HANDLE_CHECKED(Object, constructor, 0);
1288   CONVERT_ARG_HANDLE_CHECKED(Object, original_constructor, 1);
1289   return Runtime_NewObjectHelper(isolate, constructor, original_constructor,
1290                                  Handle<AllocationSite>::null());
1291 }
1292
1293
1294 RUNTIME_FUNCTION(Runtime_NewObjectWithAllocationSite) {
1295   HandleScope scope(isolate);
1296   DCHECK(args.length() == 3);
1297   CONVERT_ARG_HANDLE_CHECKED(Object, original_constructor, 2);
1298   CONVERT_ARG_HANDLE_CHECKED(Object, constructor, 1);
1299   CONVERT_ARG_HANDLE_CHECKED(Object, feedback, 0);
1300   Handle<AllocationSite> site;
1301   if (feedback->IsAllocationSite()) {
1302     // The feedback can be an AllocationSite or undefined.
1303     site = Handle<AllocationSite>::cast(feedback);
1304   }
1305   return Runtime_NewObjectHelper(isolate, constructor, original_constructor,
1306                                  site);
1307 }
1308
1309
1310 RUNTIME_FUNCTION(Runtime_FinalizeInstanceSize) {
1311   HandleScope scope(isolate);
1312   DCHECK(args.length() == 1);
1313
1314   CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0);
1315   function->CompleteInobjectSlackTracking();
1316
1317   return isolate->heap()->undefined_value();
1318 }
1319
1320
1321 RUNTIME_FUNCTION(Runtime_GlobalProxy) {
1322   SealHandleScope shs(isolate);
1323   DCHECK(args.length() == 1);
1324   CONVERT_ARG_CHECKED(Object, global, 0);
1325   if (!global->IsJSGlobalObject()) return isolate->heap()->null_value();
1326   return JSGlobalObject::cast(global)->global_proxy();
1327 }
1328
1329
1330 RUNTIME_FUNCTION(Runtime_LookupAccessor) {
1331   HandleScope scope(isolate);
1332   DCHECK(args.length() == 3);
1333   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, receiver, 0);
1334   CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1335   CONVERT_SMI_ARG_CHECKED(flag, 2);
1336   AccessorComponent component = flag == 0 ? ACCESSOR_GETTER : ACCESSOR_SETTER;
1337   if (!receiver->IsJSObject()) return isolate->heap()->undefined_value();
1338   Handle<Object> result;
1339   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1340       isolate, result,
1341       JSObject::GetAccessor(Handle<JSObject>::cast(receiver), name, component));
1342   return *result;
1343 }
1344
1345
1346 RUNTIME_FUNCTION(Runtime_LoadMutableDouble) {
1347   HandleScope scope(isolate);
1348   DCHECK(args.length() == 2);
1349   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1350   CONVERT_ARG_HANDLE_CHECKED(Smi, index, 1);
1351   RUNTIME_ASSERT((index->value() & 1) == 1);
1352   FieldIndex field_index =
1353       FieldIndex::ForLoadByFieldIndex(object->map(), index->value());
1354   if (field_index.is_inobject()) {
1355     RUNTIME_ASSERT(field_index.property_index() <
1356                    object->map()->inobject_properties());
1357   } else {
1358     RUNTIME_ASSERT(field_index.outobject_array_index() <
1359                    object->properties()->length());
1360   }
1361   return *JSObject::FastPropertyAt(object, Representation::Double(),
1362                                    field_index);
1363 }
1364
1365
1366 RUNTIME_FUNCTION(Runtime_TryMigrateInstance) {
1367   HandleScope scope(isolate);
1368   DCHECK(args.length() == 1);
1369   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
1370   if (!object->IsJSObject()) return Smi::FromInt(0);
1371   Handle<JSObject> js_object = Handle<JSObject>::cast(object);
1372   if (!js_object->map()->is_deprecated()) return Smi::FromInt(0);
1373   // This call must not cause lazy deopts, because it's called from deferred
1374   // code where we can't handle lazy deopts for lack of a suitable bailout
1375   // ID. So we just try migration and signal failure if necessary,
1376   // which will also trigger a deopt.
1377   if (!JSObject::TryMigrateInstance(js_object)) return Smi::FromInt(0);
1378   return *object;
1379 }
1380
1381
1382 RUNTIME_FUNCTION(Runtime_IsJSGlobalProxy) {
1383   SealHandleScope shs(isolate);
1384   DCHECK(args.length() == 1);
1385   CONVERT_ARG_CHECKED(Object, obj, 0);
1386   return isolate->heap()->ToBoolean(obj->IsJSGlobalProxy());
1387 }
1388
1389
1390 static bool IsValidAccessor(Handle<Object> obj) {
1391   return obj->IsUndefined() || obj->IsSpecFunction() || obj->IsNull();
1392 }
1393
1394
1395 // Implements part of 8.12.9 DefineOwnProperty.
1396 // There are 3 cases that lead here:
1397 // Step 4b - define a new accessor property.
1398 // Steps 9c & 12 - replace an existing data property with an accessor property.
1399 // Step 12 - update an existing accessor property with an accessor or generic
1400 //           descriptor.
1401 RUNTIME_FUNCTION(Runtime_DefineAccessorPropertyUnchecked) {
1402   HandleScope scope(isolate);
1403   DCHECK(args.length() == 5);
1404   CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0);
1405   RUNTIME_ASSERT(!obj->IsNull());
1406   CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1407   CONVERT_ARG_HANDLE_CHECKED(Object, getter, 2);
1408   RUNTIME_ASSERT(IsValidAccessor(getter));
1409   CONVERT_ARG_HANDLE_CHECKED(Object, setter, 3);
1410   RUNTIME_ASSERT(IsValidAccessor(setter));
1411   CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 4);
1412
1413   RETURN_FAILURE_ON_EXCEPTION(
1414       isolate, JSObject::DefineAccessor(obj, name, getter, setter, attrs));
1415   return isolate->heap()->undefined_value();
1416 }
1417
1418
1419 // Implements part of 8.12.9 DefineOwnProperty.
1420 // There are 3 cases that lead here:
1421 // Step 4a - define a new data property.
1422 // Steps 9b & 12 - replace an existing accessor property with a data property.
1423 // Step 12 - update an existing data property with a data or generic
1424 //           descriptor.
1425 RUNTIME_FUNCTION(Runtime_DefineDataPropertyUnchecked) {
1426   HandleScope scope(isolate);
1427   DCHECK(args.length() == 4);
1428   CONVERT_ARG_HANDLE_CHECKED(JSObject, js_object, 0);
1429   CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1430   CONVERT_ARG_HANDLE_CHECKED(Object, obj_value, 2);
1431   CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1432
1433   LookupIterator it(js_object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
1434   if (it.IsFound() && it.state() == LookupIterator::ACCESS_CHECK) {
1435     if (!isolate->MayAccess(js_object)) {
1436       return isolate->heap()->undefined_value();
1437     }
1438     it.Next();
1439   }
1440
1441   // Take special care when attributes are different and there is already
1442   // a property.
1443   if (it.state() == LookupIterator::ACCESSOR) {
1444     // Use IgnoreAttributes version since a readonly property may be
1445     // overridden and SetProperty does not allow this.
1446     Handle<Object> result;
1447     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1448         isolate, result,
1449         JSObject::SetOwnPropertyIgnoreAttributes(
1450             js_object, name, obj_value, attrs, JSObject::DONT_FORCE_FIELD));
1451     return *result;
1452   }
1453
1454   Handle<Object> result;
1455   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1456       isolate, result,
1457       Runtime::DefineObjectProperty(js_object, name, obj_value, attrs));
1458   return *result;
1459 }
1460
1461
1462 // Return property without being observable by accessors or interceptors.
1463 RUNTIME_FUNCTION(Runtime_GetDataProperty) {
1464   HandleScope scope(isolate);
1465   DCHECK(args.length() == 2);
1466   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1467   CONVERT_ARG_HANDLE_CHECKED(Name, key, 1);
1468   return *JSObject::GetDataProperty(object, key);
1469 }
1470
1471
1472 RUNTIME_FUNCTION(Runtime_HasFastPackedElements) {
1473   SealHandleScope shs(isolate);
1474   DCHECK(args.length() == 1);
1475   CONVERT_ARG_CHECKED(HeapObject, obj, 0);
1476   return isolate->heap()->ToBoolean(
1477       IsFastPackedElementsKind(obj->map()->elements_kind()));
1478 }
1479
1480
1481 RUNTIME_FUNCTION(Runtime_ValueOf) {
1482   SealHandleScope shs(isolate);
1483   DCHECK(args.length() == 1);
1484   CONVERT_ARG_CHECKED(Object, obj, 0);
1485   if (!obj->IsJSValue()) return obj;
1486   return JSValue::cast(obj)->value();
1487 }
1488
1489
1490 RUNTIME_FUNCTION(Runtime_SetValueOf) {
1491   SealHandleScope shs(isolate);
1492   DCHECK(args.length() == 2);
1493   CONVERT_ARG_CHECKED(Object, obj, 0);
1494   CONVERT_ARG_CHECKED(Object, value, 1);
1495   if (!obj->IsJSValue()) return value;
1496   JSValue::cast(obj)->set_value(value);
1497   return value;
1498 }
1499
1500
1501 RUNTIME_FUNCTION(Runtime_JSValueGetValue) {
1502   SealHandleScope shs(isolate);
1503   DCHECK(args.length() == 1);
1504   CONVERT_ARG_CHECKED(JSValue, obj, 0);
1505   return JSValue::cast(obj)->value();
1506 }
1507
1508
1509 RUNTIME_FUNCTION(Runtime_HeapObjectGetMap) {
1510   SealHandleScope shs(isolate);
1511   DCHECK(args.length() == 1);
1512   CONVERT_ARG_CHECKED(HeapObject, obj, 0);
1513   return obj->map();
1514 }
1515
1516
1517 RUNTIME_FUNCTION(Runtime_MapGetInstanceType) {
1518   SealHandleScope shs(isolate);
1519   DCHECK(args.length() == 1);
1520   CONVERT_ARG_CHECKED(Map, map, 0);
1521   return Smi::FromInt(map->instance_type());
1522 }
1523
1524
1525 RUNTIME_FUNCTION(Runtime_ObjectEquals) {
1526   SealHandleScope shs(isolate);
1527   DCHECK(args.length() == 2);
1528   CONVERT_ARG_CHECKED(Object, obj1, 0);
1529   CONVERT_ARG_CHECKED(Object, obj2, 1);
1530   return isolate->heap()->ToBoolean(obj1 == obj2);
1531 }
1532
1533
1534 RUNTIME_FUNCTION(Runtime_IsObject) {
1535   SealHandleScope shs(isolate);
1536   DCHECK(args.length() == 1);
1537   CONVERT_ARG_CHECKED(Object, obj, 0);
1538   if (!obj->IsHeapObject()) return isolate->heap()->false_value();
1539   if (obj->IsNull()) return isolate->heap()->true_value();
1540   if (obj->IsUndetectableObject()) return isolate->heap()->false_value();
1541   Map* map = HeapObject::cast(obj)->map();
1542   bool is_non_callable_spec_object =
1543       map->instance_type() >= FIRST_NONCALLABLE_SPEC_OBJECT_TYPE &&
1544       map->instance_type() <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE;
1545   return isolate->heap()->ToBoolean(is_non_callable_spec_object);
1546 }
1547
1548
1549 RUNTIME_FUNCTION(Runtime_IsUndetectableObject) {
1550   SealHandleScope shs(isolate);
1551   DCHECK(args.length() == 1);
1552   CONVERT_ARG_CHECKED(Object, obj, 0);
1553   return isolate->heap()->ToBoolean(obj->IsUndetectableObject());
1554 }
1555
1556
1557 RUNTIME_FUNCTION(Runtime_IsSpecObject) {
1558   SealHandleScope shs(isolate);
1559   DCHECK(args.length() == 1);
1560   CONVERT_ARG_CHECKED(Object, obj, 0);
1561   return isolate->heap()->ToBoolean(obj->IsSpecObject());
1562 }
1563
1564
1565 RUNTIME_FUNCTION(Runtime_ClassOf) {
1566   SealHandleScope shs(isolate);
1567   DCHECK(args.length() == 1);
1568   CONVERT_ARG_CHECKED(Object, obj, 0);
1569   if (!obj->IsJSReceiver()) return isolate->heap()->null_value();
1570   return JSReceiver::cast(obj)->class_name();
1571 }
1572
1573
1574 RUNTIME_FUNCTION(Runtime_DefineGetterPropertyUnchecked) {
1575   HandleScope scope(isolate);
1576   DCHECK(args.length() == 4);
1577   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1578   CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1579   CONVERT_ARG_HANDLE_CHECKED(JSFunction, getter, 2);
1580   CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1581
1582   RETURN_FAILURE_ON_EXCEPTION(
1583       isolate,
1584       JSObject::DefineAccessor(object, name, getter,
1585                                isolate->factory()->null_value(), attrs));
1586   return isolate->heap()->undefined_value();
1587 }
1588
1589
1590 RUNTIME_FUNCTION(Runtime_DefineSetterPropertyUnchecked) {
1591   HandleScope scope(isolate);
1592   DCHECK(args.length() == 4);
1593   CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
1594   CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
1595   CONVERT_ARG_HANDLE_CHECKED(JSFunction, setter, 2);
1596   CONVERT_PROPERTY_ATTRIBUTES_CHECKED(attrs, 3);
1597
1598   RETURN_FAILURE_ON_EXCEPTION(
1599       isolate,
1600       JSObject::DefineAccessor(object, name, isolate->factory()->null_value(),
1601                                setter, attrs));
1602   return isolate->heap()->undefined_value();
1603 }
1604 }
1605 }  // namespace v8::internal