zuzu-js 0.1.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.
Files changed (167) hide show
  1. package/LICENSE +5 -0
  2. package/README.md +113 -0
  3. package/bin/zuzu +17 -0
  4. package/bin/zuzu-build-browser-bundle +57 -0
  5. package/bin/zuzu-generate-browser-stdlib +584 -0
  6. package/bin/zuzu-js +23 -0
  7. package/bin/zuzu-js-compile +152 -0
  8. package/bin/zuzu-js-electron +19 -0
  9. package/dist/zuzu-browser-worker.js +45574 -0
  10. package/dist/zuzu-browser.js +45362 -0
  11. package/lib/browser-bundle-entry.js +160 -0
  12. package/lib/browser-gui-renderer.js +387 -0
  13. package/lib/browser-runtime.js +167 -0
  14. package/lib/browser-worker-entry.js +413 -0
  15. package/lib/browser-ztests/runner.html +103 -0
  16. package/lib/browser-ztests/runner.js +369 -0
  17. package/lib/cli.js +350 -0
  18. package/lib/collections.js +367 -0
  19. package/lib/compiler.js +303 -0
  20. package/lib/electron/launcher.js +70 -0
  21. package/lib/electron/main.js +956 -0
  22. package/lib/electron/preload.js +80 -0
  23. package/lib/electron/renderer.html +122 -0
  24. package/lib/electron/renderer.js +24 -0
  25. package/lib/execution-metadata.js +18 -0
  26. package/lib/gui/dom-renderer.js +778 -0
  27. package/lib/host/browser-host.js +278 -0
  28. package/lib/host/capabilities.js +47 -0
  29. package/lib/host/electron-host.js +15 -0
  30. package/lib/host/node-host.js +74 -0
  31. package/lib/paths.js +150 -0
  32. package/lib/runtime-entrypoints.js +60 -0
  33. package/lib/runtime-helpers.js +886 -0
  34. package/lib/runtime.js +3529 -0
  35. package/lib/tap.js +37 -0
  36. package/lib/transpiler-new/ast.js +23 -0
  37. package/lib/transpiler-new/codegen.js +2455 -0
  38. package/lib/transpiler-new/errors.js +28 -0
  39. package/lib/transpiler-new/index.js +26 -0
  40. package/lib/transpiler-new/lexer.js +834 -0
  41. package/lib/transpiler-new/parser.js +2332 -0
  42. package/lib/transpiler-new/validate-bindings.js +326 -0
  43. package/lib/transpiler-utils.js +95 -0
  44. package/lib/transpiler.js +33 -0
  45. package/lib/zuzu.js +53 -0
  46. package/modules/javascript.js +193 -0
  47. package/modules/std/archive.js +603 -0
  48. package/modules/std/clib.js +338 -0
  49. package/modules/std/data/csv.js +1331 -0
  50. package/modules/std/data/json.js +531 -0
  51. package/modules/std/data/xml.js +441 -0
  52. package/modules/std/data/yaml.js +256 -0
  53. package/modules/std/db-worker.js +250 -0
  54. package/modules/std/db.js +664 -0
  55. package/modules/std/digest/_hash.js +443 -0
  56. package/modules/std/digest/md5.js +26 -0
  57. package/modules/std/digest/sha.js +72 -0
  58. package/modules/std/eval.js +10 -0
  59. package/modules/std/gui/objects.js +1519 -0
  60. package/modules/std/internals.js +571 -0
  61. package/modules/std/io/socks-worker.js +318 -0
  62. package/modules/std/io/socks.js +186 -0
  63. package/modules/std/io.js +475 -0
  64. package/modules/std/marshal/cbor.js +463 -0
  65. package/modules/std/marshal/graph.js +1624 -0
  66. package/modules/std/marshal.js +87 -0
  67. package/modules/std/math/bignum.js +91 -0
  68. package/modules/std/math.js +79 -0
  69. package/modules/std/net/dns.js +306 -0
  70. package/modules/std/net/http.js +820 -0
  71. package/modules/std/net/smtp.js +943 -0
  72. package/modules/std/net/url.js +109 -0
  73. package/modules/std/proc.js +602 -0
  74. package/modules/std/secure.js +3724 -0
  75. package/modules/std/string/base64.js +138 -0
  76. package/modules/std/string.js +299 -0
  77. package/modules/std/task.js +914 -0
  78. package/modules/std/time.js +579 -0
  79. package/modules/std/tui.js +188 -0
  80. package/modules/std/worker-thread.js +246 -0
  81. package/modules/std/worker.js +790 -0
  82. package/package.json +67 -0
  83. package/stdlib/modules/javascript.zzm +99 -0
  84. package/stdlib/modules/perl.zzm +105 -0
  85. package/stdlib/modules/std/archive.zzm +132 -0
  86. package/stdlib/modules/std/cache/lru.zzm +174 -0
  87. package/stdlib/modules/std/clib.zzm +112 -0
  88. package/stdlib/modules/std/colour.zzm +220 -0
  89. package/stdlib/modules/std/config.zzm +818 -0
  90. package/stdlib/modules/std/data/cbor.zzm +497 -0
  91. package/stdlib/modules/std/data/csv.zzm +285 -0
  92. package/stdlib/modules/std/data/ini.zzm +472 -0
  93. package/stdlib/modules/std/data/json/schema/core.zzm +573 -0
  94. package/stdlib/modules/std/data/json/schema/format.zzm +581 -0
  95. package/stdlib/modules/std/data/json/schema/model.zzm +255 -0
  96. package/stdlib/modules/std/data/json/schema/output.zzm +272 -0
  97. package/stdlib/modules/std/data/json/schema/relative_pointer.zzm +299 -0
  98. package/stdlib/modules/std/data/json/schema/validation.zzm +1503 -0
  99. package/stdlib/modules/std/data/json/schema.zzm +306 -0
  100. package/stdlib/modules/std/data/json.zzm +102 -0
  101. package/stdlib/modules/std/data/kdl/json.zzm +460 -0
  102. package/stdlib/modules/std/data/kdl/xml.zzm +387 -0
  103. package/stdlib/modules/std/data/kdl.zzm +1631 -0
  104. package/stdlib/modules/std/data/toml.zzm +756 -0
  105. package/stdlib/modules/std/data/toon.zzm +1017 -0
  106. package/stdlib/modules/std/data/xml/escape.zzm +156 -0
  107. package/stdlib/modules/std/data/xml.zzm +276 -0
  108. package/stdlib/modules/std/data/yaml.zzm +94 -0
  109. package/stdlib/modules/std/db.zzm +173 -0
  110. package/stdlib/modules/std/defer.zzm +75 -0
  111. package/stdlib/modules/std/digest/crc32.zzm +196 -0
  112. package/stdlib/modules/std/digest/md5.zzm +54 -0
  113. package/stdlib/modules/std/digest/sha.zzm +83 -0
  114. package/stdlib/modules/std/dump.zzm +317 -0
  115. package/stdlib/modules/std/eval.zzm +63 -0
  116. package/stdlib/modules/std/getopt.zzm +432 -0
  117. package/stdlib/modules/std/gui/dialogue.zzm +592 -0
  118. package/stdlib/modules/std/gui/objects.zzm +123 -0
  119. package/stdlib/modules/std/gui.zzm +1914 -0
  120. package/stdlib/modules/std/internals.zzm +139 -0
  121. package/stdlib/modules/std/io/socks.zzm +139 -0
  122. package/stdlib/modules/std/io.zzm +157 -0
  123. package/stdlib/modules/std/lingua/en.zzm +347 -0
  124. package/stdlib/modules/std/log.zzm +169 -0
  125. package/stdlib/modules/std/mail.zzm +2726 -0
  126. package/stdlib/modules/std/marshal.zzm +138 -0
  127. package/stdlib/modules/std/math/bignum.zzm +98 -0
  128. package/stdlib/modules/std/math/range.zzm +116 -0
  129. package/stdlib/modules/std/math/roman.zzm +156 -0
  130. package/stdlib/modules/std/math.zzm +141 -0
  131. package/stdlib/modules/std/net/dns.zzm +93 -0
  132. package/stdlib/modules/std/net/http.zzm +278 -0
  133. package/stdlib/modules/std/net/smtp.zzm +257 -0
  134. package/stdlib/modules/std/net/url.zzm +69 -0
  135. package/stdlib/modules/std/path/jsonpointer.zzm +526 -0
  136. package/stdlib/modules/std/path/kdl.zzm +1003 -0
  137. package/stdlib/modules/std/path/simple.zzm +520 -0
  138. package/stdlib/modules/std/path/z/context.zzm +147 -0
  139. package/stdlib/modules/std/path/z/evaluate.zzm +549 -0
  140. package/stdlib/modules/std/path/z/functions.zzm +874 -0
  141. package/stdlib/modules/std/path/z/lexer.zzm +490 -0
  142. package/stdlib/modules/std/path/z/node.zzm +1455 -0
  143. package/stdlib/modules/std/path/z/operators.zzm +445 -0
  144. package/stdlib/modules/std/path/z/parser.zzm +359 -0
  145. package/stdlib/modules/std/path/z.zzm +403 -0
  146. package/stdlib/modules/std/path/zz/functions.zzm +828 -0
  147. package/stdlib/modules/std/path/zz/operators.zzm +1036 -0
  148. package/stdlib/modules/std/path/zz.zzm +100 -0
  149. package/stdlib/modules/std/proc.zzm +155 -0
  150. package/stdlib/modules/std/result.zzm +149 -0
  151. package/stdlib/modules/std/secure.zzm +606 -0
  152. package/stdlib/modules/std/string/base64.zzm +66 -0
  153. package/stdlib/modules/std/string/quoted_printable.zzm +485 -0
  154. package/stdlib/modules/std/string.zzm +179 -0
  155. package/stdlib/modules/std/task.zzm +221 -0
  156. package/stdlib/modules/std/template/z.zzm +531 -0
  157. package/stdlib/modules/std/template/zz.zzm +62 -0
  158. package/stdlib/modules/std/time.zzm +188 -0
  159. package/stdlib/modules/std/tui.zzm +89 -0
  160. package/stdlib/modules/std/uuid.zzm +223 -0
  161. package/stdlib/modules/std/web/session.zzm +388 -0
  162. package/stdlib/modules/std/web/static.zzm +329 -0
  163. package/stdlib/modules/std/web.zzm +1942 -0
  164. package/stdlib/modules/std/worker.zzm +202 -0
  165. package/stdlib/modules/std/zuzuzoo.zzm +3960 -0
  166. package/stdlib/modules/test/more.zzm +528 -0
  167. package/stdlib/modules/test/parser.zzm +209 -0
@@ -0,0 +1,475 @@
1
+ 'use strict';
2
+
3
+ const fs = require( 'node:fs' );
4
+ const os = require( 'node:os' );
5
+ const path = require( 'node:path' );
6
+ const { BinaryString } = require( '../../lib/runtime-helpers' );
7
+ const { Task, traceBlockingOperation } = require( './task' );
8
+
9
+ let runtimePolicy = {};
10
+
11
+ function typeName( value ) {
12
+ if ( value == null ) {
13
+ return 'Null';
14
+ }
15
+ if ( typeof value === 'string' ) {
16
+ return 'String';
17
+ }
18
+ if ( value.bytes instanceof Uint8Array ) {
19
+ return 'BinaryString';
20
+ }
21
+ return value.constructor && value.constructor.name ? value.constructor.name : typeof value;
22
+ }
23
+
24
+ function bool( value ) {
25
+ return value ? 1 : 0;
26
+ }
27
+
28
+ function formatSizeHuman( size ) {
29
+ const units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
30
+ let value = Number( size );
31
+ let unit = 0;
32
+ while ( value >= 1024 && unit < units.length - 1 ) {
33
+ value /= 1024;
34
+ unit++;
35
+ }
36
+ if ( unit === 0 ) {
37
+ return `${value} ${units[unit]}`;
38
+ }
39
+ return `${value.toFixed( 1 )} ${units[unit]}`;
40
+ }
41
+
42
+ function binaryLinesFromBuffer( buffer ) {
43
+ const out = [];
44
+ let start = 0;
45
+ for ( let i = 0; i < buffer.length; i++ ) {
46
+ if ( buffer[i] === 0x0a ) {
47
+ out.push( new BinaryString( Uint8Array.from( buffer.slice( start, i + 1 ) ) ) );
48
+ start = i + 1;
49
+ }
50
+ }
51
+ if ( start < buffer.length ) {
52
+ out.push( new BinaryString( Uint8Array.from( buffer.slice( start ) ) ) );
53
+ }
54
+ return out;
55
+ }
56
+
57
+ function textLinesFromString( text ) {
58
+ const out = [];
59
+ let start = 0;
60
+ for ( let i = 0; i < text.length; i++ ) {
61
+ if ( text[i] === '\n' ) {
62
+ out.push( text.slice( start, i + 1 ) );
63
+ start = i + 1;
64
+ }
65
+ }
66
+ if ( start < text.length ) {
67
+ out.push( text.slice( start ) );
68
+ }
69
+ return out;
70
+ }
71
+
72
+ function renderValue( value ) {
73
+ if ( value && value.bytes instanceof Uint8Array ) {
74
+ return value.to_String();
75
+ }
76
+ if ( typeof runtimePolicy.to_String === 'function' ) {
77
+ return runtimePolicy.to_String( value );
78
+ }
79
+ return String( value ?? '' );
80
+ }
81
+
82
+ function writeStdout( text ) {
83
+ if ( typeof runtimePolicy.stdout_write === 'function' ) {
84
+ runtimePolicy.stdout_write( text );
85
+ return;
86
+ }
87
+ process.stdout.write( text );
88
+ }
89
+
90
+ function writeStderr( text ) {
91
+ if ( typeof runtimePolicy.stderr_write === 'function' ) {
92
+ runtimePolicy.stderr_write( text );
93
+ return;
94
+ }
95
+ process.stderr.write( text );
96
+ }
97
+
98
+ function streamPayload( values, newline ) {
99
+ const text = values.map( renderValue ).join( '' );
100
+ return newline ? `${text}\n` : text;
101
+ }
102
+
103
+ class StandardOutputStream {
104
+ print( ...values ) {
105
+ writeStdout( streamPayload( values, false ) );
106
+ return null;
107
+ }
108
+
109
+ say( ...values ) {
110
+ writeStdout( streamPayload( values, true ) );
111
+ return null;
112
+ }
113
+ }
114
+
115
+ class StandardErrorStream {
116
+ print( ...values ) {
117
+ writeStderr( streamPayload( values, false ) );
118
+ return null;
119
+ }
120
+
121
+ say( ...values ) {
122
+ writeStderr( streamPayload( values, true ) );
123
+ return null;
124
+ }
125
+ }
126
+
127
+ class StandardInputStream {
128
+ constructor() {
129
+ this._buffer = null;
130
+ this._linePos = 0;
131
+ this._lineMode = 'text';
132
+ }
133
+
134
+ _bufferBytes() {
135
+ if ( this._buffer == null ) {
136
+ this._buffer = fs.readFileSync( 0 );
137
+ }
138
+ return this._buffer;
139
+ }
140
+
141
+ _lines( raw ) {
142
+ const buffer = this._bufferBytes();
143
+ return raw
144
+ ? binaryLinesFromBuffer( buffer )
145
+ : textLinesFromString( buffer.toString( 'utf8' ) );
146
+ }
147
+
148
+ next_line( raw = false ) {
149
+ const mode = raw ? 'raw' : 'text';
150
+ if ( this._lineMode !== mode ) {
151
+ this._lineMode = mode;
152
+ this._linePos = 0;
153
+ }
154
+ const lines = this._lines( raw );
155
+ if ( this._linePos >= lines.length ) {
156
+ return null;
157
+ }
158
+ return lines[this._linePos++];
159
+ }
160
+
161
+ each_line( fn, raw = false ) {
162
+ for ( const line of this._lines( raw ).slice( this._linePos ) ) {
163
+ fn( line );
164
+ this._linePos++;
165
+ }
166
+ return this;
167
+ }
168
+ }
169
+
170
+ function statToDict( stats ) {
171
+ if ( !stats ) {
172
+ return null;
173
+ }
174
+ const out = {
175
+ dev: stats.dev,
176
+ ino: stats.ino,
177
+ mode: stats.mode,
178
+ nlink: stats.nlink,
179
+ uid: stats.uid,
180
+ gid: stats.gid,
181
+ rdev: stats.rdev,
182
+ size: stats.size,
183
+ atime: Math.floor( stats.atimeMs / 1000 ),
184
+ mtime: Math.floor( stats.mtimeMs / 1000 ),
185
+ ctime: Math.floor( stats.ctimeMs / 1000 ),
186
+ blksize: stats.blksize ?? 0,
187
+ blocks: stats.blocks ?? 0,
188
+ };
189
+ Object.defineProperty( out, 'has', {
190
+ value( key ) {
191
+ return Object.prototype.hasOwnProperty.call( out, String( key ) ) ? 1 : 0;
192
+ },
193
+ enumerable: false,
194
+ } );
195
+ Object.defineProperty( out, 'get', {
196
+ value( key, fallback = null ) {
197
+ return Object.prototype.hasOwnProperty.call( out, String( key ) )
198
+ ? out[String( key )]
199
+ : fallback;
200
+ },
201
+ enumerable: false,
202
+ } );
203
+ return out;
204
+ }
205
+
206
+ class Path {
207
+ constructor( rawPath ) {
208
+ const pathValue = (
209
+ rawPath
210
+ && typeof rawPath === 'object'
211
+ && !Array.isArray( rawPath )
212
+ && (
213
+ Object.prototype.hasOwnProperty.call( rawPath, 'path' )
214
+ || (
215
+ typeof rawPath.get === 'function'
216
+ && typeof rawPath.has === 'function'
217
+ && rawPath.has( 'path' )
218
+ )
219
+ )
220
+ )
221
+ ? (
222
+ typeof rawPath.get === 'function'
223
+ ? rawPath.get( 'path' )
224
+ : rawPath.path
225
+ )
226
+ : rawPath;
227
+ this.value = String( pathValue ?? '' );
228
+ this._linePos = 0;
229
+ this._lineMode = 'text';
230
+ }
231
+
232
+ to_String() { return this.value; }
233
+ parent() {
234
+ const dir = path.dirname( this.value );
235
+ return new Path( dir === '' ? '.' : dir );
236
+ }
237
+ sibling( name ) {
238
+ return this.parent().child( name );
239
+ }
240
+ absolute() {
241
+ return new Path( path.resolve( this.value ) );
242
+ }
243
+ realpath() {
244
+ try {
245
+ return new Path( fs.realpathSync( this.value ) );
246
+ }
247
+ catch ( _err ) {
248
+ return null;
249
+ }
250
+ }
251
+ is_absolute() { return path.isAbsolute( this.value ) ? 1 : 0; }
252
+ basename() { return path.basename( this.value ); }
253
+ exists() { return bool( fs.existsSync( this.value ) ); }
254
+ is_file() {
255
+ try {
256
+ return bool( fs.statSync( this.value ).isFile() );
257
+ }
258
+ catch ( _err ) {
259
+ return 0;
260
+ }
261
+ }
262
+ is_dir() {
263
+ try {
264
+ return bool( fs.statSync( this.value ).isDirectory() );
265
+ }
266
+ catch ( _err ) {
267
+ return 0;
268
+ }
269
+ }
270
+ size() { return fs.statSync( this.value ).size; }
271
+ size_human() { return formatSizeHuman( this.size() ); }
272
+ stat() { return statToDict( fs.statSync( this.value ) ); }
273
+ lstat() { return statToDict( fs.lstatSync( this.value ) ); }
274
+ child( name ) { return new Path( path.join( this.value, String( name ) ) ); }
275
+ children() {
276
+ return fs.readdirSync( this.value ).map( (name) => new Path( path.join( this.value, name ) ) );
277
+ }
278
+ iterator() {
279
+ const kids = this.children();
280
+ let i = 0;
281
+ return {
282
+ next() {
283
+ if ( i >= kids.length ) {
284
+ return null;
285
+ }
286
+ return kids[i++];
287
+ },
288
+ };
289
+ }
290
+ chmod( mode ) {
291
+ fs.chmodSync( this.value, Number( mode ) );
292
+ return Number( mode );
293
+ }
294
+ copy( target ) {
295
+ const dest = target instanceof Path ? target : new Path( target );
296
+ fs.copyFileSync( this.value, dest.value );
297
+ return dest;
298
+ }
299
+ move( target ) {
300
+ const dest = target instanceof Path ? target : new Path( target );
301
+ fs.renameSync( this.value, dest.value );
302
+ return dest;
303
+ }
304
+ remove() {
305
+ try {
306
+ fs.rmSync( this.value, { force: true } );
307
+ return 1;
308
+ }
309
+ catch ( _err ) {
310
+ return 0;
311
+ }
312
+ }
313
+ remove_tree() {
314
+ try {
315
+ fs.rmSync( this.value, { recursive: true, force: true } );
316
+ return 1;
317
+ }
318
+ catch ( _err ) {
319
+ return 0;
320
+ }
321
+ }
322
+ touchpath() {
323
+ fs.mkdirSync( path.dirname( this.value ), { recursive: true } );
324
+ if ( !fs.existsSync( this.value ) ) {
325
+ fs.writeFileSync( this.value, '' );
326
+ }
327
+ return this;
328
+ }
329
+ mkdir() {
330
+ fs.mkdirSync( this.value, { recursive: true } );
331
+ return 1;
332
+ }
333
+ mkdir_exclusive() {
334
+ try {
335
+ fs.mkdirSync( this.value );
336
+ return 1;
337
+ }
338
+ catch ( err ) {
339
+ if ( err && err.code === 'EEXIST' ) {
340
+ return 0;
341
+ }
342
+ throw err;
343
+ }
344
+ }
345
+ spew( value ) {
346
+ traceBlockingOperation( 'std/io Path.spew' );
347
+ if ( !( value && value.bytes instanceof Uint8Array ) ) {
348
+ throw new Error( `TypeException: Path.spew expects BinaryString, got ${typeName( value )}` );
349
+ }
350
+ fs.writeFileSync( this.value, Buffer.from( value.bytes ) );
351
+ return this;
352
+ }
353
+ spew_async( value ) {
354
+ return new Task( async () => {
355
+ this.spew( value );
356
+ return this;
357
+ } );
358
+ }
359
+ slurp() {
360
+ traceBlockingOperation( 'std/io Path.slurp' );
361
+ return new BinaryString( Uint8Array.from( fs.readFileSync( this.value ) ) );
362
+ }
363
+ slurp_async() {
364
+ return new Task( async () => (
365
+ new BinaryString( Uint8Array.from( await fs.promises.readFile( this.value ) ) )
366
+ ) );
367
+ }
368
+ spew_utf8( text ) {
369
+ traceBlockingOperation( 'std/io Path.spew_utf8' );
370
+ if ( typeof text !== 'string' ) {
371
+ throw new Error( `TypeException: Path.spew_utf8 expects String, got ${typeName( text )}` );
372
+ }
373
+ fs.writeFileSync( this.value, text, 'utf8' );
374
+ return this;
375
+ }
376
+ spew_utf8_async( text ) {
377
+ return new Task( async () => {
378
+ if ( typeof text !== 'string' ) {
379
+ throw new Error( `TypeException: Path.spew_utf8 expects String, got ${typeName( text )}` );
380
+ }
381
+ await fs.promises.writeFile( this.value, text, 'utf8' );
382
+ return this;
383
+ } );
384
+ }
385
+ slurp_utf8() {
386
+ traceBlockingOperation( 'std/io Path.slurp_utf8' );
387
+ return fs.readFileSync( this.value, 'utf8' );
388
+ }
389
+ slurp_utf8_async() {
390
+ return new Task( async () => fs.promises.readFile( this.value, 'utf8' ) );
391
+ }
392
+ append_async( value ) {
393
+ return new Task( async () => {
394
+ if ( !( value && value.bytes instanceof Uint8Array ) ) {
395
+ throw new Error( `TypeException: Path.append expects BinaryString, got ${typeName( value )}` );
396
+ }
397
+ await fs.promises.appendFile( this.value, Buffer.from( value.bytes ) );
398
+ return this;
399
+ } );
400
+ }
401
+ append_utf8_async( text ) {
402
+ return new Task( async () => {
403
+ if ( typeof text !== 'string' ) {
404
+ throw new Error( `TypeException: Path.append_utf8 expects String, got ${typeName( text )}` );
405
+ }
406
+ await fs.promises.appendFile( this.value, text, 'utf8' );
407
+ return this;
408
+ } );
409
+ }
410
+ lines_async() {
411
+ return new Task( async () => binaryLinesFromBuffer( await fs.promises.readFile( this.value ) ) );
412
+ }
413
+ lines_utf8_async() {
414
+ return new Task( async () => textLinesFromString( await fs.promises.readFile( this.value, 'utf8' ) ) );
415
+ }
416
+ each_line( fn, raw = false ) {
417
+ traceBlockingOperation( 'std/io Path.each_line' );
418
+ const lines = raw ? binaryLinesFromBuffer( fs.readFileSync( this.value ) ) : textLinesFromString( this.slurp_utf8() );
419
+ for ( const line of lines ) {
420
+ fn( line );
421
+ }
422
+ return this;
423
+ }
424
+ next_line( raw = false ) {
425
+ traceBlockingOperation( 'std/io Path.next_line' );
426
+ const mode = raw ? 'raw' : 'text';
427
+ if ( this._lineMode !== mode ) {
428
+ this._lineMode = mode;
429
+ this._linePos = 0;
430
+ }
431
+ const lines = raw ? binaryLinesFromBuffer( fs.readFileSync( this.value ) ) : textLinesFromString( this.slurp_utf8() );
432
+ if ( this._linePos >= lines.length ) {
433
+ return null;
434
+ }
435
+ return lines[this._linePos++];
436
+ }
437
+
438
+ static cwd() { return new Path( process.cwd() ); }
439
+ static tempfile() {
440
+ const dir = fs.mkdtempSync( path.join( os.tmpdir(), 'zuzu-js-' ) );
441
+ return new Path( path.join( dir, 'tmp.txt' ) );
442
+ }
443
+ static tempdir() {
444
+ return new Path( fs.mkdtempSync( path.join( os.tmpdir(), 'zuzu-js-dir-' ) ) );
445
+ }
446
+ static join( parts ) { return new Path( path.join( ...parts.map( (p) => String( p ) ) ) ); }
447
+ static split( source ) { return path.normalize( String( source ) ).split( path.sep ).filter( Boolean ); }
448
+ static normalize( source ) { return new Path( path.normalize( String( source ) ) ); }
449
+ static glob( pattern ) {
450
+ const text = String( pattern ?? '' );
451
+ const star = text.indexOf( '*' );
452
+ if ( star < 0 ) {
453
+ return fs.existsSync( text ) ? [ new Path( text ) ] : [];
454
+ }
455
+ const dir = path.dirname( text );
456
+ const ext = text.slice( star + 1 );
457
+ return fs.readdirSync( dir )
458
+ .filter( (name) => ext === '' || name.endsWith( ext.replace( /^\./u, '.' ) ) )
459
+ .map( (name) => new Path( path.join( dir, name ) ) );
460
+ }
461
+ }
462
+
463
+ module.exports = {
464
+ Path,
465
+ STDIN: new StandardInputStream(),
466
+ STDOUT: new StandardOutputStream(),
467
+ STDERR: new StandardErrorStream(),
468
+ };
469
+
470
+ Object.defineProperty( module.exports, '__zuzu_set_runtime_policy', {
471
+ value( policy = {} ) {
472
+ runtimePolicy = policy || {};
473
+ },
474
+ enumerable: false,
475
+ } );