@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,1111 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
|
|
3
|
+
|
|
4
|
+
#include "runtime_internal.h"
|
|
5
|
+
|
|
6
|
+
#include <math.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* The DataView constructor, its prototype accessors, and the eleven get
|
|
11
|
+
* and eleven set element accessors.
|
|
12
|
+
*
|
|
13
|
+
* A view never owns a Data Block. It holds only its buffer's value, so
|
|
14
|
+
* every access rereads that buffer's current pointer, byte length, and
|
|
15
|
+
* detached flag, and the collector keeps the block alive through the
|
|
16
|
+
* buffer rather than through the view. Nothing here frees a block, and
|
|
17
|
+
* nothing here caches one across a safepoint.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/* Element Size for each element type, in bytes. */
|
|
21
|
+
static const size_t data_view_element_size[OSEO_DATA_VIEW_ELEMENT_COUNT] = {
|
|
22
|
+
1u, 1u, 2u, 2u, 4u, 4u, 2u, 4u, 8u, 8u, 8u,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
static const char *const data_view_get_names[OSEO_DATA_VIEW_ELEMENT_COUNT] = {
|
|
26
|
+
"getInt8",
|
|
27
|
+
"getUint8",
|
|
28
|
+
"getInt16",
|
|
29
|
+
"getUint16",
|
|
30
|
+
"getInt32",
|
|
31
|
+
"getUint32",
|
|
32
|
+
"getFloat16",
|
|
33
|
+
"getFloat32",
|
|
34
|
+
"getFloat64",
|
|
35
|
+
"getBigInt64",
|
|
36
|
+
"getBigUint64",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
static const char *const data_view_set_names[OSEO_DATA_VIEW_ELEMENT_COUNT] = {
|
|
40
|
+
"setInt8",
|
|
41
|
+
"setUint8",
|
|
42
|
+
"setInt16",
|
|
43
|
+
"setUint16",
|
|
44
|
+
"setInt32",
|
|
45
|
+
"setUint32",
|
|
46
|
+
"setFloat16",
|
|
47
|
+
"setFloat32",
|
|
48
|
+
"setFloat64",
|
|
49
|
+
"setBigInt64",
|
|
50
|
+
"setBigUint64",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/* IsBigIntElementType(type). */
|
|
54
|
+
static bool data_view_element_is_bigint(OseoDataViewElement element) {
|
|
55
|
+
return element == OSEO_DATA_VIEW_BIGINT64 ||
|
|
56
|
+
element == OSEO_DATA_VIEW_BIGUINT64;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
* The IEEE 754 binary interchange encoding of `value` in the format with
|
|
61
|
+
* `significand_bits` stored significand bits and `exponent_bits`
|
|
62
|
+
* exponent bits, rounded to nearest with ties to even.
|
|
63
|
+
*
|
|
64
|
+
* The whole computation is exact integer arithmetic over the operand's
|
|
65
|
+
* own binary64 encoding, so no narrowing floating conversion happens and
|
|
66
|
+
* an operand outside the destination range cannot reach one. ECMA-262
|
|
67
|
+
* leaves the encoding of a NaN to the implementation for every format,
|
|
68
|
+
* so a NaN becomes this format's canonical quiet NaN. A zero keeps its
|
|
69
|
+
* sign.
|
|
70
|
+
*/
|
|
71
|
+
static uint64_t encode_ieee(
|
|
72
|
+
double value,
|
|
73
|
+
unsigned significand_bits,
|
|
74
|
+
unsigned exponent_bits
|
|
75
|
+
) {
|
|
76
|
+
uint64_t source;
|
|
77
|
+
memcpy(&source, &value, sizeof(source));
|
|
78
|
+
uint64_t sign = (source >> 63u) << (significand_bits + exponent_bits);
|
|
79
|
+
uint64_t exponent_mask = (UINT64_C(1) << exponent_bits) - 1u;
|
|
80
|
+
uint64_t infinity = exponent_mask << significand_bits;
|
|
81
|
+
uint64_t implicit = UINT64_C(1) << significand_bits;
|
|
82
|
+
uint64_t mantissa = source & UINT64_C(0x000fffffffffffff);
|
|
83
|
+
int source_exponent = (int)((source >> 52u) & UINT64_C(0x7ff));
|
|
84
|
+
if (source_exponent == 0x7ff) {
|
|
85
|
+
if (mantissa != 0u) return sign | infinity | (implicit >> 1u);
|
|
86
|
+
return sign | infinity;
|
|
87
|
+
}
|
|
88
|
+
/* |value| is exactly `magnitude` times 2**scale. */
|
|
89
|
+
uint64_t magnitude;
|
|
90
|
+
int scale;
|
|
91
|
+
if (source_exponent == 0) {
|
|
92
|
+
magnitude = mantissa;
|
|
93
|
+
scale = -1074;
|
|
94
|
+
} else {
|
|
95
|
+
magnitude = mantissa | (UINT64_C(1) << 52u);
|
|
96
|
+
scale = source_exponent - 1075;
|
|
97
|
+
}
|
|
98
|
+
if (magnitude == 0u) return sign;
|
|
99
|
+
int highest = 63;
|
|
100
|
+
while ((magnitude >> (unsigned)highest) == 0u) highest -= 1;
|
|
101
|
+
int exponent = highest + scale;
|
|
102
|
+
int bias = (int)(exponent_mask >> 1u);
|
|
103
|
+
if (exponent > bias) return sign | infinity;
|
|
104
|
+
/* A normal of this exponent has its own quantum; every subnormal
|
|
105
|
+
* shares one, and rounding a subnormal up can reach the smallest
|
|
106
|
+
* normal, which is why the two cases stay distinct below. */
|
|
107
|
+
int minimum = 1 - bias;
|
|
108
|
+
bool subnormal = exponent < minimum;
|
|
109
|
+
int quantum = (subnormal ? minimum : exponent) - (int)significand_bits;
|
|
110
|
+
int shift = quantum - scale;
|
|
111
|
+
uint64_t rounded;
|
|
112
|
+
if (shift <= 0) {
|
|
113
|
+
/* The operand is already a multiple of the quantum; the result
|
|
114
|
+
* stays below 2**(significand_bits + 1), so the shift is exact. */
|
|
115
|
+
rounded = magnitude << (unsigned)(-shift);
|
|
116
|
+
} else if (shift >= 64) {
|
|
117
|
+
/* The exact quotient is below one half, so nothing rounds up. */
|
|
118
|
+
rounded = 0u;
|
|
119
|
+
} else {
|
|
120
|
+
uint64_t truncated = magnitude >> (unsigned)shift;
|
|
121
|
+
uint64_t remainder =
|
|
122
|
+
magnitude & ((UINT64_C(1) << (unsigned)shift) - 1u);
|
|
123
|
+
uint64_t half = UINT64_C(1) << (unsigned)(shift - 1);
|
|
124
|
+
if (remainder > half || (remainder == half && (truncated & 1u) != 0u)) {
|
|
125
|
+
truncated += 1u;
|
|
126
|
+
}
|
|
127
|
+
rounded = truncated;
|
|
128
|
+
}
|
|
129
|
+
if (subnormal) {
|
|
130
|
+
/* A subnormal, or the smallest normal the rounding carried into. */
|
|
131
|
+
if (rounded < implicit) return sign | rounded;
|
|
132
|
+
return sign | implicit;
|
|
133
|
+
}
|
|
134
|
+
if (rounded == implicit << 1u) {
|
|
135
|
+
rounded >>= 1u;
|
|
136
|
+
exponent += 1;
|
|
137
|
+
if (exponent > bias) return sign | infinity;
|
|
138
|
+
}
|
|
139
|
+
return sign | ((uint64_t)(exponent + bias) << significand_bits) |
|
|
140
|
+
(rounded - implicit);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* The exact Number one binary interchange encoding denotes. */
|
|
144
|
+
static double decode_ieee(
|
|
145
|
+
uint64_t bits,
|
|
146
|
+
unsigned significand_bits,
|
|
147
|
+
unsigned exponent_bits
|
|
148
|
+
) {
|
|
149
|
+
uint64_t exponent_mask = (UINT64_C(1) << exponent_bits) - 1u;
|
|
150
|
+
uint64_t implicit = UINT64_C(1) << significand_bits;
|
|
151
|
+
bool negative =
|
|
152
|
+
((bits >> (significand_bits + exponent_bits)) & 1u) != 0u;
|
|
153
|
+
uint64_t field = (bits >> significand_bits) & exponent_mask;
|
|
154
|
+
uint64_t fraction = bits & (implicit - 1u);
|
|
155
|
+
int bias = (int)(exponent_mask >> 1u);
|
|
156
|
+
double magnitude;
|
|
157
|
+
if (field == exponent_mask) {
|
|
158
|
+
magnitude = fraction == 0u ? (double)INFINITY : (double)NAN;
|
|
159
|
+
} else if (field == 0u) {
|
|
160
|
+
magnitude =
|
|
161
|
+
ldexp((double)fraction, 1 - bias - (int)significand_bits);
|
|
162
|
+
} else {
|
|
163
|
+
magnitude = ldexp(
|
|
164
|
+
(double)(fraction | implicit),
|
|
165
|
+
(int)field - bias - (int)significand_bits
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return negative ? -magnitude : magnitude;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/*
|
|
172
|
+
* ToInt8, ToUint8, ToInt16, ToUint16, ToInt32, and ToUint32 over an
|
|
173
|
+
* already converted Number: the exact truncated value modulo 2**bits.
|
|
174
|
+
* `fmod` is exact for binary64 operands, so the wrapped value is the
|
|
175
|
+
* specified integer and its conversion to an unsigned host integer is
|
|
176
|
+
* in range.
|
|
177
|
+
*/
|
|
178
|
+
static uint64_t number_to_wrapped(double value, size_t bytes) {
|
|
179
|
+
if (!isfinite(value)) return 0u;
|
|
180
|
+
double modulus = ldexp(1.0, (int)(bytes * 8u));
|
|
181
|
+
double wrapped = fmod(trunc(value), modulus);
|
|
182
|
+
if (wrapped < 0.0) wrapped += modulus;
|
|
183
|
+
return (uint64_t)wrapped;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
* The signed reading of a `bytes`-wide two's-complement encoding. Only
|
|
188
|
+
* the one, two, and four byte element types reach it, so the value and
|
|
189
|
+
* the modulus are both exact binary64 integers.
|
|
190
|
+
*/
|
|
191
|
+
static double wrapped_to_signed(uint64_t bits, size_t bytes) {
|
|
192
|
+
double modulus = ldexp(1.0, (int)(bytes * 8u));
|
|
193
|
+
double magnitude = (double)bits;
|
|
194
|
+
return magnitude >= modulus / 2.0 ? magnitude - modulus : magnitude;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* NumericToRawBytes for every element type whose value is a Number. */
|
|
198
|
+
static uint64_t number_to_raw_bytes(
|
|
199
|
+
double value,
|
|
200
|
+
OseoDataViewElement element
|
|
201
|
+
) {
|
|
202
|
+
if (element == OSEO_DATA_VIEW_FLOAT16) return encode_ieee(value, 10u, 5u);
|
|
203
|
+
if (element == OSEO_DATA_VIEW_FLOAT32) return encode_ieee(value, 23u, 8u);
|
|
204
|
+
if (element == OSEO_DATA_VIEW_FLOAT64) {
|
|
205
|
+
uint64_t bits;
|
|
206
|
+
memcpy(&bits, &value, sizeof(bits));
|
|
207
|
+
return bits;
|
|
208
|
+
}
|
|
209
|
+
return number_to_wrapped(value, data_view_element_size[element]);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
uint16_t oseo_internal_float16_bits(double value) {
|
|
213
|
+
return (uint16_t)encode_ieee(value, 10u, 5u);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
double oseo_internal_float16_number(uint16_t bits) {
|
|
217
|
+
return decode_ieee(bits, 10u, 5u);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* RawBytesToNumeric for every element type whose value is a Number. */
|
|
221
|
+
static double raw_bytes_to_number(
|
|
222
|
+
uint64_t bits,
|
|
223
|
+
OseoDataViewElement element
|
|
224
|
+
) {
|
|
225
|
+
if (element == OSEO_DATA_VIEW_FLOAT16) return decode_ieee(bits, 10u, 5u);
|
|
226
|
+
if (element == OSEO_DATA_VIEW_FLOAT32) return decode_ieee(bits, 23u, 8u);
|
|
227
|
+
if (element == OSEO_DATA_VIEW_FLOAT64) {
|
|
228
|
+
double value;
|
|
229
|
+
memcpy(&value, &bits, sizeof(value));
|
|
230
|
+
return value;
|
|
231
|
+
}
|
|
232
|
+
if (element == OSEO_DATA_VIEW_INT8 || element == OSEO_DATA_VIEW_INT16 ||
|
|
233
|
+
element == OSEO_DATA_VIEW_INT32) {
|
|
234
|
+
return wrapped_to_signed(bits, data_view_element_size[element]);
|
|
235
|
+
}
|
|
236
|
+
return (double)bits;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/* RequireInternalSlot(this, [[DataView]]). */
|
|
240
|
+
static OseoResult data_view_receiver(
|
|
241
|
+
OseoContext *context,
|
|
242
|
+
OseoValue receiver
|
|
243
|
+
) {
|
|
244
|
+
if (is_data_view(receiver)) return normal(receiver);
|
|
245
|
+
return oseo_internal_throw_error(
|
|
246
|
+
context,
|
|
247
|
+
OSEO_ERROR_TYPE,
|
|
248
|
+
"DataView method receiver is not a DataView."
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/*
|
|
253
|
+
* IsViewOutOfBounds over a witness record taken now. A detached buffer
|
|
254
|
+
* is out of bounds, a length-tracking view only needs its offset to
|
|
255
|
+
* still be inside the buffer, and a zero-length view exactly at the end
|
|
256
|
+
* of the buffer is in bounds.
|
|
257
|
+
*/
|
|
258
|
+
static bool data_view_out_of_bounds(const OseoDataView *view) {
|
|
259
|
+
const OseoArrayBuffer *buffer = array_buffer_object(view->buffer);
|
|
260
|
+
if (buffer->detached) return true;
|
|
261
|
+
size_t length = array_buffer_current_length(buffer);
|
|
262
|
+
if (view->byte_offset > length) return true;
|
|
263
|
+
if (view->track_length) return false;
|
|
264
|
+
return view->byte_length > length - view->byte_offset;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/*
|
|
268
|
+
* GetViewByteLength. The caller has already rejected an out-of-bounds
|
|
269
|
+
* view, so a tracking view's offset is inside its buffer and the
|
|
270
|
+
* subtraction cannot wrap.
|
|
271
|
+
*/
|
|
272
|
+
static size_t data_view_length(const OseoDataView *view) {
|
|
273
|
+
if (!view->track_length) return view->byte_length;
|
|
274
|
+
return array_buffer_current_length(array_buffer_object(view->buffer)) -
|
|
275
|
+
view->byte_offset;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/*
|
|
279
|
+
* OrdinaryCreateFromConstructor(newTarget, "%DataView.prototype%"). The
|
|
280
|
+
* `prototype` read is the specified Get, so a new target whose property
|
|
281
|
+
* is an accessor runs it, and a non-object result falls back to the
|
|
282
|
+
* realm prototype.
|
|
283
|
+
*/
|
|
284
|
+
static OseoResult data_view_prototype_from_target(
|
|
285
|
+
OseoContext *context,
|
|
286
|
+
OseoValue new_target,
|
|
287
|
+
OseoValue *prototype
|
|
288
|
+
) {
|
|
289
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
290
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
291
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
292
|
+
frame.slots[0] = new_target;
|
|
293
|
+
result = oseo_internal_ascii_string(context, "prototype");
|
|
294
|
+
frame.slots[1] = result.value;
|
|
295
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
296
|
+
result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
|
|
297
|
+
frame.slots[1] = result.value;
|
|
298
|
+
}
|
|
299
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
|
|
300
|
+
result = oseo_internal_constructor_realm_default(
|
|
301
|
+
context,
|
|
302
|
+
frame.slots[0],
|
|
303
|
+
OSEO_INTRINSIC_DATA_VIEW_PROTOTYPE
|
|
304
|
+
);
|
|
305
|
+
frame.slots[1] = result.value;
|
|
306
|
+
}
|
|
307
|
+
if (result.status == OSEO_STATUS_NORMAL) *prototype = frame.slots[1];
|
|
308
|
+
oseo_roots_release(context, &frame);
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/*
|
|
313
|
+
* One DataView over `buffer`. The record is published with its final
|
|
314
|
+
* slots, so the collector traces the buffer from the moment the view
|
|
315
|
+
* exists and the view never appears with an untraced or absent buffer.
|
|
316
|
+
*/
|
|
317
|
+
static OseoResult data_view_allocate(
|
|
318
|
+
OseoContext *context,
|
|
319
|
+
OseoValue prototype,
|
|
320
|
+
OseoValue buffer,
|
|
321
|
+
size_t byte_offset,
|
|
322
|
+
size_t byte_length,
|
|
323
|
+
bool track_length
|
|
324
|
+
) {
|
|
325
|
+
OseoValue slots[2] = {prototype, buffer};
|
|
326
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
327
|
+
oseo_roots_push(context, &frame);
|
|
328
|
+
OseoDataView *view =
|
|
329
|
+
oseo_internal_allocate_heap_bytes(context, sizeof(*view));
|
|
330
|
+
if (view == NULL) {
|
|
331
|
+
oseo_roots_pop(context, &frame);
|
|
332
|
+
return failure(context, "OSEO2001", "DataView allocation failed.");
|
|
333
|
+
}
|
|
334
|
+
view->ordinary.prototype = slots[0];
|
|
335
|
+
view->ordinary.properties = NULL;
|
|
336
|
+
view->ordinary.property_capacity = 0u;
|
|
337
|
+
view->ordinary.property_count = 0u;
|
|
338
|
+
oseo_internal_init_dense_index(&view->ordinary);
|
|
339
|
+
view->ordinary.private_elements = NULL;
|
|
340
|
+
view->ordinary.private_element_capacity = 0u;
|
|
341
|
+
view->ordinary.private_element_count = 0u;
|
|
342
|
+
view->ordinary.shape_id = context->next_shape_id;
|
|
343
|
+
context->next_shape_id += 1u;
|
|
344
|
+
view->ordinary.array_length = 0u;
|
|
345
|
+
view->ordinary.dictionary = false;
|
|
346
|
+
view->ordinary.length_writable = false;
|
|
347
|
+
view->ordinary.extensible = true;
|
|
348
|
+
view->ordinary.module_namespace = false;
|
|
349
|
+
view->ordinary.immutable_prototype = false;
|
|
350
|
+
view->ordinary.global_object = false;
|
|
351
|
+
view->ordinary.error_data = false;
|
|
352
|
+
view->ordinary.number_data = false;
|
|
353
|
+
view->ordinary.number_value = oseo_undefined();
|
|
354
|
+
view->ordinary.primitive_data = false;
|
|
355
|
+
view->ordinary.primitive_value = oseo_undefined();
|
|
356
|
+
view->ordinary.primitive_wrapper_methods_initialized = false;
|
|
357
|
+
view->ordinary.virtual_string_iterator = false;
|
|
358
|
+
view->ordinary.virtual_string_iterator_configurable = false;
|
|
359
|
+
view->ordinary.virtual_string_iterator_enumerable = false;
|
|
360
|
+
view->ordinary.virtual_string_iterator_writable = false;
|
|
361
|
+
view->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
|
|
362
|
+
view->ordinary.iterator_target = oseo_undefined();
|
|
363
|
+
view->ordinary.iterator_index = 0u;
|
|
364
|
+
view->ordinary.regexp_string_iterator = false;
|
|
365
|
+
view->ordinary.regexp_iterator_regexp = oseo_undefined();
|
|
366
|
+
view->ordinary.regexp_iterator_subject = oseo_undefined();
|
|
367
|
+
view->ordinary.regexp_iterator_global = false;
|
|
368
|
+
view->ordinary.regexp_iterator_unicode = false;
|
|
369
|
+
view->ordinary.regexp_iterator_complete = false;
|
|
370
|
+
view->ordinary.async_from_sync = false;
|
|
371
|
+
view->ordinary.async_sync_iterator = oseo_undefined();
|
|
372
|
+
view->ordinary.wrap_for_valid_iterator = false;
|
|
373
|
+
view->ordinary.wrapped_iterator = oseo_undefined();
|
|
374
|
+
view->ordinary.wrapped_next = oseo_undefined();
|
|
375
|
+
view->ordinary.generator = NULL;
|
|
376
|
+
view->ordinary.arguments_object = false;
|
|
377
|
+
view->ordinary.mapped_arguments = false;
|
|
378
|
+
view->buffer = slots[1];
|
|
379
|
+
view->byte_offset = byte_offset;
|
|
380
|
+
view->byte_length = byte_length;
|
|
381
|
+
view->track_length = track_length;
|
|
382
|
+
OseoResult published = oseo_internal_publish_heap(
|
|
383
|
+
context,
|
|
384
|
+
&view->ordinary.header,
|
|
385
|
+
OSEO_HEAP_DATA_VIEW
|
|
386
|
+
);
|
|
387
|
+
oseo_roots_pop(context, &frame);
|
|
388
|
+
return published;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/*
|
|
392
|
+
* The DataView constructor, 25.3.2.1. Both length arguments run through
|
|
393
|
+
* ToIndex before the buffer is inspected again, the new target's
|
|
394
|
+
* `prototype` read sits between the two validations the specification
|
|
395
|
+
* performs, and the second validation observes whatever that arbitrary
|
|
396
|
+
* code did to the buffer.
|
|
397
|
+
*/
|
|
398
|
+
static OseoResult data_view_construct(
|
|
399
|
+
OseoContext *context,
|
|
400
|
+
size_t argument_count,
|
|
401
|
+
const OseoValue *arguments,
|
|
402
|
+
OseoValue new_target
|
|
403
|
+
) {
|
|
404
|
+
if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
|
|
405
|
+
return oseo_internal_throw_error(
|
|
406
|
+
context,
|
|
407
|
+
OSEO_ERROR_TYPE,
|
|
408
|
+
"DataView requires new."
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
OseoValue buffer = argument_count == 0u ? oseo_undefined() : arguments[0];
|
|
412
|
+
if (!is_array_buffer(buffer)) {
|
|
413
|
+
return oseo_internal_throw_error(
|
|
414
|
+
context,
|
|
415
|
+
OSEO_ERROR_TYPE,
|
|
416
|
+
"DataView requires an ArrayBuffer."
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
OseoValue slots[3] = {new_target, buffer, oseo_undefined()};
|
|
420
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
421
|
+
oseo_roots_push(context, &frame);
|
|
422
|
+
double offset = 0.0;
|
|
423
|
+
OseoResult result = oseo_internal_to_index(
|
|
424
|
+
context,
|
|
425
|
+
argument_count < 2u ? oseo_undefined() : arguments[1],
|
|
426
|
+
"DataView byte offset is outside the admitted index range.",
|
|
427
|
+
&offset
|
|
428
|
+
);
|
|
429
|
+
/* The conversion is a safepoint and can run arbitrary code, so the
|
|
430
|
+
* buffer record is reacquired from the rooted value every time. */
|
|
431
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
432
|
+
array_buffer_object(slots[1])->detached) {
|
|
433
|
+
result = oseo_internal_throw_error(
|
|
434
|
+
context,
|
|
435
|
+
OSEO_ERROR_TYPE,
|
|
436
|
+
"DataView requires an attached ArrayBuffer."
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
double buffer_length = 0.0;
|
|
440
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
441
|
+
buffer_length = (double)array_buffer_current_length(
|
|
442
|
+
array_buffer_object(slots[1])
|
|
443
|
+
);
|
|
444
|
+
if (offset > buffer_length) {
|
|
445
|
+
result = oseo_internal_throw_error(
|
|
446
|
+
context,
|
|
447
|
+
OSEO_ERROR_RANGE,
|
|
448
|
+
"DataView byte offset is past the end of its buffer."
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
OseoValue requested =
|
|
453
|
+
argument_count < 3u ? oseo_undefined() : arguments[2];
|
|
454
|
+
bool track_length = false;
|
|
455
|
+
double length = 0.0;
|
|
456
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
457
|
+
tag_of(requested) == OSEO_TAG_UNDEFINED) {
|
|
458
|
+
/* A resizable buffer gives an absent length the specification's
|
|
459
|
+
* `auto`; a fixed-length buffer fixes it now. */
|
|
460
|
+
track_length = array_buffer_object(slots[1])->resizable;
|
|
461
|
+
length = track_length ? 0.0 : buffer_length - offset;
|
|
462
|
+
} else if (result.status == OSEO_STATUS_NORMAL) {
|
|
463
|
+
result = oseo_internal_to_index(
|
|
464
|
+
context,
|
|
465
|
+
requested,
|
|
466
|
+
"DataView byte length is outside the admitted index range.",
|
|
467
|
+
&length
|
|
468
|
+
);
|
|
469
|
+
/*
|
|
470
|
+
* The specification compares against the byte length it read
|
|
471
|
+
* before this conversion, not a fresh one, so a conversion that
|
|
472
|
+
* grows a resizable buffer still fails this bound.
|
|
473
|
+
*/
|
|
474
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
475
|
+
offset + length > buffer_length) {
|
|
476
|
+
result = oseo_internal_throw_error(
|
|
477
|
+
context,
|
|
478
|
+
OSEO_ERROR_RANGE,
|
|
479
|
+
"DataView byte length is past the end of its buffer."
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
484
|
+
result = data_view_prototype_from_target(
|
|
485
|
+
context,
|
|
486
|
+
slots[0],
|
|
487
|
+
&slots[2]
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
/* The `prototype` read is arbitrary code, so the buffer may have
|
|
491
|
+
* been detached or resized while it ran. */
|
|
492
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
493
|
+
array_buffer_object(slots[1])->detached) {
|
|
494
|
+
result = oseo_internal_throw_error(
|
|
495
|
+
context,
|
|
496
|
+
OSEO_ERROR_TYPE,
|
|
497
|
+
"DataView buffer became detached."
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
501
|
+
double current = (double)array_buffer_current_length(
|
|
502
|
+
array_buffer_object(slots[1])
|
|
503
|
+
);
|
|
504
|
+
if (offset > current) {
|
|
505
|
+
result = oseo_internal_throw_error(
|
|
506
|
+
context,
|
|
507
|
+
OSEO_ERROR_RANGE,
|
|
508
|
+
"DataView byte offset is past the end of its buffer."
|
|
509
|
+
);
|
|
510
|
+
} else if (tag_of(requested) != OSEO_TAG_UNDEFINED &&
|
|
511
|
+
offset + length > current) {
|
|
512
|
+
result = oseo_internal_throw_error(
|
|
513
|
+
context,
|
|
514
|
+
OSEO_ERROR_RANGE,
|
|
515
|
+
"DataView byte length is past the end of its buffer."
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
520
|
+
/* Both values passed a comparison against a live byte length, so
|
|
521
|
+
* each is inside the reviewed Data Block limit and converts
|
|
522
|
+
* exactly. */
|
|
523
|
+
result = data_view_allocate(
|
|
524
|
+
context,
|
|
525
|
+
slots[2],
|
|
526
|
+
slots[1],
|
|
527
|
+
(size_t)offset,
|
|
528
|
+
(size_t)length,
|
|
529
|
+
track_length
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
oseo_roots_pop(context, &frame);
|
|
533
|
+
return result;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
static OseoResult data_view_buffer_accessor(
|
|
537
|
+
OseoContext *context,
|
|
538
|
+
OseoValue receiver
|
|
539
|
+
) {
|
|
540
|
+
OseoResult checked = data_view_receiver(context, receiver);
|
|
541
|
+
if (checked.status != OSEO_STATUS_NORMAL) return checked;
|
|
542
|
+
return normal(data_view_object(receiver)->buffer);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/*
|
|
546
|
+
* Both length accessors reject a view whose buffer detached or shrank
|
|
547
|
+
* out from under it, which is the one place a detached buffer is a
|
|
548
|
+
* TypeError rather than a reported zero.
|
|
549
|
+
*/
|
|
550
|
+
static OseoResult data_view_measure(
|
|
551
|
+
OseoContext *context,
|
|
552
|
+
OseoValue receiver,
|
|
553
|
+
bool report_offset
|
|
554
|
+
) {
|
|
555
|
+
OseoResult checked = data_view_receiver(context, receiver);
|
|
556
|
+
if (checked.status != OSEO_STATUS_NORMAL) return checked;
|
|
557
|
+
const OseoDataView *view = data_view_object(receiver);
|
|
558
|
+
if (data_view_out_of_bounds(view)) {
|
|
559
|
+
return oseo_internal_throw_error(
|
|
560
|
+
context,
|
|
561
|
+
OSEO_ERROR_TYPE,
|
|
562
|
+
"DataView is outside the bounds of its buffer."
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
return normal(oseo_number(
|
|
566
|
+
(double)(report_offset ? view->byte_offset : data_view_length(view))
|
|
567
|
+
));
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/*
|
|
571
|
+
* The bounds shared by GetViewValue and SetViewValue, run after every
|
|
572
|
+
* conversion the specification orders first. `offset` reports the index
|
|
573
|
+
* of the element inside the buffer's Data Block.
|
|
574
|
+
*/
|
|
575
|
+
static OseoResult data_view_locate(
|
|
576
|
+
OseoContext *context,
|
|
577
|
+
OseoValue receiver,
|
|
578
|
+
double request,
|
|
579
|
+
OseoDataViewElement element,
|
|
580
|
+
size_t *offset
|
|
581
|
+
) {
|
|
582
|
+
const OseoDataView *view = data_view_object(receiver);
|
|
583
|
+
if (data_view_out_of_bounds(view)) {
|
|
584
|
+
return oseo_internal_throw_error(
|
|
585
|
+
context,
|
|
586
|
+
OSEO_ERROR_TYPE,
|
|
587
|
+
"DataView is outside the bounds of its buffer."
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
size_t view_size = data_view_length(view);
|
|
591
|
+
size_t element_size = data_view_element_size[element];
|
|
592
|
+
/* The first comparison keeps the conversion below in range; the
|
|
593
|
+
* second is `request + elementSize > viewSize` written so that no
|
|
594
|
+
* host addition can wrap. */
|
|
595
|
+
if (!(request <= (double)view_size) ||
|
|
596
|
+
element_size > view_size - (size_t)request) {
|
|
597
|
+
return oseo_internal_throw_error(
|
|
598
|
+
context,
|
|
599
|
+
OSEO_ERROR_RANGE,
|
|
600
|
+
"DataView access is outside the bounds of its view."
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
*offset = view->byte_offset + (size_t)request;
|
|
604
|
+
return normal(oseo_undefined());
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/*
|
|
608
|
+
* GetValueFromBuffer's byte order. The bytes are assembled through a
|
|
609
|
+
* local copy rather than a cast, so no access depends on the Data Block
|
|
610
|
+
* being aligned for the element type.
|
|
611
|
+
*/
|
|
612
|
+
static uint64_t data_view_load(
|
|
613
|
+
const OseoDataView *view,
|
|
614
|
+
size_t offset,
|
|
615
|
+
size_t element_size,
|
|
616
|
+
bool little_endian
|
|
617
|
+
) {
|
|
618
|
+
const OseoArrayBuffer *buffer = array_buffer_object(view->buffer);
|
|
619
|
+
uint8_t raw[8] = {0};
|
|
620
|
+
/* The bounds proved that `element_size` bytes lie inside the block,
|
|
621
|
+
* so the block exists and the copy stays in range. */
|
|
622
|
+
memcpy(raw, buffer->data + offset, element_size);
|
|
623
|
+
uint64_t bits = 0u;
|
|
624
|
+
for (size_t index = element_size; index > 0u; index -= 1u) {
|
|
625
|
+
size_t byte = little_endian ? index - 1u : element_size - index;
|
|
626
|
+
bits = (bits << 8u) | (uint64_t)raw[byte];
|
|
627
|
+
}
|
|
628
|
+
return bits;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/* SetValueInBuffer's byte order, with the same alignment independence. */
|
|
632
|
+
static void data_view_store(
|
|
633
|
+
const OseoDataView *view,
|
|
634
|
+
size_t offset,
|
|
635
|
+
size_t element_size,
|
|
636
|
+
bool little_endian,
|
|
637
|
+
uint64_t bits
|
|
638
|
+
) {
|
|
639
|
+
OseoArrayBuffer *buffer = array_buffer_object(view->buffer);
|
|
640
|
+
uint8_t raw[8] = {0};
|
|
641
|
+
for (size_t index = 0u; index < element_size; index += 1u) {
|
|
642
|
+
size_t byte = little_endian ? index : element_size - 1u - index;
|
|
643
|
+
raw[byte] = (uint8_t)(bits & 0xffu);
|
|
644
|
+
bits >>= 8u;
|
|
645
|
+
}
|
|
646
|
+
memcpy(buffer->data + offset, raw, element_size);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/* GetViewValue(view, requestIndex, isLittleEndian, type), 25.3.1.1. */
|
|
650
|
+
static OseoResult data_view_get(
|
|
651
|
+
OseoContext *context,
|
|
652
|
+
OseoValue receiver,
|
|
653
|
+
size_t argument_count,
|
|
654
|
+
const OseoValue *arguments,
|
|
655
|
+
OseoDataViewElement element
|
|
656
|
+
) {
|
|
657
|
+
OseoResult checked = data_view_receiver(context, receiver);
|
|
658
|
+
if (checked.status != OSEO_STATUS_NORMAL) return checked;
|
|
659
|
+
OseoValue slot = receiver;
|
|
660
|
+
OseoRootFrame frame = {NULL, &slot, 1u};
|
|
661
|
+
oseo_roots_push(context, &frame);
|
|
662
|
+
double request = 0.0;
|
|
663
|
+
OseoResult result = oseo_internal_to_index(
|
|
664
|
+
context,
|
|
665
|
+
argument_count == 0u ? oseo_undefined() : arguments[0],
|
|
666
|
+
"DataView byte offset is outside the admitted index range.",
|
|
667
|
+
&request
|
|
668
|
+
);
|
|
669
|
+
size_t element_size = data_view_element_size[element];
|
|
670
|
+
/* A one-byte element has no byte order, so the specification passes
|
|
671
|
+
* true rather than reading the argument. */
|
|
672
|
+
bool little_endian = element_size == 1u ||
|
|
673
|
+
(argument_count > 1u && oseo_to_boolean(arguments[1]));
|
|
674
|
+
size_t offset = 0u;
|
|
675
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
676
|
+
/* The conversion is a safepoint, so the view and its buffer are
|
|
677
|
+
* reread from the rooted receiver. */
|
|
678
|
+
result = data_view_locate(context, slot, request, element, &offset);
|
|
679
|
+
}
|
|
680
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
681
|
+
uint64_t bits = data_view_load(
|
|
682
|
+
data_view_object(slot),
|
|
683
|
+
offset,
|
|
684
|
+
element_size,
|
|
685
|
+
little_endian
|
|
686
|
+
);
|
|
687
|
+
if (element == OSEO_DATA_VIEW_BIGINT64) {
|
|
688
|
+
bool negative = (bits >> 63u) != 0u;
|
|
689
|
+
result = oseo_internal_bigint_from_uint64(
|
|
690
|
+
context,
|
|
691
|
+
negative ? UINT64_C(0) - bits : bits,
|
|
692
|
+
negative
|
|
693
|
+
);
|
|
694
|
+
} else if (element == OSEO_DATA_VIEW_BIGUINT64) {
|
|
695
|
+
result = oseo_internal_bigint_from_uint64(context, bits, false);
|
|
696
|
+
} else {
|
|
697
|
+
result = normal(
|
|
698
|
+
oseo_number(raw_bytes_to_number(bits, element))
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
oseo_roots_pop(context, &frame);
|
|
703
|
+
return result;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/* SetViewValue(view, requestIndex, isLittleEndian, type, value). */
|
|
707
|
+
static OseoResult data_view_set(
|
|
708
|
+
OseoContext *context,
|
|
709
|
+
OseoValue receiver,
|
|
710
|
+
size_t argument_count,
|
|
711
|
+
const OseoValue *arguments,
|
|
712
|
+
OseoDataViewElement element
|
|
713
|
+
) {
|
|
714
|
+
OseoResult checked = data_view_receiver(context, receiver);
|
|
715
|
+
if (checked.status != OSEO_STATUS_NORMAL) return checked;
|
|
716
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
717
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
718
|
+
oseo_roots_push(context, &frame);
|
|
719
|
+
double request = 0.0;
|
|
720
|
+
OseoResult result = oseo_internal_to_index(
|
|
721
|
+
context,
|
|
722
|
+
argument_count == 0u ? oseo_undefined() : arguments[0],
|
|
723
|
+
"DataView byte offset is outside the admitted index range.",
|
|
724
|
+
&request
|
|
725
|
+
);
|
|
726
|
+
OseoValue value = argument_count < 2u ? oseo_undefined() : arguments[1];
|
|
727
|
+
/* The numeric conversion runs before every bounds check, so a value
|
|
728
|
+
* whose conversion detaches the buffer still observes the specified
|
|
729
|
+
* TypeError afterwards. */
|
|
730
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
731
|
+
result = data_view_element_is_bigint(element)
|
|
732
|
+
? oseo_internal_to_bigint(context, value)
|
|
733
|
+
: oseo_internal_to_number(context, value);
|
|
734
|
+
slots[1] = result.value;
|
|
735
|
+
}
|
|
736
|
+
size_t element_size = data_view_element_size[element];
|
|
737
|
+
bool little_endian = element_size == 1u ||
|
|
738
|
+
(argument_count > 2u && oseo_to_boolean(arguments[2]));
|
|
739
|
+
size_t offset = 0u;
|
|
740
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
741
|
+
result = data_view_locate(
|
|
742
|
+
context,
|
|
743
|
+
slots[0],
|
|
744
|
+
request,
|
|
745
|
+
element,
|
|
746
|
+
&offset
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
750
|
+
uint64_t bits = data_view_element_is_bigint(element)
|
|
751
|
+
? oseo_internal_bigint_to_raw_uint64(slots[1])
|
|
752
|
+
: number_to_raw_bytes(number_value(slots[1]), element);
|
|
753
|
+
data_view_store(
|
|
754
|
+
data_view_object(slots[0]),
|
|
755
|
+
offset,
|
|
756
|
+
element_size,
|
|
757
|
+
little_endian,
|
|
758
|
+
bits
|
|
759
|
+
);
|
|
760
|
+
result = normal(oseo_undefined());
|
|
761
|
+
}
|
|
762
|
+
oseo_roots_pop(context, &frame);
|
|
763
|
+
return result;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
OseoResult oseo_internal_data_view_builtin_dispatch(
|
|
767
|
+
OseoContext *context,
|
|
768
|
+
size_t code_id,
|
|
769
|
+
OseoValue callee,
|
|
770
|
+
OseoValue receiver,
|
|
771
|
+
size_t argument_count,
|
|
772
|
+
const OseoValue *arguments,
|
|
773
|
+
OseoValue new_target
|
|
774
|
+
) {
|
|
775
|
+
(void)callee;
|
|
776
|
+
if (code_id == OSEO_DATA_VIEW_CONSTRUCTOR_CODE_ID) {
|
|
777
|
+
return data_view_construct(
|
|
778
|
+
context,
|
|
779
|
+
argument_count,
|
|
780
|
+
arguments,
|
|
781
|
+
new_target
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
785
|
+
return oseo_internal_throw_error(
|
|
786
|
+
context,
|
|
787
|
+
OSEO_ERROR_TYPE,
|
|
788
|
+
"DataView method is not a constructor."
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
if (code_id == OSEO_DATA_VIEW_BUFFER_CODE_ID) {
|
|
792
|
+
return data_view_buffer_accessor(context, receiver);
|
|
793
|
+
}
|
|
794
|
+
if (code_id == OSEO_DATA_VIEW_BYTE_LENGTH_CODE_ID) {
|
|
795
|
+
return data_view_measure(context, receiver, false);
|
|
796
|
+
}
|
|
797
|
+
if (code_id == OSEO_DATA_VIEW_BYTE_OFFSET_CODE_ID) {
|
|
798
|
+
return data_view_measure(context, receiver, true);
|
|
799
|
+
}
|
|
800
|
+
if (code_id >= OSEO_DATA_VIEW_GET_CODE_ID_FIRST &&
|
|
801
|
+
code_id <= OSEO_DATA_VIEW_GET_CODE_ID_LAST) {
|
|
802
|
+
return data_view_get(
|
|
803
|
+
context,
|
|
804
|
+
receiver,
|
|
805
|
+
argument_count,
|
|
806
|
+
arguments,
|
|
807
|
+
(OseoDataViewElement)(OSEO_DATA_VIEW_GET_CODE_ID_LAST - code_id)
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
if (code_id >= OSEO_DATA_VIEW_SET_CODE_ID_FIRST &&
|
|
811
|
+
code_id <= OSEO_DATA_VIEW_SET_CODE_ID_LAST) {
|
|
812
|
+
return data_view_set(
|
|
813
|
+
context,
|
|
814
|
+
receiver,
|
|
815
|
+
argument_count,
|
|
816
|
+
arguments,
|
|
817
|
+
(OseoDataViewElement)(OSEO_DATA_VIEW_SET_CODE_ID_LAST - code_id)
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
return oseo_unknown_function(context, code_id);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
static OseoResult create_data_view_builtin(
|
|
824
|
+
OseoContext *context,
|
|
825
|
+
size_t code_id,
|
|
826
|
+
const char *name,
|
|
827
|
+
size_t length,
|
|
828
|
+
OseoFunctionKind kind,
|
|
829
|
+
OseoFunctionNamePrefix prefix
|
|
830
|
+
) {
|
|
831
|
+
size_t name_length = strlen(name);
|
|
832
|
+
uint16_t units[32];
|
|
833
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
834
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
835
|
+
}
|
|
836
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
837
|
+
units[index] = (uint16_t)(unsigned char)name[index];
|
|
838
|
+
}
|
|
839
|
+
OseoValue environment = oseo_undefined();
|
|
840
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
841
|
+
oseo_roots_push(context, &frame);
|
|
842
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
843
|
+
environment = result.value;
|
|
844
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
845
|
+
result = oseo_function_create(
|
|
846
|
+
context,
|
|
847
|
+
code_id,
|
|
848
|
+
environment,
|
|
849
|
+
units,
|
|
850
|
+
name_length,
|
|
851
|
+
length,
|
|
852
|
+
kind,
|
|
853
|
+
oseo_undefined(),
|
|
854
|
+
oseo_undefined(),
|
|
855
|
+
prefix
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
oseo_roots_pop(context, &frame);
|
|
859
|
+
return result;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
static OseoResult define_data_view_property(
|
|
863
|
+
OseoContext *context,
|
|
864
|
+
OseoValue object,
|
|
865
|
+
const char *name,
|
|
866
|
+
OseoValue value,
|
|
867
|
+
OseoPropertyAttributes attributes
|
|
868
|
+
) {
|
|
869
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
870
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
871
|
+
oseo_roots_push(context, &frame);
|
|
872
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
873
|
+
slots[2] = result.value;
|
|
874
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
875
|
+
result = oseo_object_define(
|
|
876
|
+
context,
|
|
877
|
+
slots[0],
|
|
878
|
+
slots[2],
|
|
879
|
+
slots[1],
|
|
880
|
+
attributes
|
|
881
|
+
);
|
|
882
|
+
}
|
|
883
|
+
oseo_roots_pop(context, &frame);
|
|
884
|
+
return result;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/* One getter-only own accessor of %DataView.prototype%. */
|
|
888
|
+
static OseoResult define_data_view_accessor(
|
|
889
|
+
OseoContext *context,
|
|
890
|
+
OseoValue object,
|
|
891
|
+
const char *name,
|
|
892
|
+
OseoValue getter
|
|
893
|
+
) {
|
|
894
|
+
OseoValue slots[3] = {object, getter, oseo_undefined()};
|
|
895
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
896
|
+
oseo_roots_push(context, &frame);
|
|
897
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
898
|
+
slots[2] = result.value;
|
|
899
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
900
|
+
result = oseo_object_define_accessor(
|
|
901
|
+
context,
|
|
902
|
+
slots[0],
|
|
903
|
+
slots[2],
|
|
904
|
+
slots[1],
|
|
905
|
+
oseo_undefined(),
|
|
906
|
+
true,
|
|
907
|
+
false,
|
|
908
|
+
(OseoPropertyAttributes){true, false, false, true}
|
|
909
|
+
);
|
|
910
|
+
}
|
|
911
|
+
oseo_roots_pop(context, &frame);
|
|
912
|
+
return result;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/*
|
|
916
|
+
* Materializes %DataView%, %DataView.prototype%, and every own property
|
|
917
|
+
* ECMA-262 gives them.
|
|
918
|
+
* `OSEO_INTRINSIC_DATA_VIEW_SET_BIG_UINT64` is filled last, so it
|
|
919
|
+
* doubles as the completion marker: a partially built cluster leaves it
|
|
920
|
+
* undefined and the failure path clears every slot the attempt filled.
|
|
921
|
+
* The marker holds the uninitialized sentinel while the attempt runs, so
|
|
922
|
+
* a dependency that reentered the build reports that instead of
|
|
923
|
+
* splitting the constructor and prototype identities across two
|
|
924
|
+
* attempts.
|
|
925
|
+
*/
|
|
926
|
+
static OseoResult data_view_intrinsic_build(OseoContext *context) {
|
|
927
|
+
OseoValue *marker =
|
|
928
|
+
&context->realm->intrinsics[OSEO_INTRINSIC_DATA_VIEW_SET_BIG_UINT64];
|
|
929
|
+
if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
|
|
930
|
+
return failure(
|
|
931
|
+
context,
|
|
932
|
+
"OSEO2001",
|
|
933
|
+
"The DataView intrinsic cluster is already being built."
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
|
|
937
|
+
size_t entry_allocations = context->allocations;
|
|
938
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
939
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 5u);
|
|
940
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
941
|
+
*marker = oseo_uninitialized();
|
|
942
|
+
result = oseo_internal_intrinsic(
|
|
943
|
+
context,
|
|
944
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
945
|
+
);
|
|
946
|
+
frame.slots[0] = result.value;
|
|
947
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
948
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
949
|
+
frame.slots[0] = result.value;
|
|
950
|
+
}
|
|
951
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
952
|
+
context->realm->intrinsics[OSEO_INTRINSIC_DATA_VIEW_PROTOTYPE] =
|
|
953
|
+
frame.slots[0];
|
|
954
|
+
result = create_data_view_builtin(
|
|
955
|
+
context,
|
|
956
|
+
OSEO_DATA_VIEW_CONSTRUCTOR_CODE_ID,
|
|
957
|
+
"DataView",
|
|
958
|
+
1u,
|
|
959
|
+
OSEO_FUNCTION_ORDINARY,
|
|
960
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
961
|
+
);
|
|
962
|
+
frame.slots[1] = result.value;
|
|
963
|
+
}
|
|
964
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
965
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
966
|
+
context->realm->intrinsics[OSEO_INTRINSIC_DATA_VIEW] = frame.slots[1];
|
|
967
|
+
OseoFunction *constructor = function_object(frame.slots[1]);
|
|
968
|
+
constructor->prototype_object = frame.slots[0];
|
|
969
|
+
constructor->prototype_writable = false;
|
|
970
|
+
result = define_data_view_property(
|
|
971
|
+
context,
|
|
972
|
+
frame.slots[0],
|
|
973
|
+
"constructor",
|
|
974
|
+
frame.slots[1],
|
|
975
|
+
method
|
|
976
|
+
);
|
|
977
|
+
}
|
|
978
|
+
static const OseoIntrinsic accessor_intrinsics[] = {
|
|
979
|
+
OSEO_INTRINSIC_DATA_VIEW_BUFFER,
|
|
980
|
+
OSEO_INTRINSIC_DATA_VIEW_BYTE_LENGTH,
|
|
981
|
+
OSEO_INTRINSIC_DATA_VIEW_BYTE_OFFSET,
|
|
982
|
+
};
|
|
983
|
+
static const size_t accessor_codes[] = {
|
|
984
|
+
OSEO_DATA_VIEW_BUFFER_CODE_ID,
|
|
985
|
+
OSEO_DATA_VIEW_BYTE_LENGTH_CODE_ID,
|
|
986
|
+
OSEO_DATA_VIEW_BYTE_OFFSET_CODE_ID,
|
|
987
|
+
};
|
|
988
|
+
static const char *const accessor_names[] = {
|
|
989
|
+
"buffer",
|
|
990
|
+
"byteLength",
|
|
991
|
+
"byteOffset",
|
|
992
|
+
};
|
|
993
|
+
for (size_t index = 0u;
|
|
994
|
+
result.status == OSEO_STATUS_NORMAL && index < 3u;
|
|
995
|
+
index += 1u) {
|
|
996
|
+
result = create_data_view_builtin(
|
|
997
|
+
context,
|
|
998
|
+
accessor_codes[index],
|
|
999
|
+
accessor_names[index],
|
|
1000
|
+
0u,
|
|
1001
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1002
|
+
OSEO_FUNCTION_NAME_PREFIX_GET
|
|
1003
|
+
);
|
|
1004
|
+
frame.slots[2] = result.value;
|
|
1005
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
1006
|
+
context->realm->intrinsics[accessor_intrinsics[index]] = frame.slots[2];
|
|
1007
|
+
result = define_data_view_accessor(
|
|
1008
|
+
context,
|
|
1009
|
+
frame.slots[0],
|
|
1010
|
+
accessor_names[index],
|
|
1011
|
+
frame.slots[2]
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1015
|
+
result = oseo_internal_well_known_symbol(
|
|
1016
|
+
context,
|
|
1017
|
+
OSEO_WELL_KNOWN_TO_STRING_TAG
|
|
1018
|
+
);
|
|
1019
|
+
frame.slots[3] = result.value;
|
|
1020
|
+
}
|
|
1021
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1022
|
+
result = oseo_internal_ascii_string(context, "DataView");
|
|
1023
|
+
frame.slots[4] = result.value;
|
|
1024
|
+
}
|
|
1025
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1026
|
+
result = oseo_object_define(
|
|
1027
|
+
context,
|
|
1028
|
+
frame.slots[0],
|
|
1029
|
+
frame.slots[3],
|
|
1030
|
+
frame.slots[4],
|
|
1031
|
+
(OseoPropertyAttributes){true, false, false, false}
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
/* The eleven get accessors and then the eleven set accessors, in
|
|
1035
|
+
* element order, so each intrinsic slot and code ID is its element
|
|
1036
|
+
* index away from the first of its group. */
|
|
1037
|
+
for (size_t index = 0u;
|
|
1038
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
1039
|
+
index < 2u * OSEO_DATA_VIEW_ELEMENT_COUNT;
|
|
1040
|
+
index += 1u) {
|
|
1041
|
+
bool setter = index >= OSEO_DATA_VIEW_ELEMENT_COUNT;
|
|
1042
|
+
size_t element =
|
|
1043
|
+
setter ? index - OSEO_DATA_VIEW_ELEMENT_COUNT : index;
|
|
1044
|
+
const char *name = setter
|
|
1045
|
+
? data_view_set_names[element]
|
|
1046
|
+
: data_view_get_names[element];
|
|
1047
|
+
result = create_data_view_builtin(
|
|
1048
|
+
context,
|
|
1049
|
+
(setter ? OSEO_DATA_VIEW_SET_CODE_ID_LAST
|
|
1050
|
+
: OSEO_DATA_VIEW_GET_CODE_ID_LAST) - element,
|
|
1051
|
+
name,
|
|
1052
|
+
setter ? 2u : 1u,
|
|
1053
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1054
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1055
|
+
);
|
|
1056
|
+
frame.slots[2] = result.value;
|
|
1057
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
1058
|
+
context->realm->intrinsics[
|
|
1059
|
+
(size_t)OSEO_INTRINSIC_DATA_VIEW_GET_INT8 + index
|
|
1060
|
+
] = frame.slots[2];
|
|
1061
|
+
result = define_data_view_property(
|
|
1062
|
+
context,
|
|
1063
|
+
frame.slots[0],
|
|
1064
|
+
name,
|
|
1065
|
+
frame.slots[2],
|
|
1066
|
+
method
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1070
|
+
for (size_t index = OSEO_INTRINSIC_DATA_VIEW_PROTOTYPE;
|
|
1071
|
+
index <= OSEO_INTRINSIC_DATA_VIEW_SET_BIG_UINT64;
|
|
1072
|
+
index += 1u) {
|
|
1073
|
+
context->realm->intrinsics[index] = oseo_undefined();
|
|
1074
|
+
}
|
|
1075
|
+
oseo_roots_release(context, &frame);
|
|
1076
|
+
return result;
|
|
1077
|
+
}
|
|
1078
|
+
if (context->observe_specialization) {
|
|
1079
|
+
context->allocations = entry_allocations;
|
|
1080
|
+
}
|
|
1081
|
+
oseo_roots_release(context, &frame);
|
|
1082
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_DATA_VIEW]);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
OseoResult oseo_internal_data_view_intrinsic(OseoContext *context) {
|
|
1086
|
+
OseoResult built = data_view_intrinsic_build(context);
|
|
1087
|
+
if (built.status != OSEO_STATUS_NORMAL) return built;
|
|
1088
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_DATA_VIEW]);
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
OseoResult oseo_internal_install_data_view_global(
|
|
1092
|
+
OseoContext *context,
|
|
1093
|
+
OseoValue global
|
|
1094
|
+
) {
|
|
1095
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
1096
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1097
|
+
oseo_roots_push(context, &frame);
|
|
1098
|
+
OseoResult result = oseo_internal_data_view_intrinsic(context);
|
|
1099
|
+
slots[1] = result.value;
|
|
1100
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1101
|
+
result = define_data_view_property(
|
|
1102
|
+
context,
|
|
1103
|
+
slots[0],
|
|
1104
|
+
"DataView",
|
|
1105
|
+
slots[1],
|
|
1106
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1109
|
+
oseo_roots_pop(context, &frame);
|
|
1110
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
|
|
1111
|
+
}
|