Source code

Revision control

Other Tools

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 "jsapi.h"
10
11
#include "builtin/TypedObject.h"
12
#include "gc/Tracer.h"
13
#include "js/PropertySpec.h"
14
#include "js/TracingAPI.h"
15
#include "vm/ArrayBufferObject.h"
16
#include "vm/BigIntType.h"
17
#include "vm/SelfHosting.h"
18
#include "vm/TaggedProto.h"
19
20
#include "vm/JSObject-inl.h"
21
22
using namespace js;
23
24
static MOZ_ALWAYS_INLINE bool IsBigInt(HandleValue v) {
25
return v.isBigInt() || (v.isObject() && v.toObject().is<BigIntObject>());
26
}
27
28
// BigInt proposal section 5.1.3
29
static bool BigIntConstructor(JSContext* cx, unsigned argc, Value* vp) {
30
CallArgs args = CallArgsFromVp(argc, vp);
31
32
// Step 1.
33
if (args.isConstructing()) {
34
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
35
JSMSG_NOT_CONSTRUCTOR, "BigInt");
36
return false;
37
}
38
39
// Step 2.
40
RootedValue v(cx, args.get(0));
41
if (!ToPrimitive(cx, JSTYPE_NUMBER, &v)) {
42
return false;
43
}
44
45
// Steps 3-4.
46
BigInt* bi =
47
v.isNumber() ? NumberToBigInt(cx, v.toNumber()) : ToBigInt(cx, v);
48
if (!bi) {
49
return false;
50
}
51
52
args.rval().setBigInt(bi);
53
return true;
54
}
55
56
JSObject* BigIntObject::create(JSContext* cx, HandleBigInt bigInt) {
57
BigIntObject* bn = NewBuiltinClassInstance<BigIntObject>(cx);
58
if (!bn) {
59
return nullptr;
60
}
61
bn->setFixedSlot(PRIMITIVE_VALUE_SLOT, BigIntValue(bigInt));
62
return bn;
63
}
64
65
BigInt* BigIntObject::unbox() const {
66
return getFixedSlot(PRIMITIVE_VALUE_SLOT).toBigInt();
67
}
68
69
// BigInt proposal section 5.3.4
70
bool BigIntObject::valueOf_impl(JSContext* cx, const CallArgs& args) {
71
// Step 1.
72
HandleValue thisv = args.thisv();
73
MOZ_ASSERT(IsBigInt(thisv));
74
BigInt* bi = thisv.isBigInt() ? thisv.toBigInt()
75
: thisv.toObject().as<BigIntObject>().unbox();
76
77
args.rval().setBigInt(bi);
78
return true;
79
}
80
81
bool BigIntObject::valueOf(JSContext* cx, unsigned argc, Value* vp) {
82
CallArgs args = CallArgsFromVp(argc, vp);
83
return CallNonGenericMethod<IsBigInt, valueOf_impl>(cx, args);
84
}
85
86
// BigInt proposal section 5.3.3
87
bool BigIntObject::toString_impl(JSContext* cx, const CallArgs& args) {
88
// Step 1.
89
HandleValue thisv = args.thisv();
90
MOZ_ASSERT(IsBigInt(thisv));
91
RootedBigInt bi(cx, thisv.isBigInt()
92
? thisv.toBigInt()
93
: thisv.toObject().as<BigIntObject>().unbox());
94
95
// Steps 2-3.
96
uint8_t radix = 10;
97
98
// Steps 4-5.
99
if (args.hasDefined(0)) {
100
double d;
101
if (!ToInteger(cx, args[0], &d)) {
102
return false;
103
}
104
if (d < 2 || d > 36) {
105
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BAD_RADIX);
106
return false;
107
}
108
radix = d;
109
}
110
111
// Steps 6-7.
112
JSLinearString* str = BigInt::toString<CanGC>(cx, bi, radix);
113
if (!str) {
114
return false;
115
}
116
args.rval().setString(str);
117
return true;
118
}
119
120
bool BigIntObject::toString(JSContext* cx, unsigned argc, Value* vp) {
121
CallArgs args = CallArgsFromVp(argc, vp);
122
return CallNonGenericMethod<IsBigInt, toString_impl>(cx, args);
123
}
124
125
#ifndef JS_HAS_INTL_API
126
// BigInt proposal section 5.3.2. "This function is
127
// implementation-dependent, and it is permissible, but not encouraged,
128
// for it to return the same thing as toString."
129
bool BigIntObject::toLocaleString_impl(JSContext* cx, const CallArgs& args) {
130
HandleValue thisv = args.thisv();
131
MOZ_ASSERT(IsBigInt(thisv));
132
RootedBigInt bi(cx, thisv.isBigInt()
133
? thisv.toBigInt()
134
: thisv.toObject().as<BigIntObject>().unbox());
135
136
JSString* str = BigInt::toString<CanGC>(cx, bi, 10);
137
if (!str) {
138
return false;
139
}
140
args.rval().setString(str);
141
return true;
142
}
143
144
bool BigIntObject::toLocaleString(JSContext* cx, unsigned argc, Value* vp) {
145
CallArgs args = CallArgsFromVp(argc, vp);
146
return CallNonGenericMethod<IsBigInt, toLocaleString_impl>(cx, args);
147
}
148
#endif /* !JS_HAS_INTL_API */
149
150
// BigInt proposal section 5.2.1. BigInt.asUintN ( bits, bigint )
151
bool BigIntObject::asUintN(JSContext* cx, unsigned argc, Value* vp) {
152
CallArgs args = CallArgsFromVp(argc, vp);
153
154
// Step 1.
155
uint64_t bits;
156
if (!ToIndex(cx, args.get(0), &bits)) {
157
return false;
158
}
159
160
// Step 2.
161
RootedBigInt bi(cx, ToBigInt(cx, args.get(1)));
162
if (!bi) {
163
return false;
164
}
165
166
// Step 3.
167
BigInt* res = BigInt::asUintN(cx, bi, bits);
168
if (!res) {
169
return false;
170
}
171
172
args.rval().setBigInt(res);
173
return true;
174
}
175
176
// BigInt proposal section 5.2.2. BigInt.asIntN ( bits, bigint )
177
bool BigIntObject::asIntN(JSContext* cx, unsigned argc, Value* vp) {
178
CallArgs args = CallArgsFromVp(argc, vp);
179
180
// Step 1.
181
uint64_t bits;
182
if (!ToIndex(cx, args.get(0), &bits)) {
183
return false;
184
}
185
186
// Step 2.
187
RootedBigInt bi(cx, ToBigInt(cx, args.get(1)));
188
if (!bi) {
189
return false;
190
}
191
192
// Step 3.
193
BigInt* res = BigInt::asIntN(cx, bi, bits);
194
if (!res) {
195
return false;
196
}
197
198
args.rval().setBigInt(res);
199
return true;
200
}
201
202
const ClassSpec BigIntObject::classSpec_ = {
203
GenericCreateConstructor<BigIntConstructor, 1, gc::AllocKind::FUNCTION>,
204
GenericCreatePrototype<BigIntObject>,
205
BigIntObject::staticMethods,
206
nullptr,
207
BigIntObject::methods,
208
BigIntObject::properties};
209
210
// The class is named "Object" as a workaround for bug 1277801.
211
const JSClass BigIntObject::class_ = {
212
"Object",
213
JSCLASS_HAS_CACHED_PROTO(JSProto_BigInt) |
214
JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS),
215
JS_NULL_CLASS_OPS, &BigIntObject::classSpec_};
216
217
const JSClass BigIntObject::protoClass_ = {
218
js_Object_str, JSCLASS_HAS_CACHED_PROTO(JSProto_BigInt), JS_NULL_CLASS_OPS,
219
&BigIntObject::classSpec_};
220
221
const JSPropertySpec BigIntObject::properties[] = {
222
// BigInt proposal section 5.3.5
223
JS_STRING_SYM_PS(toStringTag, "BigInt", JSPROP_READONLY), JS_PS_END};
224
225
const JSFunctionSpec BigIntObject::methods[] = {
226
JS_FN("valueOf", valueOf, 0, 0), JS_FN("toString", toString, 0, 0),
227
#ifdef JS_HAS_INTL_API
228
JS_SELF_HOSTED_FN("toLocaleString", "BigInt_toLocaleString", 0, 0),
229
#else
230
JS_FN("toLocaleString", toLocaleString, 0, 0),
231
#endif
232
JS_FS_END};
233
234
const JSFunctionSpec BigIntObject::staticMethods[] = {
235
JS_FN("asUintN", asUintN, 2, 0), JS_FN("asIntN", asIntN, 2, 0), JS_FS_END};