1 // Copyright 2012 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.
5 var $functionSourceString;
6 var $objectDefineOwnProperty;
7 var $objectGetOwnPropertyDescriptor;
9 (function(global, utils) {
11 %CheckIsBootstrapping();
13 // ----------------------------------------------------------------------------
16 var GlobalArray = global.Array;
17 var GlobalBoolean = global.Boolean;
18 var GlobalFunction = global.Function;
19 var GlobalNumber = global.Number;
20 var GlobalObject = global.Object;
21 var InternalArray = utils.InternalArray;
24 var ProxyDelegateCallAndConstruct;
25 var ProxyDerivedHasOwnTrap;
26 var ProxyDerivedKeysTrap;
29 utils.Import(function(from) {
30 MathAbs = from.MathAbs;
31 StringIndexOf = from.StringIndexOf;
34 utils.ImportFromExperimental(function(from) {
35 ProxyDelegateCallAndConstruct = from.ProxyDelegateCallAndConstruct;
36 ProxyDerivedHasOwnTrap = from.ProxyDerivedHasOwnTrap;
37 ProxyDerivedKeysTrap = from.ProxyDerivedKeysTrap;
40 // ----------------------------------------------------------------------------
44 function GlobalIsNaN(number) {
45 number = TO_NUMBER_INLINE(number);
46 return NUMBER_IS_NAN(number);
51 function GlobalIsFinite(number) {
52 number = TO_NUMBER_INLINE(number);
53 return NUMBER_IS_FINITE(number);
57 // ECMA-262 - 15.1.2.2
58 function GlobalParseInt(string, radix) {
59 if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) {
60 // Some people use parseInt instead of Math.floor. This
61 // optimization makes parseInt on a Smi 12 times faster (60ns
62 // vs 800ns). The following optimization makes parseInt on a
63 // non-Smi number 9 times faster (230ns vs 2070ns). Together
64 // they make parseInt on a string 1.4% slower (274ns vs 270ns).
65 if (%_IsSmi(string)) return string;
66 if (IS_NUMBER(string) &&
67 ((0.01 < string && string < 1e9) ||
68 (-1e9 < string && string < -0.01))) {
72 string = TO_STRING_INLINE(string);
75 // The spec says ToString should be evaluated before ToInt32.
76 string = TO_STRING_INLINE(string);
77 radix = TO_INT32(radix);
78 if (!(radix == 0 || (2 <= radix && radix <= 36))) {
83 if (%_HasCachedArrayIndex(string) &&
84 (radix == 0 || radix == 10)) {
85 return %_GetCachedArrayIndex(string);
87 return %StringParseInt(string, radix);
91 // ECMA-262 - 15.1.2.3
92 function GlobalParseFloat(string) {
93 string = TO_STRING_INLINE(string);
94 if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string);
95 return %StringParseFloat(string);
99 function GlobalEval(x) {
100 if (!IS_STRING(x)) return x;
102 var global_proxy = %GlobalProxy(GlobalEval);
104 var f = %CompileString(x, false);
105 if (!IS_FUNCTION(f)) return f;
107 return %_CallFunction(global_proxy, f);
111 // ----------------------------------------------------------------------------
113 // Set up global object.
114 var attributes = DONT_ENUM | DONT_DELETE | READ_ONLY;
116 utils.InstallConstants(global, [
117 // ECMA 262 - 15.1.1.1.
119 // ECMA-262 - 15.1.1.2.
120 "Infinity", INFINITY,
121 // ECMA-262 - 15.1.1.2.
122 "undefined", UNDEFINED,
125 // Set up non-enumerable function on the global object.
126 utils.InstallFunctions(global, DONT_ENUM, [
127 "isNaN", GlobalIsNaN,
128 "isFinite", GlobalIsFinite,
129 "parseInt", GlobalParseInt,
130 "parseFloat", GlobalParseFloat,
135 // ----------------------------------------------------------------------------
138 // ECMA-262 - 15.2.4.2
139 function ObjectToString() {
140 if (IS_UNDEFINED(this)) return "[object Undefined]";
141 if (IS_NULL(this)) return "[object Null]";
142 var O = TO_OBJECT(this);
143 var builtinTag = %_ClassOf(O);
146 // TODO(caitp): cannot wait to get rid of this flag :>
147 if (harmony_tostring) {
148 tag = O[symbolToStringTag];
149 if (!IS_STRING(tag)) {
156 return `[object ${tag}]`;
160 // ECMA-262 - 15.2.4.3
161 function ObjectToLocaleString() {
162 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.toLocaleString");
163 return this.toString();
167 // ECMA-262 - 15.2.4.4
168 function ObjectValueOf() {
169 return TO_OBJECT(this);
173 // ECMA-262 - 15.2.4.5
174 function ObjectHasOwnProperty(value) {
175 var name = $toName(value);
176 var object = TO_OBJECT(this);
178 if (%_IsJSProxy(object)) {
179 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
180 if (IS_SYMBOL(value)) return false;
182 var handler = %GetHandler(object);
183 return CallTrap1(handler, "hasOwn", ProxyDerivedHasOwnTrap, name);
185 return %HasOwnProperty(object, name);
189 // ECMA-262 - 15.2.4.6
190 function ObjectIsPrototypeOf(V) {
191 if (!IS_SPEC_OBJECT(V)) return false;
192 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.isPrototypeOf");
193 return %IsInPrototypeChain(this, V);
197 // ECMA-262 - 15.2.4.6
198 function ObjectPropertyIsEnumerable(V) {
200 if (%_IsJSProxy(this)) {
201 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
202 if (IS_SYMBOL(V)) return false;
204 var desc = GetOwnPropertyJS(this, P);
205 return IS_UNDEFINED(desc) ? false : desc.isEnumerable();
207 return %IsPropertyEnumerable(TO_OBJECT(this), P);
211 // Extensions for providing property getters and setters.
212 function ObjectDefineGetter(name, fun) {
214 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
215 receiver = %GlobalProxy(ObjectDefineGetter);
217 if (!IS_SPEC_FUNCTION(fun)) {
218 throw MakeTypeError(kObjectGetterExpectingFunction);
220 var desc = new PropertyDescriptor();
222 desc.setEnumerable(true);
223 desc.setConfigurable(true);
224 DefineOwnProperty(TO_OBJECT(receiver), $toName(name), desc, false);
228 function ObjectLookupGetter(name) {
230 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
231 receiver = %GlobalProxy(ObjectLookupGetter);
233 return %LookupAccessor(TO_OBJECT(receiver), $toName(name), GETTER);
237 function ObjectDefineSetter(name, fun) {
239 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
240 receiver = %GlobalProxy(ObjectDefineSetter);
242 if (!IS_SPEC_FUNCTION(fun)) {
243 throw MakeTypeError(kObjectSetterExpectingFunction);
245 var desc = new PropertyDescriptor();
247 desc.setEnumerable(true);
248 desc.setConfigurable(true);
249 DefineOwnProperty(TO_OBJECT(receiver), $toName(name), desc, false);
253 function ObjectLookupSetter(name) {
255 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
256 receiver = %GlobalProxy(ObjectLookupSetter);
258 return %LookupAccessor(TO_OBJECT(receiver), $toName(name), SETTER);
262 function ObjectKeys(obj) {
263 obj = TO_OBJECT(obj);
264 if (%_IsJSProxy(obj)) {
265 var handler = %GetHandler(obj);
266 var names = CallTrap0(handler, "keys", ProxyDerivedKeysTrap);
267 return ToNameArray(names, "keys", false);
269 return %OwnKeys(obj);
274 function IsAccessorDescriptor(desc) {
275 if (IS_UNDEFINED(desc)) return false;
276 return desc.hasGetter() || desc.hasSetter();
281 function IsDataDescriptor(desc) {
282 if (IS_UNDEFINED(desc)) return false;
283 return desc.hasValue() || desc.hasWritable();
288 function IsGenericDescriptor(desc) {
289 if (IS_UNDEFINED(desc)) return false;
290 return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc));
294 function IsInconsistentDescriptor(desc) {
295 return IsAccessorDescriptor(desc) && IsDataDescriptor(desc);
300 function FromPropertyDescriptor(desc) {
301 if (IS_UNDEFINED(desc)) return desc;
303 if (IsDataDescriptor(desc)) {
304 return { value: desc.getValue(),
305 writable: desc.isWritable(),
306 enumerable: desc.isEnumerable(),
307 configurable: desc.isConfigurable() };
309 // Must be an AccessorDescriptor then. We never return a generic descriptor.
310 return { get: desc.getGet(),
312 enumerable: desc.isEnumerable(),
313 configurable: desc.isConfigurable() };
318 function FromGenericPropertyDescriptor(desc) {
319 if (IS_UNDEFINED(desc)) return desc;
320 var obj = new GlobalObject();
322 if (desc.hasValue()) {
323 %AddNamedProperty(obj, "value", desc.getValue(), NONE);
325 if (desc.hasWritable()) {
326 %AddNamedProperty(obj, "writable", desc.isWritable(), NONE);
328 if (desc.hasGetter()) {
329 %AddNamedProperty(obj, "get", desc.getGet(), NONE);
331 if (desc.hasSetter()) {
332 %AddNamedProperty(obj, "set", desc.getSet(), NONE);
334 if (desc.hasEnumerable()) {
335 %AddNamedProperty(obj, "enumerable", desc.isEnumerable(), NONE);
337 if (desc.hasConfigurable()) {
338 %AddNamedProperty(obj, "configurable", desc.isConfigurable(), NONE);
345 function ToPropertyDescriptor(obj) {
346 if (!IS_SPEC_OBJECT(obj)) throw MakeTypeError(kPropertyDescObject, obj);
348 var desc = new PropertyDescriptor();
350 if ("enumerable" in obj) {
351 desc.setEnumerable($toBoolean(obj.enumerable));
354 if ("configurable" in obj) {
355 desc.setConfigurable($toBoolean(obj.configurable));
358 if ("value" in obj) {
359 desc.setValue(obj.value);
362 if ("writable" in obj) {
363 desc.setWritable($toBoolean(obj.writable));
368 if (!IS_UNDEFINED(get) && !IS_SPEC_FUNCTION(get)) {
369 throw MakeTypeError(kObjectGetterCallable, get);
376 if (!IS_UNDEFINED(set) && !IS_SPEC_FUNCTION(set)) {
377 throw MakeTypeError(kObjectSetterCallable, set);
382 if (IsInconsistentDescriptor(desc)) {
383 throw MakeTypeError(kValueAndAccessor, obj);
389 // For Harmony proxies.
390 function ToCompletePropertyDescriptor(obj) {
391 var desc = ToPropertyDescriptor(obj);
392 if (IsGenericDescriptor(desc) || IsDataDescriptor(desc)) {
393 if (!desc.hasValue()) desc.setValue(UNDEFINED);
394 if (!desc.hasWritable()) desc.setWritable(false);
396 // Is accessor descriptor.
397 if (!desc.hasGetter()) desc.setGet(UNDEFINED);
398 if (!desc.hasSetter()) desc.setSet(UNDEFINED);
400 if (!desc.hasEnumerable()) desc.setEnumerable(false);
401 if (!desc.hasConfigurable()) desc.setConfigurable(false);
406 function PropertyDescriptor() {
407 // Initialize here so they are all in-object and have the same map.
408 // Default values from ES5 8.6.1.
409 this.value_ = UNDEFINED;
410 this.hasValue_ = false;
411 this.writable_ = false;
412 this.hasWritable_ = false;
413 this.enumerable_ = false;
414 this.hasEnumerable_ = false;
415 this.configurable_ = false;
416 this.hasConfigurable_ = false;
417 this.get_ = UNDEFINED;
418 this.hasGetter_ = false;
419 this.set_ = UNDEFINED;
420 this.hasSetter_ = false;
423 utils.SetUpLockedPrototype(PropertyDescriptor, [
437 "toString", function PropertyDescriptor_ToString() {
438 return "[object PropertyDescriptor]";
440 "setValue", function PropertyDescriptor_SetValue(value) {
442 this.hasValue_ = true;
444 "getValue", function PropertyDescriptor_GetValue() {
447 "hasValue", function PropertyDescriptor_HasValue() {
448 return this.hasValue_;
450 "setEnumerable", function PropertyDescriptor_SetEnumerable(enumerable) {
451 this.enumerable_ = enumerable;
452 this.hasEnumerable_ = true;
454 "isEnumerable", function PropertyDescriptor_IsEnumerable() {
455 return this.enumerable_;
457 "hasEnumerable", function PropertyDescriptor_HasEnumerable() {
458 return this.hasEnumerable_;
460 "setWritable", function PropertyDescriptor_SetWritable(writable) {
461 this.writable_ = writable;
462 this.hasWritable_ = true;
464 "isWritable", function PropertyDescriptor_IsWritable() {
465 return this.writable_;
467 "hasWritable", function PropertyDescriptor_HasWritable() {
468 return this.hasWritable_;
471 function PropertyDescriptor_SetConfigurable(configurable) {
472 this.configurable_ = configurable;
473 this.hasConfigurable_ = true;
475 "hasConfigurable", function PropertyDescriptor_HasConfigurable() {
476 return this.hasConfigurable_;
478 "isConfigurable", function PropertyDescriptor_IsConfigurable() {
479 return this.configurable_;
481 "setGet", function PropertyDescriptor_SetGetter(get) {
483 this.hasGetter_ = true;
485 "getGet", function PropertyDescriptor_GetGetter() {
488 "hasGetter", function PropertyDescriptor_HasGetter() {
489 return this.hasGetter_;
491 "setSet", function PropertyDescriptor_SetSetter(set) {
493 this.hasSetter_ = true;
495 "getSet", function PropertyDescriptor_GetSetter() {
498 "hasSetter", function PropertyDescriptor_HasSetter() {
499 return this.hasSetter_;
504 // Converts an array returned from Runtime_GetOwnProperty to an actual
505 // property descriptor. For a description of the array layout please
506 // see the runtime.cc file.
507 function ConvertDescriptorArrayToDescriptor(desc_array) {
508 if (IS_UNDEFINED(desc_array)) {
512 var desc = new PropertyDescriptor();
513 // This is an accessor.
514 if (desc_array[IS_ACCESSOR_INDEX]) {
515 desc.setGet(desc_array[GETTER_INDEX]);
516 desc.setSet(desc_array[SETTER_INDEX]);
518 desc.setValue(desc_array[VALUE_INDEX]);
519 desc.setWritable(desc_array[WRITABLE_INDEX]);
521 desc.setEnumerable(desc_array[ENUMERABLE_INDEX]);
522 desc.setConfigurable(desc_array[CONFIGURABLE_INDEX]);
528 // For Harmony proxies.
529 function GetTrap(handler, name, defaultTrap) {
530 var trap = handler[name];
531 if (IS_UNDEFINED(trap)) {
532 if (IS_UNDEFINED(defaultTrap)) {
533 throw MakeTypeError(kProxyHandlerTrapMissing, handler, name);
536 } else if (!IS_SPEC_FUNCTION(trap)) {
537 throw MakeTypeError(kProxyHandlerTrapMustBeCallable, handler, name);
543 function CallTrap0(handler, name, defaultTrap) {
544 return %_CallFunction(handler, GetTrap(handler, name, defaultTrap));
548 function CallTrap1(handler, name, defaultTrap, x) {
549 return %_CallFunction(handler, x, GetTrap(handler, name, defaultTrap));
553 function CallTrap2(handler, name, defaultTrap, x, y) {
554 return %_CallFunction(handler, x, y, GetTrap(handler, name, defaultTrap));
558 // ES5 section 8.12.1.
559 function GetOwnPropertyJS(obj, v) {
561 if (%_IsJSProxy(obj)) {
562 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
563 if (IS_SYMBOL(v)) return UNDEFINED;
565 var handler = %GetHandler(obj);
566 var descriptor = CallTrap1(
567 handler, "getOwnPropertyDescriptor", UNDEFINED, p);
568 if (IS_UNDEFINED(descriptor)) return descriptor;
569 var desc = ToCompletePropertyDescriptor(descriptor);
570 if (!desc.isConfigurable()) {
571 throw MakeTypeError(kProxyPropNotConfigurable,
572 handler, p, "getOwnPropertyDescriptor");
577 // GetOwnProperty returns an array indexed by the constants
578 // defined in macros.py.
579 // If p is not a property on obj undefined is returned.
580 var props = %GetOwnProperty(TO_OBJECT(obj), p);
582 return ConvertDescriptorArrayToDescriptor(props);
586 // ES5 section 8.12.7.
587 function Delete(obj, p, should_throw) {
588 var desc = GetOwnPropertyJS(obj, p);
589 if (IS_UNDEFINED(desc)) return true;
590 if (desc.isConfigurable()) {
591 %DeleteProperty_Sloppy(obj, p);
593 } else if (should_throw) {
594 throw MakeTypeError(kDefineDisallowed, p);
601 // ES6, draft 12-24-14, section 7.3.8
602 function GetMethod(obj, p) {
604 if (IS_NULL_OR_UNDEFINED(func)) return UNDEFINED;
605 if (IS_SPEC_FUNCTION(func)) return func;
606 throw MakeTypeError(kCalledNonCallable, typeof func);
611 function DefineProxyProperty(obj, p, attributes, should_throw) {
612 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
613 if (IS_SYMBOL(p)) return false;
615 var handler = %GetHandler(obj);
616 var result = CallTrap2(handler, "defineProperty", UNDEFINED, p, attributes);
617 if (!$toBoolean(result)) {
619 throw MakeTypeError(kProxyHandlerReturned,
620 handler, "false", "defineProperty");
630 function DefineObjectProperty(obj, p, desc, should_throw) {
631 var current_array = %GetOwnProperty(obj, $toName(p));
632 var current = ConvertDescriptorArrayToDescriptor(current_array);
633 var extensible = %IsExtensible(obj);
635 // Error handling according to spec.
637 if (IS_UNDEFINED(current) && !extensible) {
639 throw MakeTypeError(kDefineDisallowed, p);
645 if (!IS_UNDEFINED(current)) {
647 if ((IsGenericDescriptor(desc) ||
648 IsDataDescriptor(desc) == IsDataDescriptor(current)) &&
649 (!desc.hasEnumerable() ||
650 $sameValue(desc.isEnumerable(), current.isEnumerable())) &&
651 (!desc.hasConfigurable() ||
652 $sameValue(desc.isConfigurable(), current.isConfigurable())) &&
653 (!desc.hasWritable() ||
654 $sameValue(desc.isWritable(), current.isWritable())) &&
656 $sameValue(desc.getValue(), current.getValue())) &&
657 (!desc.hasGetter() ||
658 $sameValue(desc.getGet(), current.getGet())) &&
659 (!desc.hasSetter() ||
660 $sameValue(desc.getSet(), current.getSet()))) {
663 if (!current.isConfigurable()) {
665 if (desc.isConfigurable() ||
666 (desc.hasEnumerable() &&
667 desc.isEnumerable() != current.isEnumerable())) {
669 throw MakeTypeError(kRedefineDisallowed, p);
675 if (!IsGenericDescriptor(desc)) {
677 if (IsDataDescriptor(current) != IsDataDescriptor(desc)) {
679 throw MakeTypeError(kRedefineDisallowed, p);
685 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) {
686 var currentIsWritable = current.isWritable();
687 if (currentIsWritable != desc.isWritable()) {
688 if (!currentIsWritable || IS_STRONG(obj)) {
690 throw currentIsWritable
691 ? MakeTypeError(kStrongRedefineDisallowed, obj, p)
692 : MakeTypeError(kRedefineDisallowed, p);
698 if (!currentIsWritable && desc.hasValue() &&
699 !$sameValue(desc.getValue(), current.getValue())) {
701 throw MakeTypeError(kRedefineDisallowed, p);
708 if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) {
709 if (desc.hasSetter() &&
710 !$sameValue(desc.getSet(), current.getSet())) {
712 throw MakeTypeError(kRedefineDisallowed, p);
717 if (desc.hasGetter() && !$sameValue(desc.getGet(),current.getGet())) {
719 throw MakeTypeError(kRedefineDisallowed, p);
729 // Send flags - enumerable and configurable are common - writable is
730 // only send to the data descriptor.
731 // Take special care if enumerable and configurable is not defined on
732 // desc (we need to preserve the existing values from current).
734 if (desc.hasEnumerable()) {
735 flag |= desc.isEnumerable() ? 0 : DONT_ENUM;
736 } else if (!IS_UNDEFINED(current)) {
737 flag |= current.isEnumerable() ? 0 : DONT_ENUM;
742 if (desc.hasConfigurable()) {
743 flag |= desc.isConfigurable() ? 0 : DONT_DELETE;
744 } else if (!IS_UNDEFINED(current)) {
745 flag |= current.isConfigurable() ? 0 : DONT_DELETE;
749 if (IsDataDescriptor(desc) ||
750 (IsGenericDescriptor(desc) &&
751 (IS_UNDEFINED(current) || IsDataDescriptor(current)))) {
752 // There are 3 cases that lead here:
753 // Step 4a - defining a new data property.
754 // Steps 9b & 12 - replacing an existing accessor property with a data
756 // Step 12 - updating an existing data property with a data or generic
759 if (desc.hasWritable()) {
760 flag |= desc.isWritable() ? 0 : READ_ONLY;
761 } else if (!IS_UNDEFINED(current)) {
762 flag |= current.isWritable() ? 0 : READ_ONLY;
767 var value = UNDEFINED; // Default value is undefined.
768 if (desc.hasValue()) {
769 value = desc.getValue();
770 } else if (!IS_UNDEFINED(current) && IsDataDescriptor(current)) {
771 value = current.getValue();
774 %DefineDataPropertyUnchecked(obj, p, value, flag);
776 // There are 3 cases that lead here:
777 // Step 4b - defining a new accessor property.
778 // Steps 9c & 12 - replacing an existing data property with an accessor
780 // Step 12 - updating an existing accessor property with an accessor
783 if (desc.hasGetter()) {
784 getter = desc.getGet();
785 } else if (IsAccessorDescriptor(current) && current.hasGetter()) {
786 getter = current.getGet();
789 if (desc.hasSetter()) {
790 setter = desc.getSet();
791 } else if (IsAccessorDescriptor(current) && current.hasSetter()) {
792 setter = current.getSet();
794 %DefineAccessorPropertyUnchecked(obj, p, getter, setter, flag);
800 // ES5 section 15.4.5.1.
801 function DefineArrayProperty(obj, p, desc, should_throw) {
802 // Step 3 - Special handling for array index.
804 var index = TO_UINT32(p);
805 var emit_splice = false;
806 if ($toString(index) == p && index != 4294967295) {
807 var length = obj.length;
808 if (index >= length && %IsObserved(obj)) {
810 $observeBeginPerformSplice(obj);
813 var length_desc = GetOwnPropertyJS(obj, "length");
814 if ((index >= length && !length_desc.isWritable()) ||
815 !DefineObjectProperty(obj, p, desc, true)) {
817 $observeEndPerformSplice(obj);
819 throw MakeTypeError(kDefineDisallowed, p);
824 if (index >= length) {
825 obj.length = index + 1;
828 $observeEndPerformSplice(obj);
829 $observeEnqueueSpliceRecord(obj, length, [], index + 1 - length);
835 // Step 5 - Fallback to default implementation.
836 return DefineObjectProperty(obj, p, desc, should_throw);
840 // ES5 section 8.12.9, ES5 section 15.4.5.1 and Harmony proxies.
841 function DefineOwnProperty(obj, p, desc, should_throw) {
842 if (%_IsJSProxy(obj)) {
843 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
844 if (IS_SYMBOL(p)) return false;
846 var attributes = FromGenericPropertyDescriptor(desc);
847 return DefineProxyProperty(obj, p, attributes, should_throw);
848 } else if (IS_ARRAY(obj)) {
849 return DefineArrayProperty(obj, p, desc, should_throw);
851 return DefineObjectProperty(obj, p, desc, should_throw);
856 function DefineOwnPropertyFromAPI(obj, p, value, desc) {
857 return DefineOwnProperty(obj, p, ToPropertyDescriptor({
861 configurable: desc[2]
867 // ES6 section 19.1.2.9
868 function ObjectGetPrototypeOf(obj) {
869 return %_GetPrototype(TO_OBJECT(obj));
872 // ES6 section 19.1.2.19.
873 function ObjectSetPrototypeOf(obj, proto) {
874 CHECK_OBJECT_COERCIBLE(obj, "Object.setPrototypeOf");
876 if (proto !== null && !IS_SPEC_OBJECT(proto)) {
877 throw MakeTypeError(kProtoObjectOrNull, proto);
880 if (IS_SPEC_OBJECT(obj)) {
881 %SetPrototype(obj, proto);
888 // ES6 section 19.1.2.6
889 function ObjectGetOwnPropertyDescriptor(obj, p) {
890 var desc = GetOwnPropertyJS(TO_OBJECT(obj), p);
891 return FromPropertyDescriptor(desc);
895 // For Harmony proxies
896 function ToNameArray(obj, trap, includeSymbols) {
897 if (!IS_SPEC_OBJECT(obj)) {
898 throw MakeTypeError(kProxyNonObjectPropNames, trap, obj);
900 var n = TO_UINT32(obj.length);
901 var array = new GlobalArray(n);
903 var names = { __proto__: null }; // TODO(rossberg): use sets once ready.
904 for (var index = 0; index < n; index++) {
905 var s = $toName(obj[index]);
906 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
907 if (IS_SYMBOL(s) && !includeSymbols) continue;
908 if (%HasOwnProperty(names, s)) {
909 throw MakeTypeError(kProxyRepeatedPropName, trap, s);
911 array[realLength] = s;
915 array.length = realLength;
920 function ObjectGetOwnPropertyKeys(obj, filter) {
921 var nameArrays = new InternalArray();
922 filter |= PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL;
923 var interceptorInfo = %GetInterceptorInfo(obj);
925 // Find all the indexed properties.
927 // Only get own element names if we want to include string keys.
928 if ((filter & PROPERTY_ATTRIBUTES_STRING) === 0) {
929 var ownElementNames = %GetOwnElementNames(obj);
930 for (var i = 0; i < ownElementNames.length; ++i) {
931 ownElementNames[i] = %_NumberToString(ownElementNames[i]);
933 nameArrays.push(ownElementNames);
934 // Get names for indexed interceptor properties.
935 if ((interceptorInfo & 1) != 0) {
936 var indexedInterceptorNames = %GetIndexedInterceptorElementNames(obj);
937 if (!IS_UNDEFINED(indexedInterceptorNames)) {
938 nameArrays.push(indexedInterceptorNames);
943 // Find all the named properties.
945 // Get own property names.
946 nameArrays.push(%GetOwnPropertyNames(obj, filter));
948 // Get names for named interceptor properties if any.
949 if ((interceptorInfo & 2) != 0) {
950 var namedInterceptorNames =
951 %GetNamedInterceptorPropertyNames(obj);
952 if (!IS_UNDEFINED(namedInterceptorNames)) {
953 nameArrays.push(namedInterceptorNames);
958 %Apply(InternalArray.prototype.concat,
959 nameArrays[0], nameArrays, 1, nameArrays.length - 1);
961 // Property names are expected to be unique strings,
962 // but interceptors can interfere with that assumption.
963 if (interceptorInfo != 0) {
964 var seenKeys = { __proto__: null };
966 for (var i = 0; i < propertyNames.length; ++i) {
967 var name = propertyNames[i];
968 if (IS_SYMBOL(name)) {
969 if ((filter & PROPERTY_ATTRIBUTES_SYMBOLIC) || IS_PRIVATE(name)) {
973 if (filter & PROPERTY_ATTRIBUTES_STRING) continue;
974 name = $toString(name);
976 if (seenKeys[name]) continue;
977 seenKeys[name] = true;
978 propertyNames[j++] = name;
980 propertyNames.length = j;
983 return propertyNames;
987 // ES6 section 9.1.12 / 9.5.12
988 function OwnPropertyKeys(obj) {
989 if (%_IsJSProxy(obj)) {
990 var handler = %GetHandler(obj);
991 // TODO(caitp): Proxy.[[OwnPropertyKeys]] can not be implemented to spec
992 // without an implementation of Direct Proxies.
993 var names = CallTrap0(handler, "ownKeys", UNDEFINED);
994 return ToNameArray(names, "getOwnPropertyNames", false);
996 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL);
1000 // ES5 section 15.2.3.4.
1001 function ObjectGetOwnPropertyNames(obj) {
1002 obj = TO_OBJECT(obj);
1003 // Special handling for proxies.
1004 if (%_IsJSProxy(obj)) {
1005 var handler = %GetHandler(obj);
1006 var names = CallTrap0(handler, "getOwnPropertyNames", UNDEFINED);
1007 return ToNameArray(names, "getOwnPropertyNames", false);
1010 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_SYMBOLIC);
1014 // ES5 section 15.2.3.5.
1015 function ObjectCreate(proto, properties) {
1016 if (!IS_SPEC_OBJECT(proto) && proto !== null) {
1017 throw MakeTypeError(kProtoObjectOrNull, proto);
1020 %InternalSetPrototype(obj, proto);
1021 if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties);
1026 // ES5 section 15.2.3.6.
1027 function ObjectDefineProperty(obj, p, attributes) {
1028 if (!IS_SPEC_OBJECT(obj)) {
1029 throw MakeTypeError(kCalledOnNonObject, "Object.defineProperty");
1031 var name = $toName(p);
1032 if (%_IsJSProxy(obj)) {
1033 // Clone the attributes object for protection.
1034 // TODO(rossberg): not spec'ed yet, so not sure if this should involve
1035 // non-own properties as it does (or non-enumerable ones, as it doesn't?).
1036 var attributesClone = { __proto__: null };
1037 for (var a in attributes) {
1038 attributesClone[a] = attributes[a];
1040 DefineProxyProperty(obj, name, attributesClone, true);
1041 // The following would implement the spec as in the current proposal,
1042 // but after recent comments on es-discuss, is most likely obsolete.
1044 var defineObj = FromGenericPropertyDescriptor(desc);
1045 var names = ObjectGetOwnPropertyNames(attributes);
1047 {value: 0, writable: 0, get: 0, set: 0, enumerable: 0, configurable: 0};
1048 for (var i = 0; i < names.length; i++) {
1050 if (!(%HasOwnProperty(standardNames, N))) {
1051 var attr = GetOwnPropertyJS(attributes, N);
1052 DefineOwnProperty(descObj, N, attr, true);
1055 // This is really confusing the types, but it is what the proxies spec
1056 // currently requires:
1060 var desc = ToPropertyDescriptor(attributes);
1061 DefineOwnProperty(obj, name, desc, true);
1067 function GetOwnEnumerablePropertyNames(object) {
1068 var names = new InternalArray();
1069 for (var key in object) {
1070 if (%HasOwnProperty(object, key)) {
1075 var filter = PROPERTY_ATTRIBUTES_STRING | PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL;
1076 var symbols = %GetOwnPropertyNames(object, filter);
1077 for (var i = 0; i < symbols.length; ++i) {
1078 var symbol = symbols[i];
1079 if (IS_SYMBOL(symbol)) {
1080 var desc = ObjectGetOwnPropertyDescriptor(object, symbol);
1081 if (desc.enumerable) names.push(symbol);
1089 // ES5 section 15.2.3.7.
1090 function ObjectDefineProperties(obj, properties) {
1091 if (!IS_SPEC_OBJECT(obj)) {
1092 throw MakeTypeError(kCalledOnNonObject, "Object.defineProperties");
1094 var props = TO_OBJECT(properties);
1095 var names = GetOwnEnumerablePropertyNames(props);
1096 var descriptors = new InternalArray();
1097 for (var i = 0; i < names.length; i++) {
1098 descriptors.push(ToPropertyDescriptor(props[names[i]]));
1100 for (var i = 0; i < names.length; i++) {
1101 DefineOwnProperty(obj, names[i], descriptors[i], true);
1108 function ProxyFix(obj) {
1109 var handler = %GetHandler(obj);
1110 var props = CallTrap0(handler, "fix", UNDEFINED);
1111 if (IS_UNDEFINED(props)) {
1112 throw MakeTypeError(kProxyHandlerReturned, handler, "undefined", "fix");
1115 if (%IsJSFunctionProxy(obj)) {
1116 var callTrap = %GetCallTrap(obj);
1117 var constructTrap = %GetConstructTrap(obj);
1118 var code = ProxyDelegateCallAndConstruct(callTrap, constructTrap);
1119 %Fix(obj); // becomes a regular function
1120 %SetCode(obj, code);
1121 // TODO(rossberg): What about length and other properties? Not specified.
1122 // We just put in some half-reasonable defaults for now.
1123 var prototype = new GlobalObject();
1124 ObjectDefineProperty(prototype, "constructor",
1125 {value: obj, writable: true, enumerable: false, configurable: true});
1126 // TODO(v8:1530): defineProperty does not handle prototype and length.
1127 %FunctionSetPrototype(obj, prototype);
1132 ObjectDefineProperties(obj, props);
1136 // ES5 section 15.2.3.8.
1137 function ObjectSealJS(obj) {
1138 if (!IS_SPEC_OBJECT(obj)) return obj;
1139 var isProxy = %_IsJSProxy(obj);
1140 if (isProxy || %HasSloppyArgumentsElements(obj) || %IsObserved(obj)) {
1144 var names = ObjectGetOwnPropertyNames(obj);
1145 for (var i = 0; i < names.length; i++) {
1146 var name = names[i];
1147 var desc = GetOwnPropertyJS(obj, name);
1148 if (desc.isConfigurable()) {
1149 desc.setConfigurable(false);
1150 DefineOwnProperty(obj, name, desc, true);
1153 %PreventExtensions(obj);
1155 // TODO(adamk): Is it worth going to this fast path if the
1156 // object's properties are already in dictionary mode?
1163 // ES5 section 15.2.3.9.
1164 function ObjectFreezeJS(obj) {
1165 if (!IS_SPEC_OBJECT(obj)) return obj;
1166 var isProxy = %_IsJSProxy(obj);
1167 // TODO(conradw): Investigate modifying the fast path to accommodate strong
1169 if (isProxy || %HasSloppyArgumentsElements(obj) || %IsObserved(obj) ||
1174 var names = ObjectGetOwnPropertyNames(obj);
1175 for (var i = 0; i < names.length; i++) {
1176 var name = names[i];
1177 var desc = GetOwnPropertyJS(obj, name);
1178 if (desc.isWritable() || desc.isConfigurable()) {
1179 if (IsDataDescriptor(desc)) desc.setWritable(false);
1180 desc.setConfigurable(false);
1181 DefineOwnProperty(obj, name, desc, true);
1184 %PreventExtensions(obj);
1186 // TODO(adamk): Is it worth going to this fast path if the
1187 // object's properties are already in dictionary mode?
1194 // ES5 section 15.2.3.10
1195 function ObjectPreventExtension(obj) {
1196 if (!IS_SPEC_OBJECT(obj)) return obj;
1197 if (%_IsJSProxy(obj)) {
1200 %PreventExtensions(obj);
1205 // ES5 section 15.2.3.11
1206 function ObjectIsSealed(obj) {
1207 if (!IS_SPEC_OBJECT(obj)) return true;
1208 if (%_IsJSProxy(obj)) {
1211 if (%IsExtensible(obj)) {
1214 var names = ObjectGetOwnPropertyNames(obj);
1215 for (var i = 0; i < names.length; i++) {
1216 var name = names[i];
1217 var desc = GetOwnPropertyJS(obj, name);
1218 if (desc.isConfigurable()) {
1226 // ES5 section 15.2.3.12
1227 function ObjectIsFrozen(obj) {
1228 if (!IS_SPEC_OBJECT(obj)) return true;
1229 if (%_IsJSProxy(obj)) {
1232 if (%IsExtensible(obj)) {
1235 var names = ObjectGetOwnPropertyNames(obj);
1236 for (var i = 0; i < names.length; i++) {
1237 var name = names[i];
1238 var desc = GetOwnPropertyJS(obj, name);
1239 if (IsDataDescriptor(desc) && desc.isWritable()) return false;
1240 if (desc.isConfigurable()) return false;
1246 // ES5 section 15.2.3.13
1247 function ObjectIsExtensible(obj) {
1248 if (!IS_SPEC_OBJECT(obj)) return false;
1249 if (%_IsJSProxy(obj)) {
1252 return %IsExtensible(obj);
1256 // ECMA-262, Edition 6, section 19.1.2.10
1257 function ObjectIs(obj1, obj2) {
1258 return $sameValue(obj1, obj2);
1262 // ECMA-262, Edition 6, section B.2.2.1.1
1263 function ObjectGetProto() {
1264 return %_GetPrototype(TO_OBJECT(this));
1268 // ECMA-262, Edition 6, section B.2.2.1.2
1269 function ObjectSetProto(proto) {
1270 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.__proto__");
1272 if ((IS_SPEC_OBJECT(proto) || IS_NULL(proto)) && IS_SPEC_OBJECT(this)) {
1273 %SetPrototype(this, proto);
1278 function ObjectConstructor(x) {
1279 if (%_IsConstructCall()) {
1280 if (x == null) return this;
1281 return TO_OBJECT(x);
1283 if (x == null) return { };
1284 return TO_OBJECT(x);
1289 // ----------------------------------------------------------------------------
1292 %SetNativeFlag(GlobalObject);
1293 %SetCode(GlobalObject, ObjectConstructor);
1295 %AddNamedProperty(GlobalObject.prototype, "constructor", GlobalObject,
1298 // Set up non-enumerable functions on the Object.prototype object.
1299 utils.InstallFunctions(GlobalObject.prototype, DONT_ENUM, [
1300 "toString", ObjectToString,
1301 "toLocaleString", ObjectToLocaleString,
1302 "valueOf", ObjectValueOf,
1303 "hasOwnProperty", ObjectHasOwnProperty,
1304 "isPrototypeOf", ObjectIsPrototypeOf,
1305 "propertyIsEnumerable", ObjectPropertyIsEnumerable,
1306 "__defineGetter__", ObjectDefineGetter,
1307 "__lookupGetter__", ObjectLookupGetter,
1308 "__defineSetter__", ObjectDefineSetter,
1309 "__lookupSetter__", ObjectLookupSetter
1311 utils.InstallGetterSetter(GlobalObject.prototype, "__proto__", ObjectGetProto,
1314 // Set up non-enumerable functions in the Object object.
1315 utils.InstallFunctions(GlobalObject, DONT_ENUM, [
1317 "create", ObjectCreate,
1318 "defineProperty", ObjectDefineProperty,
1319 "defineProperties", ObjectDefineProperties,
1320 "freeze", ObjectFreezeJS,
1321 "getPrototypeOf", ObjectGetPrototypeOf,
1322 "setPrototypeOf", ObjectSetPrototypeOf,
1323 "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor,
1324 "getOwnPropertyNames", ObjectGetOwnPropertyNames,
1325 // getOwnPropertySymbols is added in symbol.js.
1327 "isExtensible", ObjectIsExtensible,
1328 "isFrozen", ObjectIsFrozen,
1329 "isSealed", ObjectIsSealed,
1330 "preventExtensions", ObjectPreventExtension,
1331 "seal", ObjectSealJS
1332 // deliverChangeRecords, getNotifier, observe and unobserve are added
1333 // in object-observe.js.
1337 // ----------------------------------------------------------------------------
1340 function BooleanConstructor(x) {
1341 if (%_IsConstructCall()) {
1342 %_SetValueOf(this, $toBoolean(x));
1344 return $toBoolean(x);
1349 function BooleanToString() {
1350 // NOTE: Both Boolean objects and values can enter here as
1351 // 'this'. This is not as dictated by ECMA-262.
1353 if (!IS_BOOLEAN(b)) {
1354 if (!IS_BOOLEAN_WRAPPER(b)) {
1355 throw MakeTypeError(kNotGeneric, 'Boolean.prototype.toString');
1359 return b ? 'true' : 'false';
1363 function BooleanValueOf() {
1364 // NOTE: Both Boolean objects and values can enter here as
1365 // 'this'. This is not as dictated by ECMA-262.
1366 if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) {
1367 throw MakeTypeError(kNotGeneric, 'Boolean.prototype.valueOf');
1369 return %_ValueOf(this);
1373 // ----------------------------------------------------------------------------
1375 %SetCode(GlobalBoolean, BooleanConstructor);
1376 %FunctionSetPrototype(GlobalBoolean, new GlobalBoolean(false));
1377 %AddNamedProperty(GlobalBoolean.prototype, "constructor", GlobalBoolean,
1380 utils.InstallFunctions(GlobalBoolean.prototype, DONT_ENUM, [
1381 "toString", BooleanToString,
1382 "valueOf", BooleanValueOf
1386 // ----------------------------------------------------------------------------
1389 function NumberConstructor(x) {
1390 var value = %_ArgumentsLength() == 0 ? 0 : $toNumber(x);
1391 if (%_IsConstructCall()) {
1392 %_SetValueOf(this, value);
1399 // ECMA-262 section 15.7.4.2.
1400 function NumberToStringJS(radix) {
1401 // NOTE: Both Number objects and values can enter here as
1402 // 'this'. This is not as dictated by ECMA-262.
1404 if (!IS_NUMBER(this)) {
1405 if (!IS_NUMBER_WRAPPER(this)) {
1406 throw MakeTypeError(kNotGeneric, 'Number.prototype.toString');
1408 // Get the value of this number in case it's an object.
1409 number = %_ValueOf(this);
1411 // Fast case: Convert number in radix 10.
1412 if (IS_UNDEFINED(radix) || radix === 10) {
1413 return %_NumberToString(number);
1416 // Convert the radix to an integer and check the range.
1417 radix = TO_INTEGER(radix);
1418 if (radix < 2 || radix > 36) throw MakeRangeError(kToRadixFormatRange);
1419 // Convert the number to a string in the given radix.
1420 return %NumberToRadixString(number, radix);
1424 // ECMA-262 section 15.7.4.3
1425 function NumberToLocaleString() {
1426 return %_CallFunction(this, NumberToStringJS);
1430 // ECMA-262 section 15.7.4.4
1431 function NumberValueOf() {
1432 // NOTE: Both Number objects and values can enter here as
1433 // 'this'. This is not as dictated by ECMA-262.
1434 if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) {
1435 throw MakeTypeError(kNotGeneric, 'Number.prototype.valueOf');
1437 return %_ValueOf(this);
1441 // ECMA-262 section 15.7.4.5
1442 function NumberToFixedJS(fractionDigits) {
1444 if (!IS_NUMBER(this)) {
1445 if (!IS_NUMBER_WRAPPER(this)) {
1446 throw MakeTypeError(kIncompatibleMethodReceiver,
1447 "Number.prototype.toFixed", this);
1449 // Get the value of this number in case it's an object.
1450 x = %_ValueOf(this);
1452 var f = TO_INTEGER(fractionDigits);
1454 if (f < 0 || f > 20) {
1455 throw MakeRangeError(kNumberFormatRange, "toFixed() digits");
1458 if (NUMBER_IS_NAN(x)) return "NaN";
1459 if (x == INFINITY) return "Infinity";
1460 if (x == -INFINITY) return "-Infinity";
1462 return %NumberToFixed(x, f);
1466 // ECMA-262 section 15.7.4.6
1467 function NumberToExponentialJS(fractionDigits) {
1469 if (!IS_NUMBER(this)) {
1470 if (!IS_NUMBER_WRAPPER(this)) {
1471 throw MakeTypeError(kIncompatibleMethodReceiver,
1472 "Number.prototype.toExponential", this);
1474 // Get the value of this number in case it's an object.
1475 x = %_ValueOf(this);
1477 var f = IS_UNDEFINED(fractionDigits) ? UNDEFINED : TO_INTEGER(fractionDigits);
1479 if (NUMBER_IS_NAN(x)) return "NaN";
1480 if (x == INFINITY) return "Infinity";
1481 if (x == -INFINITY) return "-Infinity";
1483 if (IS_UNDEFINED(f)) {
1484 f = -1; // Signal for runtime function that f is not defined.
1485 } else if (f < 0 || f > 20) {
1486 throw MakeRangeError(kNumberFormatRange, "toExponential()");
1488 return %NumberToExponential(x, f);
1492 // ECMA-262 section 15.7.4.7
1493 function NumberToPrecisionJS(precision) {
1495 if (!IS_NUMBER(this)) {
1496 if (!IS_NUMBER_WRAPPER(this)) {
1497 throw MakeTypeError(kIncompatibleMethodReceiver,
1498 "Number.prototype.toPrecision", this);
1500 // Get the value of this number in case it's an object.
1501 x = %_ValueOf(this);
1503 if (IS_UNDEFINED(precision)) return $toString(%_ValueOf(this));
1504 var p = TO_INTEGER(precision);
1506 if (NUMBER_IS_NAN(x)) return "NaN";
1507 if (x == INFINITY) return "Infinity";
1508 if (x == -INFINITY) return "-Infinity";
1510 if (p < 1 || p > 21) {
1511 throw MakeRangeError(kToPrecisionFormatRange);
1513 return %NumberToPrecision(x, p);
1517 // Harmony isFinite.
1518 function NumberIsFinite(number) {
1519 return IS_NUMBER(number) && NUMBER_IS_FINITE(number);
1523 // Harmony isInteger
1524 function NumberIsInteger(number) {
1525 return NumberIsFinite(number) && TO_INTEGER(number) == number;
1530 function NumberIsNaN(number) {
1531 return IS_NUMBER(number) && NUMBER_IS_NAN(number);
1535 // Harmony isSafeInteger
1536 function NumberIsSafeInteger(number) {
1537 if (NumberIsFinite(number)) {
1538 var integral = TO_INTEGER(number);
1539 if (integral == number) {
1540 return MathAbs(integral) <= GlobalNumber.MAX_SAFE_INTEGER;
1547 // ----------------------------------------------------------------------------
1549 %SetCode(GlobalNumber, NumberConstructor);
1550 %FunctionSetPrototype(GlobalNumber, new GlobalNumber(0));
1552 %OptimizeObjectForAddingMultipleProperties(GlobalNumber.prototype, 8);
1553 // Set up the constructor property on the Number prototype object.
1554 %AddNamedProperty(GlobalNumber.prototype, "constructor", GlobalNumber,
1557 utils.InstallConstants(GlobalNumber, [
1558 // ECMA-262 section 15.7.3.1.
1559 "MAX_VALUE", 1.7976931348623157e+308,
1560 // ECMA-262 section 15.7.3.2.
1561 "MIN_VALUE", 5e-324,
1562 // ECMA-262 section 15.7.3.3.
1564 // ECMA-262 section 15.7.3.4.
1565 "NEGATIVE_INFINITY", -INFINITY,
1566 // ECMA-262 section 15.7.3.5.
1567 "POSITIVE_INFINITY", INFINITY,
1569 // --- Harmony constants (no spec refs until settled.)
1571 "MAX_SAFE_INTEGER", %_MathPow(2, 53) - 1,
1572 "MIN_SAFE_INTEGER", -%_MathPow(2, 53) + 1,
1573 "EPSILON", %_MathPow(2, -52)
1576 // Set up non-enumerable functions on the Number prototype object.
1577 utils.InstallFunctions(GlobalNumber.prototype, DONT_ENUM, [
1578 "toString", NumberToStringJS,
1579 "toLocaleString", NumberToLocaleString,
1580 "valueOf", NumberValueOf,
1581 "toFixed", NumberToFixedJS,
1582 "toExponential", NumberToExponentialJS,
1583 "toPrecision", NumberToPrecisionJS
1586 // Harmony Number constructor additions
1587 utils.InstallFunctions(GlobalNumber, DONT_ENUM, [
1588 "isFinite", NumberIsFinite,
1589 "isInteger", NumberIsInteger,
1590 "isNaN", NumberIsNaN,
1591 "isSafeInteger", NumberIsSafeInteger,
1592 "parseInt", GlobalParseInt,
1593 "parseFloat", GlobalParseFloat
1596 %SetForceInlineFlag(NumberIsNaN);
1599 // ----------------------------------------------------------------------------
1602 function NativeCodeFunctionSourceString(func) {
1603 var name = %FunctionGetName(func);
1605 // Mimic what KJS does.
1606 return 'function ' + name + '() { [native code] }';
1609 return 'function () { [native code] }';
1612 function FunctionSourceString(func) {
1613 while (%IsJSFunctionProxy(func)) {
1614 func = %GetCallTrap(func);
1617 if (!IS_FUNCTION(func)) {
1618 throw MakeTypeError(kNotGeneric, 'Function.prototype.toString');
1621 if (%FunctionIsBuiltin(func)) {
1622 return NativeCodeFunctionSourceString(func);
1625 var classSource = %ClassGetSourceCode(func);
1626 if (IS_STRING(classSource)) {
1630 var source = %FunctionGetSourceCode(func);
1631 if (!IS_STRING(source)) {
1632 return NativeCodeFunctionSourceString(func);
1635 if (%FunctionIsArrow(func)) {
1639 var name = %FunctionNameShouldPrintAsAnonymous(func)
1641 : %FunctionGetName(func);
1643 var isGenerator = %FunctionIsGenerator(func);
1644 var head = %FunctionIsConciseMethod(func)
1645 ? (isGenerator ? '*' : '')
1646 : (isGenerator ? 'function* ' : 'function ');
1647 return head + name + source;
1651 function FunctionToString() {
1652 return FunctionSourceString(this);
1657 function FunctionBind(this_arg) { // Length is 1.
1658 if (!IS_SPEC_FUNCTION(this)) throw MakeTypeError(kFunctionBind);
1660 var boundFunction = function () {
1661 // Poison .arguments and .caller, but is otherwise not detectable.
1663 // This function must not use any object literals (Object, Array, RegExp),
1664 // since the literals-array is being used to store the bound data.
1665 if (%_IsConstructCall()) {
1666 return %NewObjectFromBound(boundFunction);
1668 var bindings = %BoundFunctionGetBindings(boundFunction);
1670 var argc = %_ArgumentsLength();
1672 return %Apply(bindings[0], bindings[1], bindings, 2, bindings.length - 2);
1674 if (bindings.length === 2) {
1675 return %Apply(bindings[0], bindings[1], arguments, 0, argc);
1677 var bound_argc = bindings.length - 2;
1678 var argv = new InternalArray(bound_argc + argc);
1679 for (var i = 0; i < bound_argc; i++) {
1680 argv[i] = bindings[i + 2];
1682 for (var j = 0; j < argc; j++) {
1683 argv[i++] = %_Arguments(j);
1685 return %Apply(bindings[0], bindings[1], argv, 0, bound_argc + argc);
1689 var old_length = this.length;
1690 // FunctionProxies might provide a non-UInt32 value. If so, ignore it.
1691 if ((typeof old_length === "number") &&
1692 ((old_length >>> 0) === old_length)) {
1693 var argc = %_ArgumentsLength();
1694 if (argc > 0) argc--; // Don't count the thisArg as parameter.
1695 new_length = old_length - argc;
1696 if (new_length < 0) new_length = 0;
1698 // This runtime function finds any remaining arguments on the stack,
1699 // so we don't pass the arguments object.
1700 var result = %FunctionBindArguments(boundFunction, this,
1701 this_arg, new_length);
1703 var name = this.name;
1704 var bound_name = IS_STRING(name) ? name : "";
1705 %DefineDataPropertyUnchecked(result, "name", "bound " + bound_name,
1706 DONT_ENUM | READ_ONLY);
1708 // We already have caller and arguments properties on functions,
1709 // which are non-configurable. It therefore makes no sence to
1710 // try to redefine these as defined by the spec. The spec says
1711 // that bind should make these throw a TypeError if get or set
1712 // is called and make them non-enumerable and non-configurable.
1713 // To be consistent with our normal functions we leave this as it is.
1714 // TODO(lrn): Do set these to be thrower.
1719 function NewFunctionString(args, function_token) {
1720 var n = args.length;
1723 p = $toString(args[0]);
1724 for (var i = 1; i < n - 1; i++) {
1725 p += ',' + $toString(args[i]);
1727 // If the formal parameters string include ) - an illegal
1728 // character - it may make the combined function expression
1729 // compile. We avoid this problem by checking for this early on.
1730 if (%_CallFunction(p, ')', StringIndexOf) != -1) {
1731 throw MakeSyntaxError(kParenthesisInArgString);
1733 // If the formal parameters include an unbalanced block comment, the
1734 // function must be rejected. Since JavaScript does not allow nested
1735 // comments we can include a trailing block comment to catch this.
1738 var body = (n > 0) ? $toString(args[n - 1]) : '';
1739 return '(' + function_token + '(' + p + ') {\n' + body + '\n})';
1743 function FunctionConstructor(arg1) { // length == 1
1744 var source = NewFunctionString(arguments, 'function');
1745 var global_proxy = %GlobalProxy(FunctionConstructor);
1746 // Compile the string in the constructor and not a helper so that errors
1747 // appear to come from here.
1748 var f = %_CallFunction(global_proxy, %CompileString(source, true));
1749 %FunctionMarkNameShouldPrintAsAnonymous(f);
1754 // ----------------------------------------------------------------------------
1756 %SetCode(GlobalFunction, FunctionConstructor);
1757 %AddNamedProperty(GlobalFunction.prototype, "constructor", GlobalFunction,
1760 utils.InstallFunctions(GlobalFunction.prototype, DONT_ENUM, [
1761 "bind", FunctionBind,
1762 "toString", FunctionToString
1765 // ----------------------------------------------------------------------------
1766 // Iterator related spec functions.
1768 // ES6 rev 33, 2015-02-12
1769 // 7.4.1 GetIterator ( obj, method )
1770 function GetIterator(obj, method) {
1771 if (IS_UNDEFINED(method)) {
1772 method = obj[symbolIterator];
1774 if (!IS_SPEC_FUNCTION(method)) {
1775 throw MakeTypeError(kNotIterable, obj);
1777 var iterator = %_CallFunction(obj, method);
1778 if (!IS_SPEC_OBJECT(iterator)) {
1779 throw MakeTypeError(kNotAnIterator, iterator);
1784 // ----------------------------------------------------------------------------
1787 $functionSourceString = FunctionSourceString;
1788 $objectDefineOwnProperty = DefineOwnPropertyFromAPI;
1789 $objectGetOwnPropertyDescriptor = ObjectGetOwnPropertyDescriptor;
1791 utils.ObjectDefineProperties = ObjectDefineProperties;
1792 utils.ObjectDefineProperty = ObjectDefineProperty;
1794 utils.Export(function(to) {
1796 to.GetIterator = GetIterator;
1797 to.GetMethod = GetMethod;
1798 to.IsFinite = GlobalIsFinite;
1799 to.IsNaN = GlobalIsNaN;
1800 to.NewFunctionString = NewFunctionString;
1801 to.NumberIsNaN = NumberIsNaN;
1802 to.ObjectDefineProperty = ObjectDefineProperty;
1803 to.ObjectFreeze = ObjectFreezeJS;
1804 to.ObjectGetOwnPropertyKeys = ObjectGetOwnPropertyKeys;
1805 to.ObjectHasOwnProperty = ObjectHasOwnProperty;
1806 to.ObjectIsFrozen = ObjectIsFrozen;
1807 to.ObjectIsSealed = ObjectIsSealed;
1808 to.ObjectToString = ObjectToString;
1809 to.OwnPropertyKeys = OwnPropertyKeys;
1810 to.ToNameArray = ToNameArray;
1813 utils.ExportToRuntime(function(to) {
1814 to.GlobalEval = GlobalEval;
1815 to.ToCompletePropertyDescriptor = ToCompletePropertyDescriptor;