@bytecodealliance/preview2-shim 0.0.10 → 0.0.12

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.
@@ -1,5 +1,6 @@
1
1
  function _hrtimeBigint () {
2
- return BigInt(Math.floor(performance.now() * 1e9));
2
+ // performance.now() is in milliseconds, but we want nanoseconds
3
+ return BigInt(Math.floor(performance.now() * 1e6));
3
4
  }
4
5
 
5
6
  let _hrStart = _hrtimeBigint();
@@ -33,8 +34,9 @@ export const timezone = {
33
34
 
34
35
  export const wallClock = {
35
36
  now() {
36
- const seconds = BigInt(Math.floor(Date.now() / 1e3));
37
- const nanoseconds = (Date.now() % 1e3) * 1e6;
37
+ let now = Date.now(); // in milliseconds
38
+ const seconds = BigInt(Math.floor(now / 1e3));
39
+ const nanoseconds = (now % 1e3) * 1e6;
38
40
  return { seconds, nanoseconds };
39
41
  },
40
42
 
package/lib/browser/io.js CHANGED
@@ -18,26 +18,24 @@ export const streams = {
18
18
  console.log(`[streams] Drop input stream ${s}`);
19
19
  },
20
20
  write(s, buf) {
21
+ streams.blockingWrite(s, buf);
22
+ },
23
+ blockingWrite(s, buf) {
21
24
  switch (s) {
22
25
  case 0:
23
26
  throw new Error(`TODO: write stdin`);
24
27
  case 1: {
25
- const decoder = new TextDecoder();
26
- console.log(decoder.decode(buf));
27
- return BigInt(buf.byteLength);
28
+ process.stdout.write(buf);
29
+ return [BigInt(buf.byteLength), 'ended'];
28
30
  }
29
31
  case 2: {
30
- const decoder = new TextDecoder();
31
- console.error(decoder.decode(buf));
32
- return BigInt(buf.byteLength);
32
+ process.stderr.write(buf);
33
+ return [BigInt(buf.byteLength), 'ended'];
33
34
  }
34
35
  default:
35
36
  throw new Error(`TODO: write ${s}`);
36
37
  }
37
38
  },
38
- blockingWrite(s, _buf) {
39
- console.log(`[streams] Blocking write ${s}`);
40
- },
41
39
  writeZeroes(s, _len) {
42
40
  console.log(`[streams] Write zeroes ${s}`);
43
41
  },
package/lib/nodejs/io.js CHANGED
@@ -71,33 +71,28 @@ export function _dropFsStream(stream) {
71
71
 
72
72
  export const streams = {
73
73
  read(s, len) {
74
- switch (s) {
75
- case 0:
76
- return [process.stdin.read(len), true];
77
- default:
78
- throw new Error(`TODO: write ${s}`);
79
- }
74
+ return streams.blockingRead(s, len);
80
75
  },
81
76
  blockingRead(s, len) {
82
77
  len = Number(len);
83
78
  const stream = _streams[s];
84
- if (!stream) throw null;
85
- switch (stream.type) {
79
+ switch (stream?.type) {
86
80
  case 'file': {
87
81
  const buf = Buffer.alloc(Number(len));
88
82
  try {
89
83
  const readBytes = fsReadSync(stream.fd, buf, 0, Number(len));
90
- if (readBytes < Number(len))
91
- return [new Uint8Array(), true];
92
- return [new Uint8Array(buf.buffer, 0, readBytes), false];
84
+ if (readBytes < Number(len)) {
85
+ return [new Uint8Array(buf.buffer, 0, readBytes), 'ended'];
86
+ }
87
+ return [new Uint8Array(buf.buffer, 0, readBytes), 'open'];
93
88
  }
94
89
  catch (e) {
95
90
  _convertFsError(e);
96
91
  }
97
92
  break;
98
93
  }
99
- default: throw null;
100
94
  }
95
+ throw null;
101
96
  },
102
97
  skip(s, _len) {
103
98
  console.log(`[streams] Skip ${s}`);
@@ -112,24 +107,24 @@ export const streams = {
112
107
  delete _streams[s];
113
108
  },
114
109
  write(s, buf) {
110
+ return streams.blockingWrite(s, buf);
111
+ },
112
+ blockingWrite(s, buf) {
115
113
  switch (s) {
116
114
  case 0:
117
115
  throw new Error(`TODO: write stdin`);
118
116
  case 1: {
119
117
  process.stdout.write(buf);
120
- return BigInt(buf.byteLength);
118
+ return [BigInt(buf.byteLength), 'ended'];
121
119
  }
122
120
  case 2: {
123
121
  process.stderr.write(buf);
124
- return BigInt(buf.byteLength);
122
+ return [BigInt(buf.byteLength), 'ended'];
125
123
  }
126
124
  default:
127
125
  throw new Error(`TODO: write ${s}`);
128
126
  }
129
127
  },
130
- blockingWrite(s, _buf) {
131
- console.log(`[streams] Blocking write ${s}`);
132
- },
133
128
  writeZeroes(s, _len) {
134
129
  console.log(`[streams] Write zeroes ${s}`);
135
130
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytecodealliance/preview2-shim",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
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",