| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "builtin/BigInt.h" |
| 8 |
|
| 9 |
#include "jit/InlinableNatives.h" |
| 10 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 11 |
#include "js/PropertySpec.h" |
| 12 |
#include "vm/BigIntType.h" |
| 13 |
|
| 14 |
#include "vm/GeckoProfiler-inl.h" |
| 15 |
#include "vm/JSObject-inl.h" |
| 16 |
|
| 17 |
using namespace js; |
| 18 |
|
| 19 |
static MOZ_ALWAYS_INLINE bool IsBigInt(HandleValue v) { |
| 20 |
return v.isBigInt() || (v.isObject() && v.toObject().is<BigIntObject>()); |
| 21 |
} |
| 22 |
|
| 23 |
// BigInt proposal section 5.1.3 |
| 24 |
static bool BigIntConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 25 |
AutoJSConstructorProfilerEntry pseudoFrame(cx, "BigInt"); |
| 26 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 27 |
|
| 28 |
// Step 1. |
| 29 |
if (args.isConstructing()) { |
| 30 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 31 |
JSMSG_NOT_CONSTRUCTOR, "BigInt"); |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
// Step 2. |
| 36 |
RootedValue v(cx, args.get(0)); |
| 37 |
if (!ToPrimitive(cx, JSTYPE_NUMBER, &v)) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
// Steps 3-4. |
| 42 |
BigInt* bi; |
| 43 |
if (!v.isNumber()) { |
| 44 |
bi = ToBigInt(cx, v); |
| 45 |
} else if (v.isInt32()) { |
| 46 |
bi = BigInt::createFromInt64(cx, int64_t(v.toInt32())); |
| 47 |
} else { |
| 48 |
bi = NumberToBigInt(cx, v.toDouble()); |
| 49 |
} |
| 50 |
if (!bi) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
args.rval().setBigInt(bi); |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
JSObject* BigIntObject::create(JSContext* cx, HandleBigInt bigInt) { |
| 59 |
BigIntObject* bn = NewBuiltinClassInstance<BigIntObject>(cx); |
| 60 |
if (!bn) { |
| 61 |
return nullptr; |
| 62 |
} |
| 63 |
bn->setFixedSlot(PRIMITIVE_VALUE_SLOT, BigIntValue(bigInt)); |
| 64 |
return bn; |
| 65 |
} |
| 66 |
|
| 67 |
BigInt* BigIntObject::unbox() const { |
| 68 |
return getFixedSlot(PRIMITIVE_VALUE_SLOT).toBigInt(); |
| 69 |
} |
| 70 |
|
| 71 |
// BigInt proposal section 5.3.4 |
| 72 |
bool BigIntObject::valueOf_impl(JSContext* cx, const CallArgs& args) { |
| 73 |
// Step 1. |
| 74 |
HandleValue thisv = args.thisv(); |
| 75 |
MOZ_ASSERT(IsBigInt(thisv)); |
| 76 |
BigInt* bi = thisv.isBigInt() ? thisv.toBigInt() |
| 77 |
: thisv.toObject().as<BigIntObject>().unbox(); |
| 78 |
|
| 79 |
args.rval().setBigInt(bi); |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
bool BigIntObject::valueOf(JSContext* cx, unsigned argc, Value* vp) { |
| 84 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 85 |
return CallNonGenericMethod<IsBigInt, valueOf_impl>(cx, args); |
| 86 |
} |
| 87 |
|
| 88 |
// BigInt proposal section 5.3.3 |
| 89 |
bool BigIntObject::toString_impl(JSContext* cx, const CallArgs& args) { |
| 90 |
// Step 1. |
| 91 |
HandleValue thisv = args.thisv(); |
| 92 |
MOZ_ASSERT(IsBigInt(thisv)); |
| 93 |
RootedBigInt bi(cx, thisv.isBigInt() |
| 94 |
? thisv.toBigInt() |
| 95 |
: thisv.toObject().as<BigIntObject>().unbox()); |
| 96 |
|
| 97 |
// Steps 2-3. |
| 98 |
uint8_t radix = 10; |
| 99 |
|
| 100 |
// Steps 4-5. |
| 101 |
if (args.hasDefined(0)) { |
| 102 |
double d; |
| 103 |
if (!ToInteger(cx, args[0], &d)) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
if (d < 2 || d > 36) { |
| 107 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BAD_RADIX); |
| 108 |
return false; |
| 109 |
} |
| 110 |
radix = d; |
| 111 |
} |
| 112 |
|
| 113 |
// Steps 6-7. |
| 114 |
JSLinearString* str = BigInt::toString<CanGC>(cx, bi, radix); |
| 115 |
if (!str) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
args.rval().setString(str); |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
bool BigIntObject::toString(JSContext* cx, unsigned argc, Value* vp) { |
| 123 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "BigInt.prototype", "toString"); |
| 124 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 125 |
return CallNonGenericMethod<IsBigInt, toString_impl>(cx, args); |
| 126 |
} |
| 127 |
|
| 128 |
#ifndef JS_HAS_INTL_API |
| 129 |
// BigInt proposal section 5.3.2. "This function is |
| 130 |
// implementation-dependent, and it is permissible, but not encouraged, |
| 131 |
// for it to return the same thing as toString." |
| 132 |
bool BigIntObject::toLocaleString_impl(JSContext* cx, const CallArgs& args) { |
| 133 |
HandleValue thisv = args.thisv(); |
| 134 |
MOZ_ASSERT(IsBigInt(thisv)); |
| 135 |
RootedBigInt bi(cx, thisv.isBigInt() |
| 136 |
? thisv.toBigInt() |
| 137 |
: thisv.toObject().as<BigIntObject>().unbox()); |
| 138 |
|
| 139 |
JSString* str = BigInt::toString<CanGC>(cx, bi, 10); |
| 140 |
if (!str) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
args.rval().setString(str); |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
bool BigIntObject::toLocaleString(JSContext* cx, unsigned argc, Value* vp) { |
| 148 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "BigInt.prototype", |
| 149 |
"toLocaleString"); |
| 150 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 151 |
return CallNonGenericMethod<IsBigInt, toLocaleString_impl>(cx, args); |
| 152 |
} |
| 153 |
#endif /* !JS_HAS_INTL_API */ |
| 154 |
|
| 155 |
// BigInt proposal section 5.2.1. BigInt.asUintN ( bits, bigint ) |
| 156 |
bool BigIntObject::asUintN(JSContext* cx, unsigned argc, Value* vp) { |
| 157 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 158 |
|
| 159 |
// Step 1. |
| 160 |
uint64_t bits; |
| 161 |
if (!ToIndex(cx, args.get(0), &bits)) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
// Step 2. |
| 166 |
RootedBigInt bi(cx, ToBigInt(cx, args.get(1))); |
| 167 |
if (!bi) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
// Step 3. |
| 172 |
BigInt* res = BigInt::asUintN(cx, bi, bits); |
| 173 |
if (!res) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
args.rval().setBigInt(res); |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
// BigInt proposal section 5.2.2. BigInt.asIntN ( bits, bigint ) |
| 182 |
bool BigIntObject::asIntN(JSContext* cx, unsigned argc, Value* vp) { |
| 183 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 184 |
|
| 185 |
// Step 1. |
| 186 |
uint64_t bits; |
| 187 |
if (!ToIndex(cx, args.get(0), &bits)) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
// Step 2. |
| 192 |
RootedBigInt bi(cx, ToBigInt(cx, args.get(1))); |
| 193 |
if (!bi) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
// Step 3. |
| 198 |
BigInt* res = BigInt::asIntN(cx, bi, bits); |
| 199 |
if (!res) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
args.rval().setBigInt(res); |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
const ClassSpec BigIntObject::classSpec_ = { |
| 208 |
GenericCreateConstructor<BigIntConstructor, 1, gc::AllocKind::FUNCTION, |
| 209 |
&jit::JitInfo_BigInt>, |
| 210 |
GenericCreatePrototype<BigIntObject>, |
| 211 |
BigIntObject::staticMethods, |
| 212 |
nullptr, |
| 213 |
BigIntObject::methods, |
| 214 |
BigIntObject::properties, |
| 215 |
}; |
| 216 |
|
| 217 |
const JSClass BigIntObject::class_ = { |
| 218 |
"BigInt", |
| 219 |
JSCLASS_HAS_CACHED_PROTO(JSProto_BigInt) | |
| 220 |
JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS), |
| 221 |
JS_NULL_CLASS_OPS, |
| 222 |
&BigIntObject::classSpec_, |
| 223 |
}; |
| 224 |
|
| 225 |
const JSClass BigIntObject::protoClass_ = { |
| 226 |
"BigInt.prototype", |
| 227 |
JSCLASS_HAS_CACHED_PROTO(JSProto_BigInt), |
| 228 |
JS_NULL_CLASS_OPS, |
| 229 |
&BigIntObject::classSpec_, |
| 230 |
}; |
| 231 |
|
| 232 |
const JSPropertySpec BigIntObject::properties[] = { |
| 233 |
// BigInt proposal section 5.3.5 |
| 234 |
JS_STRING_SYM_PS(toStringTag, "BigInt", JSPROP_READONLY), |
| 235 |
JS_PS_END, |
| 236 |
}; |
| 237 |
|
| 238 |
const JSFunctionSpec BigIntObject::methods[] = { |
| 239 |
JS_FN("valueOf", valueOf, 0, 0), |
| 240 |
JS_FN("toString", toString, 0, 0), |
| 241 |
#ifdef JS_HAS_INTL_API |
| 242 |
JS_SELF_HOSTED_FN("toLocaleString", "BigInt_toLocaleString", 0, 0), |
| 243 |
#else |
| 244 |
JS_FN("toLocaleString", toLocaleString, 0, 0), |
| 245 |
#endif |
| 246 |
JS_FS_END, |
| 247 |
}; |
| 248 |
|
| 249 |
const JSFunctionSpec BigIntObject::staticMethods[] = { |
| 250 |
JS_INLINABLE_FN("asUintN", asUintN, 2, 0, BigIntAsUintN), |
| 251 |
JS_INLINABLE_FN("asIntN", asIntN, 2, 0, BigIntAsIntN), |
| 252 |
JS_FS_END, |
| 253 |
}; |