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;
7 (function(global, utils) {
9 %CheckIsBootstrapping();
11 // ----------------------------------------------------------------------------
14 var GlobalArray = global.Array;
15 var GlobalBoolean = global.Boolean;
16 var GlobalFunction = global.Function;
17 var GlobalNumber = global.Number;
18 var GlobalObject = global.Object;
19 var InternalArray = utils.InternalArray;
22 var ProxyDelegateCallAndConstruct;
23 var ProxyDerivedHasOwnTrap;
24 var ProxyDerivedKeysTrap;
27 utils.Import(function(from) {
28 MathAbs = from.MathAbs;
29 StringIndexOf = from.StringIndexOf;
32 utils.ImportFromExperimental(function(from) {
33 ProxyDelegateCallAndConstruct = from.ProxyDelegateCallAndConstruct;
34 ProxyDerivedHasOwnTrap = from.ProxyDerivedHasOwnTrap;
35 ProxyDerivedKeysTrap = from.ProxyDerivedKeysTrap;
38 // ----------------------------------------------------------------------------
42 function GlobalIsNaN(number) {
43 number = TO_NUMBER_INLINE(number);
44 return NUMBER_IS_NAN(number);
49 function GlobalIsFinite(number) {
50 number = TO_NUMBER_INLINE(number);
51 return NUMBER_IS_FINITE(number);
55 // ECMA-262 - 15.1.2.2
56 function GlobalParseInt(string, radix) {
57 if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) {
58 // Some people use parseInt instead of Math.floor. This
59 // optimization makes parseInt on a Smi 12 times faster (60ns
60 // vs 800ns). The following optimization makes parseInt on a
61 // non-Smi number 9 times faster (230ns vs 2070ns). Together
62 // they make parseInt on a string 1.4% slower (274ns vs 270ns).
63 if (%_IsSmi(string)) return string;
64 if (IS_NUMBER(string) &&
65 ((0.01 < string && string < 1e9) ||
66 (-1e9 < string && string < -0.01))) {
70 string = TO_STRING_INLINE(string);
73 // The spec says ToString should be evaluated before ToInt32.
74 string = TO_STRING_INLINE(string);
75 radix = TO_INT32(radix);
76 if (!(radix == 0 || (2 <= radix && radix <= 36))) {
81 if (%_HasCachedArrayIndex(string) &&
82 (radix == 0 || radix == 10)) {
83 return %_GetCachedArrayIndex(string);
85 return %StringParseInt(string, radix);
89 // ECMA-262 - 15.1.2.3
90 function GlobalParseFloat(string) {
91 string = TO_STRING_INLINE(string);
92 if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string);
93 return %StringParseFloat(string);
97 function GlobalEval(x) {
98 if (!IS_STRING(x)) return x;
100 var global_proxy = %GlobalProxy(GlobalEval);
102 var f = %CompileString(x, false);
103 if (!IS_FUNCTION(f)) return f;
105 return %_CallFunction(global_proxy, f);
109 // ----------------------------------------------------------------------------
111 // Set up global object.
112 var attributes = DONT_ENUM | DONT_DELETE | READ_ONLY;
114 utils.InstallConstants(global, [
115 // ECMA 262 - 15.1.1.1.
117 // ECMA-262 - 15.1.1.2.
118 "Infinity", INFINITY,
119 // ECMA-262 - 15.1.1.2.
120 "undefined", UNDEFINED,
123 // Set up non-enumerable function on the global object.
124 utils.InstallFunctions(global, DONT_ENUM, [
125 "isNaN", GlobalIsNaN,
126 "isFinite", GlobalIsFinite,
127 "parseInt", GlobalParseInt,
128 "parseFloat", GlobalParseFloat,
133 // ----------------------------------------------------------------------------
136 // ECMA-262 - 15.2.4.2
137 function ObjectToString() {
138 if (IS_UNDEFINED(this)) return "[object Undefined]";
139 if (IS_NULL(this)) return "[object Null]";
140 var O = TO_OBJECT(this);
141 var builtinTag = %_ClassOf(O);
144 // TODO(caitp): cannot wait to get rid of this flag :>
145 if (harmony_tostring) {
146 tag = O[symbolToStringTag];
147 if (!IS_STRING(tag)) {
154 return `[object ${tag}]`;
158 // ECMA-262 - 15.2.4.3
159 function ObjectToLocaleString() {
160 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.toLocaleString");
161 return this.toString();
165 // ECMA-262 - 15.2.4.4
166 function ObjectValueOf() {
167 return TO_OBJECT(this);
171 // ECMA-262 - 15.2.4.5
172 function ObjectHasOwnProperty(value) {
173 var name = $toName(value);
174 var object = TO_OBJECT(this);
176 if (%_IsJSProxy(object)) {
177 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
178 if (IS_SYMBOL(value)) return false;
180 var handler = %GetHandler(object);
181 return CallTrap1(handler, "hasOwn", ProxyDerivedHasOwnTrap, name);
183 return %HasOwnProperty(object, name);
187 // ECMA-262 - 15.2.4.6
188 function ObjectIsPrototypeOf(V) {
189 if (!IS_SPEC_OBJECT(V)) return false;
190 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.isPrototypeOf");
191 return %IsInPrototypeChain(this, V);
195 // ECMA-262 - 15.2.4.6
196 function ObjectPropertyIsEnumerable(V) {
198 if (%_IsJSProxy(this)) {
199 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
200 if (IS_SYMBOL(V)) return false;
202 var desc = GetOwnPropertyJS(this, P);
203 return IS_UNDEFINED(desc) ? false : desc.isEnumerable();
205 return %IsPropertyEnumerable(TO_OBJECT(this), P);
209 // Extensions for providing property getters and setters.
210 function ObjectDefineGetter(name, fun) {
212 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
213 receiver = %GlobalProxy(ObjectDefineGetter);
215 if (!IS_SPEC_FUNCTION(fun)) {
216 throw MakeTypeError(kObjectGetterExpectingFunction);
218 var desc = new PropertyDescriptor();
220 desc.setEnumerable(true);
221 desc.setConfigurable(true);
222 DefineOwnProperty(TO_OBJECT(receiver), $toName(name), desc, false);
226 function ObjectLookupGetter(name) {
228 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
229 receiver = %GlobalProxy(ObjectLookupGetter);
231 return %LookupAccessor(TO_OBJECT(receiver), $toName(name), GETTER);
235 function ObjectDefineSetter(name, fun) {
237 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
238 receiver = %GlobalProxy(ObjectDefineSetter);
240 if (!IS_SPEC_FUNCTION(fun)) {
241 throw MakeTypeError(kObjectSetterExpectingFunction);
243 var desc = new PropertyDescriptor();
245 desc.setEnumerable(true);
246 desc.setConfigurable(true);
247 DefineOwnProperty(TO_OBJECT(receiver), $toName(name), desc, false);
251 function ObjectLookupSetter(name) {
253 if (IS_NULL(receiver) || IS_UNDEFINED(receiver)) {
254 receiver = %GlobalProxy(ObjectLookupSetter);
256 return %LookupAccessor(TO_OBJECT(receiver), $toName(name), SETTER);
260 function ObjectKeys(obj) {
261 obj = TO_OBJECT(obj);
262 if (%_IsJSProxy(obj)) {
263 var handler = %GetHandler(obj);
264 var names = CallTrap0(handler, "keys", ProxyDerivedKeysTrap);
265 return ToNameArray(names, "keys", false);
267 return %OwnKeys(obj);
272 function IsAccessorDescriptor(desc) {
273 if (IS_UNDEFINED(desc)) return false;
274 return desc.hasGetter() || desc.hasSetter();
279 function IsDataDescriptor(desc) {
280 if (IS_UNDEFINED(desc)) return false;
281 return desc.hasValue() || desc.hasWritable();
286 function IsGenericDescriptor(desc) {
287 if (IS_UNDEFINED(desc)) return false;
288 return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc));
292 function IsInconsistentDescriptor(desc) {
293 return IsAccessorDescriptor(desc) && IsDataDescriptor(desc);
298 function FromPropertyDescriptor(desc) {
299 if (IS_UNDEFINED(desc)) return desc;
301 if (IsDataDescriptor(desc)) {
302 return { value: desc.getValue(),
303 writable: desc.isWritable(),
304 enumerable: desc.isEnumerable(),
305 configurable: desc.isConfigurable() };
307 // Must be an AccessorDescriptor then. We never return a generic descriptor.
308 return { get: desc.getGet(),
310 enumerable: desc.isEnumerable(),
311 configurable: desc.isConfigurable() };
316 function FromGenericPropertyDescriptor(desc) {
317 if (IS_UNDEFINED(desc)) return desc;
318 var obj = new GlobalObject();
320 if (desc.hasValue()) {
321 %AddNamedProperty(obj, "value", desc.getValue(), NONE);
323 if (desc.hasWritable()) {
324 %AddNamedProperty(obj, "writable", desc.isWritable(), NONE);
326 if (desc.hasGetter()) {
327 %AddNamedProperty(obj, "get", desc.getGet(), NONE);
329 if (desc.hasSetter()) {
330 %AddNamedProperty(obj, "set", desc.getSet(), NONE);
332 if (desc.hasEnumerable()) {
333 %AddNamedProperty(obj, "enumerable", desc.isEnumerable(), NONE);
335 if (desc.hasConfigurable()) {
336 %AddNamedProperty(obj, "configurable", desc.isConfigurable(), NONE);
343 function ToPropertyDescriptor(obj) {
344 if (!IS_SPEC_OBJECT(obj)) throw MakeTypeError(kPropertyDescObject, obj);
346 var desc = new PropertyDescriptor();
348 if ("enumerable" in obj) {
349 desc.setEnumerable($toBoolean(obj.enumerable));
352 if ("configurable" in obj) {
353 desc.setConfigurable($toBoolean(obj.configurable));
356 if ("value" in obj) {
357 desc.setValue(obj.value);
360 if ("writable" in obj) {
361 desc.setWritable($toBoolean(obj.writable));
366 if (!IS_UNDEFINED(get) && !IS_SPEC_FUNCTION(get)) {
367 throw MakeTypeError(kObjectGetterCallable, get);
374 if (!IS_UNDEFINED(set) && !IS_SPEC_FUNCTION(set)) {
375 throw MakeTypeError(kObjectSetterCallable, set);
380 if (IsInconsistentDescriptor(desc)) {
381 throw MakeTypeError(kValueAndAccessor, obj);
387 // For Harmony proxies.
388 function ToCompletePropertyDescriptor(obj) {
389 var desc = ToPropertyDescriptor(obj);
390 if (IsGenericDescriptor(desc) || IsDataDescriptor(desc)) {
391 if (!desc.hasValue()) desc.setValue(UNDEFINED);
392 if (!desc.hasWritable()) desc.setWritable(false);
394 // Is accessor descriptor.
395 if (!desc.hasGetter()) desc.setGet(UNDEFINED);
396 if (!desc.hasSetter()) desc.setSet(UNDEFINED);
398 if (!desc.hasEnumerable()) desc.setEnumerable(false);
399 if (!desc.hasConfigurable()) desc.setConfigurable(false);
404 function PropertyDescriptor() {
405 // Initialize here so they are all in-object and have the same map.
406 // Default values from ES5 8.6.1.
407 this.value_ = UNDEFINED;
408 this.hasValue_ = false;
409 this.writable_ = false;
410 this.hasWritable_ = false;
411 this.enumerable_ = false;
412 this.hasEnumerable_ = false;
413 this.configurable_ = false;
414 this.hasConfigurable_ = false;
415 this.get_ = UNDEFINED;
416 this.hasGetter_ = false;
417 this.set_ = UNDEFINED;
418 this.hasSetter_ = false;
421 utils.SetUpLockedPrototype(PropertyDescriptor, [
435 "toString", function PropertyDescriptor_ToString() {
436 return "[object PropertyDescriptor]";
438 "setValue", function PropertyDescriptor_SetValue(value) {
440 this.hasValue_ = true;
442 "getValue", function PropertyDescriptor_GetValue() {
445 "hasValue", function PropertyDescriptor_HasValue() {
446 return this.hasValue_;
448 "setEnumerable", function PropertyDescriptor_SetEnumerable(enumerable) {
449 this.enumerable_ = enumerable;
450 this.hasEnumerable_ = true;
452 "isEnumerable", function PropertyDescriptor_IsEnumerable() {
453 return this.enumerable_;
455 "hasEnumerable", function PropertyDescriptor_HasEnumerable() {
456 return this.hasEnumerable_;
458 "setWritable", function PropertyDescriptor_SetWritable(writable) {
459 this.writable_ = writable;
460 this.hasWritable_ = true;
462 "isWritable", function PropertyDescriptor_IsWritable() {
463 return this.writable_;
465 "hasWritable", function PropertyDescriptor_HasWritable() {
466 return this.hasWritable_;
469 function PropertyDescriptor_SetConfigurable(configurable) {
470 this.configurable_ = configurable;
471 this.hasConfigurable_ = true;
473 "hasConfigurable", function PropertyDescriptor_HasConfigurable() {
474 return this.hasConfigurable_;
476 "isConfigurable", function PropertyDescriptor_IsConfigurable() {
477 return this.configurable_;
479 "setGet", function PropertyDescriptor_SetGetter(get) {
481 this.hasGetter_ = true;
483 "getGet", function PropertyDescriptor_GetGetter() {
486 "hasGetter", function PropertyDescriptor_HasGetter() {
487 return this.hasGetter_;
489 "setSet", function PropertyDescriptor_SetSetter(set) {
491 this.hasSetter_ = true;
493 "getSet", function PropertyDescriptor_GetSetter() {
496 "hasSetter", function PropertyDescriptor_HasSetter() {
497 return this.hasSetter_;
502 // Converts an array returned from Runtime_GetOwnProperty to an actual
503 // property descriptor. For a description of the array layout please
504 // see the runtime.cc file.
505 function ConvertDescriptorArrayToDescriptor(desc_array) {
506 if (IS_UNDEFINED(desc_array)) {
510 var desc = new PropertyDescriptor();
511 // This is an accessor.
512 if (desc_array[IS_ACCESSOR_INDEX]) {
513 desc.setGet(desc_array[GETTER_INDEX]);
514 desc.setSet(desc_array[SETTER_INDEX]);
516 desc.setValue(desc_array[VALUE_INDEX]);
517 desc.setWritable(desc_array[WRITABLE_INDEX]);
519 desc.setEnumerable(desc_array[ENUMERABLE_INDEX]);
520 desc.setConfigurable(desc_array[CONFIGURABLE_INDEX]);
526 // For Harmony proxies.
527 function GetTrap(handler, name, defaultTrap) {
528 var trap = handler[name];
529 if (IS_UNDEFINED(trap)) {
530 if (IS_UNDEFINED(defaultTrap)) {
531 throw MakeTypeError(kProxyHandlerTrapMissing, handler, name);
534 } else if (!IS_SPEC_FUNCTION(trap)) {
535 throw MakeTypeError(kProxyHandlerTrapMustBeCallable, handler, name);
541 function CallTrap0(handler, name, defaultTrap) {
542 return %_CallFunction(handler, GetTrap(handler, name, defaultTrap));
546 function CallTrap1(handler, name, defaultTrap, x) {
547 return %_CallFunction(handler, x, GetTrap(handler, name, defaultTrap));
551 function CallTrap2(handler, name, defaultTrap, x, y) {
552 return %_CallFunction(handler, x, y, GetTrap(handler, name, defaultTrap));
556 // ES5 section 8.12.1.
557 function GetOwnPropertyJS(obj, v) {
559 if (%_IsJSProxy(obj)) {
560 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
561 if (IS_SYMBOL(v)) return UNDEFINED;
563 var handler = %GetHandler(obj);
564 var descriptor = CallTrap1(
565 handler, "getOwnPropertyDescriptor", UNDEFINED, p);
566 if (IS_UNDEFINED(descriptor)) return descriptor;
567 var desc = ToCompletePropertyDescriptor(descriptor);
568 if (!desc.isConfigurable()) {
569 throw MakeTypeError(kProxyPropNotConfigurable,
570 handler, p, "getOwnPropertyDescriptor");
575 // GetOwnProperty returns an array indexed by the constants
576 // defined in macros.py.
577 // If p is not a property on obj undefined is returned.
578 var props = %GetOwnProperty(TO_OBJECT(obj), p);
580 return ConvertDescriptorArrayToDescriptor(props);
584 // ES5 section 8.12.7.
585 function Delete(obj, p, should_throw) {
586 var desc = GetOwnPropertyJS(obj, p);
587 if (IS_UNDEFINED(desc)) return true;
588 if (desc.isConfigurable()) {
589 %DeleteProperty_Sloppy(obj, p);
591 } else if (should_throw) {
592 throw MakeTypeError(kDefineDisallowed, p);
599 // ES6, draft 12-24-14, section 7.3.8
600 function GetMethod(obj, p) {
602 if (IS_NULL_OR_UNDEFINED(func)) return UNDEFINED;
603 if (IS_SPEC_FUNCTION(func)) return func;
604 throw MakeTypeError(kCalledNonCallable, typeof func);
609 function DefineProxyProperty(obj, p, attributes, should_throw) {
610 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
611 if (IS_SYMBOL(p)) return false;
613 var handler = %GetHandler(obj);
614 var result = CallTrap2(handler, "defineProperty", UNDEFINED, p, attributes);
615 if (!$toBoolean(result)) {
617 throw MakeTypeError(kProxyHandlerReturned,
618 handler, "false", "defineProperty");
628 function DefineObjectProperty(obj, p, desc, should_throw) {
629 var current_array = %GetOwnProperty(obj, $toName(p));
630 var current = ConvertDescriptorArrayToDescriptor(current_array);
631 var extensible = %IsExtensible(obj);
633 // Error handling according to spec.
635 if (IS_UNDEFINED(current) && !extensible) {
637 throw MakeTypeError(kDefineDisallowed, p);
643 if (!IS_UNDEFINED(current)) {
645 if ((IsGenericDescriptor(desc) ||
646 IsDataDescriptor(desc) == IsDataDescriptor(current)) &&
647 (!desc.hasEnumerable() ||
648 $sameValue(desc.isEnumerable(), current.isEnumerable())) &&
649 (!desc.hasConfigurable() ||
650 $sameValue(desc.isConfigurable(), current.isConfigurable())) &&
651 (!desc.hasWritable() ||
652 $sameValue(desc.isWritable(), current.isWritable())) &&
654 $sameValue(desc.getValue(), current.getValue())) &&
655 (!desc.hasGetter() ||
656 $sameValue(desc.getGet(), current.getGet())) &&
657 (!desc.hasSetter() ||
658 $sameValue(desc.getSet(), current.getSet()))) {
661 if (!current.isConfigurable()) {
663 if (desc.isConfigurable() ||
664 (desc.hasEnumerable() &&
665 desc.isEnumerable() != current.isEnumerable())) {
667 throw MakeTypeError(kRedefineDisallowed, p);
673 if (!IsGenericDescriptor(desc)) {
675 if (IsDataDescriptor(current) != IsDataDescriptor(desc)) {
677 throw MakeTypeError(kRedefineDisallowed, p);
683 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) {
684 var currentIsWritable = current.isWritable();
685 if (currentIsWritable != desc.isWritable()) {
686 if (!currentIsWritable || IS_STRONG(obj)) {
688 throw currentIsWritable
689 ? MakeTypeError(kStrongRedefineDisallowed, obj, p)
690 : MakeTypeError(kRedefineDisallowed, p);
696 if (!currentIsWritable && desc.hasValue() &&
697 !$sameValue(desc.getValue(), current.getValue())) {
699 throw MakeTypeError(kRedefineDisallowed, p);
706 if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) {
707 if (desc.hasSetter() &&
708 !$sameValue(desc.getSet(), current.getSet())) {
710 throw MakeTypeError(kRedefineDisallowed, p);
715 if (desc.hasGetter() && !$sameValue(desc.getGet(),current.getGet())) {
717 throw MakeTypeError(kRedefineDisallowed, p);
727 // Send flags - enumerable and configurable are common - writable is
728 // only send to the data descriptor.
729 // Take special care if enumerable and configurable is not defined on
730 // desc (we need to preserve the existing values from current).
732 if (desc.hasEnumerable()) {
733 flag |= desc.isEnumerable() ? 0 : DONT_ENUM;
734 } else if (!IS_UNDEFINED(current)) {
735 flag |= current.isEnumerable() ? 0 : DONT_ENUM;
740 if (desc.hasConfigurable()) {
741 flag |= desc.isConfigurable() ? 0 : DONT_DELETE;
742 } else if (!IS_UNDEFINED(current)) {
743 flag |= current.isConfigurable() ? 0 : DONT_DELETE;
747 if (IsDataDescriptor(desc) ||
748 (IsGenericDescriptor(desc) &&
749 (IS_UNDEFINED(current) || IsDataDescriptor(current)))) {
750 // There are 3 cases that lead here:
751 // Step 4a - defining a new data property.
752 // Steps 9b & 12 - replacing an existing accessor property with a data
754 // Step 12 - updating an existing data property with a data or generic
757 if (desc.hasWritable()) {
758 flag |= desc.isWritable() ? 0 : READ_ONLY;
759 } else if (!IS_UNDEFINED(current)) {
760 flag |= current.isWritable() ? 0 : READ_ONLY;
765 var value = UNDEFINED; // Default value is undefined.
766 if (desc.hasValue()) {
767 value = desc.getValue();
768 } else if (!IS_UNDEFINED(current) && IsDataDescriptor(current)) {
769 value = current.getValue();
772 %DefineDataPropertyUnchecked(obj, p, value, flag);
774 // There are 3 cases that lead here:
775 // Step 4b - defining a new accessor property.
776 // Steps 9c & 12 - replacing an existing data property with an accessor
778 // Step 12 - updating an existing accessor property with an accessor
781 if (desc.hasGetter()) {
782 getter = desc.getGet();
783 } else if (IsAccessorDescriptor(current) && current.hasGetter()) {
784 getter = current.getGet();
787 if (desc.hasSetter()) {
788 setter = desc.getSet();
789 } else if (IsAccessorDescriptor(current) && current.hasSetter()) {
790 setter = current.getSet();
792 %DefineAccessorPropertyUnchecked(obj, p, getter, setter, flag);
798 // ES5 section 15.4.5.1.
799 function DefineArrayProperty(obj, p, desc, should_throw) {
800 // Step 3 - Special handling for array index.
802 var index = TO_UINT32(p);
803 var emit_splice = false;
804 if ($toString(index) == p && index != 4294967295) {
805 var length = obj.length;
806 if (index >= length && %IsObserved(obj)) {
808 $observeBeginPerformSplice(obj);
811 var length_desc = GetOwnPropertyJS(obj, "length");
812 if ((index >= length && !length_desc.isWritable()) ||
813 !DefineObjectProperty(obj, p, desc, true)) {
815 $observeEndPerformSplice(obj);
817 throw MakeTypeError(kDefineDisallowed, p);
822 if (index >= length) {
823 obj.length = index + 1;
826 $observeEndPerformSplice(obj);
827 $observeEnqueueSpliceRecord(obj, length, [], index + 1 - length);
833 // Step 5 - Fallback to default implementation.
834 return DefineObjectProperty(obj, p, desc, should_throw);
838 // ES5 section 8.12.9, ES5 section 15.4.5.1 and Harmony proxies.
839 function DefineOwnProperty(obj, p, desc, should_throw) {
840 if (%_IsJSProxy(obj)) {
841 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
842 if (IS_SYMBOL(p)) return false;
844 var attributes = FromGenericPropertyDescriptor(desc);
845 return DefineProxyProperty(obj, p, attributes, should_throw);
846 } else if (IS_ARRAY(obj)) {
847 return DefineArrayProperty(obj, p, desc, should_throw);
849 return DefineObjectProperty(obj, p, desc, should_throw);
854 function DefineOwnPropertyFromAPI(obj, p, value, desc) {
855 return DefineOwnProperty(obj, p, ToPropertyDescriptor({
859 configurable: desc[2]
865 // ES6 section 19.1.2.9
866 function ObjectGetPrototypeOf(obj) {
867 return %_GetPrototype(TO_OBJECT(obj));
870 // ES6 section 19.1.2.19.
871 function ObjectSetPrototypeOf(obj, proto) {
872 CHECK_OBJECT_COERCIBLE(obj, "Object.setPrototypeOf");
874 if (proto !== null && !IS_SPEC_OBJECT(proto)) {
875 throw MakeTypeError(kProtoObjectOrNull, proto);
878 if (IS_SPEC_OBJECT(obj)) {
879 %SetPrototype(obj, proto);
886 // ES6 section 19.1.2.6
887 function ObjectGetOwnPropertyDescriptor(obj, p) {
888 var desc = GetOwnPropertyJS(TO_OBJECT(obj), p);
889 return FromPropertyDescriptor(desc);
893 // For Harmony proxies
894 function ToNameArray(obj, trap, includeSymbols) {
895 if (!IS_SPEC_OBJECT(obj)) {
896 throw MakeTypeError(kProxyNonObjectPropNames, trap, obj);
898 var n = TO_UINT32(obj.length);
899 var array = new GlobalArray(n);
901 var names = { __proto__: null }; // TODO(rossberg): use sets once ready.
902 for (var index = 0; index < n; index++) {
903 var s = $toName(obj[index]);
904 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
905 if (IS_SYMBOL(s) && !includeSymbols) continue;
906 if (%HasOwnProperty(names, s)) {
907 throw MakeTypeError(kProxyRepeatedPropName, trap, s);
909 array[realLength] = s;
913 array.length = realLength;
918 function ObjectGetOwnPropertyKeys(obj, filter) {
919 var nameArrays = new InternalArray();
920 filter |= PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL;
921 var interceptorInfo = %GetInterceptorInfo(obj);
923 // Find all the indexed properties.
925 // Only get own element names if we want to include string keys.
926 if ((filter & PROPERTY_ATTRIBUTES_STRING) === 0) {
927 var ownElementNames = %GetOwnElementNames(obj);
928 for (var i = 0; i < ownElementNames.length; ++i) {
929 ownElementNames[i] = %_NumberToString(ownElementNames[i]);
931 nameArrays.push(ownElementNames);
932 // Get names for indexed interceptor properties.
933 if ((interceptorInfo & 1) != 0) {
934 var indexedInterceptorNames = %GetIndexedInterceptorElementNames(obj);
935 if (!IS_UNDEFINED(indexedInterceptorNames)) {
936 nameArrays.push(indexedInterceptorNames);
941 // Find all the named properties.
943 // Get own property names.
944 nameArrays.push(%GetOwnPropertyNames(obj, filter));
946 // Get names for named interceptor properties if any.
947 if ((interceptorInfo & 2) != 0) {
948 var namedInterceptorNames =
949 %GetNamedInterceptorPropertyNames(obj);
950 if (!IS_UNDEFINED(namedInterceptorNames)) {
951 nameArrays.push(namedInterceptorNames);
956 %Apply(InternalArray.prototype.concat,
957 nameArrays[0], nameArrays, 1, nameArrays.length - 1);
959 // Property names are expected to be unique strings,
960 // but interceptors can interfere with that assumption.
961 if (interceptorInfo != 0) {
962 var seenKeys = { __proto__: null };
964 for (var i = 0; i < propertyNames.length; ++i) {
965 var name = propertyNames[i];
966 if (IS_SYMBOL(name)) {
967 if ((filter & PROPERTY_ATTRIBUTES_SYMBOLIC) || IS_PRIVATE(name)) {
971 if (filter & PROPERTY_ATTRIBUTES_STRING) continue;
972 name = $toString(name);
974 if (seenKeys[name]) continue;
975 seenKeys[name] = true;
976 propertyNames[j++] = name;
978 propertyNames.length = j;
981 return propertyNames;
985 // ES6 section 9.1.12 / 9.5.12
986 function OwnPropertyKeys(obj) {
987 if (%_IsJSProxy(obj)) {
988 var handler = %GetHandler(obj);
989 // TODO(caitp): Proxy.[[OwnPropertyKeys]] can not be implemented to spec
990 // without an implementation of Direct Proxies.
991 var names = CallTrap0(handler, "ownKeys", UNDEFINED);
992 return ToNameArray(names, "getOwnPropertyNames", false);
994 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL);
998 // ES5 section 15.2.3.4.
999 function ObjectGetOwnPropertyNames(obj) {
1000 obj = TO_OBJECT(obj);
1001 // Special handling for proxies.
1002 if (%_IsJSProxy(obj)) {
1003 var handler = %GetHandler(obj);
1004 var names = CallTrap0(handler, "getOwnPropertyNames", UNDEFINED);
1005 return ToNameArray(names, "getOwnPropertyNames", false);
1008 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_SYMBOLIC);
1012 // ES5 section 15.2.3.5.
1013 function ObjectCreate(proto, properties) {
1014 if (!IS_SPEC_OBJECT(proto) && proto !== null) {
1015 throw MakeTypeError(kProtoObjectOrNull, proto);
1018 %InternalSetPrototype(obj, proto);
1019 if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties);
1024 // ES5 section 15.2.3.6.
1025 function ObjectDefineProperty(obj, p, attributes) {
1026 if (!IS_SPEC_OBJECT(obj)) {
1027 throw MakeTypeError(kCalledOnNonObject, "Object.defineProperty");
1029 var name = $toName(p);
1030 if (%_IsJSProxy(obj)) {
1031 // Clone the attributes object for protection.
1032 // TODO(rossberg): not spec'ed yet, so not sure if this should involve
1033 // non-own properties as it does (or non-enumerable ones, as it doesn't?).
1034 var attributesClone = { __proto__: null };
1035 for (var a in attributes) {
1036 attributesClone[a] = attributes[a];
1038 DefineProxyProperty(obj, name, attributesClone, true);
1039 // The following would implement the spec as in the current proposal,
1040 // but after recent comments on es-discuss, is most likely obsolete.
1042 var defineObj = FromGenericPropertyDescriptor(desc);
1043 var names = ObjectGetOwnPropertyNames(attributes);
1045 {value: 0, writable: 0, get: 0, set: 0, enumerable: 0, configurable: 0};
1046 for (var i = 0; i < names.length; i++) {
1048 if (!(%HasOwnProperty(standardNames, N))) {
1049 var attr = GetOwnPropertyJS(attributes, N);
1050 DefineOwnProperty(descObj, N, attr, true);
1053 // This is really confusing the types, but it is what the proxies spec
1054 // currently requires:
1058 var desc = ToPropertyDescriptor(attributes);
1059 DefineOwnProperty(obj, name, desc, true);
1065 function GetOwnEnumerablePropertyNames(object) {
1066 var names = new InternalArray();
1067 for (var key in object) {
1068 if (%HasOwnProperty(object, key)) {
1073 var filter = PROPERTY_ATTRIBUTES_STRING | PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL;
1074 var symbols = %GetOwnPropertyNames(object, filter);
1075 for (var i = 0; i < symbols.length; ++i) {
1076 var symbol = symbols[i];
1077 if (IS_SYMBOL(symbol)) {
1078 var desc = ObjectGetOwnPropertyDescriptor(object, symbol);
1079 if (desc.enumerable) names.push(symbol);
1087 // ES5 section 15.2.3.7.
1088 function ObjectDefineProperties(obj, properties) {
1089 if (!IS_SPEC_OBJECT(obj)) {
1090 throw MakeTypeError(kCalledOnNonObject, "Object.defineProperties");
1092 var props = TO_OBJECT(properties);
1093 var names = GetOwnEnumerablePropertyNames(props);
1094 var descriptors = new InternalArray();
1095 for (var i = 0; i < names.length; i++) {
1096 descriptors.push(ToPropertyDescriptor(props[names[i]]));
1098 for (var i = 0; i < names.length; i++) {
1099 DefineOwnProperty(obj, names[i], descriptors[i], true);
1106 function ProxyFix(obj) {
1107 var handler = %GetHandler(obj);
1108 var props = CallTrap0(handler, "fix", UNDEFINED);
1109 if (IS_UNDEFINED(props)) {
1110 throw MakeTypeError(kProxyHandlerReturned, handler, "undefined", "fix");
1113 if (%IsJSFunctionProxy(obj)) {
1114 var callTrap = %GetCallTrap(obj);
1115 var constructTrap = %GetConstructTrap(obj);
1116 var code = ProxyDelegateCallAndConstruct(callTrap, constructTrap);
1117 %Fix(obj); // becomes a regular function
1118 %SetCode(obj, code);
1119 // TODO(rossberg): What about length and other properties? Not specified.
1120 // We just put in some half-reasonable defaults for now.
1121 var prototype = new GlobalObject();
1122 ObjectDefineProperty(prototype, "constructor",
1123 {value: obj, writable: true, enumerable: false, configurable: true});
1124 // TODO(v8:1530): defineProperty does not handle prototype and length.
1125 %FunctionSetPrototype(obj, prototype);
1130 ObjectDefineProperties(obj, props);
1134 // ES5 section 15.2.3.8.
1135 function ObjectSealJS(obj) {
1136 if (!IS_SPEC_OBJECT(obj)) return obj;
1137 var isProxy = %_IsJSProxy(obj);
1138 if (isProxy || %HasSloppyArgumentsElements(obj) || %IsObserved(obj)) {
1142 var names = ObjectGetOwnPropertyNames(obj);
1143 for (var i = 0; i < names.length; i++) {
1144 var name = names[i];
1145 var desc = GetOwnPropertyJS(obj, name);
1146 if (desc.isConfigurable()) {
1147 desc.setConfigurable(false);
1148 DefineOwnProperty(obj, name, desc, true);
1151 %PreventExtensions(obj);
1153 // TODO(adamk): Is it worth going to this fast path if the
1154 // object's properties are already in dictionary mode?
1161 // ES5 section 15.2.3.9.
1162 function ObjectFreezeJS(obj) {
1163 if (!IS_SPEC_OBJECT(obj)) return obj;
1164 var isProxy = %_IsJSProxy(obj);
1165 // TODO(conradw): Investigate modifying the fast path to accommodate strong
1167 if (isProxy || %HasSloppyArgumentsElements(obj) || %IsObserved(obj) ||
1172 var names = ObjectGetOwnPropertyNames(obj);
1173 for (var i = 0; i < names.length; i++) {
1174 var name = names[i];
1175 var desc = GetOwnPropertyJS(obj, name);
1176 if (desc.isWritable() || desc.isConfigurable()) {
1177 if (IsDataDescriptor(desc)) desc.setWritable(false);
1178 desc.setConfigurable(false);
1179 DefineOwnProperty(obj, name, desc, true);
1182 %PreventExtensions(obj);
1184 // TODO(adamk): Is it worth going to this fast path if the
1185 // object's properties are already in dictionary mode?
1192 // ES5 section 15.2.3.10
1193 function ObjectPreventExtension(obj) {
1194 if (!IS_SPEC_OBJECT(obj)) return obj;
1195 if (%_IsJSProxy(obj)) {
1198 %PreventExtensions(obj);
1203 // ES5 section 15.2.3.11
1204 function ObjectIsSealed(obj) {
1205 if (!IS_SPEC_OBJECT(obj)) return true;
1206 if (%_IsJSProxy(obj)) {
1209 if (%IsExtensible(obj)) {
1212 var names = ObjectGetOwnPropertyNames(obj);
1213 for (var i = 0; i < names.length; i++) {
1214 var name = names[i];
1215 var desc = GetOwnPropertyJS(obj, name);
1216 if (desc.isConfigurable()) {
1224 // ES5 section 15.2.3.12
1225 function ObjectIsFrozen(obj) {
1226 if (!IS_SPEC_OBJECT(obj)) return true;
1227 if (%_IsJSProxy(obj)) {
1230 if (%IsExtensible(obj)) {
1233 var names = ObjectGetOwnPropertyNames(obj);
1234 for (var i = 0; i < names.length; i++) {
1235 var name = names[i];
1236 var desc = GetOwnPropertyJS(obj, name);
1237 if (IsDataDescriptor(desc) && desc.isWritable()) return false;
1238 if (desc.isConfigurable()) return false;
1244 // ES5 section 15.2.3.13
1245 function ObjectIsExtensible(obj) {
1246 if (!IS_SPEC_OBJECT(obj)) return false;
1247 if (%_IsJSProxy(obj)) {
1250 return %IsExtensible(obj);
1254 // ECMA-262, Edition 6, section 19.1.2.10
1255 function ObjectIs(obj1, obj2) {
1256 return $sameValue(obj1, obj2);
1260 // ECMA-262, Edition 6, section B.2.2.1.1
1261 function ObjectGetProto() {
1262 return %_GetPrototype(TO_OBJECT(this));
1266 // ECMA-262, Edition 6, section B.2.2.1.2
1267 function ObjectSetProto(proto) {
1268 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.__proto__");
1270 if ((IS_SPEC_OBJECT(proto) || IS_NULL(proto)) && IS_SPEC_OBJECT(this)) {
1271 %SetPrototype(this, proto);
1276 function ObjectConstructor(x) {
1277 if (%_IsConstructCall()) {
1278 if (x == null) return this;
1279 return TO_OBJECT(x);
1281 if (x == null) return { };
1282 return TO_OBJECT(x);
1287 // ----------------------------------------------------------------------------
1290 %SetNativeFlag(GlobalObject);
1291 %SetCode(GlobalObject, ObjectConstructor);
1293 %AddNamedProperty(GlobalObject.prototype, "constructor", GlobalObject,
1296 // Set up non-enumerable functions on the Object.prototype object.
1297 utils.InstallFunctions(GlobalObject.prototype, DONT_ENUM, [
1298 "toString", ObjectToString,
1299 "toLocaleString", ObjectToLocaleString,
1300 "valueOf", ObjectValueOf,
1301 "hasOwnProperty", ObjectHasOwnProperty,
1302 "isPrototypeOf", ObjectIsPrototypeOf,
1303 "propertyIsEnumerable", ObjectPropertyIsEnumerable,
1304 "__defineGetter__", ObjectDefineGetter,
1305 "__lookupGetter__", ObjectLookupGetter,
1306 "__defineSetter__", ObjectDefineSetter,
1307 "__lookupSetter__", ObjectLookupSetter
1309 utils.InstallGetterSetter(GlobalObject.prototype, "__proto__", ObjectGetProto,
1312 // Set up non-enumerable functions in the Object object.
1313 utils.InstallFunctions(GlobalObject, DONT_ENUM, [
1315 "create", ObjectCreate,
1316 "defineProperty", ObjectDefineProperty,
1317 "defineProperties", ObjectDefineProperties,
1318 "freeze", ObjectFreezeJS,
1319 "getPrototypeOf", ObjectGetPrototypeOf,
1320 "setPrototypeOf", ObjectSetPrototypeOf,
1321 "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor,
1322 "getOwnPropertyNames", ObjectGetOwnPropertyNames,
1323 // getOwnPropertySymbols is added in symbol.js.
1325 "isExtensible", ObjectIsExtensible,
1326 "isFrozen", ObjectIsFrozen,
1327 "isSealed", ObjectIsSealed,
1328 "preventExtensions", ObjectPreventExtension,
1329 "seal", ObjectSealJS
1330 // deliverChangeRecords, getNotifier, observe and unobserve are added
1331 // in object-observe.js.
1335 // ----------------------------------------------------------------------------
1338 function BooleanConstructor(x) {
1339 if (%_IsConstructCall()) {
1340 %_SetValueOf(this, $toBoolean(x));
1342 return $toBoolean(x);
1347 function BooleanToString() {
1348 // NOTE: Both Boolean objects and values can enter here as
1349 // 'this'. This is not as dictated by ECMA-262.
1351 if (!IS_BOOLEAN(b)) {
1352 if (!IS_BOOLEAN_WRAPPER(b)) {
1353 throw MakeTypeError(kNotGeneric, 'Boolean.prototype.toString');
1357 return b ? 'true' : 'false';
1361 function BooleanValueOf() {
1362 // NOTE: Both Boolean objects and values can enter here as
1363 // 'this'. This is not as dictated by ECMA-262.
1364 if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) {
1365 throw MakeTypeError(kNotGeneric, 'Boolean.prototype.valueOf');
1367 return %_ValueOf(this);
1371 // ----------------------------------------------------------------------------
1373 %SetCode(GlobalBoolean, BooleanConstructor);
1374 %FunctionSetPrototype(GlobalBoolean, new GlobalBoolean(false));
1375 %AddNamedProperty(GlobalBoolean.prototype, "constructor", GlobalBoolean,
1378 utils.InstallFunctions(GlobalBoolean.prototype, DONT_ENUM, [
1379 "toString", BooleanToString,
1380 "valueOf", BooleanValueOf
1384 // ----------------------------------------------------------------------------
1387 function NumberConstructor(x) {
1388 var value = %_ArgumentsLength() == 0 ? 0 : $toNumber(x);
1389 if (%_IsConstructCall()) {
1390 %_SetValueOf(this, value);
1397 // ECMA-262 section 15.7.4.2.
1398 function NumberToStringJS(radix) {
1399 // NOTE: Both Number objects and values can enter here as
1400 // 'this'. This is not as dictated by ECMA-262.
1402 if (!IS_NUMBER(this)) {
1403 if (!IS_NUMBER_WRAPPER(this)) {
1404 throw MakeTypeError(kNotGeneric, 'Number.prototype.toString');
1406 // Get the value of this number in case it's an object.
1407 number = %_ValueOf(this);
1409 // Fast case: Convert number in radix 10.
1410 if (IS_UNDEFINED(radix) || radix === 10) {
1411 return %_NumberToString(number);
1414 // Convert the radix to an integer and check the range.
1415 radix = TO_INTEGER(radix);
1416 if (radix < 2 || radix > 36) throw MakeRangeError(kToRadixFormatRange);
1417 // Convert the number to a string in the given radix.
1418 return %NumberToRadixString(number, radix);
1422 // ECMA-262 section 15.7.4.3
1423 function NumberToLocaleString() {
1424 return %_CallFunction(this, NumberToStringJS);
1428 // ECMA-262 section 15.7.4.4
1429 function NumberValueOf() {
1430 // NOTE: Both Number objects and values can enter here as
1431 // 'this'. This is not as dictated by ECMA-262.
1432 if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) {
1433 throw MakeTypeError(kNotGeneric, 'Number.prototype.valueOf');
1435 return %_ValueOf(this);
1439 // ECMA-262 section 15.7.4.5
1440 function NumberToFixedJS(fractionDigits) {
1442 if (!IS_NUMBER(this)) {
1443 if (!IS_NUMBER_WRAPPER(this)) {
1444 throw MakeTypeError(kIncompatibleMethodReceiver,
1445 "Number.prototype.toFixed", this);
1447 // Get the value of this number in case it's an object.
1448 x = %_ValueOf(this);
1450 var f = TO_INTEGER(fractionDigits);
1452 if (f < 0 || f > 20) {
1453 throw MakeRangeError(kNumberFormatRange, "toFixed() digits");
1456 if (NUMBER_IS_NAN(x)) return "NaN";
1457 if (x == INFINITY) return "Infinity";
1458 if (x == -INFINITY) return "-Infinity";
1460 return %NumberToFixed(x, f);
1464 // ECMA-262 section 15.7.4.6
1465 function NumberToExponentialJS(fractionDigits) {
1467 if (!IS_NUMBER(this)) {
1468 if (!IS_NUMBER_WRAPPER(this)) {
1469 throw MakeTypeError(kIncompatibleMethodReceiver,
1470 "Number.prototype.toExponential", this);
1472 // Get the value of this number in case it's an object.
1473 x = %_ValueOf(this);
1475 var f = IS_UNDEFINED(fractionDigits) ? UNDEFINED : TO_INTEGER(fractionDigits);
1477 if (NUMBER_IS_NAN(x)) return "NaN";
1478 if (x == INFINITY) return "Infinity";
1479 if (x == -INFINITY) return "-Infinity";
1481 if (IS_UNDEFINED(f)) {
1482 f = -1; // Signal for runtime function that f is not defined.
1483 } else if (f < 0 || f > 20) {
1484 throw MakeRangeError(kNumberFormatRange, "toExponential()");
1486 return %NumberToExponential(x, f);
1490 // ECMA-262 section 15.7.4.7
1491 function NumberToPrecisionJS(precision) {
1493 if (!IS_NUMBER(this)) {
1494 if (!IS_NUMBER_WRAPPER(this)) {
1495 throw MakeTypeError(kIncompatibleMethodReceiver,
1496 "Number.prototype.toPrecision", this);
1498 // Get the value of this number in case it's an object.
1499 x = %_ValueOf(this);
1501 if (IS_UNDEFINED(precision)) return $toString(%_ValueOf(this));
1502 var p = TO_INTEGER(precision);
1504 if (NUMBER_IS_NAN(x)) return "NaN";
1505 if (x == INFINITY) return "Infinity";
1506 if (x == -INFINITY) return "-Infinity";
1508 if (p < 1 || p > 21) {
1509 throw MakeRangeError(kToPrecisionFormatRange);
1511 return %NumberToPrecision(x, p);
1515 // Harmony isFinite.
1516 function NumberIsFinite(number) {
1517 return IS_NUMBER(number) && NUMBER_IS_FINITE(number);
1521 // Harmony isInteger
1522 function NumberIsInteger(number) {
1523 return NumberIsFinite(number) && TO_INTEGER(number) == number;
1528 function NumberIsNaN(number) {
1529 return IS_NUMBER(number) && NUMBER_IS_NAN(number);
1533 // Harmony isSafeInteger
1534 function NumberIsSafeInteger(number) {
1535 if (NumberIsFinite(number)) {
1536 var integral = TO_INTEGER(number);
1537 if (integral == number) {
1538 return MathAbs(integral) <= GlobalNumber.MAX_SAFE_INTEGER;
1545 // ----------------------------------------------------------------------------
1547 %SetCode(GlobalNumber, NumberConstructor);
1548 %FunctionSetPrototype(GlobalNumber, new GlobalNumber(0));
1550 %OptimizeObjectForAddingMultipleProperties(GlobalNumber.prototype, 8);
1551 // Set up the constructor property on the Number prototype object.
1552 %AddNamedProperty(GlobalNumber.prototype, "constructor", GlobalNumber,
1555 utils.InstallConstants(GlobalNumber, [
1556 // ECMA-262 section 15.7.3.1.
1557 "MAX_VALUE", 1.7976931348623157e+308,
1558 // ECMA-262 section 15.7.3.2.
1559 "MIN_VALUE", 5e-324,
1560 // ECMA-262 section 15.7.3.3.
1562 // ECMA-262 section 15.7.3.4.
1563 "NEGATIVE_INFINITY", -INFINITY,
1564 // ECMA-262 section 15.7.3.5.
1565 "POSITIVE_INFINITY", INFINITY,
1567 // --- Harmony constants (no spec refs until settled.)
1569 "MAX_SAFE_INTEGER", %_MathPow(2, 53) - 1,
1570 "MIN_SAFE_INTEGER", -%_MathPow(2, 53) + 1,
1571 "EPSILON", %_MathPow(2, -52)
1574 // Set up non-enumerable functions on the Number prototype object.
1575 utils.InstallFunctions(GlobalNumber.prototype, DONT_ENUM, [
1576 "toString", NumberToStringJS,
1577 "toLocaleString", NumberToLocaleString,
1578 "valueOf", NumberValueOf,
1579 "toFixed", NumberToFixedJS,
1580 "toExponential", NumberToExponentialJS,
1581 "toPrecision", NumberToPrecisionJS
1584 // Harmony Number constructor additions
1585 utils.InstallFunctions(GlobalNumber, DONT_ENUM, [
1586 "isFinite", NumberIsFinite,
1587 "isInteger", NumberIsInteger,
1588 "isNaN", NumberIsNaN,
1589 "isSafeInteger", NumberIsSafeInteger,
1590 "parseInt", GlobalParseInt,
1591 "parseFloat", GlobalParseFloat
1594 %SetForceInlineFlag(NumberIsNaN);
1597 // ----------------------------------------------------------------------------
1600 function NativeCodeFunctionSourceString(func) {
1601 var name = %FunctionGetName(func);
1603 // Mimic what KJS does.
1604 return 'function ' + name + '() { [native code] }';
1607 return 'function () { [native code] }';
1610 function FunctionSourceString(func) {
1611 while (%IsJSFunctionProxy(func)) {
1612 func = %GetCallTrap(func);
1615 if (!IS_FUNCTION(func)) {
1616 throw MakeTypeError(kNotGeneric, 'Function.prototype.toString');
1619 if (%FunctionIsBuiltin(func)) {
1620 return NativeCodeFunctionSourceString(func);
1623 var classSource = %ClassGetSourceCode(func);
1624 if (IS_STRING(classSource)) {
1628 var source = %FunctionGetSourceCode(func);
1629 if (!IS_STRING(source)) {
1630 return NativeCodeFunctionSourceString(func);
1633 if (%FunctionIsArrow(func)) {
1637 var name = %FunctionNameShouldPrintAsAnonymous(func)
1639 : %FunctionGetName(func);
1641 var isGenerator = %FunctionIsGenerator(func);
1642 var head = %FunctionIsConciseMethod(func)
1643 ? (isGenerator ? '*' : '')
1644 : (isGenerator ? 'function* ' : 'function ');
1645 return head + name + source;
1649 function FunctionToString() {
1650 return FunctionSourceString(this);
1655 function FunctionBind(this_arg) { // Length is 1.
1656 if (!IS_SPEC_FUNCTION(this)) throw MakeTypeError(kFunctionBind);
1658 var boundFunction = function () {
1659 // Poison .arguments and .caller, but is otherwise not detectable.
1661 // This function must not use any object literals (Object, Array, RegExp),
1662 // since the literals-array is being used to store the bound data.
1663 if (%_IsConstructCall()) {
1664 return %NewObjectFromBound(boundFunction);
1666 var bindings = %BoundFunctionGetBindings(boundFunction);
1668 var argc = %_ArgumentsLength();
1670 return %Apply(bindings[0], bindings[1], bindings, 2, bindings.length - 2);
1672 if (bindings.length === 2) {
1673 return %Apply(bindings[0], bindings[1], arguments, 0, argc);
1675 var bound_argc = bindings.length - 2;
1676 var argv = new InternalArray(bound_argc + argc);
1677 for (var i = 0; i < bound_argc; i++) {
1678 argv[i] = bindings[i + 2];
1680 for (var j = 0; j < argc; j++) {
1681 argv[i++] = %_Arguments(j);
1683 return %Apply(bindings[0], bindings[1], argv, 0, bound_argc + argc);
1687 var old_length = this.length;
1688 // FunctionProxies might provide a non-UInt32 value. If so, ignore it.
1689 if ((typeof old_length === "number") &&
1690 ((old_length >>> 0) === old_length)) {
1691 var argc = %_ArgumentsLength();
1692 if (argc > 0) argc--; // Don't count the thisArg as parameter.
1693 new_length = old_length - argc;
1694 if (new_length < 0) new_length = 0;
1696 // This runtime function finds any remaining arguments on the stack,
1697 // so we don't pass the arguments object.
1698 var result = %FunctionBindArguments(boundFunction, this,
1699 this_arg, new_length);
1701 var name = this.name;
1702 var bound_name = IS_STRING(name) ? name : "";
1703 %DefineDataPropertyUnchecked(result, "name", "bound " + bound_name,
1704 DONT_ENUM | READ_ONLY);
1706 // We already have caller and arguments properties on functions,
1707 // which are non-configurable. It therefore makes no sence to
1708 // try to redefine these as defined by the spec. The spec says
1709 // that bind should make these throw a TypeError if get or set
1710 // is called and make them non-enumerable and non-configurable.
1711 // To be consistent with our normal functions we leave this as it is.
1712 // TODO(lrn): Do set these to be thrower.
1717 function NewFunctionString(args, function_token) {
1718 var n = args.length;
1721 p = $toString(args[0]);
1722 for (var i = 1; i < n - 1; i++) {
1723 p += ',' + $toString(args[i]);
1725 // If the formal parameters string include ) - an illegal
1726 // character - it may make the combined function expression
1727 // compile. We avoid this problem by checking for this early on.
1728 if (%_CallFunction(p, ')', StringIndexOf) != -1) {
1729 throw MakeSyntaxError(kParenthesisInArgString);
1731 // If the formal parameters include an unbalanced block comment, the
1732 // function must be rejected. Since JavaScript does not allow nested
1733 // comments we can include a trailing block comment to catch this.
1736 var body = (n > 0) ? $toString(args[n - 1]) : '';
1737 return '(' + function_token + '(' + p + ') {\n' + body + '\n})';
1741 function FunctionConstructor(arg1) { // length == 1
1742 var source = NewFunctionString(arguments, 'function');
1743 var global_proxy = %GlobalProxy(FunctionConstructor);
1744 // Compile the string in the constructor and not a helper so that errors
1745 // appear to come from here.
1746 var f = %_CallFunction(global_proxy, %CompileString(source, true));
1747 %FunctionMarkNameShouldPrintAsAnonymous(f);
1752 // ----------------------------------------------------------------------------
1754 %SetCode(GlobalFunction, FunctionConstructor);
1755 %AddNamedProperty(GlobalFunction.prototype, "constructor", GlobalFunction,
1758 utils.InstallFunctions(GlobalFunction.prototype, DONT_ENUM, [
1759 "bind", FunctionBind,
1760 "toString", FunctionToString
1763 // ----------------------------------------------------------------------------
1764 // Iterator related spec functions.
1766 // ES6 rev 33, 2015-02-12
1767 // 7.4.1 GetIterator ( obj, method )
1768 function GetIterator(obj, method) {
1769 if (IS_UNDEFINED(method)) {
1770 method = obj[symbolIterator];
1772 if (!IS_SPEC_FUNCTION(method)) {
1773 throw MakeTypeError(kNotIterable, obj);
1775 var iterator = %_CallFunction(obj, method);
1776 if (!IS_SPEC_OBJECT(iterator)) {
1777 throw MakeTypeError(kNotAnIterator, iterator);
1782 // ----------------------------------------------------------------------------
1785 $functionSourceString = FunctionSourceString;
1787 utils.ObjectDefineProperties = ObjectDefineProperties;
1788 utils.ObjectDefineProperty = ObjectDefineProperty;
1790 utils.Export(function(to) {
1792 to.GetIterator = GetIterator;
1793 to.GetMethod = GetMethod;
1794 to.IsFinite = GlobalIsFinite;
1795 to.IsNaN = GlobalIsNaN;
1796 to.NewFunctionString = NewFunctionString;
1797 to.NumberIsNaN = NumberIsNaN;
1798 to.ObjectDefineProperty = ObjectDefineProperty;
1799 to.ObjectFreeze = ObjectFreezeJS;
1800 to.ObjectGetOwnPropertyKeys = ObjectGetOwnPropertyKeys;
1801 to.ObjectHasOwnProperty = ObjectHasOwnProperty;
1802 to.ObjectIsFrozen = ObjectIsFrozen;
1803 to.ObjectIsSealed = ObjectIsSealed;
1804 to.ObjectToString = ObjectToString;
1805 to.OwnPropertyKeys = OwnPropertyKeys;
1806 to.ToNameArray = ToNameArray;
1809 utils.ExportToRuntime(function(to) {
1810 to.GlobalEval = GlobalEval;
1811 to.ObjectDefineOwnProperty = DefineOwnPropertyFromAPI;
1812 to.ObjectGetOwnPropertyDescriptor = ObjectGetOwnPropertyDescriptor;
1813 to.ToCompletePropertyDescriptor = ToCompletePropertyDescriptor;