@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370
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 +28 -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 +2618 -0
- package/native/runtime_agent.c +1717 -0
- package/native/runtime_arguments.c +379 -0
- package/native/runtime_array.c +5766 -0
- package/native/runtime_array_buffer.c +1748 -0
- package/native/runtime_async_generator.c +972 -0
- package/native/runtime_atomics.c +1283 -0
- package/native/runtime_bigint.c +1387 -0
- package/native/runtime_bigint_object.c +626 -0
- package/native/runtime_binding.c +1029 -0
- package/native/runtime_boolean.c +320 -0
- package/native/runtime_clock.c +181 -0
- package/native/runtime_clock_posix.c +599 -0
- package/native/runtime_core.c +488 -0
- package/native/runtime_data_view.c +1135 -0
- package/native/runtime_date.c +1997 -0
- package/native/runtime_descriptor.c +1104 -0
- package/native/runtime_disposal.c +1667 -0
- package/native/runtime_enumeration.c +636 -0
- package/native/runtime_error.c +997 -0
- package/native/runtime_event_loop.c +653 -0
- package/native/runtime_function.c +3667 -0
- package/native/runtime_generator.c +1120 -0
- package/native/runtime_internal.h +5578 -0
- package/native/runtime_iterator.c +5379 -0
- package/native/runtime_json.c +1992 -0
- package/native/runtime_map.c +1563 -0
- package/native/runtime_math.c +918 -0
- package/native/runtime_memory.c +1365 -0
- package/native/runtime_module.c +884 -0
- package/native/runtime_number.c +2077 -0
- package/native/runtime_object.c +423 -0
- package/native/runtime_object_builtin.c +3901 -0
- package/native/runtime_primitive.c +2268 -0
- package/native/runtime_process.c +744 -0
- package/native/runtime_promise.c +3378 -0
- package/native/runtime_property.c +815 -0
- package/native/runtime_proxy.c +1557 -0
- package/native/runtime_realm.c +435 -0
- package/native/runtime_reflect.c +648 -0
- package/native/runtime_regexp.c +3519 -0
- package/native/runtime_regexp_matcher.c +3559 -0
- package/native/runtime_regexp_symbol.c +1495 -0
- package/native/runtime_set.c +1775 -0
- package/native/runtime_string.c +2886 -0
- package/native/runtime_string_match.c +1185 -0
- package/native/runtime_symbol.c +1010 -0
- package/native/runtime_typed_array.c +5137 -0
- package/native/runtime_unicode_tables.h +14670 -0
- package/native/runtime_uri.c +700 -0
- package/native/runtime_weak_collection.c +1299 -0
- package/package.json +38 -3
|
@@ -0,0 +1,918 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/*
|
|
3
|
+
* Oseo C runtime library.
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
* in the Software without restriction, including without limitation the rights
|
|
12
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
* furnished to do so, subject to the following conditions:
|
|
15
|
+
*
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in
|
|
17
|
+
* all copies or substantial portions of the Software.
|
|
18
|
+
*
|
|
19
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
* SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
#include "runtime_internal.h"
|
|
29
|
+
|
|
30
|
+
#include <float.h>
|
|
31
|
+
#include <math.h>
|
|
32
|
+
#include <string.h>
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* The Math namespace object: the eight value properties of 21.3.1, the
|
|
36
|
+
* thirty-seven function properties of 21.3.2, and the realm's
|
|
37
|
+
* pseudorandom source. Math is an ordinary object, not a function, so
|
|
38
|
+
* it has neither [[Call]] nor [[Construct]] and its function properties
|
|
39
|
+
* are ordinary built-in functions that are not constructors either.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/*
|
|
43
|
+
* One Math function property. The array order is the ordering the
|
|
44
|
+
* namespace object creates its properties in and the order the code
|
|
45
|
+
* IDs count down from `OSEO_MATH_FUNCTION_CODE_ID_LAST`, so an entry's
|
|
46
|
+
* index is the only identity the builder and the dispatcher share.
|
|
47
|
+
*/
|
|
48
|
+
typedef enum {
|
|
49
|
+
OSEO_MATH_ABS = 0,
|
|
50
|
+
OSEO_MATH_ACOS = 1,
|
|
51
|
+
OSEO_MATH_ACOSH = 2,
|
|
52
|
+
OSEO_MATH_ASIN = 3,
|
|
53
|
+
OSEO_MATH_ASINH = 4,
|
|
54
|
+
OSEO_MATH_ATAN = 5,
|
|
55
|
+
OSEO_MATH_ATANH = 6,
|
|
56
|
+
OSEO_MATH_ATAN2 = 7,
|
|
57
|
+
OSEO_MATH_CBRT = 8,
|
|
58
|
+
OSEO_MATH_CEIL = 9,
|
|
59
|
+
OSEO_MATH_CLZ32 = 10,
|
|
60
|
+
OSEO_MATH_COS = 11,
|
|
61
|
+
OSEO_MATH_COSH = 12,
|
|
62
|
+
OSEO_MATH_EXP = 13,
|
|
63
|
+
OSEO_MATH_EXPM1 = 14,
|
|
64
|
+
OSEO_MATH_F16ROUND = 15,
|
|
65
|
+
OSEO_MATH_FLOOR = 16,
|
|
66
|
+
OSEO_MATH_FROUND = 17,
|
|
67
|
+
OSEO_MATH_HYPOT = 18,
|
|
68
|
+
OSEO_MATH_IMUL = 19,
|
|
69
|
+
OSEO_MATH_LOG = 20,
|
|
70
|
+
OSEO_MATH_LOG1P = 21,
|
|
71
|
+
OSEO_MATH_LOG10 = 22,
|
|
72
|
+
OSEO_MATH_LOG2 = 23,
|
|
73
|
+
OSEO_MATH_MAX = 24,
|
|
74
|
+
OSEO_MATH_MIN = 25,
|
|
75
|
+
OSEO_MATH_POW = 26,
|
|
76
|
+
OSEO_MATH_RANDOM = 27,
|
|
77
|
+
OSEO_MATH_ROUND = 28,
|
|
78
|
+
OSEO_MATH_SIGN = 29,
|
|
79
|
+
OSEO_MATH_SIN = 30,
|
|
80
|
+
OSEO_MATH_SINH = 31,
|
|
81
|
+
OSEO_MATH_SQRT = 32,
|
|
82
|
+
OSEO_MATH_TAN = 33,
|
|
83
|
+
OSEO_MATH_TANH = 34,
|
|
84
|
+
OSEO_MATH_TRUNC = 35,
|
|
85
|
+
/* ECMAScript 2026 appends sumPrecise after the earlier functions. */
|
|
86
|
+
OSEO_MATH_SUM_PRECISE = 36,
|
|
87
|
+
} OseoMathOperation;
|
|
88
|
+
|
|
89
|
+
typedef struct {
|
|
90
|
+
const char *name;
|
|
91
|
+
size_t length;
|
|
92
|
+
} OseoMathFunction;
|
|
93
|
+
|
|
94
|
+
static const OseoMathFunction math_functions[] = {
|
|
95
|
+
{"abs", 1u}, {"acos", 1u}, {"acosh", 1u}, {"asin", 1u},
|
|
96
|
+
{"asinh", 1u}, {"atan", 1u}, {"atanh", 1u}, {"atan2", 2u},
|
|
97
|
+
{"cbrt", 1u}, {"ceil", 1u}, {"clz32", 1u}, {"cos", 1u},
|
|
98
|
+
{"cosh", 1u}, {"exp", 1u}, {"expm1", 1u}, {"f16round", 1u},
|
|
99
|
+
{"floor", 1u}, {"fround", 1u}, {"hypot", 2u}, {"imul", 2u},
|
|
100
|
+
{"log", 1u}, {"log1p", 1u}, {"log10", 1u}, {"log2", 1u},
|
|
101
|
+
{"max", 2u}, {"min", 2u}, {"pow", 2u}, {"random", 0u},
|
|
102
|
+
{"round", 1u}, {"sign", 1u}, {"sin", 1u}, {"sinh", 1u},
|
|
103
|
+
{"sqrt", 1u}, {"tan", 1u}, {"tanh", 1u}, {"trunc", 1u},
|
|
104
|
+
{"sumPrecise", 1u},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
_Static_assert(
|
|
108
|
+
sizeof(math_functions) / sizeof(math_functions[0]) ==
|
|
109
|
+
OSEO_MATH_FUNCTION_COUNT,
|
|
110
|
+
"The Math function table must match its reviewed code-ID range."
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
typedef struct {
|
|
114
|
+
const char *name;
|
|
115
|
+
double value;
|
|
116
|
+
} OseoMathConstant;
|
|
117
|
+
|
|
118
|
+
/*
|
|
119
|
+
* The eight value properties, each the Number value closest to the
|
|
120
|
+
* mathematical constant it names. They are non-writable,
|
|
121
|
+
* non-enumerable, and non-configurable.
|
|
122
|
+
*/
|
|
123
|
+
static const OseoMathConstant math_constants[] = {
|
|
124
|
+
{"E", 2.7182818284590452354},
|
|
125
|
+
{"LN10", 2.302585092994046},
|
|
126
|
+
{"LN2", 0.6931471805599453},
|
|
127
|
+
{"LOG10E", 0.4342944819032518},
|
|
128
|
+
{"LOG2E", 1.4426950408889634},
|
|
129
|
+
{"PI", 3.1415926535897932},
|
|
130
|
+
{"SQRT1_2", 0.7071067811865476},
|
|
131
|
+
{"SQRT2", 1.4142135623730951},
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
#define OSEO_MATH_CONSTANT_COUNT \
|
|
135
|
+
(sizeof(math_constants) / sizeof(math_constants[0]))
|
|
136
|
+
|
|
137
|
+
/*
|
|
138
|
+
* Round one finite nonzero magnitude to the nearest value of a binary
|
|
139
|
+
* interchange format with `significand_bits` significant bits and
|
|
140
|
+
* `minimum_quantum` as the exponent of its subnormal quantum, with ties
|
|
141
|
+
* to even. The scaled operand stays below 2**significand_bits + 1, so
|
|
142
|
+
* both the scaling and the comparison are exact binary64 arithmetic and
|
|
143
|
+
* no narrowing floating conversion happens. A magnitude that rounds
|
|
144
|
+
* past `maximum_finite` becomes an infinity.
|
|
145
|
+
*/
|
|
146
|
+
static double round_to_binary(
|
|
147
|
+
double magnitude,
|
|
148
|
+
int significand_bits,
|
|
149
|
+
int minimum_quantum,
|
|
150
|
+
double maximum_finite
|
|
151
|
+
) {
|
|
152
|
+
int exponent;
|
|
153
|
+
(void)frexp(magnitude, &exponent);
|
|
154
|
+
int quantum = exponent - significand_bits;
|
|
155
|
+
if (quantum < minimum_quantum) quantum = minimum_quantum;
|
|
156
|
+
double scaled = ldexp(magnitude, -quantum);
|
|
157
|
+
double lower = floor(scaled);
|
|
158
|
+
double fraction = scaled - lower;
|
|
159
|
+
double rounded;
|
|
160
|
+
if (fraction > 0.5) {
|
|
161
|
+
rounded = lower + 1.0;
|
|
162
|
+
} else if (fraction < 0.5) {
|
|
163
|
+
rounded = lower;
|
|
164
|
+
} else {
|
|
165
|
+
rounded = fmod(lower, 2.0) == 0.0 ? lower : lower + 1.0;
|
|
166
|
+
}
|
|
167
|
+
double result = ldexp(rounded, quantum);
|
|
168
|
+
return result > maximum_finite ? (double)INFINITY : result;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* Math.fround and Math.f16round over one already converted Number. */
|
|
172
|
+
static double math_round_to_format(double value, bool half) {
|
|
173
|
+
if (isnan(value) || isinf(value) || value == 0.0) return value;
|
|
174
|
+
double magnitude = round_to_binary(
|
|
175
|
+
fabs(value),
|
|
176
|
+
half ? 11 : 24,
|
|
177
|
+
half ? -24 : -149,
|
|
178
|
+
half ? 65504.0 : (double)FLT_MAX
|
|
179
|
+
);
|
|
180
|
+
return value < 0.0 ? -magnitude : magnitude;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
* Math.round, 21.3.2.28. The specification rounds to the closest
|
|
185
|
+
* integral Number and breaks a tie toward positive infinity, which is
|
|
186
|
+
* not what C `round` does for a negative tie, and it keeps a negative
|
|
187
|
+
* operand above -0.5 at -0. Comparing the fractional part against one
|
|
188
|
+
* half decides both, and an operand whose magnitude is at least 2**52
|
|
189
|
+
* is already integral, so its fractional part is exactly zero.
|
|
190
|
+
*/
|
|
191
|
+
static double math_round(double value) {
|
|
192
|
+
if (isnan(value) || isinf(value) || value == 0.0) return value;
|
|
193
|
+
if (value > 0.0 && value < 0.5) return 0.0;
|
|
194
|
+
if (value < 0.0 && value >= -0.5) return -0.0;
|
|
195
|
+
double lower = floor(value);
|
|
196
|
+
return value - lower < 0.5 ? lower : lower + 1.0;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/* Math.sign, 21.3.2.29, which preserves both signed zeroes and NaN. */
|
|
200
|
+
static double math_sign(double value) {
|
|
201
|
+
if (isnan(value) || value == 0.0) return value;
|
|
202
|
+
return value < 0.0 ? -1.0 : 1.0;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* Math.clz32, 21.3.2.11, over the operand's ToUint32 bit pattern. */
|
|
206
|
+
static double math_clz32(double value) {
|
|
207
|
+
uint32_t bits = oseo_internal_number_to_uint32(value);
|
|
208
|
+
if (bits == 0u) return 32.0;
|
|
209
|
+
int count = 0;
|
|
210
|
+
while ((bits & UINT32_C(0x80000000)) == 0u) {
|
|
211
|
+
bits <<= 1u;
|
|
212
|
+
count += 1;
|
|
213
|
+
}
|
|
214
|
+
return (double)count;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* Math.imul, 21.3.2.20: the signed reading of a wrapped 32-bit product. */
|
|
218
|
+
static double math_imul(double left, double right) {
|
|
219
|
+
uint32_t product = oseo_internal_number_to_uint32(left) *
|
|
220
|
+
oseo_internal_number_to_uint32(right);
|
|
221
|
+
return product >= UINT32_C(0x80000000)
|
|
222
|
+
? (double)((int64_t)product - INT64_C(4294967296))
|
|
223
|
+
: (double)product;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/*
|
|
227
|
+
* The realm's xorshift128+ draw. The state is the two words the context
|
|
228
|
+
* mixed from this realm's initialization ordinal, so the sequence is
|
|
229
|
+
* uniform, identical on every supported target, and distinct from every
|
|
230
|
+
* other realm's.
|
|
231
|
+
*/
|
|
232
|
+
static double math_random(OseoContext *context) {
|
|
233
|
+
uint64_t first = context->realm->random_state[0];
|
|
234
|
+
const uint64_t second = context->realm->random_state[1];
|
|
235
|
+
context->realm->random_state[0] = second;
|
|
236
|
+
first ^= first << 23u;
|
|
237
|
+
first ^= first >> 17u;
|
|
238
|
+
first ^= second;
|
|
239
|
+
first ^= second >> 26u;
|
|
240
|
+
context->realm->random_state[1] = first;
|
|
241
|
+
/* The high 53 bits of the sum scale exactly onto [0, 1). */
|
|
242
|
+
return (double)((first + second) >> 11u) * (1.0 / 9007199254740992.0);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/* Every Math function property whose result needs one operand only. */
|
|
246
|
+
static double math_unary_value(size_t operation, double value) {
|
|
247
|
+
switch ((OseoMathOperation)operation) {
|
|
248
|
+
case OSEO_MATH_ABS: return fabs(value);
|
|
249
|
+
case OSEO_MATH_ACOS: return acos(value);
|
|
250
|
+
case OSEO_MATH_ACOSH: return acosh(value);
|
|
251
|
+
case OSEO_MATH_ASIN: return asin(value);
|
|
252
|
+
case OSEO_MATH_ASINH: return asinh(value);
|
|
253
|
+
case OSEO_MATH_ATAN: return atan(value);
|
|
254
|
+
case OSEO_MATH_ATANH: return atanh(value);
|
|
255
|
+
case OSEO_MATH_CBRT: return cbrt(value);
|
|
256
|
+
case OSEO_MATH_CEIL: return ceil(value);
|
|
257
|
+
case OSEO_MATH_CLZ32: return math_clz32(value);
|
|
258
|
+
case OSEO_MATH_COS: return cos(value);
|
|
259
|
+
case OSEO_MATH_COSH: return cosh(value);
|
|
260
|
+
case OSEO_MATH_EXP: return exp(value);
|
|
261
|
+
case OSEO_MATH_EXPM1: return expm1(value);
|
|
262
|
+
case OSEO_MATH_F16ROUND: return math_round_to_format(value, true);
|
|
263
|
+
case OSEO_MATH_FLOOR: return floor(value);
|
|
264
|
+
case OSEO_MATH_FROUND: return math_round_to_format(value, false);
|
|
265
|
+
case OSEO_MATH_LOG: return log(value);
|
|
266
|
+
case OSEO_MATH_LOG1P: return log1p(value);
|
|
267
|
+
case OSEO_MATH_LOG10: return log10(value);
|
|
268
|
+
case OSEO_MATH_LOG2: return log2(value);
|
|
269
|
+
case OSEO_MATH_ROUND: return math_round(value);
|
|
270
|
+
case OSEO_MATH_SIGN: return math_sign(value);
|
|
271
|
+
case OSEO_MATH_SIN: return sin(value);
|
|
272
|
+
case OSEO_MATH_SINH: return sinh(value);
|
|
273
|
+
case OSEO_MATH_SQRT: return sqrt(value);
|
|
274
|
+
case OSEO_MATH_TAN: return tan(value);
|
|
275
|
+
case OSEO_MATH_TANH: return tanh(value);
|
|
276
|
+
case OSEO_MATH_TRUNC: return trunc(value);
|
|
277
|
+
default: return NAN;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static bool math_is_unary(size_t operation) {
|
|
282
|
+
switch ((OseoMathOperation)operation) {
|
|
283
|
+
case OSEO_MATH_ATAN2:
|
|
284
|
+
case OSEO_MATH_HYPOT:
|
|
285
|
+
case OSEO_MATH_IMUL:
|
|
286
|
+
case OSEO_MATH_MAX:
|
|
287
|
+
case OSEO_MATH_MIN:
|
|
288
|
+
case OSEO_MATH_POW:
|
|
289
|
+
case OSEO_MATH_RANDOM:
|
|
290
|
+
case OSEO_MATH_SUM_PRECISE:
|
|
291
|
+
return false;
|
|
292
|
+
default:
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/*
|
|
298
|
+
* ToNumber over one argument position, reading `undefined` when the
|
|
299
|
+
* call supplied no argument there. The operand stays rooted across the
|
|
300
|
+
* conversion because a ToPrimitive method can allocate and collect.
|
|
301
|
+
*/
|
|
302
|
+
static OseoResult math_operand(
|
|
303
|
+
OseoContext *context,
|
|
304
|
+
size_t argument_count,
|
|
305
|
+
const OseoValue *arguments,
|
|
306
|
+
size_t index,
|
|
307
|
+
double *value
|
|
308
|
+
) {
|
|
309
|
+
OseoValue slot =
|
|
310
|
+
index < argument_count ? arguments[index] : oseo_undefined();
|
|
311
|
+
OseoRootFrame frame = {NULL, &slot, 1u};
|
|
312
|
+
oseo_roots_push(context, &frame);
|
|
313
|
+
OseoResult result = oseo_internal_to_number(context, slot);
|
|
314
|
+
oseo_roots_pop(context, &frame);
|
|
315
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
316
|
+
*value = number_value(result.value);
|
|
317
|
+
return result;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/*
|
|
321
|
+
* Convert every argument of a variadic Math function in argument order,
|
|
322
|
+
* replacing each rooted operand with its Number. Every conversion can
|
|
323
|
+
* run user code, so the whole list stays rooted until the last one
|
|
324
|
+
* completes.
|
|
325
|
+
*/
|
|
326
|
+
static OseoResult math_operand_list(
|
|
327
|
+
OseoContext *context,
|
|
328
|
+
size_t argument_count,
|
|
329
|
+
const OseoRootFrame *frame
|
|
330
|
+
) {
|
|
331
|
+
OseoResult result = normal(oseo_undefined());
|
|
332
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
333
|
+
result = oseo_internal_to_number(context, frame->slots[index]);
|
|
334
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
335
|
+
frame->slots[index] = result.value;
|
|
336
|
+
}
|
|
337
|
+
return result;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/*
|
|
341
|
+
* Math.max and Math.min, 21.3.2.24 and 21.3.2.25. Every argument is
|
|
342
|
+
* converted first, a NaN among them wins over every comparison, and
|
|
343
|
+
* +0 is treated as larger than -0 in both directions.
|
|
344
|
+
*/
|
|
345
|
+
static double math_extremum(
|
|
346
|
+
const OseoRootFrame *frame,
|
|
347
|
+
size_t argument_count,
|
|
348
|
+
bool maximum
|
|
349
|
+
) {
|
|
350
|
+
double best = maximum ? -(double)INFINITY : (double)INFINITY;
|
|
351
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
352
|
+
double value = number_value(frame->slots[index]);
|
|
353
|
+
if (isnan(value)) return NAN;
|
|
354
|
+
if (value == 0.0 && best == 0.0) {
|
|
355
|
+
bool value_negative = signbit(value) != 0;
|
|
356
|
+
bool best_negative = signbit(best) != 0;
|
|
357
|
+
if (maximum ? (best_negative && !value_negative)
|
|
358
|
+
: (!best_negative && value_negative)) {
|
|
359
|
+
best = value;
|
|
360
|
+
}
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (maximum ? value > best : value < best) best = value;
|
|
364
|
+
}
|
|
365
|
+
return best;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/*
|
|
369
|
+
* Math.hypot, 21.3.2.18. An infinite operand wins over a NaN one, and a
|
|
370
|
+
* list of zeroes is +0. The finite case scales by the largest magnitude
|
|
371
|
+
* so a representable result cannot overflow an intermediate square.
|
|
372
|
+
*/
|
|
373
|
+
static double math_hypot(
|
|
374
|
+
const OseoRootFrame *frame,
|
|
375
|
+
size_t argument_count
|
|
376
|
+
) {
|
|
377
|
+
bool has_nan = false;
|
|
378
|
+
double largest = 0.0;
|
|
379
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
380
|
+
double value = number_value(frame->slots[index]);
|
|
381
|
+
if (isinf(value)) return (double)INFINITY;
|
|
382
|
+
if (isnan(value)) has_nan = true;
|
|
383
|
+
else if (fabs(value) > largest) largest = fabs(value);
|
|
384
|
+
}
|
|
385
|
+
if (has_nan) return NAN;
|
|
386
|
+
if (largest == 0.0) return 0.0;
|
|
387
|
+
double total = 0.0;
|
|
388
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
389
|
+
double scaled = number_value(frame->slots[index]) / largest;
|
|
390
|
+
total += scaled * scaled;
|
|
391
|
+
}
|
|
392
|
+
return largest * sqrt(total);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
static OseoResult math_variadic(
|
|
396
|
+
OseoContext *context,
|
|
397
|
+
size_t operation,
|
|
398
|
+
size_t argument_count,
|
|
399
|
+
const OseoValue *arguments
|
|
400
|
+
) {
|
|
401
|
+
if (argument_count > 0u && arguments == NULL) {
|
|
402
|
+
return failure(context, "OSEO2001", "Math arguments are missing.");
|
|
403
|
+
}
|
|
404
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
405
|
+
/* One slot per argument and no more, so the count cannot overflow. */
|
|
406
|
+
OseoResult result =
|
|
407
|
+
oseo_roots_allocate(context, &frame, argument_count);
|
|
408
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
409
|
+
for (size_t index = 0u; index < argument_count; index += 1u) {
|
|
410
|
+
frame.slots[index] = arguments[index];
|
|
411
|
+
}
|
|
412
|
+
result = math_operand_list(context, argument_count, &frame);
|
|
413
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
414
|
+
double value = operation == OSEO_MATH_HYPOT
|
|
415
|
+
? math_hypot(&frame, argument_count)
|
|
416
|
+
: math_extremum(
|
|
417
|
+
&frame,
|
|
418
|
+
argument_count,
|
|
419
|
+
operation == OSEO_MATH_MAX
|
|
420
|
+
);
|
|
421
|
+
result = normal(oseo_number(value));
|
|
422
|
+
}
|
|
423
|
+
oseo_roots_release(context, &frame);
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/*
|
|
428
|
+
* Math.sumPrecise accumulates every finite operand exactly. A binary64
|
|
429
|
+
* value is an integer multiple of 2**-1074 below 2**1024, so the sum of
|
|
430
|
+
* fewer than 2**53 of them is an integer multiple of 2**-1074 below
|
|
431
|
+
* 2**1077 in magnitude. The accumulator holds that integer in two's
|
|
432
|
+
* complement over enough 64-bit limbs for 2**2151 plus a sign bit, so
|
|
433
|
+
* no addition can overflow and the only rounding is the final one.
|
|
434
|
+
*/
|
|
435
|
+
#define OSEO_SUM_PRECISE_LIMBS ((size_t)35u)
|
|
436
|
+
|
|
437
|
+
typedef struct {
|
|
438
|
+
uint64_t limbs[OSEO_SUM_PRECISE_LIMBS];
|
|
439
|
+
} OseoExactSum;
|
|
440
|
+
|
|
441
|
+
/* Add or subtract `magnitude * 2**shift` units of 2**-1074. */
|
|
442
|
+
static void exact_sum_add(
|
|
443
|
+
OseoExactSum *sum,
|
|
444
|
+
uint64_t magnitude,
|
|
445
|
+
unsigned shift,
|
|
446
|
+
bool negative
|
|
447
|
+
) {
|
|
448
|
+
size_t index = shift / 64u;
|
|
449
|
+
unsigned offset = shift % 64u;
|
|
450
|
+
uint64_t parts[2] = {
|
|
451
|
+
magnitude << offset,
|
|
452
|
+
offset == 0u ? 0u : magnitude >> (64u - offset),
|
|
453
|
+
};
|
|
454
|
+
uint64_t carry = 0u;
|
|
455
|
+
for (size_t limb = index; limb < OSEO_SUM_PRECISE_LIMBS; limb += 1u) {
|
|
456
|
+
bool in_parts = limb - index < 2u;
|
|
457
|
+
uint64_t part = in_parts ? parts[limb - index] : 0u;
|
|
458
|
+
if (!in_parts && carry == 0u) break;
|
|
459
|
+
uint64_t before = sum->limbs[limb];
|
|
460
|
+
if (negative) {
|
|
461
|
+
uint64_t after = before - part - carry;
|
|
462
|
+
carry = (before < part || before - part < carry) ? 1u : 0u;
|
|
463
|
+
sum->limbs[limb] = after;
|
|
464
|
+
} else {
|
|
465
|
+
uint64_t after = before + part + carry;
|
|
466
|
+
carry = (after < before || (carry != 0u && after == before))
|
|
467
|
+
? 1u
|
|
468
|
+
: 0u;
|
|
469
|
+
sum->limbs[limb] = after;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
static void exact_sum_add_number(OseoExactSum *sum, double value) {
|
|
475
|
+
uint64_t bits;
|
|
476
|
+
memcpy(&bits, &value, sizeof(bits));
|
|
477
|
+
uint64_t fraction = bits & ((UINT64_C(1) << 52) - 1u);
|
|
478
|
+
unsigned exponent = (unsigned)((bits >> 52) & 0x7ffu);
|
|
479
|
+
bool negative = (bits >> 63) != 0u;
|
|
480
|
+
if (exponent == 0u) {
|
|
481
|
+
exact_sum_add(sum, fraction, 0u, negative);
|
|
482
|
+
} else {
|
|
483
|
+
exact_sum_add(
|
|
484
|
+
sum,
|
|
485
|
+
fraction | (UINT64_C(1) << 52),
|
|
486
|
+
exponent - 1u,
|
|
487
|
+
negative
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
static bool exact_sum_bit(const OseoExactSum *sum, size_t bit) {
|
|
493
|
+
return ((sum->limbs[bit / 64u] >> (bit % 64u)) & 1u) != 0u;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/*
|
|
497
|
+
* Round the exact sum to the nearest Number with ties to even. A sum
|
|
498
|
+
* at or beyond 2**1024 - 2**970 rounds to an infinity, which is what
|
|
499
|
+
* the final scaling produces once the rounded significand reaches the
|
|
500
|
+
* binary64 overflow exponent. An exact zero is +0.
|
|
501
|
+
*/
|
|
502
|
+
static double exact_sum_value(OseoExactSum *sum) {
|
|
503
|
+
bool negative =
|
|
504
|
+
(sum->limbs[OSEO_SUM_PRECISE_LIMBS - 1u] >> 63) != 0u;
|
|
505
|
+
if (negative) {
|
|
506
|
+
uint64_t carry = 1u;
|
|
507
|
+
for (size_t limb = 0u; limb < OSEO_SUM_PRECISE_LIMBS; limb += 1u) {
|
|
508
|
+
uint64_t inverted = ~sum->limbs[limb];
|
|
509
|
+
sum->limbs[limb] = inverted + carry;
|
|
510
|
+
carry = carry != 0u && sum->limbs[limb] == 0u ? 1u : 0u;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
size_t top = OSEO_SUM_PRECISE_LIMBS * 64u;
|
|
514
|
+
while (top > 0u && !exact_sum_bit(sum, top - 1u)) top -= 1u;
|
|
515
|
+
if (top == 0u) return 0.0;
|
|
516
|
+
double magnitude;
|
|
517
|
+
if (top <= 53u) {
|
|
518
|
+
/* Below 2**53 units the value is exact as a subnormal or the
|
|
519
|
+
* smallest normal binade. */
|
|
520
|
+
magnitude = ldexp((double)sum->limbs[0], -1074);
|
|
521
|
+
} else {
|
|
522
|
+
size_t shift = top - 53u;
|
|
523
|
+
uint64_t significand = 0u;
|
|
524
|
+
for (size_t bit = 0u; bit < 53u; bit += 1u) {
|
|
525
|
+
if (exact_sum_bit(sum, shift + bit)) {
|
|
526
|
+
significand |= UINT64_C(1) << bit;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
bool round = exact_sum_bit(sum, shift - 1u);
|
|
530
|
+
bool sticky = false;
|
|
531
|
+
for (size_t limb = 0u; !sticky && limb * 64u < shift - 1u;
|
|
532
|
+
limb += 1u) {
|
|
533
|
+
size_t low = shift - 1u - limb * 64u;
|
|
534
|
+
uint64_t mask = low >= 64u
|
|
535
|
+
? ~UINT64_C(0)
|
|
536
|
+
: (UINT64_C(1) << low) - 1u;
|
|
537
|
+
sticky = (sum->limbs[limb] & mask) != 0u;
|
|
538
|
+
}
|
|
539
|
+
if (round && (sticky || (significand & 1u) != 0u)) {
|
|
540
|
+
significand += 1u;
|
|
541
|
+
if (significand == UINT64_C(1) << 53) {
|
|
542
|
+
significand >>= 1;
|
|
543
|
+
shift += 1u;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/* The shift stays below 2,200, so the int conversion is exact. */
|
|
547
|
+
magnitude = ldexp((double)significand, (int)shift - 1074);
|
|
548
|
+
}
|
|
549
|
+
return negative ? -magnitude : magnitude;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/* IteratorClose(iterator, throw) for an error raised by this function. */
|
|
553
|
+
static OseoResult sum_precise_close(
|
|
554
|
+
OseoContext *context,
|
|
555
|
+
OseoValue iterator,
|
|
556
|
+
OseoResult abrupt
|
|
557
|
+
) {
|
|
558
|
+
if (abrupt.status != OSEO_STATUS_THROW || context->has_diagnostic) {
|
|
559
|
+
return abrupt;
|
|
560
|
+
}
|
|
561
|
+
OseoValue slots[2] = {iterator, abrupt.value};
|
|
562
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
563
|
+
oseo_roots_push(context, &frame);
|
|
564
|
+
oseo_context_clear_language_error(context);
|
|
565
|
+
OseoResult closed = oseo_iterator_close(context, slots[0], true);
|
|
566
|
+
OseoResult result = closed.status == OSEO_STATUS_NORMAL
|
|
567
|
+
? (OseoResult){OSEO_STATUS_THROW, slots[1]}
|
|
568
|
+
: closed;
|
|
569
|
+
oseo_roots_pop(context, &frame);
|
|
570
|
+
return result;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
typedef enum {
|
|
574
|
+
OSEO_SUM_MINUS_ZERO,
|
|
575
|
+
OSEO_SUM_FINITE,
|
|
576
|
+
OSEO_SUM_PLUS_INFINITY,
|
|
577
|
+
OSEO_SUM_MINUS_INFINITY,
|
|
578
|
+
OSEO_SUM_NOT_A_NUMBER,
|
|
579
|
+
} OseoSumPreciseState;
|
|
580
|
+
|
|
581
|
+
/*
|
|
582
|
+
* Math.sumPrecise, 21.3.2.34. The iterable is required to be neither
|
|
583
|
+
* undefined nor null before GetIterator, every value must already be a
|
|
584
|
+
* Number, and a non-Number closes the iterator before the TypeError
|
|
585
|
+
* propagates. NaN and opposite infinities make the result NaN, but the
|
|
586
|
+
* iteration still runs to completion so every later value is checked.
|
|
587
|
+
*/
|
|
588
|
+
static OseoResult math_sum_precise(
|
|
589
|
+
OseoContext *context,
|
|
590
|
+
size_t argument_count,
|
|
591
|
+
const OseoValue *arguments
|
|
592
|
+
) {
|
|
593
|
+
OseoValue items =
|
|
594
|
+
argument_count > 0u ? arguments[0] : oseo_undefined();
|
|
595
|
+
uint64_t items_tag = tag_of(items);
|
|
596
|
+
if (items_tag == OSEO_TAG_UNDEFINED || items_tag == OSEO_TAG_NULL) {
|
|
597
|
+
return oseo_internal_throw_error(
|
|
598
|
+
context,
|
|
599
|
+
OSEO_ERROR_TYPE,
|
|
600
|
+
"Math.sumPrecise requires an iterable."
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
OseoValue slots[3] = {items, oseo_undefined(), oseo_undefined()};
|
|
604
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
605
|
+
oseo_roots_push(context, &frame);
|
|
606
|
+
OseoValue next_method = oseo_undefined();
|
|
607
|
+
OseoResult result = oseo_iterator_get(context, slots[0], &next_method);
|
|
608
|
+
slots[0] = result.value;
|
|
609
|
+
if (result.status == OSEO_STATUS_NORMAL) slots[1] = next_method;
|
|
610
|
+
OseoExactSum sum;
|
|
611
|
+
memset(&sum, 0, sizeof(sum));
|
|
612
|
+
OseoSumPreciseState state = OSEO_SUM_MINUS_ZERO;
|
|
613
|
+
uint64_t count = 0u;
|
|
614
|
+
bool done = false;
|
|
615
|
+
while (result.status == OSEO_STATUS_NORMAL && !done) {
|
|
616
|
+
result = oseo_iterator_next(
|
|
617
|
+
context,
|
|
618
|
+
slots[0],
|
|
619
|
+
slots[1],
|
|
620
|
+
&slots[2],
|
|
621
|
+
&done
|
|
622
|
+
);
|
|
623
|
+
if (result.status != OSEO_STATUS_NORMAL || done) break;
|
|
624
|
+
count += 1u;
|
|
625
|
+
if (count >= UINT64_C(9007199254740992)) {
|
|
626
|
+
result = sum_precise_close(
|
|
627
|
+
context,
|
|
628
|
+
slots[0],
|
|
629
|
+
oseo_internal_throw_error(
|
|
630
|
+
context,
|
|
631
|
+
OSEO_ERROR_RANGE,
|
|
632
|
+
"Math.sumPrecise received too many values."
|
|
633
|
+
)
|
|
634
|
+
);
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
if (!is_number(slots[2])) {
|
|
638
|
+
result = sum_precise_close(
|
|
639
|
+
context,
|
|
640
|
+
slots[0],
|
|
641
|
+
oseo_internal_throw_error(
|
|
642
|
+
context,
|
|
643
|
+
OSEO_ERROR_TYPE,
|
|
644
|
+
"Math.sumPrecise values must be Numbers."
|
|
645
|
+
)
|
|
646
|
+
);
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
double value = number_value(slots[2]);
|
|
650
|
+
if (state == OSEO_SUM_NOT_A_NUMBER) continue;
|
|
651
|
+
if (isnan(value)) {
|
|
652
|
+
state = OSEO_SUM_NOT_A_NUMBER;
|
|
653
|
+
} else if (isinf(value)) {
|
|
654
|
+
OseoSumPreciseState same = value > 0.0
|
|
655
|
+
? OSEO_SUM_PLUS_INFINITY
|
|
656
|
+
: OSEO_SUM_MINUS_INFINITY;
|
|
657
|
+
OseoSumPreciseState opposite = value > 0.0
|
|
658
|
+
? OSEO_SUM_MINUS_INFINITY
|
|
659
|
+
: OSEO_SUM_PLUS_INFINITY;
|
|
660
|
+
state = state == opposite ? OSEO_SUM_NOT_A_NUMBER : same;
|
|
661
|
+
} else if (!(value == 0.0 && signbit(value)) &&
|
|
662
|
+
(state == OSEO_SUM_MINUS_ZERO ||
|
|
663
|
+
state == OSEO_SUM_FINITE)) {
|
|
664
|
+
state = OSEO_SUM_FINITE;
|
|
665
|
+
exact_sum_add_number(&sum, value);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
oseo_roots_pop(context, &frame);
|
|
669
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
670
|
+
switch (state) {
|
|
671
|
+
case OSEO_SUM_NOT_A_NUMBER:
|
|
672
|
+
return normal(oseo_number(NAN));
|
|
673
|
+
case OSEO_SUM_PLUS_INFINITY:
|
|
674
|
+
return normal(oseo_number((double)INFINITY));
|
|
675
|
+
case OSEO_SUM_MINUS_INFINITY:
|
|
676
|
+
return normal(oseo_number(-(double)INFINITY));
|
|
677
|
+
case OSEO_SUM_MINUS_ZERO:
|
|
678
|
+
return normal(oseo_number(-0.0));
|
|
679
|
+
case OSEO_SUM_FINITE:
|
|
680
|
+
break;
|
|
681
|
+
}
|
|
682
|
+
return normal(oseo_number(exact_sum_value(&sum)));
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
static OseoResult math_call(
|
|
686
|
+
OseoContext *context,
|
|
687
|
+
size_t operation,
|
|
688
|
+
size_t argument_count,
|
|
689
|
+
const OseoValue *arguments
|
|
690
|
+
) {
|
|
691
|
+
if (operation == OSEO_MATH_RANDOM) {
|
|
692
|
+
return normal(oseo_number(math_random(context)));
|
|
693
|
+
}
|
|
694
|
+
if (operation == OSEO_MATH_SUM_PRECISE) {
|
|
695
|
+
if (argument_count > 0u && arguments == NULL) {
|
|
696
|
+
return failure(
|
|
697
|
+
context,
|
|
698
|
+
"OSEO2001",
|
|
699
|
+
"Math arguments are missing."
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
return math_sum_precise(context, argument_count, arguments);
|
|
703
|
+
}
|
|
704
|
+
if (operation == OSEO_MATH_HYPOT || operation == OSEO_MATH_MAX ||
|
|
705
|
+
operation == OSEO_MATH_MIN) {
|
|
706
|
+
return math_variadic(context, operation, argument_count, arguments);
|
|
707
|
+
}
|
|
708
|
+
if (argument_count > 0u && arguments == NULL) {
|
|
709
|
+
return failure(context, "OSEO2001", "Math arguments are missing.");
|
|
710
|
+
}
|
|
711
|
+
double first = 0.0;
|
|
712
|
+
OseoResult result =
|
|
713
|
+
math_operand(context, argument_count, arguments, 0u, &first);
|
|
714
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
715
|
+
if (math_is_unary(operation)) {
|
|
716
|
+
return normal(oseo_number(math_unary_value(operation, first)));
|
|
717
|
+
}
|
|
718
|
+
double second = 0.0;
|
|
719
|
+
result = math_operand(context, argument_count, arguments, 1u, &second);
|
|
720
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
721
|
+
if (operation == OSEO_MATH_ATAN2) {
|
|
722
|
+
return normal(oseo_number(atan2(first, second)));
|
|
723
|
+
}
|
|
724
|
+
if (operation == OSEO_MATH_IMUL) {
|
|
725
|
+
return normal(oseo_number(math_imul(first, second)));
|
|
726
|
+
}
|
|
727
|
+
return normal(
|
|
728
|
+
oseo_number(oseo_internal_number_exponentiate(first, second))
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
OseoResult oseo_internal_math_builtin_dispatch(
|
|
733
|
+
OseoContext *context,
|
|
734
|
+
size_t code_id,
|
|
735
|
+
OseoValue callee,
|
|
736
|
+
OseoValue receiver,
|
|
737
|
+
size_t argument_count,
|
|
738
|
+
const OseoValue *arguments,
|
|
739
|
+
OseoValue new_target
|
|
740
|
+
) {
|
|
741
|
+
(void)callee;
|
|
742
|
+
(void)receiver;
|
|
743
|
+
if (code_id < OSEO_MATH_FUNCTION_CODE_ID_FIRST ||
|
|
744
|
+
code_id > OSEO_MATH_FUNCTION_CODE_ID_LAST) {
|
|
745
|
+
return oseo_unknown_function(context, code_id);
|
|
746
|
+
}
|
|
747
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
748
|
+
return oseo_internal_throw_error(
|
|
749
|
+
context,
|
|
750
|
+
OSEO_ERROR_TYPE,
|
|
751
|
+
"Math function is not a constructor."
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
size_t operation = OSEO_MATH_FUNCTION_CODE_ID_LAST - code_id;
|
|
755
|
+
return math_call(context, operation, argument_count, arguments);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
static OseoResult create_math_function(
|
|
759
|
+
OseoContext *context,
|
|
760
|
+
size_t operation
|
|
761
|
+
) {
|
|
762
|
+
const OseoMathFunction *entry = &math_functions[operation];
|
|
763
|
+
size_t name_length = strlen(entry->name);
|
|
764
|
+
uint16_t units[16];
|
|
765
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
766
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
767
|
+
}
|
|
768
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
769
|
+
units[index] = (uint16_t)(unsigned char)entry->name[index];
|
|
770
|
+
}
|
|
771
|
+
OseoValue environment = oseo_undefined();
|
|
772
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
773
|
+
oseo_roots_push(context, &frame);
|
|
774
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
775
|
+
environment = result.value;
|
|
776
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
777
|
+
result = oseo_function_create(
|
|
778
|
+
context,
|
|
779
|
+
OSEO_MATH_FUNCTION_CODE_ID_LAST - operation,
|
|
780
|
+
environment,
|
|
781
|
+
units,
|
|
782
|
+
name_length,
|
|
783
|
+
entry->length,
|
|
784
|
+
OSEO_FUNCTION_INTERNAL,
|
|
785
|
+
oseo_undefined(),
|
|
786
|
+
oseo_undefined(),
|
|
787
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
oseo_roots_pop(context, &frame);
|
|
791
|
+
return result;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
static OseoResult define_math_property(
|
|
795
|
+
OseoContext *context,
|
|
796
|
+
OseoValue object,
|
|
797
|
+
const char *name,
|
|
798
|
+
OseoValue value,
|
|
799
|
+
OseoPropertyAttributes attributes
|
|
800
|
+
) {
|
|
801
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
802
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
803
|
+
oseo_roots_push(context, &frame);
|
|
804
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
805
|
+
slots[2] = result.value;
|
|
806
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
807
|
+
result = oseo_object_define(
|
|
808
|
+
context,
|
|
809
|
+
slots[0],
|
|
810
|
+
slots[2],
|
|
811
|
+
slots[1],
|
|
812
|
+
attributes
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
oseo_roots_pop(context, &frame);
|
|
816
|
+
return result;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
OseoResult oseo_internal_math_intrinsic(OseoContext *context) {
|
|
820
|
+
OseoValue *slot = &context->realm->intrinsics[OSEO_INTRINSIC_MATH];
|
|
821
|
+
if (is_object(*slot)) return normal(*slot);
|
|
822
|
+
size_t entry_allocations = context->allocations;
|
|
823
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
824
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
825
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
826
|
+
result = oseo_internal_intrinsic(
|
|
827
|
+
context,
|
|
828
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
829
|
+
);
|
|
830
|
+
frame.slots[0] = result.value;
|
|
831
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
832
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
833
|
+
frame.slots[0] = result.value;
|
|
834
|
+
}
|
|
835
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
836
|
+
*slot = frame.slots[0];
|
|
837
|
+
}
|
|
838
|
+
static const OseoPropertyAttributes constant_attributes =
|
|
839
|
+
{false, false, false, false};
|
|
840
|
+
for (size_t index = 0u;
|
|
841
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
842
|
+
index < OSEO_MATH_CONSTANT_COUNT;
|
|
843
|
+
index += 1u) {
|
|
844
|
+
result = define_math_property(
|
|
845
|
+
context,
|
|
846
|
+
frame.slots[0],
|
|
847
|
+
math_constants[index].name,
|
|
848
|
+
oseo_number(math_constants[index].value),
|
|
849
|
+
constant_attributes
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
static const OseoPropertyAttributes method_attributes =
|
|
853
|
+
{true, false, true, false};
|
|
854
|
+
for (size_t index = 0u;
|
|
855
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
856
|
+
index < OSEO_MATH_FUNCTION_COUNT;
|
|
857
|
+
index += 1u) {
|
|
858
|
+
result = create_math_function(context, index);
|
|
859
|
+
frame.slots[1] = result.value;
|
|
860
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
861
|
+
result = define_math_property(
|
|
862
|
+
context,
|
|
863
|
+
frame.slots[0],
|
|
864
|
+
math_functions[index].name,
|
|
865
|
+
frame.slots[1],
|
|
866
|
+
method_attributes
|
|
867
|
+
);
|
|
868
|
+
}
|
|
869
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
870
|
+
result = oseo_internal_well_known_symbol(
|
|
871
|
+
context,
|
|
872
|
+
OSEO_WELL_KNOWN_TO_STRING_TAG
|
|
873
|
+
);
|
|
874
|
+
frame.slots[1] = result.value;
|
|
875
|
+
}
|
|
876
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
877
|
+
result = oseo_internal_ascii_string(context, "Math");
|
|
878
|
+
frame.slots[2] = result.value;
|
|
879
|
+
}
|
|
880
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
881
|
+
result = oseo_object_define(
|
|
882
|
+
context,
|
|
883
|
+
frame.slots[0],
|
|
884
|
+
frame.slots[1],
|
|
885
|
+
frame.slots[2],
|
|
886
|
+
(OseoPropertyAttributes){true, false, false, false}
|
|
887
|
+
);
|
|
888
|
+
}
|
|
889
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
890
|
+
*slot = oseo_undefined();
|
|
891
|
+
} else if (context->observe_specialization) {
|
|
892
|
+
context->allocations = entry_allocations;
|
|
893
|
+
}
|
|
894
|
+
oseo_roots_release(context, &frame);
|
|
895
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(*slot) : result;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
OseoResult oseo_internal_install_math_global(
|
|
899
|
+
OseoContext *context,
|
|
900
|
+
OseoValue global
|
|
901
|
+
) {
|
|
902
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
903
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
904
|
+
oseo_roots_push(context, &frame);
|
|
905
|
+
OseoResult result = oseo_internal_math_intrinsic(context);
|
|
906
|
+
slots[1] = result.value;
|
|
907
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
908
|
+
result = define_math_property(
|
|
909
|
+
context,
|
|
910
|
+
slots[0],
|
|
911
|
+
"Math",
|
|
912
|
+
slots[1],
|
|
913
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
oseo_roots_pop(context, &frame);
|
|
917
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
|
|
918
|
+
}
|