@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,320 @@
|
|
|
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 <string.h>
|
|
31
|
+
|
|
32
|
+
/* Boolean constructor, prototype methods, and branded wrapper objects. */
|
|
33
|
+
|
|
34
|
+
static OseoResult boolean_this_value(
|
|
35
|
+
OseoContext *context,
|
|
36
|
+
OseoValue receiver
|
|
37
|
+
) {
|
|
38
|
+
if (tag_of(receiver) == OSEO_TAG_BOOLEAN) return normal(receiver);
|
|
39
|
+
if (is_object(receiver) &&
|
|
40
|
+
ordinary_object(receiver)->primitive_data &&
|
|
41
|
+
tag_of(ordinary_object(receiver)->primitive_value) ==
|
|
42
|
+
OSEO_TAG_BOOLEAN) {
|
|
43
|
+
return normal(ordinary_object(receiver)->primitive_value);
|
|
44
|
+
}
|
|
45
|
+
return oseo_internal_throw_error(
|
|
46
|
+
context,
|
|
47
|
+
OSEO_ERROR_TYPE,
|
|
48
|
+
"Boolean method requires a Boolean receiver."
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static OseoResult boolean_construct(
|
|
53
|
+
OseoContext *context,
|
|
54
|
+
OseoValue new_target,
|
|
55
|
+
size_t argument_count,
|
|
56
|
+
const OseoValue *arguments,
|
|
57
|
+
bool constructing
|
|
58
|
+
) {
|
|
59
|
+
OseoValue data = oseo_boolean(
|
|
60
|
+
argument_count > 0u && oseo_to_boolean(arguments[0])
|
|
61
|
+
);
|
|
62
|
+
if (!constructing) return normal(data);
|
|
63
|
+
|
|
64
|
+
OseoValue slots[2] = {new_target, oseo_undefined()};
|
|
65
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
66
|
+
oseo_roots_push(context, &frame);
|
|
67
|
+
OseoResult result = oseo_internal_constructor_prototype(
|
|
68
|
+
context,
|
|
69
|
+
slots[0]
|
|
70
|
+
);
|
|
71
|
+
slots[1] = result.value;
|
|
72
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
|
|
73
|
+
result = oseo_internal_constructor_realm_default(
|
|
74
|
+
context,
|
|
75
|
+
slots[0],
|
|
76
|
+
OSEO_INTRINSIC_BOOLEAN_PROTOTYPE
|
|
77
|
+
);
|
|
78
|
+
slots[1] = result.value;
|
|
79
|
+
}
|
|
80
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
81
|
+
result = oseo_object_create(context, slots[1]);
|
|
82
|
+
}
|
|
83
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
84
|
+
OseoOrdinaryObject *object = ordinary_object(result.value);
|
|
85
|
+
object->primitive_data = true;
|
|
86
|
+
object->primitive_value = data;
|
|
87
|
+
}
|
|
88
|
+
oseo_roots_pop(context, &frame);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static OseoResult boolean_to_string(
|
|
93
|
+
OseoContext *context,
|
|
94
|
+
OseoValue receiver
|
|
95
|
+
) {
|
|
96
|
+
OseoResult result = boolean_this_value(context, receiver);
|
|
97
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
98
|
+
const char *text = oseo_to_boolean(result.value) ? "true" : "false";
|
|
99
|
+
return oseo_internal_ascii_string(context, text);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
OseoResult oseo_internal_boolean_builtin_dispatch(
|
|
103
|
+
OseoContext *context,
|
|
104
|
+
size_t code_id,
|
|
105
|
+
OseoValue callee,
|
|
106
|
+
OseoValue receiver,
|
|
107
|
+
size_t argument_count,
|
|
108
|
+
const OseoValue *arguments,
|
|
109
|
+
OseoValue new_target
|
|
110
|
+
) {
|
|
111
|
+
(void)callee;
|
|
112
|
+
if (code_id == OSEO_BOOLEAN_CONSTRUCTOR_CODE_ID) {
|
|
113
|
+
return boolean_construct(
|
|
114
|
+
context,
|
|
115
|
+
new_target,
|
|
116
|
+
argument_count,
|
|
117
|
+
arguments,
|
|
118
|
+
tag_of(new_target) != OSEO_TAG_UNDEFINED
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
|
|
122
|
+
return oseo_internal_throw_error(
|
|
123
|
+
context,
|
|
124
|
+
OSEO_ERROR_TYPE,
|
|
125
|
+
"Boolean method is not a constructor."
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (code_id == OSEO_BOOLEAN_TO_STRING_CODE_ID) {
|
|
129
|
+
return boolean_to_string(context, receiver);
|
|
130
|
+
}
|
|
131
|
+
if (code_id == OSEO_BOOLEAN_VALUE_OF_CODE_ID) {
|
|
132
|
+
return boolean_this_value(context, receiver);
|
|
133
|
+
}
|
|
134
|
+
return oseo_unknown_function(context, code_id);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static OseoResult create_boolean_function(
|
|
138
|
+
OseoContext *context,
|
|
139
|
+
size_t code_id,
|
|
140
|
+
const char *name,
|
|
141
|
+
size_t length,
|
|
142
|
+
OseoFunctionKind kind
|
|
143
|
+
) {
|
|
144
|
+
size_t name_length = strlen(name);
|
|
145
|
+
uint16_t units[16];
|
|
146
|
+
if (name_length > sizeof(units) / sizeof(*units)) {
|
|
147
|
+
return failure(context, "OSEO2001", "Boolean function name is long.");
|
|
148
|
+
}
|
|
149
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
150
|
+
units[index] = (uint16_t)(unsigned char)name[index];
|
|
151
|
+
}
|
|
152
|
+
OseoValue environment = oseo_undefined();
|
|
153
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
154
|
+
oseo_roots_push(context, &frame);
|
|
155
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
156
|
+
environment = result.value;
|
|
157
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
158
|
+
result = oseo_function_create(
|
|
159
|
+
context,
|
|
160
|
+
code_id,
|
|
161
|
+
environment,
|
|
162
|
+
units,
|
|
163
|
+
name_length,
|
|
164
|
+
length,
|
|
165
|
+
kind,
|
|
166
|
+
oseo_undefined(),
|
|
167
|
+
oseo_undefined(),
|
|
168
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
oseo_roots_pop(context, &frame);
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static OseoResult define_boolean_property(
|
|
176
|
+
OseoContext *context,
|
|
177
|
+
OseoValue object,
|
|
178
|
+
const char *name,
|
|
179
|
+
OseoValue value,
|
|
180
|
+
OseoPropertyAttributes attributes
|
|
181
|
+
) {
|
|
182
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
183
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
184
|
+
oseo_roots_push(context, &frame);
|
|
185
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
186
|
+
slots[2] = result.value;
|
|
187
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
188
|
+
result = oseo_object_define(
|
|
189
|
+
context,
|
|
190
|
+
slots[0],
|
|
191
|
+
slots[2],
|
|
192
|
+
slots[1],
|
|
193
|
+
attributes
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
oseo_roots_pop(context, &frame);
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
OseoResult oseo_internal_boolean_intrinsic(OseoContext *context) {
|
|
201
|
+
OseoValue *marker =
|
|
202
|
+
&context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_VALUE_OF];
|
|
203
|
+
if (is_function(*marker)) {
|
|
204
|
+
return normal(context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN]);
|
|
205
|
+
}
|
|
206
|
+
size_t entry_allocations = context->allocations;
|
|
207
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
208
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 3u);
|
|
209
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
210
|
+
|
|
211
|
+
result = oseo_internal_intrinsic(
|
|
212
|
+
context,
|
|
213
|
+
OSEO_INTRINSIC_OBJECT_PROTOTYPE
|
|
214
|
+
);
|
|
215
|
+
frame.slots[0] = result.value;
|
|
216
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
217
|
+
result = oseo_object_create(context, frame.slots[0]);
|
|
218
|
+
frame.slots[0] = result.value;
|
|
219
|
+
}
|
|
220
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
221
|
+
OseoOrdinaryObject *prototype = ordinary_object(frame.slots[0]);
|
|
222
|
+
prototype->primitive_data = true;
|
|
223
|
+
prototype->primitive_value = oseo_boolean(false);
|
|
224
|
+
context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_PROTOTYPE] =
|
|
225
|
+
frame.slots[0];
|
|
226
|
+
result = create_boolean_function(
|
|
227
|
+
context,
|
|
228
|
+
OSEO_BOOLEAN_CONSTRUCTOR_CODE_ID,
|
|
229
|
+
"Boolean",
|
|
230
|
+
1u,
|
|
231
|
+
OSEO_FUNCTION_ORDINARY
|
|
232
|
+
);
|
|
233
|
+
frame.slots[1] = result.value;
|
|
234
|
+
}
|
|
235
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
236
|
+
context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN] = frame.slots[1];
|
|
237
|
+
OseoFunction *constructor = function_object(frame.slots[1]);
|
|
238
|
+
constructor->prototype_object = frame.slots[0];
|
|
239
|
+
constructor->prototype_writable = false;
|
|
240
|
+
result = define_boolean_property(
|
|
241
|
+
context,
|
|
242
|
+
frame.slots[0],
|
|
243
|
+
"constructor",
|
|
244
|
+
frame.slots[1],
|
|
245
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
static const OseoIntrinsic intrinsics[] = {
|
|
249
|
+
OSEO_INTRINSIC_BOOLEAN_TO_STRING,
|
|
250
|
+
OSEO_INTRINSIC_BOOLEAN_VALUE_OF,
|
|
251
|
+
};
|
|
252
|
+
static const size_t codes[] = {
|
|
253
|
+
OSEO_BOOLEAN_TO_STRING_CODE_ID,
|
|
254
|
+
OSEO_BOOLEAN_VALUE_OF_CODE_ID,
|
|
255
|
+
};
|
|
256
|
+
static const char *const names[] = {"toString", "valueOf"};
|
|
257
|
+
for (size_t index = 0u;
|
|
258
|
+
result.status == OSEO_STATUS_NORMAL && index < 2u;
|
|
259
|
+
index += 1u) {
|
|
260
|
+
result = create_boolean_function(
|
|
261
|
+
context,
|
|
262
|
+
codes[index],
|
|
263
|
+
names[index],
|
|
264
|
+
0u,
|
|
265
|
+
OSEO_FUNCTION_INTERNAL
|
|
266
|
+
);
|
|
267
|
+
frame.slots[2] = result.value;
|
|
268
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
269
|
+
context->realm->intrinsics[intrinsics[index]] = frame.slots[2];
|
|
270
|
+
result = define_boolean_property(
|
|
271
|
+
context,
|
|
272
|
+
frame.slots[0],
|
|
273
|
+
names[index],
|
|
274
|
+
frame.slots[2],
|
|
275
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
280
|
+
ordinary_object(frame.slots[0])
|
|
281
|
+
->primitive_wrapper_methods_initialized = true;
|
|
282
|
+
if (context->observe_specialization) {
|
|
283
|
+
context->allocations = entry_allocations;
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_PROTOTYPE] =
|
|
287
|
+
oseo_undefined();
|
|
288
|
+
context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN] = oseo_undefined();
|
|
289
|
+
context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_TO_STRING] =
|
|
290
|
+
oseo_undefined();
|
|
291
|
+
context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN_VALUE_OF] =
|
|
292
|
+
oseo_undefined();
|
|
293
|
+
}
|
|
294
|
+
oseo_roots_release(context, &frame);
|
|
295
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
296
|
+
? normal(context->realm->intrinsics[OSEO_INTRINSIC_BOOLEAN])
|
|
297
|
+
: result;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
OseoResult oseo_internal_install_boolean_global(
|
|
301
|
+
OseoContext *context,
|
|
302
|
+
OseoValue global
|
|
303
|
+
) {
|
|
304
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
305
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
306
|
+
oseo_roots_push(context, &frame);
|
|
307
|
+
OseoResult result = oseo_internal_boolean_intrinsic(context);
|
|
308
|
+
slots[1] = result.value;
|
|
309
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
310
|
+
result = define_boolean_property(
|
|
311
|
+
context,
|
|
312
|
+
slots[0],
|
|
313
|
+
"Boolean",
|
|
314
|
+
slots[1],
|
|
315
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
oseo_roots_pop(context, &frame);
|
|
319
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
|
|
320
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
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 <math.h>
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* The platform-neutral clock and wakeup boundary of ADR 0025: adapter
|
|
34
|
+
* installation and selection, the realm's monotonic origin and cached
|
|
35
|
+
* scheduler time, deadline waits in whole milliseconds, epoch real-time
|
|
36
|
+
* reads, and cross-thread wakeup.
|
|
37
|
+
*
|
|
38
|
+
* This component never names an operating-system facility. The platform
|
|
39
|
+
* adapter in runtime_clock_posix.c and any embedder-installed adapter
|
|
40
|
+
* reach the scheduler only through OseoClockAdapter, and the scheduler
|
|
41
|
+
* reaches them only through the functions here. Real time and the
|
|
42
|
+
* monotonic domain never meet: no deadline is derived from an epoch
|
|
43
|
+
* reading, and no epoch reading is derived from the monotonic origin.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
#define OSEO_NANOSECONDS_PER_MILLISECOND UINT64_C(1000000)
|
|
47
|
+
|
|
48
|
+
static void clock_open(OseoContext *context) {
|
|
49
|
+
if (context->clock_adapter != NULL) return;
|
|
50
|
+
context->clock_adapter = oseo_internal_platform_clock_open(
|
|
51
|
+
context->clock_restrictions,
|
|
52
|
+
&context->clock_state
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static void clock_close(OseoContext *context) {
|
|
57
|
+
const OseoClockAdapter *adapter = context->clock_adapter;
|
|
58
|
+
if (adapter != NULL) adapter->close(context->clock_state);
|
|
59
|
+
context->clock_adapter = NULL;
|
|
60
|
+
context->clock_state = NULL;
|
|
61
|
+
context->clock_started = false;
|
|
62
|
+
context->clock_origin = 0u;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
void oseo_internal_clock_destroy(OseoContext *context) {
|
|
66
|
+
clock_close(context);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void oseo_context_set_clock(
|
|
70
|
+
OseoContext *context,
|
|
71
|
+
const OseoClockAdapter *adapter,
|
|
72
|
+
void *state
|
|
73
|
+
) {
|
|
74
|
+
clock_close(context);
|
|
75
|
+
context->clock_adapter = adapter;
|
|
76
|
+
context->clock_state = state;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
void oseo_context_restrict_clock(
|
|
80
|
+
OseoContext *context,
|
|
81
|
+
unsigned restrictions
|
|
82
|
+
) {
|
|
83
|
+
context->clock_restrictions = restrictions;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
bool oseo_clock_open(OseoContext *context) {
|
|
87
|
+
OseoClockCapabilities capabilities;
|
|
88
|
+
oseo_clock_capabilities(context, &capabilities);
|
|
89
|
+
return capabilities.wakeup != NULL;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
bool oseo_clock_wake(OseoContext *context) {
|
|
93
|
+
const OseoClockAdapter *adapter = context->clock_adapter;
|
|
94
|
+
return adapter != NULL && adapter->wake(context->clock_state);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
bool oseo_clock_real_time(OseoContext *context, double *milliseconds) {
|
|
98
|
+
clock_open(context);
|
|
99
|
+
double value = 0.0;
|
|
100
|
+
if (!context->clock_adapter->real_time(context->clock_state, &value) ||
|
|
101
|
+
!isfinite(value)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
*milliseconds = value;
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
void oseo_clock_capabilities(
|
|
109
|
+
OseoContext *context,
|
|
110
|
+
OseoClockCapabilities *capabilities
|
|
111
|
+
) {
|
|
112
|
+
clock_open(context);
|
|
113
|
+
context->clock_adapter->describe(context->clock_state, capabilities);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static OseoResult monotonic_failure(OseoContext *context) {
|
|
117
|
+
return failure(
|
|
118
|
+
context,
|
|
119
|
+
"OSEO2001",
|
|
120
|
+
"The host monotonic clock is unavailable."
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
OseoResult oseo_internal_clock_start(OseoContext *context) {
|
|
125
|
+
if (context->clock_started) return normal(oseo_undefined());
|
|
126
|
+
clock_open(context);
|
|
127
|
+
uint64_t origin = 0u;
|
|
128
|
+
if (!context->clock_adapter->monotonic(context->clock_state, &origin)) {
|
|
129
|
+
return monotonic_failure(context);
|
|
130
|
+
}
|
|
131
|
+
context->clock_origin = origin;
|
|
132
|
+
context->clock_started = true;
|
|
133
|
+
context->clock_milliseconds = 0u;
|
|
134
|
+
return normal(oseo_undefined());
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
OseoResult oseo_internal_clock_now(
|
|
138
|
+
OseoContext *context,
|
|
139
|
+
uint64_t *milliseconds
|
|
140
|
+
) {
|
|
141
|
+
OseoResult result = oseo_internal_clock_start(context);
|
|
142
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
143
|
+
uint64_t now = 0u;
|
|
144
|
+
if (!context->clock_adapter->monotonic(context->clock_state, &now)) {
|
|
145
|
+
return monotonic_failure(context);
|
|
146
|
+
}
|
|
147
|
+
uint64_t elapsed = now > context->clock_origin
|
|
148
|
+
? (now - context->clock_origin) / OSEO_NANOSECONDS_PER_MILLISECOND
|
|
149
|
+
: 0u;
|
|
150
|
+
/*
|
|
151
|
+
* An adapter that reports a smaller reading than an earlier one
|
|
152
|
+
* breaks its own contract; the scheduler still never moves backward.
|
|
153
|
+
*/
|
|
154
|
+
*milliseconds = elapsed < context->clock_milliseconds
|
|
155
|
+
? context->clock_milliseconds
|
|
156
|
+
: elapsed;
|
|
157
|
+
return normal(oseo_undefined());
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
OseoResult oseo_internal_clock_wait_until(
|
|
161
|
+
OseoContext *context,
|
|
162
|
+
uint64_t deadline
|
|
163
|
+
) {
|
|
164
|
+
OseoResult result = oseo_internal_clock_start(context);
|
|
165
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
166
|
+
uint64_t span = deadline > UINT64_MAX / OSEO_NANOSECONDS_PER_MILLISECOND
|
|
167
|
+
? UINT64_MAX
|
|
168
|
+
: deadline * OSEO_NANOSECONDS_PER_MILLISECOND;
|
|
169
|
+
uint64_t target = UINT64_MAX - context->clock_origin < span
|
|
170
|
+
? UINT64_MAX
|
|
171
|
+
: context->clock_origin + span;
|
|
172
|
+
OseoClockWaitResult outcome = context->clock_adapter->wait(
|
|
173
|
+
context->clock_state,
|
|
174
|
+
true,
|
|
175
|
+
target
|
|
176
|
+
);
|
|
177
|
+
if (outcome == OSEO_CLOCK_WAIT_FAILED) {
|
|
178
|
+
return failure(context, "OSEO2001", "The host clock wait failed.");
|
|
179
|
+
}
|
|
180
|
+
return normal(oseo_undefined());
|
|
181
|
+
}
|