2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// This file relies on the fact that the following declarations have been made
|
2008-10-03 07:14:31 +00:00
|
|
|
//
|
2008-07-03 15:10:15 +00:00
|
|
|
// in runtime.js:
|
|
|
|
// const $Object = global.Object;
|
|
|
|
// const $Boolean = global.Boolean;
|
|
|
|
// const $Number = global.Number;
|
|
|
|
// const $Function = global.Function;
|
|
|
|
// const $Array = global.Array;
|
2008-08-27 13:47:52 +00:00
|
|
|
// const $NaN = 0/0;
|
2008-10-03 07:14:31 +00:00
|
|
|
//
|
|
|
|
// in math.js:
|
|
|
|
// const $floor = MathFloor
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
const $isNaN = GlobalIsNaN;
|
|
|
|
const $isFinite = GlobalIsFinite;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// Helper function used to install functions on objects.
|
|
|
|
function InstallFunctions(object, attributes, functions) {
|
2009-08-13 12:35:59 +00:00
|
|
|
if (functions.length >= 8) {
|
2009-08-18 09:47:45 +00:00
|
|
|
%OptimizeObjectForAddingMultipleProperties(object, functions.length >> 1);
|
2009-08-13 12:35:59 +00:00
|
|
|
}
|
2008-10-03 18:00:28 +00:00
|
|
|
for (var i = 0; i < functions.length; i += 2) {
|
|
|
|
var key = functions[i];
|
|
|
|
var f = functions[i + 1];
|
|
|
|
%FunctionSetName(f, key);
|
2010-04-28 12:05:40 +00:00
|
|
|
%FunctionRemovePrototype(f);
|
2008-10-03 18:00:28 +00:00
|
|
|
%SetProperty(object, key, f, attributes);
|
2011-05-30 13:49:22 +00:00
|
|
|
%SetNativeFlag(f);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2010-02-17 11:26:00 +00:00
|
|
|
%ToFastProperties(object);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-06 13:19:55 +00:00
|
|
|
// Emulates JSC by installing functions on a hidden prototype that
|
|
|
|
// lies above the current object/prototype. This lets you override
|
|
|
|
// functions on String.prototype etc. and then restore the old function
|
|
|
|
// with delete. See http://code.google.com/p/chromium/issues/detail?id=1717
|
|
|
|
function InstallFunctionsOnHiddenPrototype(object, attributes, functions) {
|
|
|
|
var hidden_prototype = new $Object();
|
|
|
|
%SetHiddenPrototype(object, hidden_prototype);
|
|
|
|
InstallFunctions(hidden_prototype, attributes, functions);
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA 262 - 15.1.4
|
2008-10-03 18:00:28 +00:00
|
|
|
function GlobalIsNaN(number) {
|
2008-08-06 10:02:49 +00:00
|
|
|
var n = ToNumber(number);
|
|
|
|
return NUMBER_IS_NAN(n);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA 262 - 15.1.5
|
2008-10-03 18:00:28 +00:00
|
|
|
function GlobalIsFinite(number) {
|
2011-01-05 13:52:00 +00:00
|
|
|
if (!IS_NUMBER(number)) number = NonNumberToNumber(number);
|
2010-04-06 10:40:25 +00:00
|
|
|
|
|
|
|
// NaN - NaN == NaN, Infinity - Infinity == NaN, -Infinity - -Infinity == NaN.
|
|
|
|
return %_IsSmi(number) || number - number == 0;
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 - 15.1.2.2
|
2008-10-03 18:00:28 +00:00
|
|
|
function GlobalParseInt(string, radix) {
|
2011-02-22 11:21:15 +00:00
|
|
|
if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Some people use parseInt instead of Math.floor. This
|
|
|
|
// optimization makes parseInt on a Smi 12 times faster (60ns
|
|
|
|
// vs 800ns). The following optimization makes parseInt on a
|
|
|
|
// non-Smi number 9 times faster (230ns vs 2070ns). Together
|
|
|
|
// they make parseInt on a string 1.4% slower (274ns vs 270ns).
|
|
|
|
if (%_IsSmi(string)) return string;
|
2009-02-16 10:18:34 +00:00
|
|
|
if (IS_NUMBER(string) &&
|
2009-11-17 13:54:05 +00:00
|
|
|
((0.01 < string && string < 1e9) ||
|
|
|
|
(-1e9 < string && string < -0.01))) {
|
2009-02-16 10:18:34 +00:00
|
|
|
// Truncate number.
|
|
|
|
return string | 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-06-28 12:31:42 +00:00
|
|
|
radix = radix | 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
radix = TO_INT32(radix);
|
|
|
|
if (!(radix == 0 || (2 <= radix && radix <= 36)))
|
|
|
|
return $NaN;
|
|
|
|
}
|
2010-08-27 11:47:12 +00:00
|
|
|
string = TO_STRING_INLINE(string);
|
|
|
|
if (%_HasCachedArrayIndex(string) &&
|
|
|
|
(radix == 0 || radix == 10)) {
|
|
|
|
return %_GetCachedArrayIndex(string);
|
|
|
|
}
|
|
|
|
return %StringParseInt(string, radix);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 - 15.1.2.3
|
2008-10-03 18:00:28 +00:00
|
|
|
function GlobalParseFloat(string) {
|
2010-08-27 11:47:12 +00:00
|
|
|
string = TO_STRING_INLINE(string);
|
|
|
|
if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string);
|
|
|
|
return %StringParseFloat(string);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
|
|
|
|
2008-11-21 15:01:41 +00:00
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function GlobalEval(x) {
|
|
|
|
if (!IS_STRING(x)) return x;
|
|
|
|
|
2011-05-30 13:49:22 +00:00
|
|
|
var receiver = this;
|
2009-06-08 09:46:09 +00:00
|
|
|
var global_receiver = %GlobalReceiver(global);
|
2011-05-30 13:49:22 +00:00
|
|
|
|
|
|
|
if (receiver == null && !IS_UNDETECTABLE(receiver)) {
|
|
|
|
receiver = global_receiver;
|
|
|
|
}
|
|
|
|
|
|
|
|
var this_is_global_receiver = (receiver === global_receiver);
|
2009-06-08 09:46:09 +00:00
|
|
|
var global_is_detached = (global === global_receiver);
|
|
|
|
|
2011-05-30 13:49:22 +00:00
|
|
|
// For consistency with JSC we require the global object passed to
|
|
|
|
// eval to be the global object from which 'eval' originated. This
|
|
|
|
// is not mandated by the spec.
|
2009-06-08 09:46:09 +00:00
|
|
|
if (!this_is_global_receiver || global_is_detached) {
|
|
|
|
throw new $EvalError('The "this" object passed to eval must ' +
|
2008-11-14 14:36:46 +00:00
|
|
|
'be the global object from which eval originated');
|
2008-11-14 13:14:49 +00:00
|
|
|
}
|
2009-06-08 09:46:09 +00:00
|
|
|
|
2010-10-27 09:19:43 +00:00
|
|
|
var f = %CompileString(x);
|
2008-10-03 18:00:28 +00:00
|
|
|
if (!IS_FUNCTION(f)) return f;
|
|
|
|
|
2011-05-30 13:49:22 +00:00
|
|
|
return %_CallFunction(receiver, f);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
function SetupGlobal() {
|
|
|
|
// ECMA 262 - 15.1.1.1.
|
2011-07-20 10:06:53 +00:00
|
|
|
%SetProperty(global, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY);
|
2008-10-03 18:00:28 +00:00
|
|
|
|
|
|
|
// ECMA-262 - 15.1.1.2.
|
2011-07-20 10:06:53 +00:00
|
|
|
%SetProperty(global, "Infinity", 1/0, DONT_ENUM | DONT_DELETE | READ_ONLY);
|
2008-10-03 18:00:28 +00:00
|
|
|
|
|
|
|
// ECMA-262 - 15.1.1.3.
|
2011-07-20 10:06:53 +00:00
|
|
|
%SetProperty(global, "undefined", void 0,
|
|
|
|
DONT_ENUM | DONT_DELETE | READ_ONLY);
|
2009-06-29 08:14:06 +00:00
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// Setup non-enumerable function on the global object.
|
|
|
|
InstallFunctions(global, DONT_ENUM, $Array(
|
|
|
|
"isNaN", GlobalIsNaN,
|
|
|
|
"isFinite", GlobalIsFinite,
|
|
|
|
"parseInt", GlobalParseInt,
|
|
|
|
"parseFloat", GlobalParseFloat,
|
2011-05-19 08:10:27 +00:00
|
|
|
"eval", GlobalEval
|
2008-10-03 18:00:28 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupGlobal();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Boolean (first part of definition)
|
|
|
|
|
|
|
|
|
|
|
|
%SetCode($Boolean, function(x) {
|
2009-06-29 08:14:06 +00:00
|
|
|
if (%_IsConstructCall()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%_SetValueOf(this, ToBoolean(x));
|
|
|
|
} else {
|
|
|
|
return ToBoolean(x);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
%FunctionSetPrototype($Boolean, new $Boolean(false));
|
|
|
|
|
2008-10-03 12:14:29 +00:00
|
|
|
%SetProperty($Boolean.prototype, "constructor", $Boolean, DONT_ENUM);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Object
|
|
|
|
|
|
|
|
$Object.prototype.constructor = $Object;
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 - 15.2.4.2
|
|
|
|
function ObjectToString() {
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
return '[object Undefined]';
|
|
|
|
}
|
|
|
|
if (IS_NULL(this)) return '[object Null]';
|
2010-01-19 14:15:47 +00:00
|
|
|
return "[object " + %_ClassOf(ToObject(this)) + "]";
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 - 15.2.4.3
|
|
|
|
function ObjectToLocaleString() {
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
throw MakeTypeError("called_on_null_or_undefined",
|
|
|
|
["Object.prototype.toLocaleString"]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return this.toString();
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 - 15.2.4.4
|
|
|
|
function ObjectValueOf() {
|
2010-01-19 14:15:47 +00:00
|
|
|
return ToObject(this);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 - 15.2.4.5
|
|
|
|
function ObjectHasOwnProperty(V) {
|
2011-05-12 16:19:03 +00:00
|
|
|
return %HasLocalProperty(TO_OBJECT_INLINE(this), TO_STRING_INLINE(V));
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 - 15.2.4.6
|
|
|
|
function ObjectIsPrototypeOf(V) {
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
throw MakeTypeError("called_on_null_or_undefined",
|
|
|
|
["Object.prototype.isPrototypeOf"]);
|
|
|
|
}
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(V)) return false;
|
2008-07-03 15:10:15 +00:00
|
|
|
return %IsInPrototypeChain(this, V);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 - 15.2.4.6
|
|
|
|
function ObjectPropertyIsEnumerable(V) {
|
2010-07-16 11:21:08 +00:00
|
|
|
return %IsPropertyEnumerable(ToObject(this), ToString(V));
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Extensions for providing property getters and setters.
|
2008-10-03 18:00:28 +00:00
|
|
|
function ObjectDefineGetter(name, fun) {
|
2011-05-30 13:49:22 +00:00
|
|
|
var receiver = this;
|
|
|
|
if (receiver == null && !IS_UNDETECTABLE(receiver)) {
|
|
|
|
receiver = %GlobalReceiver(global);
|
2008-10-03 12:14:29 +00:00
|
|
|
}
|
|
|
|
if (!IS_FUNCTION(fun)) {
|
2008-10-03 13:26:00 +00:00
|
|
|
throw new $TypeError('Object.prototype.__defineGetter__: Expecting function');
|
2008-10-03 12:14:29 +00:00
|
|
|
}
|
2011-03-15 12:25:13 +00:00
|
|
|
var desc = new PropertyDescriptor();
|
|
|
|
desc.setGet(fun);
|
|
|
|
desc.setEnumerable(true);
|
|
|
|
desc.setConfigurable(true);
|
2011-05-30 13:49:22 +00:00
|
|
|
DefineOwnProperty(ToObject(receiver), ToString(name), desc, false);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function ObjectLookupGetter(name) {
|
2011-05-30 13:49:22 +00:00
|
|
|
var receiver = this;
|
|
|
|
if (receiver == null && !IS_UNDETECTABLE(receiver)) {
|
|
|
|
receiver = %GlobalReceiver(global);
|
2008-10-03 12:14:29 +00:00
|
|
|
}
|
2011-05-30 13:49:22 +00:00
|
|
|
return %LookupAccessor(ToObject(receiver), ToString(name), GETTER);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function ObjectDefineSetter(name, fun) {
|
2011-05-30 13:49:22 +00:00
|
|
|
var receiver = this;
|
|
|
|
if (receiver == null && !IS_UNDETECTABLE(receiver)) {
|
|
|
|
receiver = %GlobalReceiver(global);
|
2008-10-03 12:14:29 +00:00
|
|
|
}
|
|
|
|
if (!IS_FUNCTION(fun)) {
|
|
|
|
throw new $TypeError(
|
|
|
|
'Object.prototype.__defineSetter__: Expecting function');
|
|
|
|
}
|
2011-03-15 12:25:13 +00:00
|
|
|
var desc = new PropertyDescriptor();
|
|
|
|
desc.setSet(fun);
|
|
|
|
desc.setEnumerable(true);
|
|
|
|
desc.setConfigurable(true);
|
2011-05-30 13:49:22 +00:00
|
|
|
DefineOwnProperty(ToObject(receiver), ToString(name), desc, false);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function ObjectLookupSetter(name) {
|
2011-05-30 13:49:22 +00:00
|
|
|
var receiver = this;
|
|
|
|
if (receiver == null && !IS_UNDETECTABLE(receiver)) {
|
|
|
|
receiver = %GlobalReceiver(global);
|
2008-10-03 12:14:29 +00:00
|
|
|
}
|
2011-05-30 13:49:22 +00:00
|
|
|
return %LookupAccessor(ToObject(receiver), ToString(name), SETTER);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-09-15 11:51:40 +00:00
|
|
|
function ObjectKeys(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj))
|
2010-01-15 15:34:32 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["keys"]);
|
2011-07-13 11:01:17 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var keys = handler.keys;
|
|
|
|
if (IS_UNDEFINED(keys)) keys = DerivedKeysTrap;
|
|
|
|
var names = %_CallFunction(handler, keys);
|
|
|
|
return ToStringArray(names);
|
|
|
|
}
|
2009-09-15 11:51:40 +00:00
|
|
|
return %LocalKeys(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
// ES5 8.10.1.
|
|
|
|
function IsAccessorDescriptor(desc) {
|
|
|
|
if (IS_UNDEFINED(desc)) return false;
|
2011-07-07 12:41:20 +00:00
|
|
|
return desc.hasGetter() || desc.hasSetter();
|
2009-12-09 13:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ES5 8.10.2.
|
|
|
|
function IsDataDescriptor(desc) {
|
|
|
|
if (IS_UNDEFINED(desc)) return false;
|
2011-07-07 12:41:20 +00:00
|
|
|
return desc.hasValue() || desc.hasWritable();
|
2009-12-09 13:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ES5 8.10.3.
|
|
|
|
function IsGenericDescriptor(desc) {
|
|
|
|
return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function IsInconsistentDescriptor(desc) {
|
|
|
|
return IsAccessorDescriptor(desc) && IsDataDescriptor(desc);
|
|
|
|
}
|
|
|
|
|
2011-06-03 10:15:49 +00:00
|
|
|
|
2010-01-13 12:10:57 +00:00
|
|
|
// ES5 8.10.4
|
|
|
|
function FromPropertyDescriptor(desc) {
|
2010-02-03 13:10:03 +00:00
|
|
|
if (IS_UNDEFINED(desc)) return desc;
|
2010-01-13 12:10:57 +00:00
|
|
|
var obj = new $Object();
|
|
|
|
if (IsDataDescriptor(desc)) {
|
|
|
|
obj.value = desc.getValue();
|
|
|
|
obj.writable = desc.isWritable();
|
|
|
|
}
|
|
|
|
if (IsAccessorDescriptor(desc)) {
|
|
|
|
obj.get = desc.getGet();
|
|
|
|
obj.set = desc.getSet();
|
|
|
|
}
|
|
|
|
obj.enumerable = desc.isEnumerable();
|
|
|
|
obj.configurable = desc.isConfigurable();
|
|
|
|
return obj;
|
|
|
|
}
|
2009-12-09 13:56:58 +00:00
|
|
|
|
2011-07-07 12:41:20 +00:00
|
|
|
// Harmony Proxies
|
|
|
|
function FromGenericPropertyDescriptor(desc) {
|
|
|
|
if (IS_UNDEFINED(desc)) return desc;
|
|
|
|
var obj = new $Object();
|
|
|
|
if (desc.hasValue()) obj.value = desc.getValue();
|
|
|
|
if (desc.hasWritable()) obj.writable = desc.isWritable();
|
|
|
|
if (desc.hasGetter()) obj.get = desc.getGet();
|
|
|
|
if (desc.hasSetter()) obj.set = desc.getSet();
|
|
|
|
if (desc.hasEnumerable()) obj.enumerable = desc.isEnumerable();
|
|
|
|
if (desc.hasConfigurable()) obj.configurable = desc.isConfigurable();
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
// ES5 8.10.5.
|
|
|
|
function ToPropertyDescriptor(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2009-12-09 13:56:58 +00:00
|
|
|
throw MakeTypeError("property_desc_object", [obj]);
|
|
|
|
}
|
|
|
|
var desc = new PropertyDescriptor();
|
|
|
|
|
|
|
|
if ("enumerable" in obj) {
|
|
|
|
desc.setEnumerable(ToBoolean(obj.enumerable));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("configurable" in obj) {
|
|
|
|
desc.setConfigurable(ToBoolean(obj.configurable));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("value" in obj) {
|
|
|
|
desc.setValue(obj.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("writable" in obj) {
|
|
|
|
desc.setWritable(ToBoolean(obj.writable));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("get" in obj) {
|
|
|
|
var get = obj.get;
|
|
|
|
if (!IS_UNDEFINED(get) && !IS_FUNCTION(get)) {
|
|
|
|
throw MakeTypeError("getter_must_be_callable", [get]);
|
|
|
|
}
|
|
|
|
desc.setGet(get);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("set" in obj) {
|
|
|
|
var set = obj.set;
|
|
|
|
if (!IS_UNDEFINED(set) && !IS_FUNCTION(set)) {
|
|
|
|
throw MakeTypeError("setter_must_be_callable", [set]);
|
|
|
|
}
|
|
|
|
desc.setSet(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsInconsistentDescriptor(desc)) {
|
|
|
|
throw MakeTypeError("value_and_accessor", [obj]);
|
|
|
|
}
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-03 10:15:49 +00:00
|
|
|
// For Harmony proxies.
|
|
|
|
function ToCompletePropertyDescriptor(obj) {
|
|
|
|
var desc = ToPropertyDescriptor(obj)
|
|
|
|
if (IsGenericDescriptor(desc) || IsDataDescriptor(desc)) {
|
2011-07-07 12:41:20 +00:00
|
|
|
if (!desc.hasValue()) desc.setValue(void 0);
|
|
|
|
if (!desc.hasWritable()) desc.setWritable(false);
|
2011-06-03 10:15:49 +00:00
|
|
|
} else {
|
|
|
|
// Is accessor descriptor.
|
2011-07-07 12:41:20 +00:00
|
|
|
if (!desc.hasGetter()) desc.setGet(void 0);
|
|
|
|
if (!desc.hasSetter()) desc.setSet(void 0);
|
2011-06-03 10:15:49 +00:00
|
|
|
}
|
2011-07-07 12:41:20 +00:00
|
|
|
if (!desc.hasEnumerable()) desc.setEnumerable(false);
|
|
|
|
if (!desc.hasConfigurable()) desc.setConfigurable(false);
|
2011-06-03 10:15:49 +00:00
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
function PropertyDescriptor() {
|
|
|
|
// Initialize here so they are all in-object and have the same map.
|
|
|
|
// Default values from ES5 8.6.1.
|
|
|
|
this.value_ = void 0;
|
|
|
|
this.hasValue_ = false;
|
|
|
|
this.writable_ = false;
|
|
|
|
this.hasWritable_ = false;
|
|
|
|
this.enumerable_ = false;
|
2010-02-03 13:10:03 +00:00
|
|
|
this.hasEnumerable_ = false;
|
2009-12-09 13:56:58 +00:00
|
|
|
this.configurable_ = false;
|
2010-02-03 13:10:03 +00:00
|
|
|
this.hasConfigurable_ = false;
|
2009-12-09 13:56:58 +00:00
|
|
|
this.get_ = void 0;
|
|
|
|
this.hasGetter_ = false;
|
|
|
|
this.set_ = void 0;
|
|
|
|
this.hasSetter_ = false;
|
|
|
|
}
|
|
|
|
|
2011-03-15 12:25:13 +00:00
|
|
|
PropertyDescriptor.prototype.__proto__ = null;
|
|
|
|
PropertyDescriptor.prototype.toString = function() {
|
|
|
|
return "[object PropertyDescriptor]";
|
|
|
|
};
|
2009-12-09 13:56:58 +00:00
|
|
|
|
|
|
|
PropertyDescriptor.prototype.setValue = function(value) {
|
|
|
|
this.value_ = value;
|
|
|
|
this.hasValue_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.getValue = function() {
|
|
|
|
return this.value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:10:03 +00:00
|
|
|
PropertyDescriptor.prototype.hasValue = function() {
|
|
|
|
return this.hasValue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
PropertyDescriptor.prototype.setEnumerable = function(enumerable) {
|
|
|
|
this.enumerable_ = enumerable;
|
2010-02-03 13:10:03 +00:00
|
|
|
this.hasEnumerable_ = true;
|
2009-12-09 13:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.isEnumerable = function () {
|
|
|
|
return this.enumerable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:10:03 +00:00
|
|
|
PropertyDescriptor.prototype.hasEnumerable = function() {
|
|
|
|
return this.hasEnumerable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
PropertyDescriptor.prototype.setWritable = function(writable) {
|
|
|
|
this.writable_ = writable;
|
|
|
|
this.hasWritable_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.isWritable = function() {
|
|
|
|
return this.writable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-25 06:25:27 +00:00
|
|
|
PropertyDescriptor.prototype.hasWritable = function() {
|
|
|
|
return this.hasWritable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
PropertyDescriptor.prototype.setConfigurable = function(configurable) {
|
|
|
|
this.configurable_ = configurable;
|
2010-02-03 13:10:03 +00:00
|
|
|
this.hasConfigurable_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.hasConfigurable = function() {
|
|
|
|
return this.hasConfigurable_;
|
2009-12-09 13:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.isConfigurable = function() {
|
|
|
|
return this.configurable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.setGet = function(get) {
|
|
|
|
this.get_ = get;
|
|
|
|
this.hasGetter_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.getGet = function() {
|
|
|
|
return this.get_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:10:03 +00:00
|
|
|
PropertyDescriptor.prototype.hasGetter = function() {
|
|
|
|
return this.hasGetter_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
PropertyDescriptor.prototype.setSet = function(set) {
|
|
|
|
this.set_ = set;
|
|
|
|
this.hasSetter_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PropertyDescriptor.prototype.getSet = function() {
|
|
|
|
return this.set_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:10:03 +00:00
|
|
|
PropertyDescriptor.prototype.hasSetter = function() {
|
|
|
|
return this.hasSetter_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-04 12:14:56 +00:00
|
|
|
// Converts an array returned from Runtime_GetOwnProperty to an actual
|
|
|
|
// property descriptor. For a description of the array layout please
|
|
|
|
// see the runtime.cc file.
|
|
|
|
function ConvertDescriptorArrayToDescriptor(desc_array) {
|
2011-03-11 13:57:20 +00:00
|
|
|
if (desc_array === false) {
|
2011-02-04 12:14:56 +00:00
|
|
|
throw 'Internal error: invalid desc_array';
|
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
|
2011-02-04 12:14:56 +00:00
|
|
|
if (IS_UNDEFINED(desc_array)) {
|
|
|
|
return void 0;
|
|
|
|
}
|
2010-01-13 12:10:57 +00:00
|
|
|
|
2011-02-04 12:14:56 +00:00
|
|
|
var desc = new PropertyDescriptor();
|
|
|
|
// This is an accessor.
|
|
|
|
if (desc_array[IS_ACCESSOR_INDEX]) {
|
|
|
|
desc.setGet(desc_array[GETTER_INDEX]);
|
|
|
|
desc.setSet(desc_array[SETTER_INDEX]);
|
2010-01-13 12:10:57 +00:00
|
|
|
} else {
|
2011-02-04 12:14:56 +00:00
|
|
|
desc.setValue(desc_array[VALUE_INDEX]);
|
|
|
|
desc.setWritable(desc_array[WRITABLE_INDEX]);
|
2010-01-13 12:10:57 +00:00
|
|
|
}
|
2011-02-04 12:14:56 +00:00
|
|
|
desc.setEnumerable(desc_array[ENUMERABLE_INDEX]);
|
|
|
|
desc.setConfigurable(desc_array[CONFIGURABLE_INDEX]);
|
2010-01-13 12:10:57 +00:00
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:10:03 +00:00
|
|
|
// ES5 section 8.12.2.
|
|
|
|
function GetProperty(obj, p) {
|
2011-06-03 10:15:49 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var getProperty = handler.getPropertyDescriptor;
|
|
|
|
if (IS_UNDEFINED(getProperty)) {
|
|
|
|
throw MakeTypeError("handler_trap_missing",
|
|
|
|
[handler, "getPropertyDescriptor"]);
|
|
|
|
}
|
2011-07-13 11:01:17 +00:00
|
|
|
var descriptor = %_CallFunction(handler, p, getProperty);
|
2011-06-03 10:15:49 +00:00
|
|
|
if (IS_UNDEFINED(descriptor)) return descriptor;
|
|
|
|
var desc = ToCompletePropertyDescriptor(descriptor);
|
2011-07-13 11:57:15 +00:00
|
|
|
if (!desc.isConfigurable()) {
|
2011-06-03 10:15:49 +00:00
|
|
|
throw MakeTypeError("proxy_prop_not_configurable",
|
|
|
|
[handler, "getPropertyDescriptor", p, descriptor]);
|
|
|
|
}
|
|
|
|
return desc;
|
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
var prop = GetOwnProperty(obj);
|
|
|
|
if (!IS_UNDEFINED(prop)) return prop;
|
2011-06-03 10:15:49 +00:00
|
|
|
var proto = %GetPrototype(obj);
|
2010-02-03 13:10:03 +00:00
|
|
|
if (IS_NULL(proto)) return void 0;
|
|
|
|
return GetProperty(proto, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ES5 section 8.12.6
|
|
|
|
function HasProperty(obj, p) {
|
2011-06-03 10:15:49 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
2011-06-09 09:05:15 +00:00
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var has = handler.has;
|
|
|
|
if (IS_UNDEFINED(has)) has = DerivedHasTrap;
|
2011-07-13 11:01:17 +00:00
|
|
|
return ToBoolean(%_CallFunction(handler, obj, p, has));
|
2011-06-03 10:15:49 +00:00
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
var desc = GetProperty(obj, p);
|
|
|
|
return IS_UNDEFINED(desc) ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-04 12:14:56 +00:00
|
|
|
// ES5 section 8.12.1.
|
|
|
|
function GetOwnProperty(obj, p) {
|
2011-07-13 11:57:15 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var getOwnProperty = handler.getOwnPropertyDescriptor;
|
|
|
|
if (IS_UNDEFINED(getOwnProperty)) {
|
|
|
|
throw MakeTypeError("handler_trap_missing",
|
|
|
|
[handler, "getOwnPropertyDescriptor"]);
|
|
|
|
}
|
|
|
|
var descriptor = %_CallFunction(handler, p, getOwnProperty);
|
|
|
|
if (IS_UNDEFINED(descriptor)) return descriptor;
|
|
|
|
var desc = ToCompletePropertyDescriptor(descriptor);
|
|
|
|
if (!desc.isConfigurable()) {
|
|
|
|
throw MakeTypeError("proxy_prop_not_configurable",
|
|
|
|
[handler, "getOwnPropertyDescriptor", p, descriptor]);
|
|
|
|
}
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:14:56 +00:00
|
|
|
// GetOwnProperty returns an array indexed by the constants
|
|
|
|
// defined in macros.py.
|
|
|
|
// If p is not a property on obj undefined is returned.
|
|
|
|
var props = %GetOwnProperty(ToObject(obj), ToString(p));
|
|
|
|
|
|
|
|
// A false value here means that access checks failed.
|
2011-03-11 13:57:20 +00:00
|
|
|
if (props === false) return void 0;
|
2011-02-04 12:14:56 +00:00
|
|
|
|
|
|
|
return ConvertDescriptorArrayToDescriptor(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-07 12:41:20 +00:00
|
|
|
// Harmony proxies.
|
|
|
|
function DefineProxyProperty(obj, p, attributes, should_throw) {
|
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var defineProperty = handler.defineProperty;
|
|
|
|
if (IS_UNDEFINED(defineProperty)) {
|
|
|
|
throw MakeTypeError("handler_trap_missing", [handler, "defineProperty"]);
|
|
|
|
}
|
2011-07-13 11:01:17 +00:00
|
|
|
var result = %_CallFunction(handler, p, attributes, defineProperty);
|
2011-07-07 12:41:20 +00:00
|
|
|
if (!ToBoolean(result)) {
|
|
|
|
if (should_throw) {
|
2011-07-18 13:04:52 +00:00
|
|
|
throw MakeTypeError("handler_returned_false",
|
|
|
|
[handler, "defineProperty"]);
|
2011-07-07 12:41:20 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 07:15:23 +00:00
|
|
|
// ES5 8.12.9.
|
2009-12-09 13:56:58 +00:00
|
|
|
function DefineOwnProperty(obj, p, desc, should_throw) {
|
2011-07-07 12:41:20 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
var attributes = FromGenericPropertyDescriptor(desc);
|
|
|
|
return DefineProxyProperty(obj, p, attributes, should_throw);
|
|
|
|
}
|
|
|
|
|
2011-02-04 12:14:56 +00:00
|
|
|
var current_or_access = %GetOwnProperty(ToObject(obj), ToString(p));
|
|
|
|
// A false value here means that access checks failed.
|
2011-03-11 13:57:20 +00:00
|
|
|
if (current_or_access === false) return void 0;
|
2011-02-04 12:14:56 +00:00
|
|
|
|
|
|
|
var current = ConvertDescriptorArrayToDescriptor(current_or_access);
|
2010-02-03 13:10:03 +00:00
|
|
|
var extensible = %IsExtensible(ToObject(obj));
|
|
|
|
|
|
|
|
// Error handling according to spec.
|
|
|
|
// Step 3
|
2011-03-15 14:19:18 +00:00
|
|
|
if (IS_UNDEFINED(current) && !extensible) {
|
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("define_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
|
2011-02-03 19:29:10 +00:00
|
|
|
if (!IS_UNDEFINED(current)) {
|
2010-07-23 10:08:55 +00:00
|
|
|
// Step 5 and 6
|
2011-02-03 19:29:10 +00:00
|
|
|
if ((IsGenericDescriptor(desc) ||
|
|
|
|
IsDataDescriptor(desc) == IsDataDescriptor(current)) &&
|
|
|
|
(!desc.hasEnumerable() ||
|
|
|
|
SameValue(desc.isEnumerable(), current.isEnumerable())) &&
|
2010-10-15 13:03:59 +00:00
|
|
|
(!desc.hasConfigurable() ||
|
2010-07-23 10:08:55 +00:00
|
|
|
SameValue(desc.isConfigurable(), current.isConfigurable())) &&
|
2010-10-15 13:03:59 +00:00
|
|
|
(!desc.hasWritable() ||
|
2010-07-23 10:08:55 +00:00
|
|
|
SameValue(desc.isWritable(), current.isWritable())) &&
|
|
|
|
(!desc.hasValue() ||
|
|
|
|
SameValue(desc.getValue(), current.getValue())) &&
|
|
|
|
(!desc.hasGetter() ||
|
|
|
|
SameValue(desc.getGet(), current.getGet())) &&
|
|
|
|
(!desc.hasSetter() ||
|
|
|
|
SameValue(desc.getSet(), current.getSet()))) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
if (!current.isConfigurable()) {
|
|
|
|
// Step 7
|
|
|
|
if (desc.isConfigurable() ||
|
|
|
|
(desc.hasEnumerable() &&
|
2011-02-14 10:43:21 +00:00
|
|
|
desc.isEnumerable() != current.isEnumerable())) {
|
2011-03-15 14:19:18 +00:00
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("redefine_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 10:43:21 +00:00
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
// Step 8
|
|
|
|
if (!IsGenericDescriptor(desc)) {
|
|
|
|
// Step 9a
|
2011-02-14 10:43:21 +00:00
|
|
|
if (IsDataDescriptor(current) != IsDataDescriptor(desc)) {
|
2011-03-15 14:19:18 +00:00
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("redefine_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 10:43:21 +00:00
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
// Step 10a
|
|
|
|
if (IsDataDescriptor(current) && IsDataDescriptor(desc)) {
|
2011-02-14 10:43:21 +00:00
|
|
|
if (!current.isWritable() && desc.isWritable()) {
|
2011-03-15 14:19:18 +00:00
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("redefine_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 10:43:21 +00:00
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
if (!current.isWritable() && desc.hasValue() &&
|
|
|
|
!SameValue(desc.getValue(), current.getValue())) {
|
2011-03-15 14:19:18 +00:00
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("redefine_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Step 11
|
|
|
|
if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) {
|
2011-02-14 10:43:21 +00:00
|
|
|
if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())) {
|
2011-03-15 14:19:18 +00:00
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("redefine_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
}
|
2011-02-14 10:43:21 +00:00
|
|
|
if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet())) {
|
2011-03-15 14:19:18 +00:00
|
|
|
if (should_throw) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("redefine_disallowed", [p]);
|
2011-03-15 14:19:18 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 10:43:21 +00:00
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-30 07:15:23 +00:00
|
|
|
// Send flags - enumerable and configurable are common - writable is
|
2010-02-03 13:10:03 +00:00
|
|
|
// only send to the data descriptor.
|
|
|
|
// Take special care if enumerable and configurable is not defined on
|
|
|
|
// desc (we need to preserve the existing values from current).
|
|
|
|
var flag = NONE;
|
|
|
|
if (desc.hasEnumerable()) {
|
|
|
|
flag |= desc.isEnumerable() ? 0 : DONT_ENUM;
|
|
|
|
} else if (!IS_UNDEFINED(current)) {
|
|
|
|
flag |= current.isEnumerable() ? 0 : DONT_ENUM;
|
2009-12-09 13:56:58 +00:00
|
|
|
} else {
|
2010-02-03 13:10:03 +00:00
|
|
|
flag |= DONT_ENUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.hasConfigurable()) {
|
|
|
|
flag |= desc.isConfigurable() ? 0 : DONT_DELETE;
|
|
|
|
} else if (!IS_UNDEFINED(current)) {
|
|
|
|
flag |= current.isConfigurable() ? 0 : DONT_DELETE;
|
|
|
|
} else
|
|
|
|
flag |= DONT_DELETE;
|
|
|
|
|
2011-02-03 19:29:10 +00:00
|
|
|
if (IsDataDescriptor(desc) ||
|
|
|
|
(IsGenericDescriptor(desc) &&
|
|
|
|
(IS_UNDEFINED(current) || IsDataDescriptor(current)))) {
|
|
|
|
// There are 3 cases that lead here:
|
|
|
|
// Step 4a - defining a new data property.
|
|
|
|
// Steps 9b & 12 - replacing an existing accessor property with a data
|
|
|
|
// property.
|
|
|
|
// Step 12 - updating an existing data property with a data or generic
|
|
|
|
// descriptor.
|
|
|
|
|
2010-05-26 08:31:57 +00:00
|
|
|
if (desc.hasWritable()) {
|
|
|
|
flag |= desc.isWritable() ? 0 : READ_ONLY;
|
|
|
|
} else if (!IS_UNDEFINED(current)) {
|
|
|
|
flag |= current.isWritable() ? 0 : READ_ONLY;
|
|
|
|
} else {
|
|
|
|
flag |= READ_ONLY;
|
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
|
2010-12-10 13:07:52 +00:00
|
|
|
var value = void 0; // Default value is undefined.
|
2010-12-10 11:27:15 +00:00
|
|
|
if (desc.hasValue()) {
|
|
|
|
value = desc.getValue();
|
2011-02-03 19:29:10 +00:00
|
|
|
} else if (!IS_UNDEFINED(current) && IsDataDescriptor(current)) {
|
2010-12-10 11:27:15 +00:00
|
|
|
value = current.getValue();
|
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
|
2010-12-10 11:27:15 +00:00
|
|
|
%DefineOrRedefineDataProperty(obj, p, value, flag);
|
2011-02-03 19:29:10 +00:00
|
|
|
} else if (IsGenericDescriptor(desc)) {
|
|
|
|
// Step 12 - updating an existing accessor property with generic
|
|
|
|
// descriptor. Changing flags only.
|
|
|
|
%DefineOrRedefineAccessorProperty(obj, p, GETTER, current.getGet(), flag);
|
2010-02-03 13:10:03 +00:00
|
|
|
} else {
|
2011-02-03 19:29:10 +00:00
|
|
|
// There are 3 cases that lead here:
|
|
|
|
// Step 4b - defining a new accessor property.
|
|
|
|
// Steps 9c & 12 - replacing an existing data property with an accessor
|
|
|
|
// property.
|
|
|
|
// Step 12 - updating an existing accessor property with an accessor
|
|
|
|
// descriptor.
|
|
|
|
if (desc.hasGetter()) {
|
|
|
|
%DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag);
|
2010-02-03 13:10:03 +00:00
|
|
|
}
|
2011-02-03 19:29:10 +00:00
|
|
|
if (desc.hasSetter()) {
|
2010-02-03 13:10:03 +00:00
|
|
|
%DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag);
|
|
|
|
}
|
2009-12-09 13:56:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-07 10:01:24 +00:00
|
|
|
// ES5 section 15.2.3.2.
|
|
|
|
function ObjectGetPrototypeOf(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj))
|
2010-01-15 15:34:32 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["getPrototypeOf"]);
|
2011-06-03 10:15:49 +00:00
|
|
|
return %GetPrototype(obj);
|
2010-01-07 10:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 07:15:23 +00:00
|
|
|
// ES5 section 15.2.3.3
|
2010-01-13 12:10:57 +00:00
|
|
|
function ObjectGetOwnPropertyDescriptor(obj, p) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj))
|
2011-07-13 11:57:15 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object",
|
|
|
|
["getOwnPropertyDescriptor"]);
|
2010-01-13 12:10:57 +00:00
|
|
|
var desc = GetOwnProperty(obj, p);
|
|
|
|
return FromPropertyDescriptor(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-03 10:15:49 +00:00
|
|
|
// For Harmony proxies
|
|
|
|
function ToStringArray(obj, trap) {
|
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
|
|
|
throw MakeTypeError("proxy_non_object_prop_names", [obj, trap]);
|
|
|
|
}
|
|
|
|
var n = ToUint32(obj.length);
|
|
|
|
var array = new $Array(n);
|
|
|
|
var names = {}
|
|
|
|
for (var index = 0; index < n; index++) {
|
|
|
|
var s = ToString(obj[index]);
|
|
|
|
if (s in names) {
|
|
|
|
throw MakeTypeError("proxy_repeated_prop_name", [obj, trap, s])
|
|
|
|
}
|
|
|
|
array[index] = s;
|
|
|
|
names.s = 0;
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-15 15:34:32 +00:00
|
|
|
// ES5 section 15.2.3.4.
|
|
|
|
function ObjectGetOwnPropertyNames(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj))
|
2010-01-15 15:34:32 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["getOwnPropertyNames"]);
|
|
|
|
|
2011-06-03 10:15:49 +00:00
|
|
|
// Special handling for proxies.
|
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var getOwnPropertyNames = handler.getOwnPropertyNames;
|
|
|
|
if (IS_UNDEFINED(getOwnPropertyNames)) {
|
|
|
|
throw MakeTypeError("handler_trap_missing",
|
|
|
|
[handler, "getOwnPropertyNames"]);
|
|
|
|
}
|
2011-07-13 11:01:17 +00:00
|
|
|
var names = %_CallFunction(handler, getOwnPropertyNames);
|
2011-06-03 10:15:49 +00:00
|
|
|
return ToStringArray(names, "getOwnPropertyNames");
|
|
|
|
}
|
|
|
|
|
2010-01-15 15:34:32 +00:00
|
|
|
// Find all the indexed properties.
|
|
|
|
|
|
|
|
// Get the local element names.
|
|
|
|
var propertyNames = %GetLocalElementNames(obj);
|
|
|
|
|
|
|
|
// Get names for indexed interceptor properties.
|
|
|
|
if (%GetInterceptorInfo(obj) & 1) {
|
|
|
|
var indexedInterceptorNames =
|
|
|
|
%GetIndexedInterceptorElementNames(obj);
|
2010-02-16 10:08:39 +00:00
|
|
|
if (indexedInterceptorNames)
|
2010-01-15 15:34:32 +00:00
|
|
|
propertyNames = propertyNames.concat(indexedInterceptorNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all the named properties.
|
|
|
|
|
|
|
|
// Get the local property names.
|
|
|
|
propertyNames = propertyNames.concat(%GetLocalPropertyNames(obj));
|
|
|
|
|
|
|
|
// Get names for named interceptor properties if any.
|
|
|
|
|
|
|
|
if (%GetInterceptorInfo(obj) & 2) {
|
|
|
|
var namedInterceptorNames =
|
|
|
|
%GetNamedInterceptorPropertyNames(obj);
|
|
|
|
if (namedInterceptorNames) {
|
|
|
|
propertyNames = propertyNames.concat(namedInterceptorNames);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 08:31:31 +00:00
|
|
|
// Property names are expected to be unique strings.
|
|
|
|
var propertySet = {};
|
|
|
|
var j = 0;
|
|
|
|
for (var i = 0; i < propertyNames.length; ++i) {
|
|
|
|
var name = ToString(propertyNames[i]);
|
2010-06-28 09:25:09 +00:00
|
|
|
// We need to check for the exact property value since for intrinsic
|
|
|
|
// properties like toString if(propertySet["toString"]) will always
|
|
|
|
// succeed.
|
|
|
|
if (propertySet[name] === true)
|
2010-06-28 08:31:31 +00:00
|
|
|
continue;
|
|
|
|
propertySet[name] = true;
|
|
|
|
propertyNames[j++] = name;
|
|
|
|
}
|
|
|
|
propertyNames.length = j;
|
2010-02-16 10:08:39 +00:00
|
|
|
|
2010-01-15 15:34:32 +00:00
|
|
|
return propertyNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-09 13:56:58 +00:00
|
|
|
// ES5 section 15.2.3.5.
|
|
|
|
function ObjectCreate(proto, properties) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(proto) && proto !== null) {
|
2009-12-09 13:56:58 +00:00
|
|
|
throw MakeTypeError("proto_object_or_null", [proto]);
|
|
|
|
}
|
|
|
|
var obj = new $Object();
|
|
|
|
obj.__proto__ = proto;
|
|
|
|
if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 13:10:03 +00:00
|
|
|
// ES5 section 15.2.3.6.
|
|
|
|
function ObjectDefineProperty(obj, p, attributes) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2010-02-03 13:10:03 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["defineProperty"]);
|
2010-05-25 06:25:27 +00:00
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
var name = ToString(p);
|
2011-07-07 12:41:20 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
// Clone the attributes object for protection.
|
|
|
|
// TODO(rossberg): not spec'ed yet, so not sure if this should involve
|
|
|
|
// non-own properties as it does (or non-enumerable ones, as it doesn't?).
|
|
|
|
var attributesClone = {}
|
|
|
|
for (var a in attributes) {
|
|
|
|
attributesClone[a] = attributes[a];
|
|
|
|
}
|
|
|
|
DefineProxyProperty(obj, name, attributesClone, true);
|
|
|
|
// The following would implement the spec as in the current proposal,
|
|
|
|
// but after recent comments on es-discuss, is most likely obsolete.
|
|
|
|
/*
|
|
|
|
var defineObj = FromGenericPropertyDescriptor(desc);
|
|
|
|
var names = ObjectGetOwnPropertyNames(attributes);
|
|
|
|
var standardNames =
|
|
|
|
{value: 0, writable: 0, get: 0, set: 0, enumerable: 0, configurable: 0};
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var N = names[i];
|
|
|
|
if (!(%HasLocalProperty(standardNames, N))) {
|
|
|
|
var attr = GetOwnProperty(attributes, N);
|
|
|
|
DefineOwnProperty(descObj, N, attr, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This is really confusing the types, but it is what the proxies spec
|
|
|
|
// currently requires:
|
|
|
|
desc = descObj;
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
var desc = ToPropertyDescriptor(attributes);
|
|
|
|
DefineOwnProperty(obj, name, desc, true);
|
|
|
|
}
|
2010-02-03 13:10:03 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ES5 section 15.2.3.7.
|
2009-12-09 13:56:58 +00:00
|
|
|
function ObjectDefineProperties(obj, properties) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj))
|
2010-02-03 13:10:03 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["defineProperties"]);
|
2009-12-09 13:56:58 +00:00
|
|
|
var props = ToObject(properties);
|
|
|
|
var key_values = [];
|
|
|
|
for (var key in props) {
|
|
|
|
if (%HasLocalProperty(props, key)) {
|
|
|
|
key_values.push(key);
|
|
|
|
var value = props[key];
|
|
|
|
var desc = ToPropertyDescriptor(value);
|
|
|
|
key_values.push(desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i = 0; i < key_values.length; i += 2) {
|
|
|
|
var key = key_values[i];
|
|
|
|
var desc = key_values[i + 1];
|
|
|
|
DefineOwnProperty(obj, key, desc, true);
|
|
|
|
}
|
2010-02-04 11:28:03 +00:00
|
|
|
return obj;
|
2009-12-09 13:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-18 13:04:52 +00:00
|
|
|
// Harmony proxies.
|
|
|
|
function ProxyFix(obj) {
|
|
|
|
var handler = %GetHandler(obj);
|
|
|
|
var fix = handler.fix;
|
|
|
|
if (IS_UNDEFINED(fix)) {
|
|
|
|
throw MakeTypeError("handler_trap_missing", [handler, "fix"]);
|
|
|
|
}
|
|
|
|
var props = %_CallFunction(handler, fix);
|
|
|
|
if (IS_UNDEFINED(props)) {
|
|
|
|
throw MakeTypeError("handler_returned_undefined", [handler, "fix"]);
|
|
|
|
}
|
|
|
|
%Fix(obj);
|
|
|
|
ObjectDefineProperties(obj, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-15 07:51:14 +00:00
|
|
|
// ES5 section 15.2.3.8.
|
|
|
|
function ObjectSeal(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2010-07-15 07:51:14 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["seal"]);
|
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
ProxyFix(obj);
|
|
|
|
}
|
2010-07-15 07:51:14 +00:00
|
|
|
var names = ObjectGetOwnPropertyNames(obj);
|
2010-08-26 08:35:49 +00:00
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
2010-07-15 07:51:14 +00:00
|
|
|
var desc = GetOwnProperty(obj, name);
|
2011-06-10 09:45:02 +00:00
|
|
|
if (desc.isConfigurable()) {
|
|
|
|
desc.setConfigurable(false);
|
|
|
|
DefineOwnProperty(obj, name, desc, true);
|
|
|
|
}
|
2010-10-15 13:03:59 +00:00
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
%PreventExtensions(obj);
|
|
|
|
return obj;
|
2010-07-15 07:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 12:58:02 +00:00
|
|
|
// ES5 section 15.2.3.9.
|
|
|
|
function ObjectFreeze(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2010-07-13 12:58:02 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["freeze"]);
|
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
ProxyFix(obj);
|
|
|
|
}
|
2010-07-13 12:58:02 +00:00
|
|
|
var names = ObjectGetOwnPropertyNames(obj);
|
2010-08-26 08:35:49 +00:00
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
2010-07-13 12:58:02 +00:00
|
|
|
var desc = GetOwnProperty(obj, name);
|
2011-06-10 09:45:02 +00:00
|
|
|
if (desc.isWritable() || desc.isConfigurable()) {
|
|
|
|
if (IsDataDescriptor(desc)) desc.setWritable(false);
|
|
|
|
desc.setConfigurable(false);
|
|
|
|
DefineOwnProperty(obj, name, desc, true);
|
|
|
|
}
|
2010-10-15 13:03:59 +00:00
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
%PreventExtensions(obj);
|
|
|
|
return obj;
|
2010-07-13 12:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-02 14:36:34 +00:00
|
|
|
// ES5 section 15.2.3.10
|
|
|
|
function ObjectPreventExtension(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2010-07-02 14:36:34 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["preventExtension"]);
|
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
ProxyFix(obj);
|
|
|
|
}
|
2010-07-02 14:36:34 +00:00
|
|
|
%PreventExtensions(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-15 07:51:14 +00:00
|
|
|
// ES5 section 15.2.3.11
|
|
|
|
function ObjectIsSealed(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2010-07-15 07:51:14 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["isSealed"]);
|
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-15 07:51:14 +00:00
|
|
|
var names = ObjectGetOwnPropertyNames(obj);
|
2010-08-26 08:35:49 +00:00
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
2010-07-15 07:51:14 +00:00
|
|
|
var desc = GetOwnProperty(obj, name);
|
2010-08-26 13:03:17 +00:00
|
|
|
if (desc.isConfigurable()) return false;
|
2010-07-15 07:51:14 +00:00
|
|
|
}
|
|
|
|
if (!ObjectIsExtensible(obj)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 12:58:02 +00:00
|
|
|
// ES5 section 15.2.3.12
|
|
|
|
function ObjectIsFrozen(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2010-07-13 12:58:02 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["isFrozen"]);
|
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-13 12:58:02 +00:00
|
|
|
var names = ObjectGetOwnPropertyNames(obj);
|
2010-08-26 08:35:49 +00:00
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
2010-07-13 12:58:02 +00:00
|
|
|
var desc = GetOwnProperty(obj, name);
|
2010-07-14 13:15:43 +00:00
|
|
|
if (IsDataDescriptor(desc) && desc.isWritable()) return false;
|
|
|
|
if (desc.isConfigurable()) return false;
|
2010-07-13 12:58:02 +00:00
|
|
|
}
|
|
|
|
if (!ObjectIsExtensible(obj)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-02 14:36:34 +00:00
|
|
|
// ES5 section 15.2.3.13
|
|
|
|
function ObjectIsExtensible(obj) {
|
2010-07-16 11:21:08 +00:00
|
|
|
if (!IS_SPEC_OBJECT(obj)) {
|
2011-06-16 06:37:49 +00:00
|
|
|
throw MakeTypeError("obj_ctor_property_non_object", ["isExtensible"]);
|
2010-07-02 14:36:34 +00:00
|
|
|
}
|
2011-07-18 13:04:52 +00:00
|
|
|
if (%IsJSProxy(obj)) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-02 14:36:34 +00:00
|
|
|
return %IsExtensible(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
%SetCode($Object, function(x) {
|
2009-06-29 08:14:06 +00:00
|
|
|
if (%_IsConstructCall()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (x == null) return this;
|
|
|
|
return ToObject(x);
|
|
|
|
} else {
|
|
|
|
if (x == null) return { };
|
|
|
|
return ToObject(x);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-08-25 17:50:37 +00:00
|
|
|
%SetExpectedNumberOfProperties($Object, 4);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function SetupObject() {
|
|
|
|
// Setup non-enumerable functions on the Object.prototype object.
|
|
|
|
InstallFunctions($Object.prototype, DONT_ENUM, $Array(
|
|
|
|
"toString", ObjectToString,
|
|
|
|
"toLocaleString", ObjectToLocaleString,
|
|
|
|
"valueOf", ObjectValueOf,
|
|
|
|
"hasOwnProperty", ObjectHasOwnProperty,
|
|
|
|
"isPrototypeOf", ObjectIsPrototypeOf,
|
|
|
|
"propertyIsEnumerable", ObjectPropertyIsEnumerable,
|
|
|
|
"__defineGetter__", ObjectDefineGetter,
|
|
|
|
"__lookupGetter__", ObjectLookupGetter,
|
|
|
|
"__defineSetter__", ObjectDefineSetter,
|
|
|
|
"__lookupSetter__", ObjectLookupSetter
|
|
|
|
));
|
2009-09-15 11:51:40 +00:00
|
|
|
InstallFunctions($Object, DONT_ENUM, $Array(
|
2009-12-09 13:56:58 +00:00
|
|
|
"keys", ObjectKeys,
|
2010-01-07 10:01:24 +00:00
|
|
|
"create", ObjectCreate,
|
2010-02-03 13:10:03 +00:00
|
|
|
"defineProperty", ObjectDefineProperty,
|
|
|
|
"defineProperties", ObjectDefineProperties,
|
2010-07-13 12:58:02 +00:00
|
|
|
"freeze", ObjectFreeze,
|
2010-01-13 12:10:57 +00:00
|
|
|
"getPrototypeOf", ObjectGetPrototypeOf,
|
2010-01-15 15:34:32 +00:00
|
|
|
"getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor,
|
2010-07-02 14:36:34 +00:00
|
|
|
"getOwnPropertyNames", ObjectGetOwnPropertyNames,
|
|
|
|
"isExtensible", ObjectIsExtensible,
|
2010-07-13 12:58:02 +00:00
|
|
|
"isFrozen", ObjectIsFrozen,
|
2010-07-15 07:51:14 +00:00
|
|
|
"isSealed", ObjectIsSealed,
|
|
|
|
"preventExtensions", ObjectPreventExtension,
|
|
|
|
"seal", ObjectSeal
|
2009-09-15 11:51:40 +00:00
|
|
|
));
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
SetupObject();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Boolean
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function BooleanToString() {
|
2008-07-03 15:10:15 +00:00
|
|
|
// NOTE: Both Boolean objects and values can enter here as
|
|
|
|
// 'this'. This is not as dictated by ECMA-262.
|
2011-01-05 13:52:00 +00:00
|
|
|
var b = this;
|
|
|
|
if (!IS_BOOLEAN(b)) {
|
|
|
|
if (!IS_BOOLEAN_WRAPPER(b)) {
|
|
|
|
throw new $TypeError('Boolean.prototype.toString is not generic');
|
|
|
|
}
|
|
|
|
b = %_ValueOf(b);
|
|
|
|
}
|
|
|
|
return b ? 'true' : 'false';
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function BooleanValueOf() {
|
2008-07-03 15:10:15 +00:00
|
|
|
// NOTE: Both Boolean objects and values can enter here as
|
|
|
|
// 'this'. This is not as dictated by ECMA-262.
|
2009-06-29 10:45:16 +00:00
|
|
|
if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this))
|
2008-07-03 15:10:15 +00:00
|
|
|
throw new $TypeError('Boolean.prototype.valueOf is not generic');
|
|
|
|
return %_ValueOf(this);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function SetupBoolean() {
|
|
|
|
InstallFunctions($Boolean.prototype, DONT_ENUM, $Array(
|
|
|
|
"toString", BooleanToString,
|
2010-12-15 09:31:05 +00:00
|
|
|
"valueOf", BooleanValueOf
|
2008-10-03 18:00:28 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupBoolean();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Number
|
|
|
|
|
|
|
|
// Set the Number function and constructor.
|
|
|
|
%SetCode($Number, function(x) {
|
|
|
|
var value = %_ArgumentsLength() == 0 ? 0 : ToNumber(x);
|
2009-06-29 08:14:06 +00:00
|
|
|
if (%_IsConstructCall()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%_SetValueOf(this, value);
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
%FunctionSetPrototype($Number, new $Number(0));
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.4.2.
|
2008-10-03 18:00:28 +00:00
|
|
|
function NumberToString(radix) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// NOTE: Both Number objects and values can enter here as
|
|
|
|
// 'this'. This is not as dictated by ECMA-262.
|
|
|
|
var number = this;
|
|
|
|
if (!IS_NUMBER(this)) {
|
2009-06-29 10:45:16 +00:00
|
|
|
if (!IS_NUMBER_WRAPPER(this))
|
2008-07-03 15:10:15 +00:00
|
|
|
throw new $TypeError('Number.prototype.toString is not generic');
|
|
|
|
// Get the value of this number in case it's an object.
|
|
|
|
number = %_ValueOf(this);
|
|
|
|
}
|
|
|
|
// Fast case: Convert number in radix 10.
|
|
|
|
if (IS_UNDEFINED(radix) || radix === 10) {
|
2011-01-05 13:52:00 +00:00
|
|
|
return %_NumberToString(number);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the radix to an integer and check the range.
|
|
|
|
radix = TO_INTEGER(radix);
|
|
|
|
if (radix < 2 || radix > 36) {
|
|
|
|
throw new $RangeError('toString() radix argument must be between 2 and 36');
|
|
|
|
}
|
|
|
|
// Convert the number to a string in the given radix.
|
|
|
|
return %NumberToRadixString(number, radix);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.4.3
|
2008-10-03 18:00:28 +00:00
|
|
|
function NumberToLocaleString() {
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
throw MakeTypeError("called_on_null_or_undefined",
|
|
|
|
["Number.prototype.toLocaleString"]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return this.toString();
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.4.4
|
2008-10-03 18:00:28 +00:00
|
|
|
function NumberValueOf() {
|
2008-07-03 15:10:15 +00:00
|
|
|
// NOTE: Both Number objects and values can enter here as
|
|
|
|
// 'this'. This is not as dictated by ECMA-262.
|
2009-06-29 10:45:16 +00:00
|
|
|
if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this))
|
2008-07-03 15:10:15 +00:00
|
|
|
throw new $TypeError('Number.prototype.valueOf is not generic');
|
|
|
|
return %_ValueOf(this);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.4.5
|
2008-10-03 18:00:28 +00:00
|
|
|
function NumberToFixed(fractionDigits) {
|
2008-07-03 15:10:15 +00:00
|
|
|
var f = TO_INTEGER(fractionDigits);
|
|
|
|
if (f < 0 || f > 20) {
|
|
|
|
throw new $RangeError("toFixed() digits argument must be between 0 and 20");
|
|
|
|
}
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
throw MakeTypeError("called_on_null_or_undefined",
|
|
|
|
["Number.prototype.toFixed"]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
var x = ToNumber(this);
|
|
|
|
return %NumberToFixed(x, f);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.4.6
|
2008-10-03 18:00:28 +00:00
|
|
|
function NumberToExponential(fractionDigits) {
|
2008-07-03 15:10:15 +00:00
|
|
|
var f = -1;
|
|
|
|
if (!IS_UNDEFINED(fractionDigits)) {
|
|
|
|
f = TO_INTEGER(fractionDigits);
|
|
|
|
if (f < 0 || f > 20) {
|
|
|
|
throw new $RangeError("toExponential() argument must be between 0 and 20");
|
|
|
|
}
|
|
|
|
}
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
throw MakeTypeError("called_on_null_or_undefined",
|
|
|
|
["Number.prototype.toExponential"]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
var x = ToNumber(this);
|
|
|
|
return %NumberToExponential(x, f);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.4.7
|
2008-10-03 18:00:28 +00:00
|
|
|
function NumberToPrecision(precision) {
|
2011-05-05 05:21:30 +00:00
|
|
|
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
|
|
|
throw MakeTypeError("called_on_null_or_undefined",
|
|
|
|
["Number.prototype.toPrecision"]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
if (IS_UNDEFINED(precision)) return ToString(%_ValueOf(this));
|
|
|
|
var p = TO_INTEGER(precision);
|
|
|
|
if (p < 1 || p > 21) {
|
|
|
|
throw new $RangeError("toPrecision() argument must be between 1 and 21");
|
|
|
|
}
|
|
|
|
var x = ToNumber(this);
|
|
|
|
return %NumberToPrecision(x, p);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function SetupNumber() {
|
2009-08-13 12:35:59 +00:00
|
|
|
%OptimizeObjectForAddingMultipleProperties($Number.prototype, 8);
|
2009-08-13 14:04:49 +00:00
|
|
|
// Setup the constructor property on the Number prototype object.
|
2008-10-03 18:00:28 +00:00
|
|
|
%SetProperty($Number.prototype, "constructor", $Number, DONT_ENUM);
|
|
|
|
|
2009-08-13 12:35:59 +00:00
|
|
|
%OptimizeObjectForAddingMultipleProperties($Number, 5);
|
2008-10-03 18:00:28 +00:00
|
|
|
// ECMA-262 section 15.7.3.1.
|
|
|
|
%SetProperty($Number,
|
|
|
|
"MAX_VALUE",
|
|
|
|
1.7976931348623157e+308,
|
|
|
|
DONT_ENUM | DONT_DELETE | READ_ONLY);
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.3.2.
|
|
|
|
%SetProperty($Number, "MIN_VALUE", 5e-324, DONT_ENUM | DONT_DELETE | READ_ONLY);
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.3.3.
|
|
|
|
%SetProperty($Number, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY);
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.3.4.
|
|
|
|
%SetProperty($Number,
|
|
|
|
"NEGATIVE_INFINITY",
|
|
|
|
-1/0,
|
|
|
|
DONT_ENUM | DONT_DELETE | READ_ONLY);
|
|
|
|
|
|
|
|
// ECMA-262 section 15.7.3.5.
|
|
|
|
%SetProperty($Number,
|
|
|
|
"POSITIVE_INFINITY",
|
|
|
|
1/0,
|
|
|
|
DONT_ENUM | DONT_DELETE | READ_ONLY);
|
2010-02-17 11:26:00 +00:00
|
|
|
%ToFastProperties($Number);
|
2008-10-03 18:00:28 +00:00
|
|
|
|
|
|
|
// Setup non-enumerable functions on the Number prototype object.
|
|
|
|
InstallFunctions($Number.prototype, DONT_ENUM, $Array(
|
|
|
|
"toString", NumberToString,
|
|
|
|
"toLocaleString", NumberToLocaleString,
|
|
|
|
"valueOf", NumberValueOf,
|
|
|
|
"toFixed", NumberToFixed,
|
|
|
|
"toExponential", NumberToExponential,
|
2010-12-15 09:31:05 +00:00
|
|
|
"toPrecision", NumberToPrecision
|
2008-10-03 18:00:28 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupNumber();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Function
|
|
|
|
|
|
|
|
$Function.prototype.constructor = $Function;
|
|
|
|
|
|
|
|
function FunctionSourceString(func) {
|
2009-06-29 10:45:16 +00:00
|
|
|
if (!IS_FUNCTION(func)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
throw new $TypeError('Function.prototype.toString is not generic');
|
2009-06-29 10:45:16 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
var source = %FunctionGetSourceCode(func);
|
2009-09-17 11:11:01 +00:00
|
|
|
if (!IS_STRING(source) || %FunctionIsBuiltin(func)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
var name = %FunctionGetName(func);
|
|
|
|
if (name) {
|
|
|
|
// Mimic what KJS does.
|
|
|
|
return 'function ' + name + '() { [native code] }';
|
|
|
|
} else {
|
|
|
|
return 'function () { [native code] }';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var name = %FunctionGetName(func);
|
|
|
|
return 'function ' + name + source;
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 18:00:28 +00:00
|
|
|
function FunctionToString() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return FunctionSourceString(this);
|
2008-10-03 18:00:28 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2010-07-23 10:08:55 +00:00
|
|
|
// ES5 15.3.4.5
|
|
|
|
function FunctionBind(this_arg) { // Length is 1.
|
|
|
|
if (!IS_FUNCTION(this)) {
|
|
|
|
throw new $TypeError('Bind must be called on a function');
|
|
|
|
}
|
|
|
|
// this_arg is not an argument that should be bound.
|
2010-07-27 09:20:21 +00:00
|
|
|
var argc_bound = (%_ArgumentsLength() || 1) - 1;
|
2011-02-08 10:08:47 +00:00
|
|
|
var fn = this;
|
|
|
|
if (argc_bound == 0) {
|
|
|
|
var result = function() {
|
|
|
|
if (%_IsConstructCall()) {
|
|
|
|
// %NewObjectFromBound implicitly uses arguments passed to this
|
|
|
|
// function. We do not pass the arguments object explicitly to avoid
|
|
|
|
// materializing it and guarantee that this function will be optimized.
|
|
|
|
return %NewObjectFromBound(fn, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn.apply(this_arg, arguments);
|
|
|
|
};
|
|
|
|
} else {
|
2011-03-03 11:49:03 +00:00
|
|
|
var bound_args = new InternalArray(argc_bound);
|
2010-07-23 10:08:55 +00:00
|
|
|
for(var i = 0; i < argc_bound; i++) {
|
|
|
|
bound_args[i] = %_Arguments(i+1);
|
2010-10-15 13:03:59 +00:00
|
|
|
}
|
2011-02-08 10:08:47 +00:00
|
|
|
|
|
|
|
var result = function() {
|
|
|
|
// If this is a construct call we use a special runtime method
|
|
|
|
// to generate the actual object using the bound function.
|
|
|
|
if (%_IsConstructCall()) {
|
|
|
|
// %NewObjectFromBound implicitly uses arguments passed to this
|
|
|
|
// function. We do not pass the arguments object explicitly to avoid
|
|
|
|
// materializing it and guarantee that this function will be optimized.
|
|
|
|
return %NewObjectFromBound(fn, bound_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine the args we got from the bind call with the args
|
|
|
|
// given as argument to the invocation.
|
|
|
|
var argc = %_ArgumentsLength();
|
2011-03-03 11:49:03 +00:00
|
|
|
var args = new InternalArray(argc + argc_bound);
|
2011-02-08 10:08:47 +00:00
|
|
|
// Add bound arguments.
|
|
|
|
for (var i = 0; i < argc_bound; i++) {
|
|
|
|
args[i] = bound_args[i];
|
|
|
|
}
|
|
|
|
// Add arguments from call.
|
|
|
|
for (var i = 0; i < argc; i++) {
|
|
|
|
args[argc_bound + i] = %_Arguments(i);
|
|
|
|
}
|
|
|
|
return fn.apply(this_arg, args);
|
|
|
|
};
|
2010-07-23 10:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We already have caller and arguments properties on functions,
|
|
|
|
// which are non-configurable. It therefore makes no sence to
|
|
|
|
// try to redefine these as defined by the spec. The spec says
|
|
|
|
// that bind should make these throw a TypeError if get or set
|
|
|
|
// is called and make them non-enumerable and non-configurable.
|
2010-10-15 13:03:59 +00:00
|
|
|
// To be consistent with our normal functions we leave this as it is.
|
2010-07-23 10:08:55 +00:00
|
|
|
|
|
|
|
// Set the correct length.
|
|
|
|
var length = (this.length - argc_bound) > 0 ? this.length - argc_bound : 0;
|
|
|
|
%FunctionSetLength(result, length);
|
2011-06-15 10:47:37 +00:00
|
|
|
%FunctionRemovePrototype(result);
|
|
|
|
%FunctionSetBound(result);
|
2010-07-23 10:08:55 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
function NewFunction(arg1) { // length == 1
|
|
|
|
var n = %_ArgumentsLength();
|
|
|
|
var p = '';
|
|
|
|
if (n > 1) {
|
2011-03-03 11:49:03 +00:00
|
|
|
p = new InternalArray(n - 1);
|
2011-01-05 13:52:00 +00:00
|
|
|
for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i);
|
2011-01-07 13:21:34 +00:00
|
|
|
p = Join(p, n - 1, ',', NonStringToString);
|
2008-07-03 15:10:15 +00:00
|
|
|
// If the formal parameters string include ) - an illegal
|
|
|
|
// character - it may make the combined function expression
|
|
|
|
// compile. We avoid this problem by checking for this early on.
|
|
|
|
if (p.indexOf(')') != -1) throw MakeSyntaxError('unable_to_parse',[]);
|
|
|
|
}
|
|
|
|
var body = (n > 0) ? ToString(%_Arguments(n - 1)) : '';
|
2008-09-25 07:35:45 +00:00
|
|
|
var source = '(function(' + p + ') {\n' + body + '\n})';
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// The call to SetNewFunctionAttributes will ensure the prototype
|
|
|
|
// property of the resulting function is enumerable (ECMA262, 15.3.5.2).
|
2010-10-27 09:19:43 +00:00
|
|
|
var f = %CompileString(source)();
|
2008-09-25 07:35:45 +00:00
|
|
|
%FunctionSetName(f, "anonymous");
|
|
|
|
return %SetNewFunctionAttributes(f);
|
2008-10-03 07:14:31 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
%SetCode($Function, NewFunction);
|
2008-10-03 18:00:28 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function SetupFunction() {
|
|
|
|
InstallFunctions($Function.prototype, DONT_ENUM, $Array(
|
2010-07-23 10:08:55 +00:00
|
|
|
"bind", FunctionBind,
|
2008-10-03 18:00:28 +00:00
|
|
|
"toString", FunctionToString
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupFunction();
|