v8natives.js 6.68 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
(function(global, utils) {
6 7 8

%CheckIsBootstrapping();

9 10 11
// ----------------------------------------------------------------------------
// Imports

12 13
var GlobalNumber = global.Number;
var GlobalObject = global.Object;
14
var iteratorSymbol = utils.ImportNow("iterator_symbol");
15
var NaN = %GetRootNaN();
16
var ObjectToString = utils.ImportNow("object_to_string");
17

18
// ----------------------------------------------------------------------------
19 20


21
// ES6 18.2.5 parseInt(string, radix)
22
function GlobalParseInt(string, radix) {
23
  if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) {
24 25 26 27 28 29
    // 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;
30
    if (IS_NUMBER(string) &&
31 32
        ((0.01 < string && string < 1e9) ||
            (-1e9 < string && string < -0.01))) {
33 34
      // Truncate number.
      return string | 0;
35
    }
36
    string = TO_STRING(string);
37
    radix = radix | 0;
38
  } else {
39
    // The spec says ToString should be evaluated before ToInt32.
40
    string = TO_STRING(string);
41
    radix = TO_INT32(radix);
42
    if (!(radix == 0 || (2 <= radix && radix <= 36))) {
43
      return NaN;
44
    }
45
  }
46

47 48 49 50 51
  if (%_HasCachedArrayIndex(string) &&
      (radix == 0 || radix == 10)) {
    return %_GetCachedArrayIndex(string);
  }
  return %StringParseInt(string, radix);
52
}
53 54


55
// ES6 18.2.4 parseFloat(string)
56
function GlobalParseFloat(string) {
57
  // 1. Let inputString be ? ToString(string).
58
  string = TO_STRING(string);
59 60
  if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string);
  return %StringParseFloat(string);
61 62
}

63

64 65
// ----------------------------------------------------------------------------

66
// Set up global object.
67
var attributes = DONT_ENUM | DONT_DELETE | READ_ONLY;
68

69
utils.InstallConstants(global, [
70
  // ES6 18.1.1
71
  "Infinity", INFINITY,
72 73 74
  // ES6 18.1.2
  "NaN", NaN,
  // ES6 18.1.3
75 76
  "undefined", UNDEFINED,
]);
77

78
// Set up non-enumerable function on the global object.
79
utils.InstallFunctions(global, DONT_ENUM, [
80 81 82
  "parseInt", GlobalParseInt,
  "parseFloat", GlobalParseFloat,
]);
83 84 85 86 87


// ----------------------------------------------------------------------------
// Object

88
// ES6 19.1.3.5 Object.prototype.toLocaleString([reserved1 [,reserved2]])
89
function ObjectToLocaleString() {
90
  CHECK_OBJECT_COERCIBLE(this, "Object.prototype.toLocaleString");
91
  return this.toString();
92
}
93 94


95
// ES6 19.1.3.7 Object.prototype.valueOf()
96
function ObjectValueOf() {
97
  return TO_OBJECT(this);
98
}
99 100


101
// ES6 19.1.3.3 Object.prototype.isPrototypeOf(V)
102
function ObjectIsPrototypeOf(V) {
103
  if (!IS_RECEIVER(V)) return false;
104
  var O = TO_OBJECT(this);
105
  return %HasInPrototypeChain(V, O);
106
}
107 108


109
// ES6 7.3.9
110 111 112
function GetMethod(obj, p) {
  var func = obj[p];
  if (IS_NULL_OR_UNDEFINED(func)) return UNDEFINED;
113
  if (IS_CALLABLE(func)) return func;
114
  throw %make_type_error(kCalledNonCallable, typeof func);
115 116
}

117
// ES6 section 19.1.2.18.
118 119 120
function ObjectSetPrototypeOf(obj, proto) {
  CHECK_OBJECT_COERCIBLE(obj, "Object.setPrototypeOf");

121
  if (proto !== null && !IS_RECEIVER(proto)) {
122
    throw %make_type_error(kProtoObjectOrNull, proto);
123 124
  }

125
  if (IS_RECEIVER(obj)) {
126 127 128 129 130 131
    %SetPrototype(obj, proto);
  }

  return obj;
}

132
// ES6 B.2.2.1.1
133
function ObjectGetProto() {
134
  return %object_get_prototype_of(this);
135 136 137
}


138
// ES6 B.2.2.1.2
139 140 141
function ObjectSetProto(proto) {
  CHECK_OBJECT_COERCIBLE(this, "Object.prototype.__proto__");

142
  if ((IS_RECEIVER(proto) || IS_NULL(proto)) && IS_RECEIVER(this)) {
143 144
    %SetPrototype(this, proto);
  }
145 146 147
}


148
// ES6 19.1.1.1
149
function ObjectConstructor(x) {
150 151
  if (GlobalObject != new.target && !IS_UNDEFINED(new.target)) {
    return this;
152
  }
153 154
  if (IS_NULL(x) || IS_UNDEFINED(x)) return {};
  return TO_OBJECT(x);
155
}
156 157 158


// ----------------------------------------------------------------------------
159
// Object
160

161 162 163 164 165 166 167
%SetNativeFlag(GlobalObject);
%SetCode(GlobalObject, ObjectConstructor);

%AddNamedProperty(GlobalObject.prototype, "constructor", GlobalObject,
                  DONT_ENUM);

// Set up non-enumerable functions on the Object.prototype object.
168
utils.InstallFunctions(GlobalObject.prototype, DONT_ENUM, [
169 170 171 172
  "toString", ObjectToString,
  "toLocaleString", ObjectToLocaleString,
  "valueOf", ObjectValueOf,
  "isPrototypeOf", ObjectIsPrototypeOf,
173
  // propertyIsEnumerable is added in bootstrapper.cc.
174 175 176 177
  // __defineGetter__ is added in bootstrapper.cc.
  // __lookupGetter__ is added in bootstrapper.cc.
  // __defineSetter__ is added in bootstrapper.cc.
  // __lookupSetter__ is added in bootstrapper.cc.
178
]);
179 180
utils.InstallGetterSetter(
    GlobalObject.prototype, "__proto__", ObjectGetProto, ObjectSetProto);
181 182

// Set up non-enumerable functions in the Object object.
183
utils.InstallFunctions(GlobalObject, DONT_ENUM, [
184 185
  "setPrototypeOf", ObjectSetPrototypeOf,
  // getOwnPropertySymbols is added in symbol.js.
186
  // Others are added in bootstrapper.cc.
187
]);
188

189

190

191 192 193
// ----------------------------------------------------------------------------
// Number

194
utils.InstallConstants(GlobalNumber, [
195 196 197 198 199
  // ECMA-262 section 15.7.3.1.
  "MAX_VALUE", 1.7976931348623157e+308,
  // ECMA-262 section 15.7.3.2.
  "MIN_VALUE", 5e-324,
  // ECMA-262 section 15.7.3.3.
200
  "NaN", NaN,
201 202 203 204 205 206 207
  // ECMA-262 section 15.7.3.4.
  "NEGATIVE_INFINITY", -INFINITY,
  // ECMA-262 section 15.7.3.5.
  "POSITIVE_INFINITY", INFINITY,

  // --- Harmony constants (no spec refs until settled.)

208 209 210
  "MAX_SAFE_INTEGER", 9007199254740991,
  "MIN_SAFE_INTEGER", -9007199254740991,
  "EPSILON", 2.220446049250313e-16,
211
]);
212

213
// Harmony Number constructor additions
214
utils.InstallFunctions(GlobalNumber, DONT_ENUM, [
215 216 217 218
  "parseInt", GlobalParseInt,
  "parseFloat", GlobalParseFloat
]);

219

220

221 222 223
// ----------------------------------------------------------------------------
// Iterator related spec functions.

224
// ES6 7.4.1 GetIterator(obj, method)
225 226
function GetIterator(obj, method) {
  if (IS_UNDEFINED(method)) {
227
    method = obj[iteratorSymbol];
228
  }
229
  if (!IS_CALLABLE(method)) {
230
    throw %make_type_error(kNotIterable, obj);
231
  }
232
  var iterator = %_Call(method, obj);
233
  if (!IS_RECEIVER(iterator)) {
234
    throw %make_type_error(kNotAnIterator, iterator);
235 236 237
  }
  return iterator;
}
238

239 240
// ----------------------------------------------------------------------------
// Exports
241

242 243 244
utils.Export(function(to) {
  to.GetIterator = GetIterator;
  to.GetMethod = GetMethod;
245
  to.ObjectHasOwnProperty = GlobalObject.prototype.hasOwnProperty;
246
});
247

248
%InstallToContext([
249
  "object_value_of", ObjectValueOf,
250
]);
251

252
})