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