@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/MIT-LICENSE.txt +21 -0
- package/README.md +1677 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/native/oseo_runtime.h +2594 -0
- package/native/runtime_agent.c +1693 -0
- package/native/runtime_arguments.c +355 -0
- package/native/runtime_array.c +5742 -0
- package/native/runtime_array_buffer.c +1724 -0
- package/native/runtime_async_generator.c +948 -0
- package/native/runtime_atomics.c +1259 -0
- package/native/runtime_bigint.c +1363 -0
- package/native/runtime_bigint_object.c +602 -0
- package/native/runtime_binding.c +1005 -0
- package/native/runtime_boolean.c +296 -0
- package/native/runtime_clock.c +157 -0
- package/native/runtime_clock_posix.c +575 -0
- package/native/runtime_core.c +464 -0
- package/native/runtime_data_view.c +1111 -0
- package/native/runtime_date.c +1973 -0
- package/native/runtime_descriptor.c +1080 -0
- package/native/runtime_disposal.c +1643 -0
- package/native/runtime_enumeration.c +612 -0
- package/native/runtime_error.c +973 -0
- package/native/runtime_event_loop.c +629 -0
- package/native/runtime_function.c +3643 -0
- package/native/runtime_generator.c +1096 -0
- package/native/runtime_internal.h +5554 -0
- package/native/runtime_iterator.c +5355 -0
- package/native/runtime_json.c +1968 -0
- package/native/runtime_map.c +1539 -0
- package/native/runtime_math.c +894 -0
- package/native/runtime_memory.c +1341 -0
- package/native/runtime_module.c +860 -0
- package/native/runtime_number.c +2053 -0
- package/native/runtime_object.c +399 -0
- package/native/runtime_object_builtin.c +3877 -0
- package/native/runtime_primitive.c +2244 -0
- package/native/runtime_process.c +720 -0
- package/native/runtime_promise.c +3354 -0
- package/native/runtime_property.c +791 -0
- package/native/runtime_proxy.c +1533 -0
- package/native/runtime_realm.c +411 -0
- package/native/runtime_reflect.c +624 -0
- package/native/runtime_regexp.c +3495 -0
- package/native/runtime_regexp_matcher.c +3535 -0
- package/native/runtime_regexp_symbol.c +1471 -0
- package/native/runtime_set.c +1751 -0
- package/native/runtime_string.c +2862 -0
- package/native/runtime_string_match.c +1161 -0
- package/native/runtime_symbol.c +986 -0
- package/native/runtime_typed_array.c +5113 -0
- package/native/runtime_unicode_tables.h +14647 -0
- package/native/runtime_uri.c +676 -0
- package/native/runtime_weak_collection.c +1275 -0
- package/package.json +38 -3
|
@@ -0,0 +1,2053 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
|
|
3
|
+
|
|
4
|
+
#include "runtime_internal.h"
|
|
5
|
+
|
|
6
|
+
#include <float.h>
|
|
7
|
+
#include <math.h>
|
|
8
|
+
#include <stdio.h>
|
|
9
|
+
#include <stdlib.h>
|
|
10
|
+
#include <string.h>
|
|
11
|
+
|
|
12
|
+
/* Number constructor, wrapper state, constants, and static operations. */
|
|
13
|
+
|
|
14
|
+
#define OSEO_MAX_SAFE_INTEGER 9007199254740991.0
|
|
15
|
+
|
|
16
|
+
static bool number_whitespace(uint16_t unit) {
|
|
17
|
+
return unit == UINT16_C(0x0009) || unit == UINT16_C(0x000a) ||
|
|
18
|
+
unit == UINT16_C(0x000b) || unit == UINT16_C(0x000c) ||
|
|
19
|
+
unit == UINT16_C(0x000d) || unit == UINT16_C(0x0020) ||
|
|
20
|
+
unit == UINT16_C(0x00a0) || unit == UINT16_C(0x1680) ||
|
|
21
|
+
(unit >= UINT16_C(0x2000) && unit <= UINT16_C(0x200a)) ||
|
|
22
|
+
unit == UINT16_C(0x2028) || unit == UINT16_C(0x2029) ||
|
|
23
|
+
unit == UINT16_C(0x202f) || unit == UINT16_C(0x205f) ||
|
|
24
|
+
unit == UINT16_C(0x3000) || unit == UINT16_C(0xfeff);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static int number_digit(uint16_t unit) {
|
|
28
|
+
if (unit >= UINT16_C('0') && unit <= UINT16_C('9')) {
|
|
29
|
+
return (int)(unit - UINT16_C('0'));
|
|
30
|
+
}
|
|
31
|
+
if (unit >= UINT16_C('a') && unit <= UINT16_C('z')) {
|
|
32
|
+
return (int)(unit - UINT16_C('a')) + 10;
|
|
33
|
+
}
|
|
34
|
+
if (unit >= UINT16_C('A') && unit <= UINT16_C('Z')) {
|
|
35
|
+
return (int)(unit - UINT16_C('A')) + 10;
|
|
36
|
+
}
|
|
37
|
+
return -1;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static OseoResult number_argument(
|
|
41
|
+
OseoContext *context,
|
|
42
|
+
size_t argument_count,
|
|
43
|
+
const OseoValue *arguments
|
|
44
|
+
) {
|
|
45
|
+
if (argument_count == 0u) return normal(oseo_number(0.0));
|
|
46
|
+
OseoResult result = oseo_to_numeric(context, arguments[0]);
|
|
47
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
48
|
+
if (is_bigint(result.value)) {
|
|
49
|
+
return normal(oseo_number(
|
|
50
|
+
oseo_internal_bigint_to_number(result.value)
|
|
51
|
+
));
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
* 21.1.1.1. The argument conversion runs first and
|
|
58
|
+
* OrdinaryCreateFromConstructor follows it, so a new target whose
|
|
59
|
+
* `prototype` is an accessor observes that order: a valueOf on the
|
|
60
|
+
* argument runs before the getter, and an abrupt getter reports after
|
|
61
|
+
* the conversion rather than before it. The read is a real Get because
|
|
62
|
+
* `Reflect.construct` can name a bound function, which owns no
|
|
63
|
+
* synthetic `prototype` slot to read.
|
|
64
|
+
*/
|
|
65
|
+
static OseoResult number_construct(
|
|
66
|
+
OseoContext *context,
|
|
67
|
+
OseoValue new_target,
|
|
68
|
+
size_t argument_count,
|
|
69
|
+
const OseoValue *arguments,
|
|
70
|
+
bool constructing
|
|
71
|
+
) {
|
|
72
|
+
/* Slots: 0 the new target, 1 the prototype the read produced. The
|
|
73
|
+
* argument conversion is a safepoint and can run arbitrary code, so
|
|
74
|
+
* the new target is rooted across it; the converted number stays a
|
|
75
|
+
* local because a Number is an immediate value. */
|
|
76
|
+
OseoValue slots[2] = {new_target, oseo_undefined()};
|
|
77
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
78
|
+
oseo_roots_push(context, &frame);
|
|
79
|
+
OseoResult result = number_argument(context, argument_count, arguments);
|
|
80
|
+
OseoValue data = result.value;
|
|
81
|
+
if (result.status == OSEO_STATUS_NORMAL && constructing) {
|
|
82
|
+
result = oseo_internal_constructor_prototype(context, slots[0]);
|
|
83
|
+
slots[1] = result.value;
|
|
84
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
|
|
85
|
+
result = oseo_internal_constructor_realm_default(
|
|
86
|
+
context,
|
|
87
|
+
slots[0],
|
|
88
|
+
OSEO_INTRINSIC_NUMBER_PROTOTYPE
|
|
89
|
+
);
|
|
90
|
+
slots[1] = result.value;
|
|
91
|
+
}
|
|
92
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
93
|
+
result = oseo_object_create(context, slots[1]);
|
|
94
|
+
}
|
|
95
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
96
|
+
OseoOrdinaryObject *object = ordinary_object(result.value);
|
|
97
|
+
object->number_data = true;
|
|
98
|
+
object->number_value = data;
|
|
99
|
+
object->primitive_data = true;
|
|
100
|
+
object->primitive_value = data;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
oseo_roots_pop(context, &frame);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static OseoResult number_is_finite(
|
|
108
|
+
size_t argument_count,
|
|
109
|
+
const OseoValue *arguments
|
|
110
|
+
) {
|
|
111
|
+
if (argument_count == 0u || !is_number(arguments[0])) {
|
|
112
|
+
return normal(oseo_boolean(false));
|
|
113
|
+
}
|
|
114
|
+
return normal(oseo_boolean(isfinite(number_value(arguments[0]))));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static OseoResult number_is_integer(
|
|
118
|
+
size_t argument_count,
|
|
119
|
+
const OseoValue *arguments,
|
|
120
|
+
bool safe
|
|
121
|
+
) {
|
|
122
|
+
if (argument_count == 0u || !is_number(arguments[0])) {
|
|
123
|
+
return normal(oseo_boolean(false));
|
|
124
|
+
}
|
|
125
|
+
double value = number_value(arguments[0]);
|
|
126
|
+
bool integer = isfinite(value) && trunc(value) == value;
|
|
127
|
+
if (safe) integer = integer && fabs(value) <= OSEO_MAX_SAFE_INTEGER;
|
|
128
|
+
return normal(oseo_boolean(integer));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static OseoResult number_is_nan(
|
|
132
|
+
size_t argument_count,
|
|
133
|
+
const OseoValue *arguments
|
|
134
|
+
) {
|
|
135
|
+
bool result = argument_count > 0u && is_number(arguments[0]) &&
|
|
136
|
+
isnan(number_value(arguments[0]));
|
|
137
|
+
return normal(oseo_boolean(result));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/*
|
|
141
|
+
* The global isFinite and isNaN functions, 19.2.2 and 19.2.3. Unlike the
|
|
142
|
+
* Number statics of the same names, each converts its operand with
|
|
143
|
+
* ToNumber first, so an object's ToPrimitive protocol runs and a Symbol
|
|
144
|
+
* or BigInt operand throws the TypeError that conversion raises.
|
|
145
|
+
*/
|
|
146
|
+
static OseoResult global_is_finite(
|
|
147
|
+
OseoContext *context,
|
|
148
|
+
size_t argument_count,
|
|
149
|
+
const OseoValue *arguments
|
|
150
|
+
) {
|
|
151
|
+
OseoResult converted = oseo_internal_to_number(
|
|
152
|
+
context,
|
|
153
|
+
argument_count == 0u ? oseo_undefined() : arguments[0]
|
|
154
|
+
);
|
|
155
|
+
if (converted.status != OSEO_STATUS_NORMAL) return converted;
|
|
156
|
+
return normal(oseo_boolean(isfinite(number_value(converted.value))));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static OseoResult global_is_nan(
|
|
160
|
+
OseoContext *context,
|
|
161
|
+
size_t argument_count,
|
|
162
|
+
const OseoValue *arguments
|
|
163
|
+
) {
|
|
164
|
+
OseoResult converted = oseo_internal_to_number(
|
|
165
|
+
context,
|
|
166
|
+
argument_count == 0u ? oseo_undefined() : arguments[0]
|
|
167
|
+
);
|
|
168
|
+
if (converted.status != OSEO_STATUS_NORMAL) return converted;
|
|
169
|
+
return normal(oseo_boolean(isnan(number_value(converted.value))));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static bool number_matches(
|
|
173
|
+
const OseoString *string,
|
|
174
|
+
size_t start,
|
|
175
|
+
const char *text
|
|
176
|
+
) {
|
|
177
|
+
size_t length = strlen(text);
|
|
178
|
+
if (start > string->length || length > string->length - start) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
182
|
+
if (string->units[start + index] !=
|
|
183
|
+
(uint16_t)(unsigned char)text[index]) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
static OseoResult number_parse_float(
|
|
191
|
+
OseoContext *context,
|
|
192
|
+
size_t argument_count,
|
|
193
|
+
const OseoValue *arguments
|
|
194
|
+
) {
|
|
195
|
+
OseoValue input = argument_count == 0u
|
|
196
|
+
? oseo_undefined()
|
|
197
|
+
: arguments[0];
|
|
198
|
+
OseoResult converted = oseo_internal_value_string(context, input);
|
|
199
|
+
if (converted.status != OSEO_STATUS_NORMAL) return converted;
|
|
200
|
+
const OseoString *string = string_object(converted.value);
|
|
201
|
+
size_t start = 0u;
|
|
202
|
+
while (start < string->length &&
|
|
203
|
+
number_whitespace(string->units[start])) {
|
|
204
|
+
start += 1u;
|
|
205
|
+
}
|
|
206
|
+
size_t cursor = start;
|
|
207
|
+
bool negative = false;
|
|
208
|
+
if (cursor < string->length &&
|
|
209
|
+
(string->units[cursor] == UINT16_C('+') ||
|
|
210
|
+
string->units[cursor] == UINT16_C('-'))) {
|
|
211
|
+
negative = string->units[cursor] == UINT16_C('-');
|
|
212
|
+
cursor += 1u;
|
|
213
|
+
}
|
|
214
|
+
if (number_matches(string, cursor, "Infinity")) {
|
|
215
|
+
return normal(oseo_number(negative ? -INFINITY : INFINITY));
|
|
216
|
+
}
|
|
217
|
+
size_t integer_start = cursor;
|
|
218
|
+
while (cursor < string->length &&
|
|
219
|
+
number_digit(string->units[cursor]) >= 0 &&
|
|
220
|
+
number_digit(string->units[cursor]) < 10) {
|
|
221
|
+
cursor += 1u;
|
|
222
|
+
}
|
|
223
|
+
bool has_digits = cursor > integer_start;
|
|
224
|
+
if (cursor < string->length &&
|
|
225
|
+
string->units[cursor] == UINT16_C('.')) {
|
|
226
|
+
cursor += 1u;
|
|
227
|
+
size_t fraction_start = cursor;
|
|
228
|
+
while (cursor < string->length &&
|
|
229
|
+
number_digit(string->units[cursor]) >= 0 &&
|
|
230
|
+
number_digit(string->units[cursor]) < 10) {
|
|
231
|
+
cursor += 1u;
|
|
232
|
+
}
|
|
233
|
+
has_digits = has_digits || cursor > fraction_start;
|
|
234
|
+
}
|
|
235
|
+
if (!has_digits) return normal(oseo_number(NAN));
|
|
236
|
+
size_t exponent_start = cursor;
|
|
237
|
+
if (cursor < string->length &&
|
|
238
|
+
(string->units[cursor] == UINT16_C('e') ||
|
|
239
|
+
string->units[cursor] == UINT16_C('E'))) {
|
|
240
|
+
cursor += 1u;
|
|
241
|
+
if (cursor < string->length &&
|
|
242
|
+
(string->units[cursor] == UINT16_C('+') ||
|
|
243
|
+
string->units[cursor] == UINT16_C('-'))) {
|
|
244
|
+
cursor += 1u;
|
|
245
|
+
}
|
|
246
|
+
size_t digits = cursor;
|
|
247
|
+
while (cursor < string->length &&
|
|
248
|
+
number_digit(string->units[cursor]) >= 0 &&
|
|
249
|
+
number_digit(string->units[cursor]) < 10) {
|
|
250
|
+
cursor += 1u;
|
|
251
|
+
}
|
|
252
|
+
if (cursor == digits) cursor = exponent_start;
|
|
253
|
+
}
|
|
254
|
+
size_t length = cursor - start;
|
|
255
|
+
char *text = malloc(length + 1u);
|
|
256
|
+
if (text == NULL) {
|
|
257
|
+
return failure(context, "OSEO2001", "Number parsing failed.");
|
|
258
|
+
}
|
|
259
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
260
|
+
text[index] = (char)string->units[start + index];
|
|
261
|
+
}
|
|
262
|
+
text[length] = '\0';
|
|
263
|
+
double value = strtod(text, NULL);
|
|
264
|
+
free(text);
|
|
265
|
+
return normal(oseo_number(value));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static int32_t number_to_int32(double value) {
|
|
269
|
+
if (!isfinite(value) || value == 0.0) return 0;
|
|
270
|
+
double modulo = fmod(trunc(value), 4294967296.0);
|
|
271
|
+
if (modulo < 0.0) modulo += 4294967296.0;
|
|
272
|
+
uint32_t bits = (uint32_t)modulo;
|
|
273
|
+
if (bits < UINT32_C(0x80000000)) return (int32_t)bits;
|
|
274
|
+
return (int32_t)((int64_t)bits - INT64_C(4294967296));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
static OseoResult number_parse_int(
|
|
278
|
+
OseoContext *context,
|
|
279
|
+
size_t argument_count,
|
|
280
|
+
const OseoValue *arguments
|
|
281
|
+
) {
|
|
282
|
+
OseoValue slots[2] = {
|
|
283
|
+
argument_count == 0u ? oseo_undefined() : arguments[0],
|
|
284
|
+
argument_count < 2u ? oseo_undefined() : arguments[1],
|
|
285
|
+
};
|
|
286
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
287
|
+
oseo_roots_push(context, &frame);
|
|
288
|
+
OseoResult result = oseo_internal_value_string(context, slots[0]);
|
|
289
|
+
slots[0] = result.value;
|
|
290
|
+
int32_t requested_radix = 0;
|
|
291
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
292
|
+
tag_of(slots[1]) != OSEO_TAG_UNDEFINED) {
|
|
293
|
+
result = oseo_internal_to_number(context, slots[1]);
|
|
294
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
295
|
+
requested_radix = number_to_int32(number_value(result.value));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
299
|
+
oseo_roots_pop(context, &frame);
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
const OseoString *string = string_object(slots[0]);
|
|
303
|
+
size_t cursor = 0u;
|
|
304
|
+
while (cursor < string->length &&
|
|
305
|
+
number_whitespace(string->units[cursor])) {
|
|
306
|
+
cursor += 1u;
|
|
307
|
+
}
|
|
308
|
+
bool negative = false;
|
|
309
|
+
if (cursor < string->length &&
|
|
310
|
+
(string->units[cursor] == UINT16_C('+') ||
|
|
311
|
+
string->units[cursor] == UINT16_C('-'))) {
|
|
312
|
+
negative = string->units[cursor] == UINT16_C('-');
|
|
313
|
+
cursor += 1u;
|
|
314
|
+
}
|
|
315
|
+
int radix = requested_radix;
|
|
316
|
+
bool strip_prefix = true;
|
|
317
|
+
if (radix != 0) {
|
|
318
|
+
if (radix < 2 || radix > 36) {
|
|
319
|
+
oseo_roots_pop(context, &frame);
|
|
320
|
+
return normal(oseo_number(NAN));
|
|
321
|
+
}
|
|
322
|
+
strip_prefix = radix == 16;
|
|
323
|
+
} else {
|
|
324
|
+
radix = 10;
|
|
325
|
+
}
|
|
326
|
+
if (strip_prefix && cursor + 1u < string->length &&
|
|
327
|
+
string->units[cursor] == UINT16_C('0') &&
|
|
328
|
+
(string->units[cursor + 1u] == UINT16_C('x') ||
|
|
329
|
+
string->units[cursor + 1u] == UINT16_C('X'))) {
|
|
330
|
+
cursor += 2u;
|
|
331
|
+
radix = 16;
|
|
332
|
+
}
|
|
333
|
+
size_t digits = 0u;
|
|
334
|
+
size_t digit_start = cursor;
|
|
335
|
+
while (cursor < string->length) {
|
|
336
|
+
int digit = number_digit(string->units[cursor]);
|
|
337
|
+
if (digit < 0 || digit >= radix) break;
|
|
338
|
+
digits += 1u;
|
|
339
|
+
cursor += 1u;
|
|
340
|
+
}
|
|
341
|
+
double value = oseo_internal_integer_digits_to_number(
|
|
342
|
+
&string->units[digit_start],
|
|
343
|
+
digits,
|
|
344
|
+
(uint32_t)radix
|
|
345
|
+
);
|
|
346
|
+
oseo_roots_pop(context, &frame);
|
|
347
|
+
if (digits == 0u) return normal(oseo_number(NAN));
|
|
348
|
+
return normal(oseo_number(negative ? -value : value));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/* ---- Exact rational arithmetic for Number.prototype formatting ---- */
|
|
352
|
+
|
|
353
|
+
/*
|
|
354
|
+
* A non-negative arbitrary-precision integer stored as decimal digits,
|
|
355
|
+
* least-significant digit first. Every formatting method below builds a
|
|
356
|
+
* numerator and denominator of this type from a double's exact mantissa
|
|
357
|
+
* and binary exponent, so toFixed, toExponential, and toPrecision round
|
|
358
|
+
* to the correctly-rounded decimal value instead of drifting through
|
|
359
|
+
* repeated floating-point arithmetic.
|
|
360
|
+
*/
|
|
361
|
+
typedef struct {
|
|
362
|
+
uint8_t *digits;
|
|
363
|
+
size_t length;
|
|
364
|
+
size_t capacity;
|
|
365
|
+
} NumberBignum;
|
|
366
|
+
|
|
367
|
+
static bool number_bignum_reserve(NumberBignum *bignum, size_t needed) {
|
|
368
|
+
if (needed <= bignum->capacity) return true;
|
|
369
|
+
size_t capacity = bignum->capacity == 0u ? 32u : bignum->capacity;
|
|
370
|
+
while (capacity < needed) capacity *= 2u;
|
|
371
|
+
uint8_t *digits = realloc(bignum->digits, capacity);
|
|
372
|
+
if (digits == NULL) return false;
|
|
373
|
+
bignum->digits = digits;
|
|
374
|
+
bignum->capacity = capacity;
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
static void number_bignum_release(NumberBignum *bignum) {
|
|
379
|
+
free(bignum->digits);
|
|
380
|
+
bignum->digits = NULL;
|
|
381
|
+
bignum->length = 0u;
|
|
382
|
+
bignum->capacity = 0u;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
static void number_bignum_trim(NumberBignum *bignum) {
|
|
386
|
+
while (bignum->length > 1u && bignum->digits[bignum->length - 1u] == 0u) {
|
|
387
|
+
bignum->length -= 1u;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
static bool number_bignum_from_u64(NumberBignum *bignum, uint64_t value) {
|
|
392
|
+
if (!number_bignum_reserve(bignum, 20u)) return false;
|
|
393
|
+
size_t length = 0u;
|
|
394
|
+
do {
|
|
395
|
+
bignum->digits[length] = (uint8_t)(value % 10u);
|
|
396
|
+
value /= 10u;
|
|
397
|
+
length += 1u;
|
|
398
|
+
} while (value != 0u);
|
|
399
|
+
bignum->length = length;
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
static bool number_bignum_copy(
|
|
404
|
+
NumberBignum *destination,
|
|
405
|
+
const NumberBignum *source
|
|
406
|
+
) {
|
|
407
|
+
if (!number_bignum_reserve(destination, source->length)) return false;
|
|
408
|
+
memcpy(destination->digits, source->digits, source->length);
|
|
409
|
+
destination->length = source->length;
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
static bool number_bignum_is_zero(const NumberBignum *bignum) {
|
|
414
|
+
return bignum->length == 1u && bignum->digits[0] == 0u;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
static int number_bignum_compare(
|
|
418
|
+
const NumberBignum *left,
|
|
419
|
+
const NumberBignum *right
|
|
420
|
+
) {
|
|
421
|
+
if (left->length != right->length) {
|
|
422
|
+
return left->length < right->length ? -1 : 1;
|
|
423
|
+
}
|
|
424
|
+
for (size_t step = left->length; step > 0u; step -= 1u) {
|
|
425
|
+
uint8_t a = left->digits[step - 1u];
|
|
426
|
+
uint8_t b = right->digits[step - 1u];
|
|
427
|
+
if (a != b) return a < b ? -1 : 1;
|
|
428
|
+
}
|
|
429
|
+
return 0;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
static bool number_bignum_multiply_small(
|
|
433
|
+
NumberBignum *bignum,
|
|
434
|
+
unsigned multiplier
|
|
435
|
+
) {
|
|
436
|
+
if (!number_bignum_reserve(bignum, bignum->length + 8u)) return false;
|
|
437
|
+
unsigned carry = 0u;
|
|
438
|
+
size_t length = bignum->length;
|
|
439
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
440
|
+
unsigned product = (unsigned)bignum->digits[index] * multiplier +
|
|
441
|
+
carry;
|
|
442
|
+
bignum->digits[index] = (uint8_t)(product % 10u);
|
|
443
|
+
carry = product / 10u;
|
|
444
|
+
}
|
|
445
|
+
while (carry != 0u) {
|
|
446
|
+
if (!number_bignum_reserve(bignum, length + 1u)) return false;
|
|
447
|
+
bignum->digits[length] = (uint8_t)(carry % 10u);
|
|
448
|
+
carry /= 10u;
|
|
449
|
+
length += 1u;
|
|
450
|
+
}
|
|
451
|
+
bignum->length = length;
|
|
452
|
+
number_bignum_trim(bignum);
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/* bignum *= multiplier, both non-negative. Schoolbook long multiply. */
|
|
457
|
+
static bool number_bignum_multiply_bignum(
|
|
458
|
+
NumberBignum *bignum,
|
|
459
|
+
const NumberBignum *multiplier
|
|
460
|
+
) {
|
|
461
|
+
if (number_bignum_is_zero(bignum) || number_bignum_is_zero(multiplier)) {
|
|
462
|
+
return number_bignum_from_u64(bignum, 0u);
|
|
463
|
+
}
|
|
464
|
+
size_t result_length = bignum->length + multiplier->length;
|
|
465
|
+
uint32_t *accumulator = calloc(result_length, sizeof(uint32_t));
|
|
466
|
+
if (accumulator == NULL) return false;
|
|
467
|
+
for (size_t i = 0u; i < bignum->length; i += 1u) {
|
|
468
|
+
unsigned carry = 0u;
|
|
469
|
+
for (size_t j = 0u; j < multiplier->length; j += 1u) {
|
|
470
|
+
uint32_t product = (uint32_t)bignum->digits[i] *
|
|
471
|
+
(uint32_t)multiplier->digits[j] +
|
|
472
|
+
accumulator[i + j] + carry;
|
|
473
|
+
accumulator[i + j] = product % 10u;
|
|
474
|
+
carry = product / 10u;
|
|
475
|
+
}
|
|
476
|
+
size_t index = i + multiplier->length;
|
|
477
|
+
while (carry != 0u) {
|
|
478
|
+
uint32_t sum = accumulator[index] + carry;
|
|
479
|
+
accumulator[index] = sum % 10u;
|
|
480
|
+
carry = sum / 10u;
|
|
481
|
+
index += 1u;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (!number_bignum_reserve(bignum, result_length)) {
|
|
485
|
+
free(accumulator);
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
for (size_t index = 0u; index < result_length; index += 1u) {
|
|
489
|
+
bignum->digits[index] = (uint8_t)accumulator[index];
|
|
490
|
+
}
|
|
491
|
+
bignum->length = result_length;
|
|
492
|
+
number_bignum_trim(bignum);
|
|
493
|
+
free(accumulator);
|
|
494
|
+
return true;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/* bignum += addend, both non-negative. */
|
|
498
|
+
static bool number_bignum_add(
|
|
499
|
+
NumberBignum *bignum,
|
|
500
|
+
const NumberBignum *addend
|
|
501
|
+
) {
|
|
502
|
+
size_t original_length = bignum->length;
|
|
503
|
+
size_t length = original_length > addend->length
|
|
504
|
+
? original_length
|
|
505
|
+
: addend->length;
|
|
506
|
+
if (!number_bignum_reserve(bignum, length + 1u)) return false;
|
|
507
|
+
unsigned carry = 0u;
|
|
508
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
509
|
+
unsigned a = index < original_length ? bignum->digits[index] : 0u;
|
|
510
|
+
unsigned b = index < addend->length ? addend->digits[index] : 0u;
|
|
511
|
+
unsigned sum = a + b + carry;
|
|
512
|
+
bignum->digits[index] = (uint8_t)(sum % 10u);
|
|
513
|
+
carry = sum / 10u;
|
|
514
|
+
}
|
|
515
|
+
if (carry != 0u) {
|
|
516
|
+
bignum->digits[length] = (uint8_t)carry;
|
|
517
|
+
length += 1u;
|
|
518
|
+
}
|
|
519
|
+
bignum->length = length;
|
|
520
|
+
number_bignum_trim(bignum);
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/* bignum -= subtrahend in place. Requires bignum >= subtrahend. */
|
|
525
|
+
static void number_bignum_subtract(
|
|
526
|
+
NumberBignum *bignum,
|
|
527
|
+
const NumberBignum *subtrahend
|
|
528
|
+
) {
|
|
529
|
+
int borrow = 0;
|
|
530
|
+
for (size_t index = 0u; index < bignum->length; index += 1u) {
|
|
531
|
+
int a = (int)bignum->digits[index];
|
|
532
|
+
int b = index < subtrahend->length
|
|
533
|
+
? (int)subtrahend->digits[index]
|
|
534
|
+
: 0;
|
|
535
|
+
int difference = a - b - borrow;
|
|
536
|
+
if (difference < 0) {
|
|
537
|
+
difference += 10;
|
|
538
|
+
borrow = 1;
|
|
539
|
+
} else {
|
|
540
|
+
borrow = 0;
|
|
541
|
+
}
|
|
542
|
+
bignum->digits[index] = (uint8_t)difference;
|
|
543
|
+
}
|
|
544
|
+
number_bignum_trim(bignum);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/*
|
|
548
|
+
* Divides bignum in place by a small divisor (2 to 36) and returns the
|
|
549
|
+
* remainder. This is ordinary base-10 long division by a small number, so
|
|
550
|
+
* it stays correct regardless of the divisor's relationship to the
|
|
551
|
+
* decimal storage base, which is what lets it extract a target-radix
|
|
552
|
+
* integer-part digit at a time.
|
|
553
|
+
*/
|
|
554
|
+
static unsigned number_bignum_divide_small(
|
|
555
|
+
NumberBignum *bignum,
|
|
556
|
+
unsigned divisor
|
|
557
|
+
) {
|
|
558
|
+
unsigned remainder = 0u;
|
|
559
|
+
for (size_t step = 0u; step < bignum->length; step += 1u) {
|
|
560
|
+
size_t index = bignum->length - 1u - step;
|
|
561
|
+
unsigned value = remainder * 10u + bignum->digits[index];
|
|
562
|
+
bignum->digits[index] = (uint8_t)(value / divisor);
|
|
563
|
+
remainder = value % divisor;
|
|
564
|
+
}
|
|
565
|
+
number_bignum_trim(bignum);
|
|
566
|
+
return remainder;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/*
|
|
570
|
+
* Computes quotient = floor(numerator / denominator) and remainder =
|
|
571
|
+
* numerator - quotient * denominator, by schoolbook long division with
|
|
572
|
+
* denominator's nine multiples precomputed once and reused for every
|
|
573
|
+
* quotient digit. denominator must be nonzero.
|
|
574
|
+
*/
|
|
575
|
+
static bool number_bignum_divmod(
|
|
576
|
+
const NumberBignum *numerator,
|
|
577
|
+
const NumberBignum *denominator,
|
|
578
|
+
NumberBignum *quotient,
|
|
579
|
+
NumberBignum *remainder
|
|
580
|
+
) {
|
|
581
|
+
NumberBignum multiples[10];
|
|
582
|
+
memset(multiples, 0, sizeof(multiples));
|
|
583
|
+
bool ok = number_bignum_from_u64(&multiples[0], 0u);
|
|
584
|
+
for (unsigned digit = 1u; ok && digit <= 9u; digit += 1u) {
|
|
585
|
+
ok = number_bignum_copy(&multiples[digit], &multiples[digit - 1u]) &&
|
|
586
|
+
number_bignum_add(&multiples[digit], denominator);
|
|
587
|
+
}
|
|
588
|
+
size_t capacity = numerator->length;
|
|
589
|
+
uint8_t *quotient_digits = ok ? malloc(capacity) : NULL;
|
|
590
|
+
if (ok && quotient_digits == NULL) ok = false;
|
|
591
|
+
if (ok) ok = number_bignum_from_u64(remainder, 0u);
|
|
592
|
+
for (size_t step = 0u; ok && step < numerator->length; step += 1u) {
|
|
593
|
+
size_t index = numerator->length - 1u - step;
|
|
594
|
+
NumberBignum single = {NULL, 0u, 0u};
|
|
595
|
+
ok = number_bignum_multiply_small(remainder, 10u) &&
|
|
596
|
+
number_bignum_from_u64(&single, numerator->digits[index]) &&
|
|
597
|
+
number_bignum_add(remainder, &single);
|
|
598
|
+
number_bignum_release(&single);
|
|
599
|
+
if (!ok) break;
|
|
600
|
+
unsigned chosen = 0u;
|
|
601
|
+
for (unsigned digit = 9u;; digit -= 1u) {
|
|
602
|
+
if (number_bignum_compare(&multiples[digit], remainder) <= 0) {
|
|
603
|
+
chosen = digit;
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
if (digit == 0u) break;
|
|
607
|
+
}
|
|
608
|
+
if (chosen != 0u) {
|
|
609
|
+
number_bignum_subtract(remainder, &multiples[chosen]);
|
|
610
|
+
}
|
|
611
|
+
quotient_digits[step] = (uint8_t)chosen;
|
|
612
|
+
}
|
|
613
|
+
for (unsigned digit = 0u; digit <= 9u; digit += 1u) {
|
|
614
|
+
number_bignum_release(&multiples[digit]);
|
|
615
|
+
}
|
|
616
|
+
if (ok) {
|
|
617
|
+
ok = number_bignum_reserve(quotient, capacity);
|
|
618
|
+
if (ok) {
|
|
619
|
+
for (size_t index = 0u; index < capacity; index += 1u) {
|
|
620
|
+
quotient->digits[index] =
|
|
621
|
+
quotient_digits[capacity - 1u - index];
|
|
622
|
+
}
|
|
623
|
+
quotient->length = capacity;
|
|
624
|
+
number_bignum_trim(quotient);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
free(quotient_digits);
|
|
628
|
+
return ok;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
static size_t number_bignum_digit_string(
|
|
632
|
+
const NumberBignum *bignum,
|
|
633
|
+
char *out
|
|
634
|
+
) {
|
|
635
|
+
for (size_t index = 0u; index < bignum->length; index += 1u) {
|
|
636
|
+
out[index] = (char)('0' + bignum->digits[bignum->length - 1u - index]);
|
|
637
|
+
}
|
|
638
|
+
return bignum->length;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/*
|
|
642
|
+
* Decomposes a finite value >= 0 into an exact mantissa and binary
|
|
643
|
+
* exponent: value == (double)mantissa * exp2(exponent). frexp/ldexp
|
|
644
|
+
* handle subnormals, so this stays exact at every magnitude a double can
|
|
645
|
+
* hold, including Number.MIN_VALUE.
|
|
646
|
+
*/
|
|
647
|
+
static uint64_t number_decompose(double value, int *exponent) {
|
|
648
|
+
if (value == 0.0) {
|
|
649
|
+
*exponent = 0;
|
|
650
|
+
return 0u;
|
|
651
|
+
}
|
|
652
|
+
int raw_exponent;
|
|
653
|
+
double fraction = frexp(value, &raw_exponent);
|
|
654
|
+
uint64_t mantissa = (uint64_t)ldexp(fraction, 53);
|
|
655
|
+
*exponent = raw_exponent - 53;
|
|
656
|
+
while (mantissa != 0u && (mantissa & 1u) == 0u) {
|
|
657
|
+
mantissa >>= 1;
|
|
658
|
+
*exponent += 1;
|
|
659
|
+
}
|
|
660
|
+
return mantissa;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/*
|
|
664
|
+
* Builds numerator/denominator bignums whose exact ratio is
|
|
665
|
+
* value * 10^decimal_shift, for a finite value >= 0. toFixed scales by
|
|
666
|
+
* 10^fractionDigits; toExponential and toPrecision scale by
|
|
667
|
+
* 10^(precision - 1 - exponent).
|
|
668
|
+
*/
|
|
669
|
+
static bool number_scaled_ratio(
|
|
670
|
+
double value,
|
|
671
|
+
int decimal_shift,
|
|
672
|
+
NumberBignum *numerator,
|
|
673
|
+
NumberBignum *denominator
|
|
674
|
+
) {
|
|
675
|
+
int exponent;
|
|
676
|
+
uint64_t mantissa = number_decompose(value, &exponent);
|
|
677
|
+
bool ok = number_bignum_from_u64(numerator, mantissa) &&
|
|
678
|
+
number_bignum_from_u64(denominator, 1u);
|
|
679
|
+
for (int step = 0; ok && step < exponent; step += 1) {
|
|
680
|
+
ok = number_bignum_multiply_small(numerator, 2u);
|
|
681
|
+
}
|
|
682
|
+
for (int step = 0; ok && step < -exponent; step += 1) {
|
|
683
|
+
ok = number_bignum_multiply_small(denominator, 2u);
|
|
684
|
+
}
|
|
685
|
+
for (int step = 0; ok && step < decimal_shift; step += 1) {
|
|
686
|
+
ok = number_bignum_multiply_small(numerator, 10u);
|
|
687
|
+
}
|
|
688
|
+
for (int step = 0; ok && step < -decimal_shift; step += 1) {
|
|
689
|
+
ok = number_bignum_multiply_small(denominator, 10u);
|
|
690
|
+
}
|
|
691
|
+
return ok;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/*
|
|
695
|
+
* Computes n = round(value * 10^decimal_shift) as an exact bignum, with
|
|
696
|
+
* ties broken away from zero, matching the toFixed, toExponential, and
|
|
697
|
+
* toPrecision rounding rule: "as close to zero as possible; if there are
|
|
698
|
+
* two such n, pick the larger n". value must be finite and non-negative.
|
|
699
|
+
*/
|
|
700
|
+
static bool number_round_scaled(
|
|
701
|
+
double value,
|
|
702
|
+
int decimal_shift,
|
|
703
|
+
NumberBignum *rounded
|
|
704
|
+
) {
|
|
705
|
+
NumberBignum numerator = {NULL, 0u, 0u};
|
|
706
|
+
NumberBignum denominator = {NULL, 0u, 0u};
|
|
707
|
+
NumberBignum quotient = {NULL, 0u, 0u};
|
|
708
|
+
NumberBignum remainder = {NULL, 0u, 0u};
|
|
709
|
+
bool ok = number_scaled_ratio(
|
|
710
|
+
value,
|
|
711
|
+
decimal_shift,
|
|
712
|
+
&numerator,
|
|
713
|
+
&denominator
|
|
714
|
+
);
|
|
715
|
+
if (ok) {
|
|
716
|
+
ok = number_bignum_divmod(
|
|
717
|
+
&numerator,
|
|
718
|
+
&denominator,
|
|
719
|
+
"ient,
|
|
720
|
+
&remainder
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
if (ok) {
|
|
724
|
+
NumberBignum doubled = {NULL, 0u, 0u};
|
|
725
|
+
ok = number_bignum_copy(&doubled, &remainder) &&
|
|
726
|
+
number_bignum_multiply_small(&doubled, 2u);
|
|
727
|
+
if (ok && number_bignum_compare(&doubled, &denominator) >= 0) {
|
|
728
|
+
NumberBignum one = {NULL, 0u, 0u};
|
|
729
|
+
ok = number_bignum_from_u64(&one, 1u) &&
|
|
730
|
+
number_bignum_add("ient, &one);
|
|
731
|
+
number_bignum_release(&one);
|
|
732
|
+
}
|
|
733
|
+
number_bignum_release(&doubled);
|
|
734
|
+
}
|
|
735
|
+
if (ok) ok = number_bignum_copy(rounded, "ient);
|
|
736
|
+
number_bignum_release(&numerator);
|
|
737
|
+
number_bignum_release(&denominator);
|
|
738
|
+
number_bignum_release("ient);
|
|
739
|
+
number_bignum_release(&remainder);
|
|
740
|
+
return ok;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
static bool number_bignum_pow10(NumberBignum *out, int exponent) {
|
|
744
|
+
bool ok = number_bignum_from_u64(out, 1u);
|
|
745
|
+
for (int step = 0; ok && step < exponent; step += 1) {
|
|
746
|
+
ok = number_bignum_multiply_small(out, 10u);
|
|
747
|
+
}
|
|
748
|
+
return ok;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/*
|
|
752
|
+
* Finds the unique integer e for which 10^e <= magnitude < 10^(e + 1),
|
|
753
|
+
* for a finite magnitude > 0. A floating-point log10 estimate only seeds
|
|
754
|
+
* the search: every candidate is confirmed by exact bignum cross-
|
|
755
|
+
* multiplication (10^e * denominator against numerator) rather than
|
|
756
|
+
* trusted, because log10 can read on the wrong side of a power-of-ten
|
|
757
|
+
* boundary by a margin far smaller than double precision resolves.
|
|
758
|
+
*/
|
|
759
|
+
static bool number_exact_decimal_exponent(double magnitude, int *out_e) {
|
|
760
|
+
NumberBignum numerator = {NULL, 0u, 0u};
|
|
761
|
+
NumberBignum denominator = {NULL, 0u, 0u};
|
|
762
|
+
bool ok = number_scaled_ratio(magnitude, 0, &numerator, &denominator);
|
|
763
|
+
int e = (int)floor(log10(magnitude));
|
|
764
|
+
for (int attempt = 0; ok && attempt < 64; attempt += 1) {
|
|
765
|
+
/*
|
|
766
|
+
* Test 10^e <= numerator / denominator < 10^(e + 1) without
|
|
767
|
+
* division. A non-negative e scales the two bounds by the
|
|
768
|
+
* denominator; a negative one instead scales the numerator by
|
|
769
|
+
* 10^-e, which turns the same test into
|
|
770
|
+
* denominator <= numerator * 10^-e < denominator * 10.
|
|
771
|
+
*/
|
|
772
|
+
NumberBignum low = {NULL, 0u, 0u};
|
|
773
|
+
NumberBignum high = {NULL, 0u, 0u};
|
|
774
|
+
NumberBignum scaled = {NULL, 0u, 0u};
|
|
775
|
+
if (e >= 0) {
|
|
776
|
+
ok = number_bignum_pow10(&low, e) &&
|
|
777
|
+
number_bignum_multiply_bignum(&low, &denominator) &&
|
|
778
|
+
number_bignum_pow10(&high, e + 1) &&
|
|
779
|
+
number_bignum_multiply_bignum(&high, &denominator) &&
|
|
780
|
+
number_bignum_copy(&scaled, &numerator);
|
|
781
|
+
} else {
|
|
782
|
+
ok = number_bignum_copy(&low, &denominator) &&
|
|
783
|
+
number_bignum_copy(&high, &denominator) &&
|
|
784
|
+
number_bignum_multiply_small(&high, 10u) &&
|
|
785
|
+
number_bignum_pow10(&scaled, -e) &&
|
|
786
|
+
number_bignum_multiply_bignum(&scaled, &numerator);
|
|
787
|
+
}
|
|
788
|
+
bool too_low = ok && number_bignum_compare(&scaled, &low) < 0;
|
|
789
|
+
bool too_high = ok && !too_low &&
|
|
790
|
+
number_bignum_compare(&scaled, &high) >= 0;
|
|
791
|
+
number_bignum_release(&low);
|
|
792
|
+
number_bignum_release(&high);
|
|
793
|
+
number_bignum_release(&scaled);
|
|
794
|
+
if (!ok) break;
|
|
795
|
+
if (too_low) {
|
|
796
|
+
e -= 1;
|
|
797
|
+
} else if (too_high) {
|
|
798
|
+
e += 1;
|
|
799
|
+
} else {
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
number_bignum_release(&numerator);
|
|
804
|
+
number_bignum_release(&denominator);
|
|
805
|
+
*out_e = e;
|
|
806
|
+
return ok;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
/*
|
|
810
|
+
* Computes p significant decimal digits of a positive, finite magnitude
|
|
811
|
+
* and the decimal exponent e of its leading digit, such that
|
|
812
|
+
* digit_buffer[0..p) interpreted as an integer, times 10^(e - p + 1), is
|
|
813
|
+
* the correctly-rounded value. Shared by toExponential's fixed-precision
|
|
814
|
+
* branch and toPrecision. e is found exactly first (never guessed from a
|
|
815
|
+
* digit count after rounding, which a power-of-ten boundary can make
|
|
816
|
+
* self-consistently wrong on either side); the one remaining case is
|
|
817
|
+
* rounding carrying p nines up to a leading 1 followed by zeros, which
|
|
818
|
+
* simply moves e up by one and truncates the known result.
|
|
819
|
+
*/
|
|
820
|
+
static bool number_significant_digits(
|
|
821
|
+
double magnitude,
|
|
822
|
+
int p,
|
|
823
|
+
char digit_buffer[128],
|
|
824
|
+
int *out_e
|
|
825
|
+
) {
|
|
826
|
+
int e;
|
|
827
|
+
if (!number_exact_decimal_exponent(magnitude, &e)) return false;
|
|
828
|
+
NumberBignum rounded = {NULL, 0u, 0u};
|
|
829
|
+
bool ok = number_round_scaled(magnitude, p - 1 - e, &rounded);
|
|
830
|
+
size_t count = ok ? number_bignum_digit_string(&rounded, digit_buffer) : 0u;
|
|
831
|
+
number_bignum_release(&rounded);
|
|
832
|
+
if (!ok) return false;
|
|
833
|
+
if ((int)count == p + 1) {
|
|
834
|
+
e += 1;
|
|
835
|
+
digit_buffer[0] = '1';
|
|
836
|
+
for (int index = 1; index < p; index += 1) digit_buffer[index] = '0';
|
|
837
|
+
count = (size_t)p;
|
|
838
|
+
}
|
|
839
|
+
*out_e = e;
|
|
840
|
+
return count == (size_t)p;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
static const char number_radix_digit_glyphs[37] =
|
|
844
|
+
"0123456789abcdefghijklmnopqrstuvwxyz";
|
|
845
|
+
|
|
846
|
+
/*
|
|
847
|
+
* Formats a finite, non-negative value in the given radix (2 to 36), the
|
|
848
|
+
* way Number::toString's specification note permits its decimal algorithm
|
|
849
|
+
* to generalize. The integer part is always exact. The fractional part
|
|
850
|
+
* emits only the digits the double actually carries: the remaining tail
|
|
851
|
+
* advances alongside the distance to the rounding boundary, and the loop
|
|
852
|
+
* stops once the tail no longer exceeds it, raising the final digit when
|
|
853
|
+
* ordinary rounding selects it and the raised value still names this same
|
|
854
|
+
* double. Every quantity is an exact bignum rational over one shared
|
|
855
|
+
* power-of-two denominator, so a radix whose expansion never terminates
|
|
856
|
+
* stops at a digit string that reads back as this double rather than
|
|
857
|
+
* running to the buffer limit.
|
|
858
|
+
*
|
|
859
|
+
* This is the round-trip stopping rule, not a shortest-string search: a
|
|
860
|
+
* shorter string that also reads back can exist when ordinary rounding
|
|
861
|
+
* does not select the raised digit, and the tie test looks at the final
|
|
862
|
+
* digit rather than the parity of the whole coefficient, which are the
|
|
863
|
+
* same only in an even radix. ECMA-262 leaves a non-decimal radix
|
|
864
|
+
* implementation-approximated, and both choices match the decimal
|
|
865
|
+
* algorithm this generalizes.
|
|
866
|
+
*/
|
|
867
|
+
static bool number_radix_text(
|
|
868
|
+
double magnitude,
|
|
869
|
+
unsigned radix,
|
|
870
|
+
char *output,
|
|
871
|
+
size_t capacity,
|
|
872
|
+
size_t *length
|
|
873
|
+
) {
|
|
874
|
+
int exponent;
|
|
875
|
+
uint64_t mantissa = number_decompose(magnitude, &exponent);
|
|
876
|
+
/*
|
|
877
|
+
* The digits may stop as soon as they name this double rather than a
|
|
878
|
+
* neighbor, so the budget is the distance to the rounding boundary on
|
|
879
|
+
* each side: half the gap to the next double above, and half the gap
|
|
880
|
+
* to the one below. The two differ, because a power of two sits twice
|
|
881
|
+
* as far from its upper neighbor as from its lower one, so one shared
|
|
882
|
+
* budget would let a truncation cross the lower boundary. Truncating
|
|
883
|
+
* spends the lower budget and rounding up spends the upper one.
|
|
884
|
+
*
|
|
885
|
+
* Both gaps are exact powers of two, and only their exponents are
|
|
886
|
+
* used, so halving them here cannot underflow the way the value
|
|
887
|
+
* itself would at the smallest denormal. The gap above overflows to
|
|
888
|
+
* infinity only at the largest finite value, whose neighbor below is
|
|
889
|
+
* the same distance away.
|
|
890
|
+
*/
|
|
891
|
+
double upper_gap = nextafter(magnitude, INFINITY) - magnitude;
|
|
892
|
+
double lower_gap = magnitude - nextafter(magnitude, 0.0);
|
|
893
|
+
if (!isfinite(upper_gap) || !(upper_gap > 0.0)) upper_gap = lower_gap;
|
|
894
|
+
if (!(lower_gap > 0.0)) lower_gap = upper_gap;
|
|
895
|
+
int upper_exponent = 0;
|
|
896
|
+
int lower_exponent = 0;
|
|
897
|
+
uint64_t upper_mantissa = number_decompose(upper_gap, &upper_exponent);
|
|
898
|
+
uint64_t lower_mantissa = number_decompose(lower_gap, &lower_exponent);
|
|
899
|
+
upper_exponent -= 1;
|
|
900
|
+
lower_exponent -= 1;
|
|
901
|
+
int fraction_bits = exponent < 0 ? -exponent : 0;
|
|
902
|
+
int shared_bits = fraction_bits;
|
|
903
|
+
if (upper_exponent < 0 && -upper_exponent > shared_bits) {
|
|
904
|
+
shared_bits = -upper_exponent;
|
|
905
|
+
}
|
|
906
|
+
if (lower_exponent < 0 && -lower_exponent > shared_bits) {
|
|
907
|
+
shared_bits = -lower_exponent;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
NumberBignum integer_value = {NULL, 0u, 0u};
|
|
911
|
+
NumberBignum fraction = {NULL, 0u, 0u};
|
|
912
|
+
NumberBignum upper_budget = {NULL, 0u, 0u};
|
|
913
|
+
NumberBignum lower_budget = {NULL, 0u, 0u};
|
|
914
|
+
NumberBignum denominator = {NULL, 0u, 0u};
|
|
915
|
+
bool ok = number_bignum_from_u64(&integer_value, mantissa) &&
|
|
916
|
+
number_bignum_from_u64(&fraction, 0u) &&
|
|
917
|
+
number_bignum_from_u64(&denominator, 1u);
|
|
918
|
+
for (int step = 0; ok && step < shared_bits; step += 1) {
|
|
919
|
+
ok = number_bignum_multiply_small(&denominator, 2u);
|
|
920
|
+
}
|
|
921
|
+
if (ok && exponent >= 0) {
|
|
922
|
+
for (int step = 0; ok && step < exponent; step += 1) {
|
|
923
|
+
ok = number_bignum_multiply_small(&integer_value, 2u);
|
|
924
|
+
}
|
|
925
|
+
} else if (ok) {
|
|
926
|
+
NumberBignum split = {NULL, 0u, 0u};
|
|
927
|
+
NumberBignum quotient = {NULL, 0u, 0u};
|
|
928
|
+
ok = number_bignum_from_u64(&split, 1u);
|
|
929
|
+
for (int step = 0; ok && step < fraction_bits; step += 1) {
|
|
930
|
+
ok = number_bignum_multiply_small(&split, 2u);
|
|
931
|
+
}
|
|
932
|
+
if (ok) {
|
|
933
|
+
ok = number_bignum_divmod(
|
|
934
|
+
&integer_value,
|
|
935
|
+
&split,
|
|
936
|
+
"ient,
|
|
937
|
+
&fraction
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
if (ok) ok = number_bignum_copy(&integer_value, "ient);
|
|
941
|
+
/* Restate the fraction over the shared denominator. */
|
|
942
|
+
for (int step = 0; ok && step < shared_bits - fraction_bits;
|
|
943
|
+
step += 1) {
|
|
944
|
+
ok = number_bignum_multiply_small(&fraction, 2u);
|
|
945
|
+
}
|
|
946
|
+
number_bignum_release(&split);
|
|
947
|
+
number_bignum_release("ient);
|
|
948
|
+
}
|
|
949
|
+
if (ok) ok = number_bignum_from_u64(&upper_budget, upper_mantissa);
|
|
950
|
+
for (int step = 0; ok && step < shared_bits + upper_exponent; step += 1) {
|
|
951
|
+
ok = number_bignum_multiply_small(&upper_budget, 2u);
|
|
952
|
+
}
|
|
953
|
+
if (ok) ok = number_bignum_from_u64(&lower_budget, lower_mantissa);
|
|
954
|
+
for (int step = 0; ok && step < shared_bits + lower_exponent; step += 1) {
|
|
955
|
+
ok = number_bignum_multiply_small(&lower_budget, 2u);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
char fraction_digits[1200];
|
|
959
|
+
size_t fraction_digit_count = 0u;
|
|
960
|
+
bool carry = false;
|
|
961
|
+
if (ok && !number_bignum_is_zero(&fraction)) {
|
|
962
|
+
NumberBignum multiples[36];
|
|
963
|
+
memset(multiples, 0, sizeof(multiples));
|
|
964
|
+
for (unsigned digit = 0u; ok && digit < radix; digit += 1u) {
|
|
965
|
+
if (digit == 0u) {
|
|
966
|
+
ok = number_bignum_from_u64(&multiples[0], 0u);
|
|
967
|
+
} else {
|
|
968
|
+
ok = number_bignum_copy(
|
|
969
|
+
&multiples[digit],
|
|
970
|
+
&multiples[digit - 1u]
|
|
971
|
+
) &&
|
|
972
|
+
number_bignum_add(&multiples[digit], &denominator);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
while (ok) {
|
|
976
|
+
if (fraction_digit_count >= sizeof(fraction_digits)) {
|
|
977
|
+
ok = false;
|
|
978
|
+
break;
|
|
979
|
+
}
|
|
980
|
+
ok = number_bignum_multiply_small(&fraction, radix) &&
|
|
981
|
+
number_bignum_multiply_small(&upper_budget, radix) &&
|
|
982
|
+
number_bignum_multiply_small(&lower_budget, radix);
|
|
983
|
+
if (!ok) break;
|
|
984
|
+
unsigned digit = 0u;
|
|
985
|
+
for (unsigned candidate = radix - 1u;; candidate -= 1u) {
|
|
986
|
+
if (number_bignum_compare(
|
|
987
|
+
&multiples[candidate],
|
|
988
|
+
&fraction
|
|
989
|
+
) <= 0) {
|
|
990
|
+
digit = candidate;
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
if (candidate == 0u) break;
|
|
994
|
+
}
|
|
995
|
+
if (digit != 0u) {
|
|
996
|
+
number_bignum_subtract(&fraction, &multiples[digit]);
|
|
997
|
+
}
|
|
998
|
+
fraction_digits[fraction_digit_count] =
|
|
999
|
+
number_radix_digit_glyphs[digit];
|
|
1000
|
+
fraction_digit_count += 1u;
|
|
1001
|
+
NumberBignum twice = {NULL, 0u, 0u};
|
|
1002
|
+
ok = number_bignum_copy(&twice, &fraction) &&
|
|
1003
|
+
number_bignum_multiply_small(&twice, 2u);
|
|
1004
|
+
int against_half = ok
|
|
1005
|
+
? number_bignum_compare(&twice, &denominator)
|
|
1006
|
+
: 0;
|
|
1007
|
+
number_bignum_release(&twice);
|
|
1008
|
+
if (!ok) break;
|
|
1009
|
+
if (against_half > 0 ||
|
|
1010
|
+
(against_half == 0 && (digit & 1u) != 0u)) {
|
|
1011
|
+
NumberBignum raised = {NULL, 0u, 0u};
|
|
1012
|
+
ok = number_bignum_copy(&raised, &fraction) &&
|
|
1013
|
+
number_bignum_add(&raised, &upper_budget);
|
|
1014
|
+
bool inside = ok &&
|
|
1015
|
+
number_bignum_compare(&raised, &denominator) > 0;
|
|
1016
|
+
number_bignum_release(&raised);
|
|
1017
|
+
if (!ok) break;
|
|
1018
|
+
if (inside) {
|
|
1019
|
+
/* Round up, carrying through the emitted digits and,
|
|
1020
|
+
* if every one of them overflows, into the integer. */
|
|
1021
|
+
carry = true;
|
|
1022
|
+
while (fraction_digit_count > 0u) {
|
|
1023
|
+
char glyph =
|
|
1024
|
+
fraction_digits[fraction_digit_count - 1u];
|
|
1025
|
+
unsigned value = glyph <= '9'
|
|
1026
|
+
? (unsigned)(glyph - '0')
|
|
1027
|
+
: (unsigned)(glyph - 'a') + 10u;
|
|
1028
|
+
if (value + 1u < radix) {
|
|
1029
|
+
fraction_digits[fraction_digit_count - 1u] =
|
|
1030
|
+
number_radix_digit_glyphs[value + 1u];
|
|
1031
|
+
carry = false;
|
|
1032
|
+
break;
|
|
1033
|
+
}
|
|
1034
|
+
fraction_digit_count -= 1u;
|
|
1035
|
+
}
|
|
1036
|
+
break;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
if (number_bignum_compare(&fraction, &lower_budget) < 0) break;
|
|
1040
|
+
}
|
|
1041
|
+
for (unsigned digit = 0u; digit < radix; digit += 1u) {
|
|
1042
|
+
number_bignum_release(&multiples[digit]);
|
|
1043
|
+
}
|
|
1044
|
+
while (fraction_digit_count > 0u &&
|
|
1045
|
+
fraction_digits[fraction_digit_count - 1u] == '0') {
|
|
1046
|
+
fraction_digit_count -= 1u;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (ok && carry) {
|
|
1050
|
+
NumberBignum one = {NULL, 0u, 0u};
|
|
1051
|
+
ok = number_bignum_from_u64(&one, 1u) &&
|
|
1052
|
+
number_bignum_add(&integer_value, &one);
|
|
1053
|
+
number_bignum_release(&one);
|
|
1054
|
+
}
|
|
1055
|
+
char integer_digits[1200];
|
|
1056
|
+
size_t integer_digit_count = 0u;
|
|
1057
|
+
if (ok && number_bignum_is_zero(&integer_value)) {
|
|
1058
|
+
integer_digits[0] = '0';
|
|
1059
|
+
integer_digit_count = 1u;
|
|
1060
|
+
}
|
|
1061
|
+
while (ok && !number_bignum_is_zero(&integer_value)) {
|
|
1062
|
+
if (integer_digit_count >= sizeof(integer_digits)) {
|
|
1063
|
+
ok = false;
|
|
1064
|
+
break;
|
|
1065
|
+
}
|
|
1066
|
+
unsigned digit = number_bignum_divide_small(&integer_value, radix);
|
|
1067
|
+
integer_digits[integer_digit_count] =
|
|
1068
|
+
number_radix_digit_glyphs[digit];
|
|
1069
|
+
integer_digit_count += 1u;
|
|
1070
|
+
}
|
|
1071
|
+
number_bignum_release(&integer_value);
|
|
1072
|
+
number_bignum_release(&fraction);
|
|
1073
|
+
number_bignum_release(&upper_budget);
|
|
1074
|
+
number_bignum_release(&lower_budget);
|
|
1075
|
+
number_bignum_release(&denominator);
|
|
1076
|
+
if (!ok) return false;
|
|
1077
|
+
size_t total = integer_digit_count +
|
|
1078
|
+
(fraction_digit_count > 0u ? 1u + fraction_digit_count : 0u);
|
|
1079
|
+
if (total >= capacity) return false;
|
|
1080
|
+
size_t position = 0u;
|
|
1081
|
+
for (size_t index = 0u; index < integer_digit_count; index += 1u) {
|
|
1082
|
+
output[position] = integer_digits[integer_digit_count - 1u - index];
|
|
1083
|
+
position += 1u;
|
|
1084
|
+
}
|
|
1085
|
+
if (fraction_digit_count > 0u) {
|
|
1086
|
+
output[position] = '.';
|
|
1087
|
+
position += 1u;
|
|
1088
|
+
memcpy(output + position, fraction_digits, fraction_digit_count);
|
|
1089
|
+
position += fraction_digit_count;
|
|
1090
|
+
}
|
|
1091
|
+
*length = position;
|
|
1092
|
+
return true;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
/* Assembles "d[.ddd]e[+-]EE" from already-computed significant digits. */
|
|
1096
|
+
static bool number_format_exponential_digits(
|
|
1097
|
+
const char *digit_buffer,
|
|
1098
|
+
size_t digit_count,
|
|
1099
|
+
int e,
|
|
1100
|
+
char *text,
|
|
1101
|
+
size_t capacity,
|
|
1102
|
+
size_t *length
|
|
1103
|
+
) {
|
|
1104
|
+
size_t position = 0u;
|
|
1105
|
+
if (capacity < 1u) return false;
|
|
1106
|
+
text[position] = digit_buffer[0];
|
|
1107
|
+
position += 1u;
|
|
1108
|
+
if (digit_count > 1u) {
|
|
1109
|
+
if (position + 1u + (digit_count - 1u) > capacity) return false;
|
|
1110
|
+
text[position] = '.';
|
|
1111
|
+
position += 1u;
|
|
1112
|
+
memcpy(text + position, digit_buffer + 1u, digit_count - 1u);
|
|
1113
|
+
position += digit_count - 1u;
|
|
1114
|
+
}
|
|
1115
|
+
if (position + 2u > capacity) return false;
|
|
1116
|
+
text[position] = 'e';
|
|
1117
|
+
position += 1u;
|
|
1118
|
+
text[position] = e < 0 ? '-' : '+';
|
|
1119
|
+
position += 1u;
|
|
1120
|
+
char exponent_text[16];
|
|
1121
|
+
int written = snprintf(
|
|
1122
|
+
exponent_text,
|
|
1123
|
+
sizeof(exponent_text),
|
|
1124
|
+
"%d",
|
|
1125
|
+
e < 0 ? -e : e
|
|
1126
|
+
);
|
|
1127
|
+
if (written < 0 || position + (size_t)written > capacity) return false;
|
|
1128
|
+
memcpy(text + position, exponent_text, (size_t)written);
|
|
1129
|
+
position += (size_t)written;
|
|
1130
|
+
*length = position;
|
|
1131
|
+
return true;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/*
|
|
1135
|
+
* Computes the exponential-notation body (without sign) for toExponential:
|
|
1136
|
+
* value 0 formats as fractionDigits (or one) zero digits with exponent 0;
|
|
1137
|
+
* a defined fractionDigits rounds to f + 1 significant digits; an
|
|
1138
|
+
* undefined fractionDigits reuses the shortest round-trip decimal digits,
|
|
1139
|
+
* matching the "as small a p as possible" free-precision rule.
|
|
1140
|
+
*/
|
|
1141
|
+
static bool number_format_exponential(
|
|
1142
|
+
double magnitude,
|
|
1143
|
+
bool fraction_digits_defined,
|
|
1144
|
+
int f,
|
|
1145
|
+
char *text,
|
|
1146
|
+
size_t capacity,
|
|
1147
|
+
size_t *length
|
|
1148
|
+
) {
|
|
1149
|
+
char digit_buffer[128];
|
|
1150
|
+
size_t digit_count;
|
|
1151
|
+
int e;
|
|
1152
|
+
if (magnitude == 0.0) {
|
|
1153
|
+
digit_count = fraction_digits_defined ? (size_t)f + 1u : 1u;
|
|
1154
|
+
for (size_t index = 0u; index < digit_count; index += 1u) {
|
|
1155
|
+
digit_buffer[index] = '0';
|
|
1156
|
+
}
|
|
1157
|
+
e = 0;
|
|
1158
|
+
} else if (fraction_digits_defined) {
|
|
1159
|
+
int p = f + 1;
|
|
1160
|
+
if (!number_significant_digits(magnitude, p, digit_buffer, &e)) {
|
|
1161
|
+
return false;
|
|
1162
|
+
}
|
|
1163
|
+
digit_count = (size_t)p;
|
|
1164
|
+
} else {
|
|
1165
|
+
char shortest[18];
|
|
1166
|
+
size_t count;
|
|
1167
|
+
int exponent;
|
|
1168
|
+
oseo_internal_number_shortest_digits(
|
|
1169
|
+
magnitude,
|
|
1170
|
+
shortest,
|
|
1171
|
+
&count,
|
|
1172
|
+
&exponent
|
|
1173
|
+
);
|
|
1174
|
+
memcpy(digit_buffer, shortest, count);
|
|
1175
|
+
digit_count = count;
|
|
1176
|
+
e = exponent - 1;
|
|
1177
|
+
}
|
|
1178
|
+
return number_format_exponential_digits(
|
|
1179
|
+
digit_buffer,
|
|
1180
|
+
digit_count,
|
|
1181
|
+
e,
|
|
1182
|
+
text,
|
|
1183
|
+
capacity,
|
|
1184
|
+
length
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
static OseoResult number_allocate_ascii(
|
|
1189
|
+
OseoContext *context,
|
|
1190
|
+
const char *text,
|
|
1191
|
+
size_t length
|
|
1192
|
+
) {
|
|
1193
|
+
uint16_t units[2400];
|
|
1194
|
+
if (length > sizeof(units) / sizeof(*units)) {
|
|
1195
|
+
return failure(context, "OSEO2001", "Number formatting overflowed.");
|
|
1196
|
+
}
|
|
1197
|
+
for (size_t index = 0u; index < length; index += 1u) {
|
|
1198
|
+
units[index] = (uint16_t)(unsigned char)text[index];
|
|
1199
|
+
}
|
|
1200
|
+
return oseo_internal_allocate_string(context, units, length);
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
/* thisNumberValue (21.1.3), the [[NumberData]] receiver brand check
|
|
1204
|
+
* shared by valueOf and every formatting method below. */
|
|
1205
|
+
static OseoResult number_this_value(OseoContext *context, OseoValue receiver) {
|
|
1206
|
+
if (is_number(receiver)) return normal(receiver);
|
|
1207
|
+
if (is_object(receiver) && ordinary_object(receiver)->number_data) {
|
|
1208
|
+
return normal(ordinary_object(receiver)->number_value);
|
|
1209
|
+
}
|
|
1210
|
+
return oseo_internal_throw_error(
|
|
1211
|
+
context,
|
|
1212
|
+
OSEO_ERROR_TYPE,
|
|
1213
|
+
"Number.prototype method requires a Number receiver."
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
/* ToIntegerOrInfinity over one already rooted input value. */
|
|
1218
|
+
static OseoResult number_integer_or_infinity(
|
|
1219
|
+
OseoContext *context,
|
|
1220
|
+
OseoValue value
|
|
1221
|
+
) {
|
|
1222
|
+
OseoResult result = oseo_internal_to_number(context, value);
|
|
1223
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1224
|
+
double number = number_value(result.value);
|
|
1225
|
+
if (isnan(number) || number == 0.0) number = 0.0;
|
|
1226
|
+
else if (isfinite(number)) number = trunc(number);
|
|
1227
|
+
return normal(oseo_number(number));
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
/* Number.prototype.toString ( [ radix ] ), 21.1.3.6. */
|
|
1231
|
+
static OseoResult number_to_string(
|
|
1232
|
+
OseoContext *context,
|
|
1233
|
+
OseoValue receiver,
|
|
1234
|
+
size_t argument_count,
|
|
1235
|
+
const OseoValue *arguments
|
|
1236
|
+
) {
|
|
1237
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1238
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1239
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1240
|
+
frame.slots[0] = receiver;
|
|
1241
|
+
result = number_this_value(context, frame.slots[0]);
|
|
1242
|
+
frame.slots[1] = result.value;
|
|
1243
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1244
|
+
oseo_roots_release(context, &frame);
|
|
1245
|
+
return result;
|
|
1246
|
+
}
|
|
1247
|
+
double x = number_value(frame.slots[1]);
|
|
1248
|
+
OseoValue radix_argument = argument_count > 0u
|
|
1249
|
+
? arguments[0]
|
|
1250
|
+
: oseo_undefined();
|
|
1251
|
+
int radix = 10;
|
|
1252
|
+
if (tag_of(radix_argument) != OSEO_TAG_UNDEFINED) {
|
|
1253
|
+
result = number_integer_or_infinity(context, radix_argument);
|
|
1254
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1255
|
+
oseo_roots_release(context, &frame);
|
|
1256
|
+
return result;
|
|
1257
|
+
}
|
|
1258
|
+
double radix_value = number_value(result.value);
|
|
1259
|
+
if (!(radix_value >= 2.0 && radix_value <= 36.0)) {
|
|
1260
|
+
oseo_roots_release(context, &frame);
|
|
1261
|
+
return oseo_internal_throw_error(
|
|
1262
|
+
context,
|
|
1263
|
+
OSEO_ERROR_RANGE,
|
|
1264
|
+
"Number.prototype.toString radix must be within 2..36."
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
radix = (int)radix_value;
|
|
1268
|
+
}
|
|
1269
|
+
oseo_roots_release(context, &frame);
|
|
1270
|
+
char text[2400];
|
|
1271
|
+
if (radix == 10) {
|
|
1272
|
+
(void)oseo_internal_number_text(x, text, sizeof(text));
|
|
1273
|
+
return number_allocate_ascii(context, text, strlen(text));
|
|
1274
|
+
}
|
|
1275
|
+
if (isnan(x)) return number_allocate_ascii(context, "NaN", 3u);
|
|
1276
|
+
if (isinf(x)) {
|
|
1277
|
+
const char *infinity = signbit(x) ? "-Infinity" : "Infinity";
|
|
1278
|
+
return number_allocate_ascii(context, infinity, strlen(infinity));
|
|
1279
|
+
}
|
|
1280
|
+
bool negative = signbit(x) && x != 0.0;
|
|
1281
|
+
double magnitude = negative ? -x : x;
|
|
1282
|
+
size_t position = 0u;
|
|
1283
|
+
if (negative) {
|
|
1284
|
+
text[position] = '-';
|
|
1285
|
+
position += 1u;
|
|
1286
|
+
}
|
|
1287
|
+
size_t length = 0u;
|
|
1288
|
+
if (!number_radix_text(
|
|
1289
|
+
magnitude,
|
|
1290
|
+
(unsigned)radix,
|
|
1291
|
+
text + position,
|
|
1292
|
+
sizeof(text) - position,
|
|
1293
|
+
&length
|
|
1294
|
+
)) {
|
|
1295
|
+
return failure(context, "OSEO2001", "Number formatting failed.");
|
|
1296
|
+
}
|
|
1297
|
+
return number_allocate_ascii(context, text, position + length);
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/* Number.prototype.toFixed ( fractionDigits ), 21.1.3.3. */
|
|
1301
|
+
static OseoResult number_to_fixed(
|
|
1302
|
+
OseoContext *context,
|
|
1303
|
+
OseoValue receiver,
|
|
1304
|
+
size_t argument_count,
|
|
1305
|
+
const OseoValue *arguments
|
|
1306
|
+
) {
|
|
1307
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1308
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1309
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1310
|
+
frame.slots[0] = receiver;
|
|
1311
|
+
result = number_this_value(context, frame.slots[0]);
|
|
1312
|
+
frame.slots[1] = result.value;
|
|
1313
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1314
|
+
oseo_roots_release(context, &frame);
|
|
1315
|
+
return result;
|
|
1316
|
+
}
|
|
1317
|
+
double x = number_value(frame.slots[1]);
|
|
1318
|
+
OseoValue digits_argument = argument_count > 0u
|
|
1319
|
+
? arguments[0]
|
|
1320
|
+
: oseo_undefined();
|
|
1321
|
+
double f_value = 0.0;
|
|
1322
|
+
if (tag_of(digits_argument) != OSEO_TAG_UNDEFINED) {
|
|
1323
|
+
result = number_integer_or_infinity(context, digits_argument);
|
|
1324
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1325
|
+
oseo_roots_release(context, &frame);
|
|
1326
|
+
return result;
|
|
1327
|
+
}
|
|
1328
|
+
f_value = number_value(result.value);
|
|
1329
|
+
if (!(f_value >= 0.0 && f_value <= 100.0)) {
|
|
1330
|
+
oseo_roots_release(context, &frame);
|
|
1331
|
+
return oseo_internal_throw_error(
|
|
1332
|
+
context,
|
|
1333
|
+
OSEO_ERROR_RANGE,
|
|
1334
|
+
"Number.prototype.toFixed digit count must be within "
|
|
1335
|
+
"0..100."
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
oseo_roots_release(context, &frame);
|
|
1340
|
+
int f = (int)f_value;
|
|
1341
|
+
if (isnan(x)) return number_allocate_ascii(context, "NaN", 3u);
|
|
1342
|
+
char text[256];
|
|
1343
|
+
size_t position = 0u;
|
|
1344
|
+
bool negative = x < 0.0;
|
|
1345
|
+
double magnitude = negative ? -x : x;
|
|
1346
|
+
if (negative) {
|
|
1347
|
+
text[position] = '-';
|
|
1348
|
+
position += 1u;
|
|
1349
|
+
}
|
|
1350
|
+
if (magnitude >= 1e21) {
|
|
1351
|
+
char decimal[64];
|
|
1352
|
+
(void)oseo_internal_number_text(magnitude, decimal, sizeof(decimal));
|
|
1353
|
+
size_t length = strlen(decimal);
|
|
1354
|
+
memcpy(text + position, decimal, length);
|
|
1355
|
+
position += length;
|
|
1356
|
+
return number_allocate_ascii(context, text, position);
|
|
1357
|
+
}
|
|
1358
|
+
NumberBignum rounded = {NULL, 0u, 0u};
|
|
1359
|
+
if (!number_round_scaled(magnitude, f, &rounded)) {
|
|
1360
|
+
return failure(context, "OSEO2001", "Number formatting failed.");
|
|
1361
|
+
}
|
|
1362
|
+
char digits[128];
|
|
1363
|
+
size_t digit_count = number_bignum_digit_string(&rounded, digits);
|
|
1364
|
+
number_bignum_release(&rounded);
|
|
1365
|
+
size_t pad = (f > 0 && digit_count <= (size_t)f)
|
|
1366
|
+
? ((size_t)f + 1u - digit_count)
|
|
1367
|
+
: 0u;
|
|
1368
|
+
char m[160];
|
|
1369
|
+
size_t m_length = 0u;
|
|
1370
|
+
for (size_t index = 0u; index < pad; index += 1u) {
|
|
1371
|
+
m[m_length] = '0';
|
|
1372
|
+
m_length += 1u;
|
|
1373
|
+
}
|
|
1374
|
+
memcpy(m + m_length, digits, digit_count);
|
|
1375
|
+
m_length += digit_count;
|
|
1376
|
+
if (f == 0) {
|
|
1377
|
+
memcpy(text + position, m, m_length);
|
|
1378
|
+
position += m_length;
|
|
1379
|
+
} else {
|
|
1380
|
+
size_t a_length = m_length - (size_t)f;
|
|
1381
|
+
memcpy(text + position, m, a_length);
|
|
1382
|
+
position += a_length;
|
|
1383
|
+
text[position] = '.';
|
|
1384
|
+
position += 1u;
|
|
1385
|
+
memcpy(text + position, m + a_length, (size_t)f);
|
|
1386
|
+
position += (size_t)f;
|
|
1387
|
+
}
|
|
1388
|
+
return number_allocate_ascii(context, text, position);
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
/* Number.prototype.toExponential ( fractionDigits ), 21.1.3.2. */
|
|
1392
|
+
static OseoResult number_to_exponential(
|
|
1393
|
+
OseoContext *context,
|
|
1394
|
+
OseoValue receiver,
|
|
1395
|
+
size_t argument_count,
|
|
1396
|
+
const OseoValue *arguments
|
|
1397
|
+
) {
|
|
1398
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1399
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1400
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1401
|
+
frame.slots[0] = receiver;
|
|
1402
|
+
result = number_this_value(context, frame.slots[0]);
|
|
1403
|
+
frame.slots[1] = result.value;
|
|
1404
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1405
|
+
oseo_roots_release(context, &frame);
|
|
1406
|
+
return result;
|
|
1407
|
+
}
|
|
1408
|
+
double x = number_value(frame.slots[1]);
|
|
1409
|
+
OseoValue digits_argument = argument_count > 0u
|
|
1410
|
+
? arguments[0]
|
|
1411
|
+
: oseo_undefined();
|
|
1412
|
+
bool digits_defined = tag_of(digits_argument) != OSEO_TAG_UNDEFINED;
|
|
1413
|
+
double f_value = 0.0;
|
|
1414
|
+
if (digits_defined) {
|
|
1415
|
+
result = number_integer_or_infinity(context, digits_argument);
|
|
1416
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1417
|
+
oseo_roots_release(context, &frame);
|
|
1418
|
+
return result;
|
|
1419
|
+
}
|
|
1420
|
+
f_value = number_value(result.value);
|
|
1421
|
+
}
|
|
1422
|
+
oseo_roots_release(context, &frame);
|
|
1423
|
+
if (isnan(x)) return number_allocate_ascii(context, "NaN", 3u);
|
|
1424
|
+
bool negative = x < 0.0;
|
|
1425
|
+
double magnitude = negative ? -x : x;
|
|
1426
|
+
if (isinf(magnitude)) {
|
|
1427
|
+
const char *infinity = negative ? "-Infinity" : "Infinity";
|
|
1428
|
+
return number_allocate_ascii(context, infinity, strlen(infinity));
|
|
1429
|
+
}
|
|
1430
|
+
if (digits_defined && !(f_value >= 0.0 && f_value <= 100.0)) {
|
|
1431
|
+
return oseo_internal_throw_error(
|
|
1432
|
+
context,
|
|
1433
|
+
OSEO_ERROR_RANGE,
|
|
1434
|
+
"Number.prototype.toExponential digit count must be within "
|
|
1435
|
+
"0..100."
|
|
1436
|
+
);
|
|
1437
|
+
}
|
|
1438
|
+
int f = (int)f_value;
|
|
1439
|
+
char text[160];
|
|
1440
|
+
size_t position = 0u;
|
|
1441
|
+
if (negative) {
|
|
1442
|
+
text[position] = '-';
|
|
1443
|
+
position += 1u;
|
|
1444
|
+
}
|
|
1445
|
+
size_t length = 0u;
|
|
1446
|
+
if (!number_format_exponential(
|
|
1447
|
+
magnitude,
|
|
1448
|
+
digits_defined,
|
|
1449
|
+
f,
|
|
1450
|
+
text + position,
|
|
1451
|
+
sizeof(text) - position,
|
|
1452
|
+
&length
|
|
1453
|
+
)) {
|
|
1454
|
+
return failure(context, "OSEO2001", "Number formatting failed.");
|
|
1455
|
+
}
|
|
1456
|
+
return number_allocate_ascii(context, text, position + length);
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
/* Number.prototype.toPrecision ( precision ), 21.1.3.5. */
|
|
1460
|
+
static OseoResult number_to_precision(
|
|
1461
|
+
OseoContext *context,
|
|
1462
|
+
OseoValue receiver,
|
|
1463
|
+
size_t argument_count,
|
|
1464
|
+
const OseoValue *arguments
|
|
1465
|
+
) {
|
|
1466
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1467
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
1468
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1469
|
+
frame.slots[0] = receiver;
|
|
1470
|
+
result = number_this_value(context, frame.slots[0]);
|
|
1471
|
+
frame.slots[1] = result.value;
|
|
1472
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1473
|
+
oseo_roots_release(context, &frame);
|
|
1474
|
+
return result;
|
|
1475
|
+
}
|
|
1476
|
+
double x = number_value(frame.slots[1]);
|
|
1477
|
+
OseoValue precision_argument = argument_count > 0u
|
|
1478
|
+
? arguments[0]
|
|
1479
|
+
: oseo_undefined();
|
|
1480
|
+
if (tag_of(precision_argument) == OSEO_TAG_UNDEFINED) {
|
|
1481
|
+
oseo_roots_release(context, &frame);
|
|
1482
|
+
char text[64];
|
|
1483
|
+
(void)oseo_internal_number_text(x, text, sizeof(text));
|
|
1484
|
+
return number_allocate_ascii(context, text, strlen(text));
|
|
1485
|
+
}
|
|
1486
|
+
result = number_integer_or_infinity(context, precision_argument);
|
|
1487
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1488
|
+
oseo_roots_release(context, &frame);
|
|
1489
|
+
return result;
|
|
1490
|
+
}
|
|
1491
|
+
double p_value = number_value(result.value);
|
|
1492
|
+
oseo_roots_release(context, &frame);
|
|
1493
|
+
if (isnan(x)) return number_allocate_ascii(context, "NaN", 3u);
|
|
1494
|
+
bool negative = x < 0.0;
|
|
1495
|
+
double magnitude = negative ? -x : x;
|
|
1496
|
+
if (isinf(magnitude)) {
|
|
1497
|
+
const char *infinity = negative ? "-Infinity" : "Infinity";
|
|
1498
|
+
return number_allocate_ascii(context, infinity, strlen(infinity));
|
|
1499
|
+
}
|
|
1500
|
+
if (!(p_value >= 1.0 && p_value <= 100.0)) {
|
|
1501
|
+
return oseo_internal_throw_error(
|
|
1502
|
+
context,
|
|
1503
|
+
OSEO_ERROR_RANGE,
|
|
1504
|
+
"Number.prototype.toPrecision precision must be within "
|
|
1505
|
+
"1..100."
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
int p = (int)p_value;
|
|
1509
|
+
char digit_buffer[128];
|
|
1510
|
+
int e = 0;
|
|
1511
|
+
if (magnitude == 0.0) {
|
|
1512
|
+
for (int index = 0; index < p; index += 1) digit_buffer[index] = '0';
|
|
1513
|
+
} else if (!number_significant_digits(magnitude, p, digit_buffer, &e)) {
|
|
1514
|
+
return failure(context, "OSEO2001", "Number formatting failed.");
|
|
1515
|
+
}
|
|
1516
|
+
char text[256];
|
|
1517
|
+
size_t position = 0u;
|
|
1518
|
+
if (negative) {
|
|
1519
|
+
text[position] = '-';
|
|
1520
|
+
position += 1u;
|
|
1521
|
+
}
|
|
1522
|
+
if (e < -6 || e >= p) {
|
|
1523
|
+
size_t length = 0u;
|
|
1524
|
+
if (!number_format_exponential_digits(
|
|
1525
|
+
digit_buffer,
|
|
1526
|
+
(size_t)p,
|
|
1527
|
+
e,
|
|
1528
|
+
text + position,
|
|
1529
|
+
sizeof(text) - position,
|
|
1530
|
+
&length
|
|
1531
|
+
)) {
|
|
1532
|
+
return failure(context, "OSEO2001", "Number formatting failed.");
|
|
1533
|
+
}
|
|
1534
|
+
position += length;
|
|
1535
|
+
} else {
|
|
1536
|
+
int decimal_position = e + 1;
|
|
1537
|
+
if (decimal_position <= 0) {
|
|
1538
|
+
text[position] = '0';
|
|
1539
|
+
position += 1u;
|
|
1540
|
+
text[position] = '.';
|
|
1541
|
+
position += 1u;
|
|
1542
|
+
for (int index = 0; index < -decimal_position; index += 1) {
|
|
1543
|
+
text[position] = '0';
|
|
1544
|
+
position += 1u;
|
|
1545
|
+
}
|
|
1546
|
+
memcpy(text + position, digit_buffer, (size_t)p);
|
|
1547
|
+
position += (size_t)p;
|
|
1548
|
+
} else {
|
|
1549
|
+
memcpy(text + position, digit_buffer, (size_t)decimal_position);
|
|
1550
|
+
position += (size_t)decimal_position;
|
|
1551
|
+
if (decimal_position < p) {
|
|
1552
|
+
text[position] = '.';
|
|
1553
|
+
position += 1u;
|
|
1554
|
+
memcpy(
|
|
1555
|
+
text + position,
|
|
1556
|
+
digit_buffer + decimal_position,
|
|
1557
|
+
(size_t)(p - decimal_position)
|
|
1558
|
+
);
|
|
1559
|
+
position += (size_t)(p - decimal_position);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return number_allocate_ascii(context, text, position);
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
/*
|
|
1567
|
+
* Number.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ),
|
|
1568
|
+
* 21.1.3.4. Without the ECMA-402 Internationalization API, the base
|
|
1569
|
+
* specification's own recommended behavior is thisNumberValue followed by
|
|
1570
|
+
* ToString: the reserved parameter positions are ignored entirely rather
|
|
1571
|
+
* than read or coerced.
|
|
1572
|
+
*/
|
|
1573
|
+
static OseoResult number_to_locale_string(
|
|
1574
|
+
OseoContext *context,
|
|
1575
|
+
OseoValue receiver
|
|
1576
|
+
) {
|
|
1577
|
+
OseoResult result = number_this_value(context, receiver);
|
|
1578
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1579
|
+
char text[64];
|
|
1580
|
+
(void)oseo_internal_number_text(
|
|
1581
|
+
number_value(result.value),
|
|
1582
|
+
text,
|
|
1583
|
+
sizeof(text)
|
|
1584
|
+
);
|
|
1585
|
+
return number_allocate_ascii(context, text, strlen(text));
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
OseoResult oseo_internal_number_builtin_dispatch(
|
|
1589
|
+
OseoContext *context,
|
|
1590
|
+
size_t code_id,
|
|
1591
|
+
OseoValue callee,
|
|
1592
|
+
OseoValue receiver,
|
|
1593
|
+
size_t argument_count,
|
|
1594
|
+
const OseoValue *arguments,
|
|
1595
|
+
OseoValue new_target
|
|
1596
|
+
) {
|
|
1597
|
+
(void)callee;
|
|
1598
|
+
if (code_id == OSEO_NUMBER_CONSTRUCTOR_CODE_ID) {
|
|
1599
|
+
return number_construct(
|
|
1600
|
+
context,
|
|
1601
|
+
new_target,
|
|
1602
|
+
argument_count,
|
|
1603
|
+
arguments,
|
|
1604
|
+
tag_of(new_target) != OSEO_TAG_UNDEFINED
|
|
1605
|
+
);
|
|
1606
|
+
}
|
|
1607
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
1608
|
+
return oseo_internal_throw_error(
|
|
1609
|
+
context,
|
|
1610
|
+
OSEO_ERROR_TYPE,
|
|
1611
|
+
"Number method is not a constructor."
|
|
1612
|
+
);
|
|
1613
|
+
}
|
|
1614
|
+
if (code_id == OSEO_NUMBER_VALUE_OF_CODE_ID) {
|
|
1615
|
+
return number_this_value(context, receiver);
|
|
1616
|
+
}
|
|
1617
|
+
if (code_id == OSEO_NUMBER_TO_STRING_CODE_ID) {
|
|
1618
|
+
return number_to_string(context, receiver, argument_count, arguments);
|
|
1619
|
+
}
|
|
1620
|
+
if (code_id == OSEO_NUMBER_TO_FIXED_CODE_ID) {
|
|
1621
|
+
return number_to_fixed(context, receiver, argument_count, arguments);
|
|
1622
|
+
}
|
|
1623
|
+
if (code_id == OSEO_NUMBER_TO_EXPONENTIAL_CODE_ID) {
|
|
1624
|
+
return number_to_exponential(
|
|
1625
|
+
context,
|
|
1626
|
+
receiver,
|
|
1627
|
+
argument_count,
|
|
1628
|
+
arguments
|
|
1629
|
+
);
|
|
1630
|
+
}
|
|
1631
|
+
if (code_id == OSEO_NUMBER_TO_PRECISION_CODE_ID) {
|
|
1632
|
+
return number_to_precision(
|
|
1633
|
+
context,
|
|
1634
|
+
receiver,
|
|
1635
|
+
argument_count,
|
|
1636
|
+
arguments
|
|
1637
|
+
);
|
|
1638
|
+
}
|
|
1639
|
+
if (code_id == OSEO_NUMBER_TO_LOCALE_STRING_CODE_ID) {
|
|
1640
|
+
return number_to_locale_string(context, receiver);
|
|
1641
|
+
}
|
|
1642
|
+
if (code_id == OSEO_NUMBER_IS_FINITE_CODE_ID) {
|
|
1643
|
+
return number_is_finite(argument_count, arguments);
|
|
1644
|
+
}
|
|
1645
|
+
if (code_id == OSEO_NUMBER_IS_INTEGER_CODE_ID) {
|
|
1646
|
+
return number_is_integer(argument_count, arguments, false);
|
|
1647
|
+
}
|
|
1648
|
+
if (code_id == OSEO_NUMBER_IS_NAN_CODE_ID) {
|
|
1649
|
+
return number_is_nan(argument_count, arguments);
|
|
1650
|
+
}
|
|
1651
|
+
if (code_id == OSEO_NUMBER_IS_SAFE_INTEGER_CODE_ID) {
|
|
1652
|
+
return number_is_integer(argument_count, arguments, true);
|
|
1653
|
+
}
|
|
1654
|
+
if (code_id == OSEO_NUMBER_PARSE_FLOAT_CODE_ID) {
|
|
1655
|
+
return number_parse_float(context, argument_count, arguments);
|
|
1656
|
+
}
|
|
1657
|
+
if (code_id == OSEO_NUMBER_PARSE_INT_CODE_ID) {
|
|
1658
|
+
return number_parse_int(context, argument_count, arguments);
|
|
1659
|
+
}
|
|
1660
|
+
if (code_id == OSEO_GLOBAL_IS_FINITE_CODE_ID) {
|
|
1661
|
+
return global_is_finite(context, argument_count, arguments);
|
|
1662
|
+
}
|
|
1663
|
+
if (code_id == OSEO_GLOBAL_IS_NAN_CODE_ID) {
|
|
1664
|
+
return global_is_nan(context, argument_count, arguments);
|
|
1665
|
+
}
|
|
1666
|
+
return oseo_unknown_function(context, code_id);
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
static OseoResult create_number_function(
|
|
1670
|
+
OseoContext *context,
|
|
1671
|
+
size_t code_id,
|
|
1672
|
+
const char *name,
|
|
1673
|
+
size_t length,
|
|
1674
|
+
OseoFunctionKind kind
|
|
1675
|
+
) {
|
|
1676
|
+
size_t name_length = strlen(name);
|
|
1677
|
+
uint16_t units[31];
|
|
1678
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
1679
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
1680
|
+
}
|
|
1681
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
1682
|
+
units[index] = (uint16_t)(unsigned char)name[index];
|
|
1683
|
+
}
|
|
1684
|
+
OseoValue environment = oseo_undefined();
|
|
1685
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
1686
|
+
oseo_roots_push(context, &frame);
|
|
1687
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
1688
|
+
environment = result.value;
|
|
1689
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1690
|
+
result = oseo_function_create(
|
|
1691
|
+
context,
|
|
1692
|
+
code_id,
|
|
1693
|
+
environment,
|
|
1694
|
+
units,
|
|
1695
|
+
name_length,
|
|
1696
|
+
length,
|
|
1697
|
+
kind,
|
|
1698
|
+
oseo_undefined(),
|
|
1699
|
+
oseo_undefined(),
|
|
1700
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1701
|
+
);
|
|
1702
|
+
}
|
|
1703
|
+
oseo_roots_pop(context, &frame);
|
|
1704
|
+
return result;
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
static OseoResult define_number_property(
|
|
1708
|
+
OseoContext *context,
|
|
1709
|
+
OseoValue object,
|
|
1710
|
+
const char *name,
|
|
1711
|
+
OseoValue value,
|
|
1712
|
+
OseoPropertyAttributes attributes
|
|
1713
|
+
) {
|
|
1714
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
1715
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1716
|
+
oseo_roots_push(context, &frame);
|
|
1717
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
1718
|
+
slots[2] = result.value;
|
|
1719
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1720
|
+
result = oseo_object_define(
|
|
1721
|
+
context,
|
|
1722
|
+
slots[0],
|
|
1723
|
+
slots[2],
|
|
1724
|
+
slots[1],
|
|
1725
|
+
attributes
|
|
1726
|
+
);
|
|
1727
|
+
}
|
|
1728
|
+
oseo_roots_pop(context, &frame);
|
|
1729
|
+
return result;
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
OseoResult oseo_internal_number_intrinsic(OseoContext *context) {
|
|
1733
|
+
OseoValue *marker =
|
|
1734
|
+
&context->realm->intrinsics[OSEO_INTRINSIC_NUMBER_PARSE_INT];
|
|
1735
|
+
if (is_function(*marker)) {
|
|
1736
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_NUMBER]);
|
|
1737
|
+
}
|
|
1738
|
+
if (is_object(*marker)) {
|
|
1739
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_NUMBER]);
|
|
1740
|
+
}
|
|
1741
|
+
size_t entry_allocations = context->allocations;
|
|
1742
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1743
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
1744
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1745
|
+
result = oseo_internal_intrinsic(
|
|
1746
|
+
context,
|
|
1747
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
1748
|
+
);
|
|
1749
|
+
frame.slots[0] = result.value;
|
|
1750
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1751
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
1752
|
+
frame.slots[0] = result.value;
|
|
1753
|
+
}
|
|
1754
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1755
|
+
OseoOrdinaryObject *prototype = ordinary_object(frame.slots[0]);
|
|
1756
|
+
prototype->number_data = true;
|
|
1757
|
+
prototype->number_value = oseo_number(0.0);
|
|
1758
|
+
prototype->primitive_data = true;
|
|
1759
|
+
prototype->primitive_value = oseo_number(0.0);
|
|
1760
|
+
context->realm->intrinsics[OSEO_INTRINSIC_NUMBER_PROTOTYPE] =
|
|
1761
|
+
frame.slots[0];
|
|
1762
|
+
*marker = frame.slots[0];
|
|
1763
|
+
result = create_number_function(
|
|
1764
|
+
context,
|
|
1765
|
+
OSEO_NUMBER_CONSTRUCTOR_CODE_ID,
|
|
1766
|
+
"Number",
|
|
1767
|
+
1u,
|
|
1768
|
+
OSEO_FUNCTION_ORDINARY
|
|
1769
|
+
);
|
|
1770
|
+
frame.slots[1] = result.value;
|
|
1771
|
+
}
|
|
1772
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1773
|
+
context->realm->intrinsics[OSEO_INTRINSIC_NUMBER] = frame.slots[1];
|
|
1774
|
+
OseoFunction *constructor = function_object(frame.slots[1]);
|
|
1775
|
+
constructor->prototype_object = frame.slots[0];
|
|
1776
|
+
constructor->prototype_writable = false;
|
|
1777
|
+
result = define_number_property(
|
|
1778
|
+
context,
|
|
1779
|
+
frame.slots[0],
|
|
1780
|
+
"constructor",
|
|
1781
|
+
frame.slots[1],
|
|
1782
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1783
|
+
);
|
|
1784
|
+
}
|
|
1785
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1786
|
+
result = create_number_function(
|
|
1787
|
+
context,
|
|
1788
|
+
OSEO_NUMBER_VALUE_OF_CODE_ID,
|
|
1789
|
+
"valueOf",
|
|
1790
|
+
0u,
|
|
1791
|
+
OSEO_FUNCTION_INTERNAL
|
|
1792
|
+
);
|
|
1793
|
+
frame.slots[2] = result.value;
|
|
1794
|
+
}
|
|
1795
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1796
|
+
result = define_number_property(
|
|
1797
|
+
context,
|
|
1798
|
+
frame.slots[0],
|
|
1799
|
+
"valueOf",
|
|
1800
|
+
frame.slots[2],
|
|
1801
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1802
|
+
);
|
|
1803
|
+
}
|
|
1804
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1805
|
+
result = create_number_function(
|
|
1806
|
+
context,
|
|
1807
|
+
OSEO_NUMBER_TO_STRING_CODE_ID,
|
|
1808
|
+
"toString",
|
|
1809
|
+
1u,
|
|
1810
|
+
OSEO_FUNCTION_INTERNAL
|
|
1811
|
+
);
|
|
1812
|
+
frame.slots[2] = result.value;
|
|
1813
|
+
}
|
|
1814
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1815
|
+
result = define_number_property(
|
|
1816
|
+
context,
|
|
1817
|
+
frame.slots[0],
|
|
1818
|
+
"toString",
|
|
1819
|
+
frame.slots[2],
|
|
1820
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1821
|
+
);
|
|
1822
|
+
}
|
|
1823
|
+
static const size_t prototype_method_codes[] = {
|
|
1824
|
+
OSEO_NUMBER_TO_FIXED_CODE_ID,
|
|
1825
|
+
OSEO_NUMBER_TO_EXPONENTIAL_CODE_ID,
|
|
1826
|
+
OSEO_NUMBER_TO_PRECISION_CODE_ID,
|
|
1827
|
+
OSEO_NUMBER_TO_LOCALE_STRING_CODE_ID,
|
|
1828
|
+
};
|
|
1829
|
+
static const char *const prototype_method_names[] = {
|
|
1830
|
+
"toFixed",
|
|
1831
|
+
"toExponential",
|
|
1832
|
+
"toPrecision",
|
|
1833
|
+
"toLocaleString",
|
|
1834
|
+
};
|
|
1835
|
+
static const size_t prototype_method_lengths[] = {1u, 1u, 1u, 0u};
|
|
1836
|
+
for (size_t index = 0u;
|
|
1837
|
+
result.status == OSEO_STATUS_NORMAL && index < 4u;
|
|
1838
|
+
index += 1u) {
|
|
1839
|
+
result = create_number_function(
|
|
1840
|
+
context,
|
|
1841
|
+
prototype_method_codes[index],
|
|
1842
|
+
prototype_method_names[index],
|
|
1843
|
+
prototype_method_lengths[index],
|
|
1844
|
+
OSEO_FUNCTION_INTERNAL
|
|
1845
|
+
);
|
|
1846
|
+
frame.slots[2] = result.value;
|
|
1847
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1848
|
+
result = define_number_property(
|
|
1849
|
+
context,
|
|
1850
|
+
frame.slots[0],
|
|
1851
|
+
prototype_method_names[index],
|
|
1852
|
+
frame.slots[2],
|
|
1853
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1854
|
+
);
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
static const OseoIntrinsic method_intrinsics[] = {
|
|
1858
|
+
OSEO_INTRINSIC_NUMBER_IS_FINITE,
|
|
1859
|
+
OSEO_INTRINSIC_NUMBER_IS_INTEGER,
|
|
1860
|
+
OSEO_INTRINSIC_NUMBER_IS_NAN,
|
|
1861
|
+
OSEO_INTRINSIC_NUMBER_IS_SAFE_INTEGER,
|
|
1862
|
+
OSEO_INTRINSIC_NUMBER_PARSE_FLOAT,
|
|
1863
|
+
OSEO_INTRINSIC_NUMBER_PARSE_INT,
|
|
1864
|
+
};
|
|
1865
|
+
static const size_t method_codes[] = {
|
|
1866
|
+
OSEO_NUMBER_IS_FINITE_CODE_ID,
|
|
1867
|
+
OSEO_NUMBER_IS_INTEGER_CODE_ID,
|
|
1868
|
+
OSEO_NUMBER_IS_NAN_CODE_ID,
|
|
1869
|
+
OSEO_NUMBER_IS_SAFE_INTEGER_CODE_ID,
|
|
1870
|
+
OSEO_NUMBER_PARSE_FLOAT_CODE_ID,
|
|
1871
|
+
OSEO_NUMBER_PARSE_INT_CODE_ID,
|
|
1872
|
+
};
|
|
1873
|
+
static const char *const method_names[] = {
|
|
1874
|
+
"isFinite",
|
|
1875
|
+
"isInteger",
|
|
1876
|
+
"isNaN",
|
|
1877
|
+
"isSafeInteger",
|
|
1878
|
+
"parseFloat",
|
|
1879
|
+
"parseInt",
|
|
1880
|
+
};
|
|
1881
|
+
static const size_t method_lengths[] = {1u, 1u, 1u, 1u, 1u, 2u};
|
|
1882
|
+
for (size_t index = 0u;
|
|
1883
|
+
result.status == OSEO_STATUS_NORMAL && index < 6u;
|
|
1884
|
+
index += 1u) {
|
|
1885
|
+
result = create_number_function(
|
|
1886
|
+
context,
|
|
1887
|
+
method_codes[index],
|
|
1888
|
+
method_names[index],
|
|
1889
|
+
method_lengths[index],
|
|
1890
|
+
OSEO_FUNCTION_INTERNAL
|
|
1891
|
+
);
|
|
1892
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1893
|
+
context->realm->intrinsics[method_intrinsics[index]] = result.value;
|
|
1894
|
+
result = define_number_property(
|
|
1895
|
+
context,
|
|
1896
|
+
frame.slots[1],
|
|
1897
|
+
method_names[index],
|
|
1898
|
+
result.value,
|
|
1899
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1900
|
+
);
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
static const char *const constant_names[] = {
|
|
1904
|
+
"EPSILON",
|
|
1905
|
+
"MAX_SAFE_INTEGER",
|
|
1906
|
+
"MAX_VALUE",
|
|
1907
|
+
"MIN_SAFE_INTEGER",
|
|
1908
|
+
"MIN_VALUE",
|
|
1909
|
+
"NaN",
|
|
1910
|
+
"NEGATIVE_INFINITY",
|
|
1911
|
+
"POSITIVE_INFINITY",
|
|
1912
|
+
};
|
|
1913
|
+
static const double constant_values[] = {
|
|
1914
|
+
DBL_EPSILON,
|
|
1915
|
+
OSEO_MAX_SAFE_INTEGER,
|
|
1916
|
+
DBL_MAX,
|
|
1917
|
+
-OSEO_MAX_SAFE_INTEGER,
|
|
1918
|
+
DBL_TRUE_MIN,
|
|
1919
|
+
NAN,
|
|
1920
|
+
-INFINITY,
|
|
1921
|
+
INFINITY,
|
|
1922
|
+
};
|
|
1923
|
+
for (size_t index = 0u;
|
|
1924
|
+
result.status == OSEO_STATUS_NORMAL && index < 8u;
|
|
1925
|
+
index += 1u) {
|
|
1926
|
+
result = define_number_property(
|
|
1927
|
+
context,
|
|
1928
|
+
frame.slots[1],
|
|
1929
|
+
constant_names[index],
|
|
1930
|
+
oseo_number(constant_values[index]),
|
|
1931
|
+
(OseoPropertyAttributes){false, false, false, false}
|
|
1932
|
+
);
|
|
1933
|
+
}
|
|
1934
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1935
|
+
for (size_t index = OSEO_INTRINSIC_NUMBER_PROTOTYPE;
|
|
1936
|
+
index <= OSEO_INTRINSIC_NUMBER_PARSE_INT;
|
|
1937
|
+
index += 1u) {
|
|
1938
|
+
context->realm->intrinsics[index] = oseo_undefined();
|
|
1939
|
+
}
|
|
1940
|
+
} else if (context->observe_specialization) {
|
|
1941
|
+
context->allocations = entry_allocations;
|
|
1942
|
+
}
|
|
1943
|
+
oseo_roots_release(context, &frame);
|
|
1944
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1945
|
+
? normal(context->realm->intrinsics[OSEO_INTRINSIC_NUMBER])
|
|
1946
|
+
: result;
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
OseoResult oseo_internal_install_number_global(
|
|
1950
|
+
OseoContext *context,
|
|
1951
|
+
OseoValue global
|
|
1952
|
+
) {
|
|
1953
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
1954
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1955
|
+
oseo_roots_push(context, &frame);
|
|
1956
|
+
OseoResult result = oseo_internal_number_intrinsic(context);
|
|
1957
|
+
slots[1] = result.value;
|
|
1958
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1959
|
+
result = define_number_property(
|
|
1960
|
+
context,
|
|
1961
|
+
slots[0],
|
|
1962
|
+
"Number",
|
|
1963
|
+
slots[1],
|
|
1964
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1965
|
+
);
|
|
1966
|
+
}
|
|
1967
|
+
oseo_roots_pop(context, &frame);
|
|
1968
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1969
|
+
? normal(slots[0])
|
|
1970
|
+
: result;
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
OseoResult oseo_internal_global_numeric_intrinsic(
|
|
1974
|
+
OseoContext *context,
|
|
1975
|
+
OseoIntrinsic intrinsic
|
|
1976
|
+
) {
|
|
1977
|
+
OseoValue *slot = &context->realm->intrinsics[intrinsic];
|
|
1978
|
+
if (is_object(*slot)) return normal(*slot);
|
|
1979
|
+
bool finite = intrinsic == OSEO_INTRINSIC_IS_FINITE;
|
|
1980
|
+
if (!finite && intrinsic != OSEO_INTRINSIC_IS_NAN) {
|
|
1981
|
+
return failure(
|
|
1982
|
+
context,
|
|
1983
|
+
"OSEO2001",
|
|
1984
|
+
"Unknown global numeric intrinsic."
|
|
1985
|
+
);
|
|
1986
|
+
}
|
|
1987
|
+
size_t entry_allocations = context->allocations;
|
|
1988
|
+
OseoResult result = create_number_function(
|
|
1989
|
+
context,
|
|
1990
|
+
finite ? OSEO_GLOBAL_IS_FINITE_CODE_ID : OSEO_GLOBAL_IS_NAN_CODE_ID,
|
|
1991
|
+
finite ? "isFinite" : "isNaN",
|
|
1992
|
+
1u,
|
|
1993
|
+
OSEO_FUNCTION_INTERNAL
|
|
1994
|
+
);
|
|
1995
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1996
|
+
*slot = result.value;
|
|
1997
|
+
if (context->observe_specialization) {
|
|
1998
|
+
context->allocations = entry_allocations;
|
|
1999
|
+
}
|
|
2000
|
+
return normal(*slot);
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
/*
|
|
2004
|
+
* The four global numeric function properties of 19.2 in the order the
|
|
2005
|
+
* clause lists them. `isFinite` and `isNaN` are this component's own
|
|
2006
|
+
* lazily created intrinsics; `parseFloat` and `parseInt` are the
|
|
2007
|
+
* %parseFloat% and %parseInt% objects `Number.parseFloat` and
|
|
2008
|
+
* `Number.parseInt` already name, so the global property and the static
|
|
2009
|
+
* are one function.
|
|
2010
|
+
*/
|
|
2011
|
+
OseoResult oseo_internal_install_global_numeric_functions(
|
|
2012
|
+
OseoContext *context,
|
|
2013
|
+
OseoValue global
|
|
2014
|
+
) {
|
|
2015
|
+
static const OseoIntrinsic intrinsics[] = {
|
|
2016
|
+
OSEO_INTRINSIC_IS_FINITE,
|
|
2017
|
+
OSEO_INTRINSIC_IS_NAN,
|
|
2018
|
+
OSEO_INTRINSIC_NUMBER_PARSE_FLOAT,
|
|
2019
|
+
OSEO_INTRINSIC_NUMBER_PARSE_INT,
|
|
2020
|
+
};
|
|
2021
|
+
static const char *const names[] = {
|
|
2022
|
+
"isFinite",
|
|
2023
|
+
"isNaN",
|
|
2024
|
+
"parseFloat",
|
|
2025
|
+
"parseInt",
|
|
2026
|
+
};
|
|
2027
|
+
_Static_assert(
|
|
2028
|
+
sizeof(intrinsics) / sizeof(intrinsics[0]) ==
|
|
2029
|
+
sizeof(names) / sizeof(names[0]),
|
|
2030
|
+
"Global numeric function tables must stay aligned."
|
|
2031
|
+
);
|
|
2032
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
2033
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
2034
|
+
oseo_roots_push(context, &frame);
|
|
2035
|
+
OseoResult result = normal(slots[0]);
|
|
2036
|
+
for (size_t index = 0u;
|
|
2037
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
2038
|
+
index < sizeof(names) / sizeof(names[0]);
|
|
2039
|
+
index += 1u) {
|
|
2040
|
+
result = oseo_internal_intrinsic(context, intrinsics[index]);
|
|
2041
|
+
slots[1] = result.value;
|
|
2042
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
2043
|
+
result = define_number_property(
|
|
2044
|
+
context,
|
|
2045
|
+
slots[0],
|
|
2046
|
+
names[index],
|
|
2047
|
+
slots[1],
|
|
2048
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
2049
|
+
);
|
|
2050
|
+
}
|
|
2051
|
+
oseo_roots_pop(context, &frame);
|
|
2052
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
|
|
2053
|
+
}
|