@bytecodealliance/preview2-shim 0.0.4 → 0.0.6

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 (45) hide show
  1. package/README.md +10 -20
  2. package/lib/browser/environment-preopens.js +4 -0
  3. package/lib/browser/filesystem.js +121 -22
  4. package/lib/browser/http.js +12 -7
  5. package/lib/browser/index.js +5 -7
  6. package/lib/browser/instance-monotonic-clock.js +4 -0
  7. package/lib/browser/instance-wall-clock.js +4 -0
  8. package/lib/browser/io.js +10 -23
  9. package/lib/browser/monotonic-clock.js +2 -2
  10. package/lib/browser/preopens.js +11 -0
  11. package/lib/browser/random.js +14 -1
  12. package/lib/browser/stderr.js +1 -0
  13. package/lib/browser/streams.js +17 -2
  14. package/lib/browser/timezone.js +12 -0
  15. package/lib/browser/wall-clock.js +2 -2
  16. package/lib/http/make-request.js +2 -2
  17. package/lib/http/wasi-http.js +335 -0
  18. package/lib/nodejs/environment-preopens.js +4 -0
  19. package/lib/nodejs/filesystem.js +109 -23
  20. package/lib/nodejs/index.js +6 -8
  21. package/lib/nodejs/instance-monotonic-clock.js +4 -0
  22. package/lib/nodejs/instance-wall-clock.js +4 -0
  23. package/lib/nodejs/io.js +16 -24
  24. package/lib/nodejs/monotonic-clock.js +6 -10
  25. package/lib/nodejs/preopens.js +11 -0
  26. package/lib/nodejs/random.js +14 -1
  27. package/lib/nodejs/stderr.js +1 -0
  28. package/lib/nodejs/streams.js +22 -4
  29. package/lib/nodejs/timezone.js +12 -0
  30. package/lib/nodejs/wall-clock.js +2 -6
  31. package/package.json +1 -1
  32. package/types/imports/environment.d.ts +1 -0
  33. package/types/imports/monotonic-clock.d.ts +3 -5
  34. package/types/imports/preopens.d.ts +15 -0
  35. package/types/imports/wall-clock.d.ts +2 -4
  36. package/types/wasi-reactor.d.ts +8 -8
  37. package/lib/browser/clocks.js +0 -47
  38. package/lib/browser/default-clocks.js +0 -7
  39. package/lib/nodejs/default-clocks.js +0 -7
  40. package/types/imports/environment-preopens.d.ts +0 -5
  41. package/types/imports/instance-monotonic-clock.d.ts +0 -5
  42. package/types/imports/instance-wall-clock.d.ts +0 -5
  43. package/types/imports/stderr.d.ts +0 -3
  44. /package/lib/browser/{console.js → logging.js} +0 -0
  45. /package/lib/nodejs/{console.js → logging.js} +0 -0
@@ -1,5 +1,10 @@
1
- export function read(s, _len) {
2
- console.log(`[streams] Read ${s}`);
1
+ export function read(s, len) {
2
+ switch (s) {
3
+ case 0:
4
+ return [process.stdin.read(len), true];
5
+ default:
6
+ throw new Error(`TODO: write ${s}`);
7
+ }
3
8
  }
4
9
  export function blockingRead(s, _len) {
5
10
  console.log(`[streams] Blocking read ${s}`);
@@ -16,8 +21,21 @@ export function subscribeToInputStream(s) {
16
21
  export function dropInputStream(s) {
17
22
  console.log(`[streams] Drop input stream ${s}`);
18
23
  }
19
- export function write(s, _buf) {
20
- console.log(`[streams] Write ${s}`);
24
+ export function write(s, buf) {
25
+ switch (s) {
26
+ case 0:
27
+ throw new Error(`TODO: write stdin`);
28
+ case 1: {
29
+ process.stdout.write(buf);
30
+ return BigInt(buf.byteLength);
31
+ }
32
+ case 2: {
33
+ process.stderr.write(buf);
34
+ return BigInt(buf.byteLength);
35
+ }
36
+ default:
37
+ throw new Error(`TODO: write ${s}`);
38
+ }
21
39
  }
22
40
  export function blockingWrite(s, _buf) {
23
41
  console.log(`[streams] Blocking write ${s}`);
@@ -0,0 +1,12 @@
1
+ export function display (timezone, when) {
2
+ console.log(`[timezone] DISPLAY ${timezone} ${when}`);
3
+ }
4
+
5
+ export function utcOffset (timezone, when) {
6
+ console.log(`[timezone] UTC OFFSET ${timezone} ${when}`);
7
+ return 0;
8
+ }
9
+
10
+ export function dropTimezone (timezone) {
11
+ console.log(`[timezone] DROP ${timezone}`);
12
+ }
@@ -4,13 +4,9 @@ export function now(clock) {
4
4
  const nanoseconds = (Date.now() % 1000) * 1000 * 1000;
5
5
  return { seconds, nanoseconds };
6
6
  }
7
- console.log("[clocks] UNKNOWN CLOCK");
7
+ console.log(`[wall-clock] now() UNKNOWN CLOCK ${clock}`);
8
8
  }
9
9
 
10
10
  export function resolution(clock) {
11
- console.log(`[clocks] Wall clock resolution ${clock}`);
12
- }
13
-
14
- export function instanceWallClock () {
15
- return 1;
11
+ console.log(`[wall-clock] Wall clock resolution ${clock}`);
16
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "WASI Preview2 shim for JS environments",
5
5
  "author": "Guy Bedford, Eduardo Rodrigues<16357187+eduardomourar@users.noreply.github.com>",
6
6
  "type": "module",
@@ -1,3 +1,4 @@
1
1
  export namespace Environment {
2
2
  export function getEnvironment(): [string, string][];
3
+ export function getArguments(): string[];
3
4
  }
@@ -1,10 +1,8 @@
1
1
  export namespace MonotonicClock {
2
- export function now(this: MonotonicClock): Instant;
3
- export function resolution(this: MonotonicClock): Instant;
4
- export function subscribe(this: MonotonicClock, when: Instant, absolute: boolean): Pollable;
5
- export function dropMonotonicClock(this: MonotonicClock): void;
2
+ export function now(): Instant;
3
+ export function resolution(): Instant;
4
+ export function subscribe(when: Instant, absolute: boolean): Pollable;
6
5
  }
7
- export type MonotonicClock = number;
8
6
  export type Instant = bigint;
9
7
  import type { Pollable } from '../imports/poll';
10
8
  export { Pollable };
@@ -0,0 +1,15 @@
1
+ export namespace Preopens {
2
+ export function getStdio(): StdioPreopens;
3
+ export function getDirectories(): [Descriptor, string][];
4
+ }
5
+ import type { InputStream } from '../imports/streams';
6
+ export { InputStream };
7
+ import type { OutputStream } from '../imports/streams';
8
+ export { OutputStream };
9
+ export interface StdioPreopens {
10
+ stdin: InputStream,
11
+ stdout: OutputStream,
12
+ stderr: OutputStream,
13
+ }
14
+ import type { Descriptor } from '../imports/filesystem';
15
+ export { Descriptor };
@@ -1,9 +1,7 @@
1
1
  export namespace WallClock {
2
- export function now(this: WallClock): Datetime;
3
- export function resolution(this: WallClock): Datetime;
4
- export function dropWallClock(this: WallClock): void;
2
+ export function now(): Datetime;
3
+ export function resolution(): Datetime;
5
4
  }
6
- export type WallClock = number;
7
5
  export interface Datetime {
8
6
  seconds: bigint,
9
7
  nanoseconds: number,
@@ -1,8 +1,6 @@
1
1
  import { WallClock as WallClockImports } from './imports/wall-clock';
2
2
  import { Poll as PollImports } from './imports/poll';
3
3
  import { MonotonicClock as MonotonicClockImports } from './imports/monotonic-clock';
4
- import { InstanceWallClock as InstanceWallClockImports } from './imports/instance-wall-clock';
5
- import { InstanceMonotonicClock as InstanceMonotonicClockImports } from './imports/instance-monotonic-clock';
6
4
  import { Timezone as TimezoneImports } from './imports/timezone';
7
5
  import { Streams as StreamsImports } from './imports/streams';
8
6
  import { Filesystem as FilesystemImports } from './imports/filesystem';
@@ -14,16 +12,16 @@ import { TcpCreateSocket as TcpCreateSocketImports } from './imports/tcp-create-
14
12
  import { Udp as UdpImports } from './imports/udp';
15
13
  import { UdpCreateSocket as UdpCreateSocketImports } from './imports/udp-create-socket';
16
14
  import { Random as RandomImports } from './imports/random';
15
+ import { Console as ConsoleImports } from './imports/console';
16
+ import { Types as TypesImports } from './imports/types';
17
+ import { DefaultOutgoingHttp as DefaultOutgoingHttpImports } from './imports/default-outgoing-HTTP';
17
18
  import { Environment as EnvironmentImports } from './imports/environment';
18
- import { EnvironmentPreopens as EnvironmentPreopensImports } from './imports/environment-preopens';
19
+ import { Preopens as PreopensImports } from './imports/preopens';
19
20
  import { Exit as ExitImports } from './imports/exit';
20
- import { Stderr as StderrImports } from './imports/stderr';
21
21
  export interface ImportObject {
22
22
  'wall-clock': typeof WallClockImports,
23
23
  'poll': typeof PollImports,
24
24
  'monotonic-clock': typeof MonotonicClockImports,
25
- 'instance-wall-clock': typeof InstanceWallClockImports,
26
- 'instance-monotonic-clock': typeof InstanceMonotonicClockImports,
27
25
  'timezone': typeof TimezoneImports,
28
26
  'streams': typeof StreamsImports,
29
27
  'filesystem': typeof FilesystemImports,
@@ -35,10 +33,12 @@ export interface ImportObject {
35
33
  'udp': typeof UdpImports,
36
34
  'udp-create-socket': typeof UdpCreateSocketImports,
37
35
  'random': typeof RandomImports,
36
+ 'console': typeof ConsoleImports,
37
+ 'types': typeof TypesImports,
38
+ 'default-outgoing-HTTP': typeof DefaultOutgoingHttpImports,
38
39
  'environment': typeof EnvironmentImports,
39
- 'environment-preopens': typeof EnvironmentPreopensImports,
40
+ 'preopens': typeof PreopensImports,
40
41
  'exit': typeof ExitImports,
41
- 'stderr': typeof StderrImports,
42
42
  }
43
43
  export interface WasiReactor {
44
44
  }
@@ -1,47 +0,0 @@
1
- export function wallClockNow(clock) {
2
- if (clock === 1) {
3
- const seconds = BigInt(Math.floor(Date.now() / 1000));
4
- const nanoseconds = (Date.now() % 1000) * 1000 * 1000;
5
- return { seconds, nanoseconds };
6
- }
7
- console.log("[clocks] UNKNOWN CLOCK");
8
- }
9
-
10
- export function monotonicClockResolution(clock) {
11
- console.log(`[clocks] Monotonic clock resolution ${clock}`);
12
- }
13
-
14
- export function wallClockResolution(clock) {
15
- console.log(`[clocks] Wall clock resolution ${clock}`);
16
- }
17
-
18
- let hrStart = hrtimeBigint();
19
-
20
- export function monotonicClockNow(clock) {
21
- if (clock === 0) {
22
- return hrtimeBigint() - hrStart;
23
- }
24
- console.log("UNKNOWN CLOCK");
25
- }
26
-
27
- function hrtime(previousTimestamp) {
28
- const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
29
- const clocktime = performance.now() * 1e-3;
30
- let seconds = Math.floor(clocktime) + baseNow;
31
- let nanoseconds = Math.floor((clocktime % 1) * 1e9);
32
-
33
- if (previousTimestamp) {
34
- seconds = seconds - previousTimestamp[0];
35
- nanoseconds = nanoseconds - previousTimestamp[1];
36
- if (nanoseconds < 0) {
37
- seconds--;
38
- nanoseconds += 1e9;
39
- }
40
- }
41
- return [seconds, nanoseconds];
42
- }
43
-
44
- function hrtimeBigint(time) {
45
- const diff = hrtime(time);
46
- return BigInt(diff[0] * 1e9 + diff[1]);
47
- }
@@ -1,7 +0,0 @@
1
- export function defaultMonotonicClock() {
2
- return 0;
3
- }
4
-
5
- export function defaultWallClock() {
6
- return 1;
7
- }
@@ -1,7 +0,0 @@
1
- export function defaultMonotonicClock() {
2
- return 0;
3
- }
4
-
5
- export function defaultWallClock() {
6
- return 1;
7
- }
@@ -1,5 +0,0 @@
1
- export namespace EnvironmentPreopens {
2
- export function preopens(): [Descriptor, string][];
3
- }
4
- import type { Descriptor } from '../imports/filesystem';
5
- export { Descriptor };
@@ -1,5 +0,0 @@
1
- export namespace InstanceMonotonicClock {
2
- export function instanceMonotonicClock(): MonotonicClock;
3
- }
4
- import type { MonotonicClock } from '../imports/monotonic-clock';
5
- export { MonotonicClock };
@@ -1,5 +0,0 @@
1
- export namespace InstanceWallClock {
2
- export function instanceWallClock(): WallClock;
3
- }
4
- import type { WallClock } from '../imports/wall-clock';
5
- export { WallClock };
@@ -1,3 +0,0 @@
1
- export namespace Stderr {
2
- export function print(message: string): void;
3
- }
File without changes
File without changes