xbintsc 0.6.24 → 0.6.28
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/AGENTS.md +1 -1
- package/dist/src/cli/extensions.js +11 -3
- package/dist/src/cli/extensions.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/state.js +7 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +19 -1
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +2 -2
- package/dist/src/driver/fingerprint.js +3 -3
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +1 -1
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/paths.js +8 -4
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +13 -1
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/catalog.js +5 -1
- package/dist/src/extensions/catalog.js.map +1 -1
- package/dist/src/extensions/gui/index.js +0 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/link-feature.d.ts +8 -0
- package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
- package/dist/src/extensions/node/stream-web/index.js +59 -4
- package/dist/src/extensions/node/stream-web/index.js.map +1 -1
- package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
- package/dist/src/extensions/node/worker_threads/index.js +24 -11
- package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +9 -1
- package/dist/src/extensions/registry.js +4 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/wasm-asset.d.ts +16 -0
- package/dist/src/extensions/wasm-asset.js +28 -0
- package/dist/src/extensions/wasm-asset.js.map +1 -0
- package/dist/src/extensions/wasm.d.ts +17 -0
- package/dist/src/extensions/wasm.js +68 -0
- package/dist/src/extensions/wasm.js.map +1 -0
- package/dist/src/extensions/webworker/index.d.ts +24 -0
- package/dist/src/extensions/webworker/index.js +54 -0
- package/dist/src/extensions/webworker/index.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/async-fs.test.js +44 -0
- package/dist/tests/e2e/async-fs.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +6 -0
- package/dist/tests/e2e/harness.js +13 -1
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
- package/dist/tests/e2e/node-crypto-random.test.js +148 -0
- package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +1 -1
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.d.ts +8 -0
- package/dist/tests/e2e/web-streams.test.js +274 -0
- package/dist/tests/e2e/web-streams.test.js.map +1 -0
- package/dist/tests/e2e/web-worker.test.d.ts +7 -0
- package/dist/tests/e2e/web-worker.test.js +74 -0
- package/dist/tests/e2e/web-worker.test.js.map +1 -0
- package/dist/tests/e2e/webassembly.test.d.ts +8 -0
- package/dist/tests/e2e/webassembly.test.js +113 -0
- package/dist/tests/e2e/webassembly.test.js.map +1 -0
- package/dist/tests/e2e/worker-api.test.d.ts +14 -0
- package/dist/tests/e2e/worker-api.test.js +460 -0
- package/dist/tests/e2e/worker-api.test.js.map +1 -0
- package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
- package/dist/tests/e2e/worker-threads.test.js +160 -0
- package/dist/tests/e2e/worker-threads.test.js.map +1 -0
- package/dist/tests/extensions/wasm.test.d.ts +1 -0
- package/dist/tests/extensions/wasm.test.js +53 -0
- package/dist/tests/extensions/wasm.test.js.map +1 -0
- package/doc/ai/cli.md +1 -1
- package/doc/ai/contributing.md +11 -4
- package/doc/ai/extensions.md +25 -2
- package/doc/ai/language-support.md +14 -2
- package/doc/ai/zh-CN/cli.md +1 -1
- package/doc/ai/zh-CN/contributing.md +8 -3
- package/doc/ai/zh-CN/extensions.md +23 -2
- package/doc/ai/zh-CN/language-support.md +13 -2
- package/doc/gui/api.md +1 -1
- package/doc/implemented/cli.md +1 -1
- package/doc/implemented/extensions.md +6 -5
- package/doc/implemented/node/stream-web.md +31 -0
- package/doc/implemented/node/worker-threads.md +88 -5
- package/doc/implemented/node.md +2 -1
- package/doc/implemented/streams.md +82 -0
- package/doc/implemented/wasm.md +80 -18
- package/doc/implemented/web-worker.md +84 -0
- package/doc/implemented.md +6 -2
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/unimplemented/node/worker-threads.md +23 -0
- package/doc/unimplemented/node.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/unimplemented/wasm.md +21 -9
- package/doc/unimplemented.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/implemented/cli.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +7 -6
- package/doc/zh-CN/implemented/node/stream-web.md +24 -0
- package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
- package/doc/zh-CN/implemented/node.md +2 -1
- package/doc/zh-CN/implemented/streams.md +60 -0
- package/doc/zh-CN/implemented/wasm.md +73 -16
- package/doc/zh-CN/implemented/web-worker.md +79 -0
- package/doc/zh-CN/implemented.md +6 -2
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
- package/doc/zh-CN/unimplemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/doc/zh-CN/unimplemented/wasm.md +16 -8
- package/doc/zh-CN/unimplemented.md +1 -1
- package/llms.txt +2 -0
- package/package.json +8 -7
- package/runtime/ext_gui/browser.cpp +1 -1
- package/runtime/ext_node/stream_web/stream_web.c +20 -6
- package/runtime/worker/worker_api.c +422 -0
- package/runtime/worker/worker_codec.c +489 -0
- package/runtime/worker/worker_core.c +445 -0
- package/runtime/worker/worker_internal.h +223 -0
- package/runtime/worker/worker_platform.c +344 -0
- package/runtime/worker/worker_ports.c +203 -0
- package/runtime/worker/worker_transport.c +464 -0
- package/runtime/worker/worker_wire.c +150 -0
- package/runtime/xt_builtins.c +15 -0
- package/runtime/xt_constructors.c +49 -2
- package/runtime/xt_stdlib2/error.inc +4 -0
- package/runtime/xt_streams/common.inc +232 -0
- package/runtime/xt_streams/constructors.inc +232 -0
- package/runtime/xt_streams/encoding.inc +124 -0
- package/runtime/xt_streams/readable-extra.inc +372 -0
- package/runtime/xt_streams/readable.inc +415 -0
- package/runtime/xt_streams/transform.inc +195 -0
- package/runtime/xt_streams/writable.inc +542 -0
- package/runtime/xt_streams.c +30 -0
- package/runtime/xt_wasm/wasm.h +148 -0
- package/runtime/xt_wasm/wasm_api.c +357 -0
- package/runtime/xt_wasm/wasm_api.h +37 -0
- package/runtime/xt_wasm/wasm_bridge.c +30 -0
- package/runtime/xt_wasm/wasm_instance.c +591 -0
- package/runtime/xt_wasm/wasm_module.c +270 -0
- package/runtime/xt_wasm/wasm_values.c +177 -0
- package/runtime/xt_webassembly.c +15 -4
- package/scripts/fetch-vendored.ts +187 -20
- package/scripts/package-release.ts +3 -2
- package/src/cli/extensions.ts +11 -3
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/state.ts +7 -0
- package/src/codegen/generator/tables.ts +19 -1
- package/src/driver/bundler/graph.ts +9 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- package/src/driver/paths.ts +8 -4
- package/src/driver/runtime-objects.ts +13 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/catalog.ts +5 -1
- package/src/extensions/gui/index.ts +0 -1
- package/src/extensions/link-feature.ts +8 -0
- package/src/extensions/node/stream-web/index.ts +59 -4
- package/src/extensions/node/worker_threads/index.ts +24 -11
- package/src/extensions/registry.ts +13 -2
- package/src/extensions/wasm-asset.ts +32 -0
- package/src/extensions/wasm.ts +74 -0
- package/src/extensions/webworker/index.ts +58 -0
- package/src/index.ts +1 -0
- package/vendor/wasm3/.pin +1 -0
- package/vendor/wasm3/LICENSE +21 -0
- package/vendor/wasm3/m3_api_libc.c +265 -0
- package/vendor/wasm3/m3_api_libc.h +19 -0
- package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
- package/vendor/wasm3/m3_api_tracer.c +173 -0
- package/vendor/wasm3/m3_api_tracer.h +19 -0
- package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
- package/vendor/wasm3/m3_api_wasi.c +1207 -0
- package/vendor/wasm3/m3_api_wasi.h +74 -0
- package/vendor/wasm3/m3_api_wasi_host.h +137 -0
- package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
- package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
- package/vendor/wasm3/m3_atomic.h +114 -0
- package/vendor/wasm3/m3_bind.c +174 -0
- package/vendor/wasm3/m3_bind.h +20 -0
- package/vendor/wasm3/m3_code.c +232 -0
- package/vendor/wasm3/m3_code.h +74 -0
- package/vendor/wasm3/m3_compile.c +6603 -0
- package/vendor/wasm3/m3_compile.h +354 -0
- package/vendor/wasm3/m3_config.h +448 -0
- package/vendor/wasm3/m3_config_platforms.h +345 -0
- package/vendor/wasm3/m3_core.c +1034 -0
- package/vendor/wasm3/m3_core.h +526 -0
- package/vendor/wasm3/m3_deterministic.c +65 -0
- package/vendor/wasm3/m3_deterministic.h +59 -0
- package/vendor/wasm3/m3_env.c +3146 -0
- package/vendor/wasm3/m3_env.h +748 -0
- package/vendor/wasm3/m3_exception.h +33 -0
- package/vendor/wasm3/m3_exec.c +8 -0
- package/vendor/wasm3/m3_exec.h +4110 -0
- package/vendor/wasm3/m3_exec_atomic.h +146 -0
- package/vendor/wasm3/m3_exec_defs.h +76 -0
- package/vendor/wasm3/m3_function.c +278 -0
- package/vendor/wasm3/m3_function.h +244 -0
- package/vendor/wasm3/m3_host.h +226 -0
- package/vendor/wasm3/m3_host_none.h +76 -0
- package/vendor/wasm3/m3_host_posix.h +543 -0
- package/vendor/wasm3/m3_host_win32.h +253 -0
- package/vendor/wasm3/m3_info.c +616 -0
- package/vendor/wasm3/m3_info.h +38 -0
- package/vendor/wasm3/m3_math_utils.h +560 -0
- package/vendor/wasm3/m3_module.c +431 -0
- package/vendor/wasm3/m3_parse.c +1549 -0
- package/vendor/wasm3/m3_snapshot.c +3825 -0
- package/vendor/wasm3/m3_validate.c +1770 -0
- package/vendor/wasm3/m3_validate.h +25 -0
- package/vendor/wasm3/m3_wasi.h +10 -0
- package/vendor/wasm3/m3_xxh64.c +211 -0
- package/vendor/wasm3/wasm3.h +684 -0
- package/vendor/wasm3/wasm3_defs.h +350 -0
- package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
|
@@ -27,13 +27,34 @@ xt_value xt_finalization_registry_ctor(int32_t argc, xt_value *argv);
|
|
|
27
27
|
|
|
28
28
|
/* -- constructor table ---------------------------------------------------- */
|
|
29
29
|
|
|
30
|
+
/* Prototypes owned by the Web Streams runtime (xt_streams.c). */
|
|
31
|
+
xt_value xt_stream_readable_proto(void);
|
|
32
|
+
xt_value xt_stream_writable_proto(void);
|
|
33
|
+
xt_value xt_stream_transform_proto(void);
|
|
34
|
+
xt_value xt_stream_count_strategy_proto(void);
|
|
35
|
+
xt_value xt_stream_byte_strategy_proto(void);
|
|
36
|
+
xt_value xt_readable_stream_ctor(int32_t argc, xt_value *argv);
|
|
37
|
+
xt_value xt_writable_stream_ctor(int32_t argc, xt_value *argv);
|
|
38
|
+
xt_value xt_transform_stream_ctor(int32_t argc, xt_value *argv);
|
|
39
|
+
xt_value xt_count_queuing_strategy_ctor(int32_t argc, xt_value *argv);
|
|
40
|
+
xt_value xt_byte_length_queuing_strategy_ctor(int32_t argc, xt_value *argv);
|
|
41
|
+
xt_value xt_text_encoder_stream_ctor(int32_t argc, xt_value *argv);
|
|
42
|
+
xt_value xt_text_decoder_stream_ctor(int32_t argc, xt_value *argv);
|
|
43
|
+
int xt_streams_has_static(const char *name, const char *method);
|
|
44
|
+
xt_value xt_streams_static(xt_value name, int32_t argc, xt_value *argv);
|
|
45
|
+
|
|
30
46
|
enum {
|
|
31
47
|
XT_PROTO_OBJECT,
|
|
32
48
|
XT_PROTO_ARRAY,
|
|
33
49
|
XT_PROTO_FUNCTION,
|
|
34
50
|
XT_PROTO_PLAIN,
|
|
35
51
|
XT_PROTO_ERROR,
|
|
36
|
-
XT_PROTO_TYPED
|
|
52
|
+
XT_PROTO_TYPED,
|
|
53
|
+
XT_PROTO_READABLE_STREAM,
|
|
54
|
+
XT_PROTO_WRITABLE_STREAM,
|
|
55
|
+
XT_PROTO_TRANSFORM_STREAM,
|
|
56
|
+
XT_PROTO_COUNT_STRATEGY,
|
|
57
|
+
XT_PROTO_BYTE_STRATEGY
|
|
37
58
|
};
|
|
38
59
|
|
|
39
60
|
typedef struct {
|
|
@@ -76,7 +97,14 @@ typedef struct {
|
|
|
76
97
|
X(String, 1, XT_PROTO_PLAIN) \
|
|
77
98
|
X(Boolean, 1, XT_PROTO_PLAIN) \
|
|
78
99
|
X(BigInt, 1, XT_PROTO_PLAIN) \
|
|
79
|
-
X(Symbol, 0, XT_PROTO_PLAIN)
|
|
100
|
+
X(Symbol, 0, XT_PROTO_PLAIN) \
|
|
101
|
+
X(ReadableStream, 0, XT_PROTO_READABLE_STREAM) \
|
|
102
|
+
X(WritableStream, 0, XT_PROTO_WRITABLE_STREAM) \
|
|
103
|
+
X(TransformStream, 0, XT_PROTO_TRANSFORM_STREAM) \
|
|
104
|
+
X(CountQueuingStrategy, 1, XT_PROTO_COUNT_STRATEGY) \
|
|
105
|
+
X(ByteLengthQueuingStrategy, 1, XT_PROTO_BYTE_STRATEGY) \
|
|
106
|
+
X(TextEncoderStream, 0, XT_PROTO_TRANSFORM_STREAM) \
|
|
107
|
+
X(TextDecoderStream, 0, XT_PROTO_TRANSFORM_STREAM)
|
|
80
108
|
|
|
81
109
|
#define XT_CTOR_ENUM(name, arity, proto) XT_CTOR_##name,
|
|
82
110
|
enum { XT_CTOR_SPECS(XT_CTOR_ENUM) XT_CTOR_COUNT };
|
|
@@ -185,6 +213,13 @@ static xt_value xt_builtin_ctor_call(const char *name, int32_t argc, xt_value *a
|
|
|
185
213
|
if (strcmp(name, "Boolean") == 0) return xt_boolean_ctor(argc, argv);
|
|
186
214
|
if (strcmp(name, "BigInt") == 0) return xt_bigint_ctor(argc, argv);
|
|
187
215
|
if (strcmp(name, "Symbol") == 0) return xt_symbol(argc, argv);
|
|
216
|
+
if (strcmp(name, "ReadableStream") == 0) return xt_readable_stream_ctor(argc, argv);
|
|
217
|
+
if (strcmp(name, "WritableStream") == 0) return xt_writable_stream_ctor(argc, argv);
|
|
218
|
+
if (strcmp(name, "TransformStream") == 0) return xt_transform_stream_ctor(argc, argv);
|
|
219
|
+
if (strcmp(name, "CountQueuingStrategy") == 0) return xt_count_queuing_strategy_ctor(argc, argv);
|
|
220
|
+
if (strcmp(name, "ByteLengthQueuingStrategy") == 0) return xt_byte_length_queuing_strategy_ctor(argc, argv);
|
|
221
|
+
if (strcmp(name, "TextEncoderStream") == 0) return xt_text_encoder_stream_ctor(argc, argv);
|
|
222
|
+
if (strcmp(name, "TextDecoderStream") == 0) return xt_text_decoder_stream_ctor(argc, argv);
|
|
188
223
|
if (xt_ctor_is_typed_array(name)) return xt_typed_array_ctor_by_name(nameValue, argc, argv);
|
|
189
224
|
if (xt_ctor_is_error(name)) return xt_error_ctor_by_name(nameValue, argc, argv);
|
|
190
225
|
return XT_UNDEFINED;
|
|
@@ -216,6 +251,16 @@ static xt_value xt_ctor_prototype(int index, xt_value nameValue) {
|
|
|
216
251
|
return xt_error_prototype(nameValue);
|
|
217
252
|
case XT_PROTO_TYPED:
|
|
218
253
|
return xt_typed_array_prototype(nameValue);
|
|
254
|
+
case XT_PROTO_READABLE_STREAM:
|
|
255
|
+
return xt_stream_readable_proto();
|
|
256
|
+
case XT_PROTO_WRITABLE_STREAM:
|
|
257
|
+
return xt_stream_writable_proto();
|
|
258
|
+
case XT_PROTO_TRANSFORM_STREAM:
|
|
259
|
+
return xt_stream_transform_proto();
|
|
260
|
+
case XT_PROTO_COUNT_STRATEGY:
|
|
261
|
+
return xt_stream_count_strategy_proto();
|
|
262
|
+
case XT_PROTO_BYTE_STRATEGY:
|
|
263
|
+
return xt_stream_byte_strategy_proto();
|
|
219
264
|
case XT_PROTO_PLAIN:
|
|
220
265
|
default:
|
|
221
266
|
if (xt_ctor_plain_protos[index] == XT_UNDEFINED) {
|
|
@@ -322,6 +367,7 @@ static int xt_ctor_has_static(const char *namespace, const char *method) {
|
|
|
322
367
|
return xt_name_in(method, XT_PROMISE_STATICS, XT_NAMES_COUNT(XT_PROMISE_STATICS));
|
|
323
368
|
if (xt_ctor_is_typed_array(namespace))
|
|
324
369
|
return xt_name_in(method, XT_TYPED_ARRAY_STATICS, XT_NAMES_COUNT(XT_TYPED_ARRAY_STATICS));
|
|
370
|
+
if (xt_streams_has_static(namespace, method)) return 1;
|
|
325
371
|
return 0;
|
|
326
372
|
}
|
|
327
373
|
|
|
@@ -339,6 +385,7 @@ static xt_value xt_builtin_static_call(const char *namespace, const char *method
|
|
|
339
385
|
if (strcmp(namespace, "Promise") == 0) return xt_promise_static(methodValue, argc, argv);
|
|
340
386
|
if (xt_ctor_is_typed_array(namespace))
|
|
341
387
|
return xt_typed_array_static_by_name(nameValue, methodValue, argc, argv);
|
|
388
|
+
if (xt_streams_has_static(namespace, method)) return xt_streams_static(methodValue, argc, argv);
|
|
342
389
|
return XT_UNDEFINED;
|
|
343
390
|
}
|
|
344
391
|
|
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
static const char *const XT_ERROR_NAMES[] = {
|
|
8
8
|
"Error", "TypeError", "RangeError", "SyntaxError",
|
|
9
9
|
"ReferenceError", "EvalError", "URIError", "AggregateError",
|
|
10
|
+
/* WebAssembly's three error constructors are ordinary Error members. The
|
|
11
|
+
* runtime names them so `instanceof WebAssembly.CompileError` and a real
|
|
12
|
+
* `.name`/`.message` work; they are not globals in the language. */
|
|
13
|
+
"CompileError", "LinkError", "RuntimeError",
|
|
10
14
|
};
|
|
11
15
|
|
|
12
16
|
static int xt_error_is_family(const char *name) {
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* WHATWG Streams shared helpers.
|
|
3
|
+
*
|
|
4
|
+
* A stream is a plain runtime object with a hidden `__state` property pointing
|
|
5
|
+
* at a mutable state object. Every reader, writer, controller and the stream
|
|
6
|
+
* itself share that object, so the collector reaches the whole graph from the
|
|
7
|
+
* handle the program kept. Keeping state in objects (instead of a C struct)
|
|
8
|
+
* means the GC needs no special tracing hook for streams.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#ifndef XT_STREAMS_COMMON_INC
|
|
12
|
+
#define XT_STREAMS_COMMON_INC
|
|
13
|
+
|
|
14
|
+
/* Stream kinds, stored in `state.kind`. */
|
|
15
|
+
#define XT_STREAMS_READABLE 0
|
|
16
|
+
#define XT_STREAMS_WRITABLE 1
|
|
17
|
+
#define XT_STREAMS_TRANSFORM 2
|
|
18
|
+
|
|
19
|
+
/* Readable state, stored in `state.state`. */
|
|
20
|
+
#define XT_STREAMS_RS_READABLE 0
|
|
21
|
+
#define XT_STREAMS_RS_CLOSED 1
|
|
22
|
+
#define XT_STREAMS_RS_ERRORED 2
|
|
23
|
+
|
|
24
|
+
/* Writable state, stored in `state.state`. */
|
|
25
|
+
#define XT_STREAMS_WS_WRITABLE 0
|
|
26
|
+
#define XT_STREAMS_WS_CLOSING 1
|
|
27
|
+
#define XT_STREAMS_WS_CLOSED 2
|
|
28
|
+
#define XT_STREAMS_WS_ERRORED 3
|
|
29
|
+
|
|
30
|
+
/* -- state object helpers ------------------------------------------------- */
|
|
31
|
+
|
|
32
|
+
static xt_value xts_get(xt_value object, const char *key) {
|
|
33
|
+
return xt_object_get_cstr(object, key);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static void xts_set(xt_value object, const char *key, xt_value value) {
|
|
37
|
+
xt_object_set(object, xt_string_from_cstr(key), value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static int xts_int(xt_value object, const char *key) {
|
|
41
|
+
return (int)xt_to_number(xts_get(object, key));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static int xts_truthy_field(xt_value object, const char *key) {
|
|
45
|
+
return xt_truthy(xts_get(object, key));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static xt_value xts_array(void) { return xt_array_new(0, NULL); }
|
|
49
|
+
|
|
50
|
+
static int xts_array_count(xt_value array) {
|
|
51
|
+
return XT_IS_ARRAY(array) ? (int)xt_to_number(xt_array_length(array)) : 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static xt_value xts_array_shift(xt_value array) {
|
|
55
|
+
if (!XT_IS_ARRAY(array) || xts_array_count(array) == 0) return XT_UNDEFINED;
|
|
56
|
+
return xt_call_method(array, xt_string_from_cstr("shift"), 0, NULL);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static xt_value xts_state_new(int kind) {
|
|
60
|
+
xt_value state = xt_object_new();
|
|
61
|
+
xts_set(state, "kind", xt_number((double)kind));
|
|
62
|
+
xts_set(state, "state", xt_number(0));
|
|
63
|
+
xts_set(state, "storedError", XT_UNDEFINED);
|
|
64
|
+
xts_set(state, "locked", XT_FALSE);
|
|
65
|
+
xts_set(state, "reader", XT_UNDEFINED);
|
|
66
|
+
xts_set(state, "writer", XT_UNDEFINED);
|
|
67
|
+
return state;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* -- promises ------------------------------------------------------------- */
|
|
71
|
+
|
|
72
|
+
/** Attach handlers to `promise` and suppress the derivative's rejection: the
|
|
73
|
+
* stream machinery always consumes it. Non-promise values resolve normally. */
|
|
74
|
+
static xt_value xts_then(xt_value promise, xt_value on_fulfilled, xt_value on_rejected) {
|
|
75
|
+
if (!xt_is_promise(promise)) return xt_promise_resolve(promise);
|
|
76
|
+
int handled = 0;
|
|
77
|
+
xt_value args[2];
|
|
78
|
+
args[0] = on_fulfilled;
|
|
79
|
+
args[1] = on_rejected;
|
|
80
|
+
xt_value result = xt_promise_method(promise, "then", 2, args, &handled);
|
|
81
|
+
if (xt_is_promise(result)) xt_promise_observe_value(result);
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static xt_value xts_resolve(xt_value value) { return xt_promise_resolve(value); }
|
|
86
|
+
static xt_value xts_reject(xt_value error) { return xt_promise_reject(error); }
|
|
87
|
+
|
|
88
|
+
/** Reject an internally-owned promise (a reader/writer `closed`/`ready`) that
|
|
89
|
+
* the program is not required to observe. Handlers still run; only the
|
|
90
|
+
* unhandled-rejection report is suppressed. */
|
|
91
|
+
static void xts_reject_observed(xt_value promise, xt_value error) {
|
|
92
|
+
if (!xt_is_promise(promise)) return;
|
|
93
|
+
xt_promise_observe_value(promise);
|
|
94
|
+
xt_promise_reject_value(promise, error);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* Build a real Error-family value. `xt_error_from_message` strips the
|
|
98
|
+
* "TypeError: " / "RangeError: " prefix from `.message` and gives the error the
|
|
99
|
+
* matching prototype, which is what `catch` and `.name` should observe. The
|
|
100
|
+
* call sites pass either a bare message or one that already carries the
|
|
101
|
+
* prefix. */
|
|
102
|
+
static xt_value xts_error_with_prefix(const char *prefix, const char *message) {
|
|
103
|
+
char buffer[512];
|
|
104
|
+
size_t prefix_length = strlen(prefix);
|
|
105
|
+
if (strncmp(message, prefix, prefix_length) == 0) {
|
|
106
|
+
snprintf(buffer, sizeof(buffer), "%s", message);
|
|
107
|
+
} else {
|
|
108
|
+
snprintf(buffer, sizeof(buffer), "%s: %s", prefix, message);
|
|
109
|
+
}
|
|
110
|
+
return xt_error_from_message(buffer);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static xt_value xts_type_error(const char *message) {
|
|
114
|
+
return xts_error_with_prefix("TypeError", message);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
static xt_value xts_range_error(const char *message) {
|
|
118
|
+
return xts_error_with_prefix("RangeError", message);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static xt_value xts_throw_type(const char *message) {
|
|
122
|
+
xt_value error = xts_type_error(message);
|
|
123
|
+
xt_throw(error);
|
|
124
|
+
return error;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** `{ value, done }`, the result shape shared by readers and async iterators. */
|
|
128
|
+
static xt_value xts_result(xt_value value, int done) {
|
|
129
|
+
xt_value result = xt_object_new();
|
|
130
|
+
xts_set(result, "value", value);
|
|
131
|
+
xts_set(result, "done", xt_bool(done));
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A native closure with a copied environment. */
|
|
136
|
+
static xt_value xts_closure(void *fn, int32_t count, xt_value *environment) {
|
|
137
|
+
return xt_closure_new(fn, count, environment);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Call a user-supplied stream hook under a C exception boundary. A throwing
|
|
142
|
+
* `start`/`pull`/`write`/`close` must error the stream (and reject the relevant
|
|
143
|
+
* promise), not unwind through the constructor or `writer.write`.
|
|
144
|
+
*/
|
|
145
|
+
static int xts_try_call(xt_value fn, xt_value thisValue, int32_t argc, xt_value *argv,
|
|
146
|
+
xt_value *result, xt_value *error) {
|
|
147
|
+
if (!XT_IS_FUNCTION(fn)) {
|
|
148
|
+
*result = XT_UNDEFINED;
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
void *frame = xt_try_enter();
|
|
152
|
+
if (xt_try_setjmp(frame) == 0) {
|
|
153
|
+
*result = xt_call_with_this(fn, thisValue, argc, argv);
|
|
154
|
+
xt_try_leave(frame);
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
*error = xt_try_exception(frame);
|
|
158
|
+
xt_try_leave(frame);
|
|
159
|
+
return 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/* -- strategy ------------------------------------------------------------- */
|
|
163
|
+
|
|
164
|
+
/** Read `highWaterMark` from a queuing strategy (default 1). */
|
|
165
|
+
static int xts_strategy_hwm(xt_value strategy) {
|
|
166
|
+
if (XT_IS_OBJECT(strategy)) {
|
|
167
|
+
xt_value hwm = xts_get(strategy, "highWaterMark");
|
|
168
|
+
if (XT_IS_NUMBER(hwm)) {
|
|
169
|
+
double value = xt_to_number(hwm);
|
|
170
|
+
if (value >= 0 && value == value) return (int)value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return 1;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** `strategy.size`, or `undefined` when the strategy has none. */
|
|
177
|
+
static xt_value xts_strategy_size(xt_value strategy) {
|
|
178
|
+
if (XT_IS_OBJECT(strategy)) {
|
|
179
|
+
xt_value size = xts_get(strategy, "size");
|
|
180
|
+
if (XT_IS_FUNCTION(size)) return size;
|
|
181
|
+
}
|
|
182
|
+
return XT_UNDEFINED;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/* -- prototypes and cross-module entry points ----------------------------- */
|
|
186
|
+
|
|
187
|
+
xt_value xt_stream_readable_proto(void);
|
|
188
|
+
xt_value xt_stream_writable_proto(void);
|
|
189
|
+
xt_value xt_stream_transform_proto(void);
|
|
190
|
+
xt_value xt_stream_readable_controller_proto(void);
|
|
191
|
+
xt_value xt_stream_writable_controller_proto(void);
|
|
192
|
+
xt_value xt_stream_reader_proto(void);
|
|
193
|
+
xt_value xt_stream_writer_proto(void);
|
|
194
|
+
xt_value xt_stream_transform_controller_proto(void);
|
|
195
|
+
xt_value xt_stream_count_strategy_proto(void);
|
|
196
|
+
xt_value xt_stream_byte_strategy_proto(void);
|
|
197
|
+
|
|
198
|
+
xt_value xt_readable_stream_ctor(int32_t argc, xt_value *argv);
|
|
199
|
+
xt_value xt_writable_stream_ctor(int32_t argc, xt_value *argv);
|
|
200
|
+
xt_value xt_transform_stream_ctor(int32_t argc, xt_value *argv);
|
|
201
|
+
xt_value xt_count_queuing_strategy_ctor(int32_t argc, xt_value *argv);
|
|
202
|
+
xt_value xt_byte_length_queuing_strategy_ctor(int32_t argc, xt_value *argv);
|
|
203
|
+
xt_value xt_text_encoder_stream_ctor(int32_t argc, xt_value *argv);
|
|
204
|
+
xt_value xt_text_decoder_stream_ctor(int32_t argc, xt_value *argv);
|
|
205
|
+
|
|
206
|
+
/* `ReadableStream.from`, exported through the built-in constructor table. */
|
|
207
|
+
int xt_streams_has_static(const char *name, const char *method);
|
|
208
|
+
xt_value xt_streams_static(xt_value name, int32_t argc, xt_value *argv);
|
|
209
|
+
int xt_streams_instance_of(xt_value value, int32_t kind);
|
|
210
|
+
|
|
211
|
+
/* -- shared stream operations (implemented in the readable/writable parts) - */
|
|
212
|
+
|
|
213
|
+
static xt_value xts_readable_create(xt_value state, xt_value source, xt_value strategy);
|
|
214
|
+
static void xts_readable_pull_if_needed(xt_value state);
|
|
215
|
+
static void xts_readable_close(xt_value state);
|
|
216
|
+
static void xts_readable_error(xt_value state, xt_value error);
|
|
217
|
+
static xt_value xts_readable_controller_enqueue(xt_value state, xt_value chunk);
|
|
218
|
+
static xt_value xts_readable_controller_close(xt_value state);
|
|
219
|
+
static xt_value xts_readable_cancel(xt_value state, xt_value reason);
|
|
220
|
+
static xt_value xts_readable_reader_new(xt_value stream, xt_value state);
|
|
221
|
+
|
|
222
|
+
static xt_value xts_writable_create(xt_value state, xt_value sink, xt_value strategy);
|
|
223
|
+
static void xts_writable_process(xt_value state);
|
|
224
|
+
static void xts_writable_error(xt_value state, xt_value error);
|
|
225
|
+
static void xts_writable_close_step(xt_value state);
|
|
226
|
+
static xt_value xts_writable_abort(xt_value state, xt_value reason);
|
|
227
|
+
static xt_value xts_writable_writer_new(xt_value stream, xt_value state);
|
|
228
|
+
|
|
229
|
+
static xt_value xts_transform_create(xt_value transformer, xt_value readable_strategy,
|
|
230
|
+
xt_value writable_strategy);
|
|
231
|
+
|
|
232
|
+
#endif /* XT_STREAMS_COMMON_INC */
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/* Global constructors, statics and `instanceof` support for the Web Streams. */
|
|
2
|
+
|
|
3
|
+
#ifndef XT_STREAMS_CONSTRUCTORS_INC
|
|
4
|
+
#define XT_STREAMS_CONSTRUCTORS_INC
|
|
5
|
+
|
|
6
|
+
xt_value xt_readable_stream_ctor(int32_t argc, xt_value *argv) {
|
|
7
|
+
xt_value state = xts_state_new(XT_STREAMS_READABLE);
|
|
8
|
+
xt_value source = xt_arg_at(argc, argv, 0);
|
|
9
|
+
xt_value strategy = xt_arg_at(argc, argv, 1);
|
|
10
|
+
return xts_readable_create(state, source, strategy);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
xt_value xt_writable_stream_ctor(int32_t argc, xt_value *argv) {
|
|
14
|
+
xt_value state = xts_state_new(XT_STREAMS_WRITABLE);
|
|
15
|
+
xt_value sink = xt_arg_at(argc, argv, 0);
|
|
16
|
+
xt_value strategy = xt_arg_at(argc, argv, 1);
|
|
17
|
+
return xts_writable_create(state, sink, strategy);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
xt_value xt_transform_stream_ctor(int32_t argc, xt_value *argv) {
|
|
21
|
+
xt_value transformer = xt_arg_at(argc, argv, 0);
|
|
22
|
+
xt_value readable_strategy = xt_arg_at(argc, argv, 1);
|
|
23
|
+
xt_value writable_strategy = xt_arg_at(argc, argv, 2);
|
|
24
|
+
return xts_transform_create(transformer, readable_strategy, writable_strategy);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* First-class constructor values for imported bindings. `xt_builtin_constructor`
|
|
28
|
+
* installs the same prototype the direct C constructor uses, so an imported
|
|
29
|
+
* `ReadableStream` supports `instanceof` and statics just like the global. */
|
|
30
|
+
static xt_value xts_constructor_value(const char *name) {
|
|
31
|
+
return xt_builtin_constructor(xt_string_from_cstr(name));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
xt_value xt_readable_stream_constructor_value(int32_t argc, xt_value *argv) {
|
|
35
|
+
(void)argc;
|
|
36
|
+
(void)argv;
|
|
37
|
+
return xts_constructor_value("ReadableStream");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
xt_value xt_writable_stream_constructor_value(int32_t argc, xt_value *argv) {
|
|
41
|
+
(void)argc;
|
|
42
|
+
(void)argv;
|
|
43
|
+
return xts_constructor_value("WritableStream");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
xt_value xt_transform_stream_constructor_value(int32_t argc, xt_value *argv) {
|
|
47
|
+
(void)argc;
|
|
48
|
+
(void)argv;
|
|
49
|
+
return xts_constructor_value("TransformStream");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
xt_value xt_count_strategy_constructor_value(int32_t argc, xt_value *argv) {
|
|
53
|
+
(void)argc;
|
|
54
|
+
(void)argv;
|
|
55
|
+
return xts_constructor_value("CountQueuingStrategy");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
xt_value xt_byte_strategy_constructor_value(int32_t argc, xt_value *argv) {
|
|
59
|
+
(void)argc;
|
|
60
|
+
(void)argv;
|
|
61
|
+
return xts_constructor_value("ByteLengthQueuingStrategy");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
xt_value xt_text_encoder_stream_constructor_value(int32_t argc, xt_value *argv) {
|
|
65
|
+
(void)argc;
|
|
66
|
+
(void)argv;
|
|
67
|
+
return xts_constructor_value("TextEncoderStream");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
xt_value xt_text_decoder_stream_constructor_value(int32_t argc, xt_value *argv) {
|
|
71
|
+
(void)argc;
|
|
72
|
+
(void)argv;
|
|
73
|
+
return xts_constructor_value("TextDecoderStream");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
int xt_streams_has_static(const char *name, const char *method) {
|
|
77
|
+
if (!name || !method) return 0;
|
|
78
|
+
if (strcmp(name, "ReadableStream") == 0 && strcmp(method, "from") == 0) return 1;
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static xt_value xts_from_async_step(xt_value pipe);
|
|
83
|
+
|
|
84
|
+
static xt_value xts_from_async_on_read(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
85
|
+
(void)thisValue;
|
|
86
|
+
(void)argc;
|
|
87
|
+
xt_value pipe = xt_closure_env(env, 0);
|
|
88
|
+
xt_value state = xts_get(pipe, "state");
|
|
89
|
+
if (xts_int(state, "state") != XT_STREAMS_RS_READABLE) return XT_UNDEFINED;
|
|
90
|
+
xt_value result = xt_arg_at(argc, argv, 0);
|
|
91
|
+
if (xt_truthy(xts_get(result, "done"))) {
|
|
92
|
+
if (!xts_truthy_field(state, "closeRequested")) xts_readable_controller_close(state);
|
|
93
|
+
return XT_UNDEFINED;
|
|
94
|
+
}
|
|
95
|
+
xts_readable_controller_enqueue(state, xts_get(result, "value"));
|
|
96
|
+
xts_from_async_step(pipe);
|
|
97
|
+
return XT_UNDEFINED;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static xt_value xts_from_async_on_error(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
101
|
+
(void)thisValue;
|
|
102
|
+
xt_value pipe = xt_closure_env(env, 0);
|
|
103
|
+
xts_readable_error(xts_get(pipe, "state"), xt_arg_at(argc, argv, 0));
|
|
104
|
+
return XT_UNDEFINED;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static xt_value xts_from_async_step(xt_value pipe) {
|
|
108
|
+
xt_value promise = xt_async_iter_next(xts_get(pipe, "iterator"));
|
|
109
|
+
xt_value environment[1] = {pipe};
|
|
110
|
+
xts_then(promise, xts_closure((void *)xts_from_async_on_read, 1, environment),
|
|
111
|
+
xts_closure((void *)xts_from_async_on_error, 1, environment));
|
|
112
|
+
return XT_UNDEFINED;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static int xts_enqueue_iterable(xt_value state, xt_value source) {
|
|
116
|
+
if (source == XT_UNDEFINED || source == XT_NULL) return 0;
|
|
117
|
+
xt_value iterator = xt_iter_open(source);
|
|
118
|
+
if (iterator == XT_UNDEFINED || iterator == XT_NULL) return -1;
|
|
119
|
+
int index = 0;
|
|
120
|
+
while (1) {
|
|
121
|
+
xt_value index_value = xt_number((double)index);
|
|
122
|
+
if (!xt_truthy(xt_iter_has(iterator, index_value))) break;
|
|
123
|
+
xt_value value = xt_iter_value(iterator, index_value);
|
|
124
|
+
xts_readable_controller_enqueue(state, value);
|
|
125
|
+
index++;
|
|
126
|
+
if (index > 100000000) break;
|
|
127
|
+
}
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
xt_value xt_streams_static(xt_value name, int32_t argc, xt_value *argv) {
|
|
132
|
+
(void)name;
|
|
133
|
+
xt_value source = xt_arg_at(argc, argv, 0);
|
|
134
|
+
xt_value state = xts_state_new(XT_STREAMS_READABLE);
|
|
135
|
+
xt_value stream = xts_readable_create(state, XT_UNDEFINED, XT_UNDEFINED);
|
|
136
|
+
|
|
137
|
+
/* Prefer the asynchronous iteration protocol when the source exposes one;
|
|
138
|
+
* its first chunk is produced when the returned stream is read. */
|
|
139
|
+
if (XT_IS_OBJECT(source)) {
|
|
140
|
+
xt_value async_method = xt_get(source, xt_symbol_well_known("asyncIterator"));
|
|
141
|
+
if (XT_IS_FUNCTION(async_method)) {
|
|
142
|
+
xt_value iterator = xt_async_iter_open(source);
|
|
143
|
+
xt_value pipe = xt_object_new();
|
|
144
|
+
xts_set(pipe, "state", state);
|
|
145
|
+
xts_set(pipe, "iterator", iterator);
|
|
146
|
+
xts_from_async_step(pipe);
|
|
147
|
+
return stream;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (xts_enqueue_iterable(state, source) < 0) {
|
|
152
|
+
xts_readable_error(state, xts_type_error("TypeError: ReadableStream.from expects an iterable"));
|
|
153
|
+
return stream;
|
|
154
|
+
}
|
|
155
|
+
xts_readable_controller_close(state);
|
|
156
|
+
return stream;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
int xt_streams_instance_of(xt_value value, int32_t kind) {
|
|
160
|
+
if (!XT_IS_OBJECT(value)) return 0;
|
|
161
|
+
xt_value state = xts_get(value, "__state");
|
|
162
|
+
if (!XT_IS_OBJECT(state)) return 0;
|
|
163
|
+
int stream_kind = xts_int(state, "kind");
|
|
164
|
+
switch (kind) {
|
|
165
|
+
case 0:
|
|
166
|
+
return stream_kind == XT_STREAMS_READABLE;
|
|
167
|
+
case 1:
|
|
168
|
+
return stream_kind == XT_STREAMS_WRITABLE;
|
|
169
|
+
case 2:
|
|
170
|
+
return stream_kind == XT_STREAMS_TRANSFORM;
|
|
171
|
+
case 3:
|
|
172
|
+
return stream_kind == XT_STREAMS_TRANSFORM && xts_int(state, "textKind") == 1;
|
|
173
|
+
case 4:
|
|
174
|
+
return stream_kind == XT_STREAMS_TRANSFORM && xts_int(state, "textKind") == 2;
|
|
175
|
+
default:
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* -- illegal constructors ------------------------------------------------- */
|
|
181
|
+
|
|
182
|
+
static xt_value xts_illegal_constructor(const char *name) {
|
|
183
|
+
char message[160];
|
|
184
|
+
snprintf(message, sizeof(message), "TypeError: Illegal constructor: %s", name);
|
|
185
|
+
xt_value error = xts_type_error(message);
|
|
186
|
+
xt_throw(error);
|
|
187
|
+
return XT_UNDEFINED;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
xt_value xt_readable_stream_default_reader_ctor(int32_t argc, xt_value *argv) {
|
|
191
|
+
(void)argc;
|
|
192
|
+
(void)argv;
|
|
193
|
+
return xts_illegal_constructor("ReadableStreamDefaultReader");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
xt_value xt_readable_stream_byob_reader_ctor(int32_t argc, xt_value *argv) {
|
|
197
|
+
(void)argc;
|
|
198
|
+
(void)argv;
|
|
199
|
+
return xts_illegal_constructor("ReadableStreamBYOBReader");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
xt_value xt_readable_stream_byob_request_ctor(int32_t argc, xt_value *argv) {
|
|
203
|
+
(void)argc;
|
|
204
|
+
(void)argv;
|
|
205
|
+
return xts_illegal_constructor("ReadableStreamBYOBRequest");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
xt_value xt_readable_stream_default_controller_ctor(int32_t argc, xt_value *argv) {
|
|
209
|
+
(void)argc;
|
|
210
|
+
(void)argv;
|
|
211
|
+
return xts_illegal_constructor("ReadableStreamDefaultController");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
xt_value xt_writable_stream_default_writer_ctor(int32_t argc, xt_value *argv) {
|
|
215
|
+
(void)argc;
|
|
216
|
+
(void)argv;
|
|
217
|
+
return xts_illegal_constructor("WritableStreamDefaultWriter");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
xt_value xt_writable_stream_default_controller_ctor(int32_t argc, xt_value *argv) {
|
|
221
|
+
(void)argc;
|
|
222
|
+
(void)argv;
|
|
223
|
+
return xts_illegal_constructor("WritableStreamDefaultController");
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
xt_value xt_transform_stream_default_controller_ctor(int32_t argc, xt_value *argv) {
|
|
227
|
+
(void)argc;
|
|
228
|
+
(void)argv;
|
|
229
|
+
return xts_illegal_constructor("TransformStreamDefaultController");
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
#endif /* XT_STREAMS_CONSTRUCTORS_INC */
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/* Queuing strategies and the TextEncoderStream / TextDecoderStream subset. */
|
|
2
|
+
|
|
3
|
+
#ifndef XT_STREAMS_ENCODING_INC
|
|
4
|
+
#define XT_STREAMS_ENCODING_INC
|
|
5
|
+
|
|
6
|
+
/* TextEncoder / TextDecoder constructors live in xt_text_codec.c. */
|
|
7
|
+
xt_value xt_text_encoder_ctor(int32_t argc, xt_value *argv);
|
|
8
|
+
xt_value xt_text_decoder_ctor(int32_t argc, xt_value *argv);
|
|
9
|
+
|
|
10
|
+
/* -- queuing strategies --------------------------------------------------- */
|
|
11
|
+
|
|
12
|
+
static xt_value xts_count_size(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
13
|
+
(void)self;
|
|
14
|
+
(void)env;
|
|
15
|
+
(void)argc;
|
|
16
|
+
(void)argv;
|
|
17
|
+
return xt_number(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static xt_value xts_byte_size(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
21
|
+
(void)self;
|
|
22
|
+
(void)env;
|
|
23
|
+
xt_value chunk = xt_arg_at(argc, argv, 0);
|
|
24
|
+
xt_value length = XT_IS_OBJECT(chunk) ? xts_get(chunk, "byteLength") : XT_UNDEFINED;
|
|
25
|
+
if (XT_IS_NUMBER(length)) return length;
|
|
26
|
+
return xt_number(0);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static xt_value xts_strategy_new(xt_value proto, xt_value options) {
|
|
30
|
+
xt_value strategy = xt_object_new_with_proto(proto);
|
|
31
|
+
xt_value hwm = XT_IS_OBJECT(options) ? xts_get(options, "highWaterMark") : XT_UNDEFINED;
|
|
32
|
+
if (!XT_IS_NUMBER(hwm)) hwm = xt_number(1);
|
|
33
|
+
xts_set(strategy, "highWaterMark", hwm);
|
|
34
|
+
return strategy;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
xt_value xt_stream_count_strategy_proto(void) {
|
|
38
|
+
static xt_value proto = XT_UNDEFINED;
|
|
39
|
+
if (XT_IS_OBJECT(proto)) return proto;
|
|
40
|
+
proto = xt_object_new();
|
|
41
|
+
xt_gc_add_root(&proto);
|
|
42
|
+
xts_set(proto, "size", xts_closure((void *)xts_count_size, 0, NULL));
|
|
43
|
+
return proto;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
xt_value xt_stream_byte_strategy_proto(void) {
|
|
47
|
+
static xt_value proto = XT_UNDEFINED;
|
|
48
|
+
if (XT_IS_OBJECT(proto)) return proto;
|
|
49
|
+
proto = xt_object_new();
|
|
50
|
+
xt_gc_add_root(&proto);
|
|
51
|
+
xts_set(proto, "size", xts_closure((void *)xts_byte_size, 0, NULL));
|
|
52
|
+
return proto;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
xt_value xt_count_queuing_strategy_ctor(int32_t argc, xt_value *argv) {
|
|
56
|
+
return xts_strategy_new(xt_stream_count_strategy_proto(), xt_arg_at(argc, argv, 0));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
xt_value xt_byte_length_queuing_strategy_ctor(int32_t argc, xt_value *argv) {
|
|
60
|
+
return xts_strategy_new(xt_stream_byte_strategy_proto(), xt_arg_at(argc, argv, 0));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* -- text encoder stream -------------------------------------------------- */
|
|
64
|
+
|
|
65
|
+
static xt_value xts_text_encoder_transform(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
66
|
+
(void)self;
|
|
67
|
+
(void)env;
|
|
68
|
+
xt_value chunk = xt_arg_at(argc, argv, 0);
|
|
69
|
+
xt_value controller = xt_arg_at(argc, argv, 1);
|
|
70
|
+
xt_value encoder = xt_text_encoder_ctor(0, NULL);
|
|
71
|
+
xt_value text = xt_to_string(chunk);
|
|
72
|
+
xt_value bytes = xt_call_method(encoder, xt_string_from_cstr("encode"), 1, &text);
|
|
73
|
+
xt_call_method(controller, xt_string_from_cstr("enqueue"), 1, &bytes);
|
|
74
|
+
return XT_UNDEFINED;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static xt_value xts_text_stream_flush(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
78
|
+
(void)self;
|
|
79
|
+
(void)env;
|
|
80
|
+
(void)argc;
|
|
81
|
+
(void)argv;
|
|
82
|
+
return XT_UNDEFINED;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
xt_value xt_text_encoder_stream_ctor(int32_t argc, xt_value *argv) {
|
|
86
|
+
(void)argc;
|
|
87
|
+
(void)argv;
|
|
88
|
+
xt_value transformer = xt_object_new();
|
|
89
|
+
xts_set(transformer, "transform", xts_closure((void *)xts_text_encoder_transform, 0, NULL));
|
|
90
|
+
xts_set(transformer, "flush", xts_closure((void *)xts_text_stream_flush, 0, NULL));
|
|
91
|
+
xt_value stream = xts_transform_create(transformer, XT_UNDEFINED, XT_UNDEFINED);
|
|
92
|
+
xts_set(stream, "encoding", xt_string_from_cstr("utf-8"));
|
|
93
|
+
xt_value state = xts_get(stream, "__state");
|
|
94
|
+
if (XT_IS_OBJECT(state)) xts_set(state, "textKind", xt_number(1));
|
|
95
|
+
return stream;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* -- text decoder stream -------------------------------------------------- */
|
|
99
|
+
|
|
100
|
+
static xt_value xts_text_decoder_transform(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
101
|
+
(void)self;
|
|
102
|
+
(void)env;
|
|
103
|
+
xt_value chunk = xt_arg_at(argc, argv, 0);
|
|
104
|
+
xt_value controller = xt_arg_at(argc, argv, 1);
|
|
105
|
+
xt_value decoder = xt_text_decoder_ctor(0, NULL);
|
|
106
|
+
xt_value text = xt_call_method(decoder, xt_string_from_cstr("decode"), 1, &chunk);
|
|
107
|
+
xt_call_method(controller, xt_string_from_cstr("enqueue"), 1, &text);
|
|
108
|
+
return XT_UNDEFINED;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
xt_value xt_text_decoder_stream_ctor(int32_t argc, xt_value *argv) {
|
|
112
|
+
(void)argc;
|
|
113
|
+
(void)argv;
|
|
114
|
+
xt_value transformer = xt_object_new();
|
|
115
|
+
xts_set(transformer, "transform", xts_closure((void *)xts_text_decoder_transform, 0, NULL));
|
|
116
|
+
xts_set(transformer, "flush", xts_closure((void *)xts_text_stream_flush, 0, NULL));
|
|
117
|
+
xt_value stream = xts_transform_create(transformer, XT_UNDEFINED, XT_UNDEFINED);
|
|
118
|
+
xts_set(stream, "encoding", xt_string_from_cstr("utf-8"));
|
|
119
|
+
xt_value state = xts_get(stream, "__state");
|
|
120
|
+
if (XT_IS_OBJECT(state)) xts_set(state, "textKind", xt_number(2));
|
|
121
|
+
return stream;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#endif /* XT_STREAMS_ENCODING_INC */
|