@bytecodealliance/preview2-shim 0.17.2 → 0.17.3

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.
@@ -3,284 +3,331 @@ import { environment } from './cli.js';
3
3
 
4
4
  const { InputStream, OutputStream } = streams;
5
5
 
6
- let _cwd = "/";
6
+ let _cwd = '/';
7
7
 
8
- export function _setCwd (cwd) {
9
- _cwd = cwd;
8
+ export function _setCwd(cwd) {
9
+ _cwd = cwd;
10
10
  }
11
11
 
12
- export function _setFileData (fileData) {
13
- _fileData = fileData;
14
- _rootPreopen[0] = new Descriptor(fileData);
15
- const cwd = environment.initialCwd();
16
- _setCwd(cwd || '/');
12
+ export function _setFileData(fileData) {
13
+ _fileData = fileData;
14
+ _rootPreopen[0] = new Descriptor(fileData);
15
+ const cwd = environment.initialCwd();
16
+ _setCwd(cwd || '/');
17
17
  }
18
18
 
19
- export function _getFileData () {
20
- return JSON.stringify(_fileData);
19
+ export function _getFileData() {
20
+ return JSON.stringify(_fileData);
21
21
  }
22
22
 
23
23
  let _fileData = { dir: {} };
24
24
 
25
25
  const timeZero = {
26
- seconds: BigInt(0),
27
- nanoseconds: 0
26
+ seconds: BigInt(0),
27
+ nanoseconds: 0,
28
28
  };
29
29
 
30
- function getChildEntry (parentEntry, subpath, openFlags) {
31
- if (subpath === '.' && _rootPreopen && descriptorGetEntry(_rootPreopen[0]) === parentEntry) {
32
- subpath = _cwd;
33
- if (subpath.startsWith('/') && subpath !== '/')
34
- subpath = subpath.slice(1);
35
- }
36
- let entry = parentEntry;
37
- let segmentIdx;
38
- do {
39
- if (!entry || !entry.dir) throw 'not-directory';
40
- segmentIdx = subpath.indexOf('/');
41
- const segment = segmentIdx === -1 ? subpath : subpath.slice(0, segmentIdx);
42
- if (segment === '..') throw 'no-entry';
43
- if (segment === '.' || segment === '');
44
- else if (!entry.dir[segment] && openFlags.create)
45
- entry = entry.dir[segment] = openFlags.directory ? { dir: {} } : { source: new Uint8Array([]) };
46
- else
47
- entry = entry.dir[segment];
48
- subpath = subpath.slice(segmentIdx + 1);
49
- } while (segmentIdx !== -1)
50
- if (!entry) throw 'no-entry';
51
- return entry;
30
+ function getChildEntry(parentEntry, subpath, openFlags) {
31
+ if (
32
+ subpath === '.' &&
33
+ _rootPreopen &&
34
+ descriptorGetEntry(_rootPreopen[0]) === parentEntry
35
+ ) {
36
+ subpath = _cwd;
37
+ if (subpath.startsWith('/') && subpath !== '/') {
38
+ subpath = subpath.slice(1);
39
+ }
40
+ }
41
+ let entry = parentEntry;
42
+ let segmentIdx;
43
+ do {
44
+ if (!entry || !entry.dir) {
45
+ throw 'not-directory';
46
+ }
47
+ segmentIdx = subpath.indexOf('/');
48
+ const segment =
49
+ segmentIdx === -1 ? subpath : subpath.slice(0, segmentIdx);
50
+ if (segment === '..') {
51
+ throw 'no-entry';
52
+ }
53
+ if (segment === '.' || segment === '') {
54
+ } else if (!entry.dir[segment] && openFlags.create) {
55
+ entry = entry.dir[segment] = openFlags.directory
56
+ ? { dir: {} }
57
+ : { source: new Uint8Array([]) };
58
+ } else {
59
+ entry = entry.dir[segment];
60
+ }
61
+ subpath = subpath.slice(segmentIdx + 1);
62
+ } while (segmentIdx !== -1);
63
+ if (!entry) {
64
+ throw 'no-entry';
65
+ }
66
+ return entry;
52
67
  }
53
68
 
54
- function getSource (fileEntry) {
55
- if (typeof fileEntry.source === 'string') {
56
- fileEntry.source = new TextEncoder().encode(fileEntry.source);
57
- }
58
- return fileEntry.source;
69
+ function getSource(fileEntry) {
70
+ if (typeof fileEntry.source === 'string') {
71
+ fileEntry.source = new TextEncoder().encode(fileEntry.source);
72
+ }
73
+ return fileEntry.source;
59
74
  }
60
75
 
61
76
  class DirectoryEntryStream {
62
- constructor (entries) {
63
- this.idx = 0;
64
- this.entries = entries;
65
- }
66
- readDirectoryEntry () {
67
- if (this.idx === this.entries.length)
68
- return null;
69
- const [name, entry] = this.entries[this.idx];
70
- this.idx += 1;
71
- return {
72
- name,
73
- type: entry.dir ? 'directory' : 'regular-file'
74
- };
75
- }
77
+ constructor(entries) {
78
+ this.idx = 0;
79
+ this.entries = entries;
80
+ }
81
+ readDirectoryEntry() {
82
+ if (this.idx === this.entries.length) {
83
+ return null;
84
+ }
85
+ const [name, entry] = this.entries[this.idx];
86
+ this.idx += 1;
87
+ return {
88
+ name,
89
+ type: entry.dir ? 'directory' : 'regular-file',
90
+ };
91
+ }
76
92
  }
77
93
 
78
94
  class Descriptor {
79
- #stream;
80
- #entry;
81
- #mtime = 0;
82
-
83
- _getEntry (descriptor) {
84
- return descriptor.#entry;
85
- }
86
-
87
- constructor (entry, isStream) {
88
- if (isStream)
89
- this.#stream = entry;
90
- else
91
- this.#entry = entry;
92
- }
93
-
94
- readViaStream(_offset) {
95
- const source = getSource(this.#entry);
96
- let offset = Number(_offset);
97
- return new InputStream({
98
- blockingRead (len) {
99
- if (offset === source.byteLength)
100
- throw { tag: 'closed' };
101
- const bytes = source.slice(offset, offset + Number(len));
102
- offset += bytes.byteLength;
103
- return bytes;
104
- }
105
- });
106
- }
107
-
108
- writeViaStream(_offset) {
109
- const entry = this.#entry;
110
- let offset = Number(_offset);
111
- return new OutputStream({
112
- write (buf) {
113
- const newSource = new Uint8Array(buf.byteLength + entry.source.byteLength);
114
- newSource.set(entry.source, 0);
115
- newSource.set(buf, offset);
116
- offset += buf.byteLength;
117
- entry.source = newSource;
118
- return buf.byteLength;
119
- }
120
- });
121
- }
122
-
123
- appendViaStream() {
124
- console.log(`[filesystem] APPEND STREAM`);
125
- }
126
-
127
- advise(descriptor, offset, length, advice) {
128
- console.log(`[filesystem] ADVISE`, descriptor, offset, length, advice);
129
- }
130
-
131
- syncData() {
132
- console.log(`[filesystem] SYNC DATA`);
133
- }
134
-
135
- getFlags() {
136
- console.log(`[filesystem] FLAGS FOR`);
137
- }
138
-
139
- getType() {
140
- if (this.#stream) return 'fifo';
141
- if (this.#entry.dir) return 'directory';
142
- if (this.#entry.source) return 'regular-file';
143
- return 'unknown';
144
- }
145
-
146
- setSize(size) {
147
- console.log(`[filesystem] SET SIZE`, size);
148
- }
149
-
150
- setTimes(dataAccessTimestamp, dataModificationTimestamp) {
151
- console.log(`[filesystem] SET TIMES`, dataAccessTimestamp, dataModificationTimestamp);
152
- }
153
-
154
- read(length, offset) {
155
- const source = getSource(this.#entry);
156
- return [source.slice(offset, offset + length), offset + length >= source.byteLength];
157
- }
158
-
159
- write(buffer, offset) {
160
- if (offset !== 0) throw 'invalid-seek';
161
- this.#entry.source = buffer;
162
- return buffer.byteLength;
163
- }
164
-
165
- readDirectory() {
166
- if (!this.#entry?.dir)
167
- throw 'bad-descriptor';
168
- return new DirectoryEntryStream(Object.entries(this.#entry.dir).sort(([a], [b]) => a > b ? 1 : -1));
169
- }
170
-
171
- sync() {
172
- console.log(`[filesystem] SYNC`);
173
- }
174
-
175
- createDirectoryAt(path) {
176
- const entry = getChildEntry(this.#entry, path, { create: true, directory: true });
177
- if (entry.source) throw 'exist';
178
- }
179
-
180
- stat() {
181
- let type = 'unknown', size = BigInt(0);
182
- if (this.#entry.source) {
183
- type = 'regular-file';
184
- const source = getSource(this.#entry);
185
- size = BigInt(source.byteLength);
186
- }
187
- else if (this.#entry.dir) {
188
- type = 'directory';
189
- }
190
- return {
191
- type,
192
- linkCount: BigInt(0),
193
- size,
194
- dataAccessTimestamp: timeZero,
195
- dataModificationTimestamp: timeZero,
196
- statusChangeTimestamp: timeZero,
197
- }
198
- }
199
-
200
- statAt(_pathFlags, path) {
201
- const entry = getChildEntry(this.#entry, path, { create: false, directory: false });
202
- let type = 'unknown', size = BigInt(0);
203
- if (entry.source) {
204
- type = 'regular-file';
205
- const source = getSource(entry);
206
- size = BigInt(source.byteLength);
207
- }
208
- else if (entry.dir) {
209
- type = 'directory';
210
- }
211
- return {
212
- type,
213
- linkCount: BigInt(0),
214
- size,
215
- dataAccessTimestamp: timeZero,
216
- dataModificationTimestamp: timeZero,
217
- statusChangeTimestamp: timeZero,
218
- };
219
- }
220
-
221
- setTimesAt() {
222
- console.log(`[filesystem] SET TIMES AT`);
223
- }
224
-
225
- linkAt() {
226
- console.log(`[filesystem] LINK AT`);
227
- }
228
-
229
- openAt(_pathFlags, path, openFlags, _descriptorFlags, _modes) {
230
- const childEntry = getChildEntry(this.#entry, path, openFlags);
231
- return new Descriptor(childEntry);
232
- }
233
-
234
- readlinkAt() {
235
- console.log(`[filesystem] READLINK AT`);
236
- }
237
-
238
- removeDirectoryAt() {
239
- console.log(`[filesystem] REMOVE DIR AT`);
240
- }
241
-
242
- renameAt() {
243
- console.log(`[filesystem] RENAME AT`);
244
- }
245
-
246
- symlinkAt() {
247
- console.log(`[filesystem] SYMLINK AT`);
248
- }
249
-
250
- unlinkFileAt() {
251
- console.log(`[filesystem] UNLINK FILE AT`);
252
- }
253
-
254
- isSameObject(other) {
255
- return other === this;
256
- }
257
-
258
- metadataHash() {
259
- let upper = BigInt(0);
260
- upper += BigInt(this.#mtime);
261
- return { upper, lower: BigInt(0) };
262
- }
263
-
264
- metadataHashAt(_pathFlags, _path) {
265
- let upper = BigInt(0);
266
- upper += BigInt(this.#mtime);
267
- return { upper, lower: BigInt(0) };
268
- }
95
+ #stream;
96
+ #entry;
97
+ #mtime = 0;
98
+
99
+ _getEntry(descriptor) {
100
+ return descriptor.#entry;
101
+ }
102
+
103
+ constructor(entry, isStream) {
104
+ if (isStream) {
105
+ this.#stream = entry;
106
+ } else {
107
+ this.#entry = entry;
108
+ }
109
+ }
110
+
111
+ readViaStream(_offset) {
112
+ const source = getSource(this.#entry);
113
+ let offset = Number(_offset);
114
+ return new InputStream({
115
+ blockingRead(len) {
116
+ if (offset === source.byteLength) {
117
+ throw { tag: 'closed' };
118
+ }
119
+ const bytes = source.slice(offset, offset + Number(len));
120
+ offset += bytes.byteLength;
121
+ return bytes;
122
+ },
123
+ });
124
+ }
125
+
126
+ writeViaStream(_offset) {
127
+ const entry = this.#entry;
128
+ let offset = Number(_offset);
129
+ return new OutputStream({
130
+ write(buf) {
131
+ const newSource = new Uint8Array(
132
+ buf.byteLength + entry.source.byteLength
133
+ );
134
+ newSource.set(entry.source, 0);
135
+ newSource.set(buf, offset);
136
+ offset += buf.byteLength;
137
+ entry.source = newSource;
138
+ return buf.byteLength;
139
+ },
140
+ });
141
+ }
142
+
143
+ appendViaStream() {
144
+ console.log(`[filesystem] APPEND STREAM`);
145
+ }
146
+
147
+ advise(descriptor, offset, length, advice) {
148
+ console.log(`[filesystem] ADVISE`, descriptor, offset, length, advice);
149
+ }
150
+
151
+ syncData() {
152
+ console.log(`[filesystem] SYNC DATA`);
153
+ }
154
+
155
+ getFlags() {
156
+ console.log(`[filesystem] FLAGS FOR`);
157
+ }
158
+
159
+ getType() {
160
+ if (this.#stream) {
161
+ return 'fifo';
162
+ }
163
+ if (this.#entry.dir) {
164
+ return 'directory';
165
+ }
166
+ if (this.#entry.source) {
167
+ return 'regular-file';
168
+ }
169
+ return 'unknown';
170
+ }
171
+
172
+ setSize(size) {
173
+ console.log(`[filesystem] SET SIZE`, size);
174
+ }
175
+
176
+ setTimes(dataAccessTimestamp, dataModificationTimestamp) {
177
+ console.log(
178
+ `[filesystem] SET TIMES`,
179
+ dataAccessTimestamp,
180
+ dataModificationTimestamp
181
+ );
182
+ }
183
+
184
+ read(length, offset) {
185
+ const source = getSource(this.#entry);
186
+ return [
187
+ source.slice(offset, offset + length),
188
+ offset + length >= source.byteLength,
189
+ ];
190
+ }
191
+
192
+ write(buffer, offset) {
193
+ if (offset !== 0) {
194
+ throw 'invalid-seek';
195
+ }
196
+ this.#entry.source = buffer;
197
+ return buffer.byteLength;
198
+ }
199
+
200
+ readDirectory() {
201
+ if (!this.#entry?.dir) {
202
+ throw 'bad-descriptor';
203
+ }
204
+ return new DirectoryEntryStream(
205
+ Object.entries(this.#entry.dir).sort(([a], [b]) => (a > b ? 1 : -1))
206
+ );
207
+ }
208
+
209
+ sync() {
210
+ console.log(`[filesystem] SYNC`);
211
+ }
212
+
213
+ createDirectoryAt(path) {
214
+ const entry = getChildEntry(this.#entry, path, {
215
+ create: true,
216
+ directory: true,
217
+ });
218
+ if (entry.source) {
219
+ throw 'exist';
220
+ }
221
+ }
222
+
223
+ stat() {
224
+ let type = 'unknown',
225
+ size = BigInt(0);
226
+ if (this.#entry.source) {
227
+ type = 'regular-file';
228
+ const source = getSource(this.#entry);
229
+ size = BigInt(source.byteLength);
230
+ } else if (this.#entry.dir) {
231
+ type = 'directory';
232
+ }
233
+ return {
234
+ type,
235
+ linkCount: BigInt(0),
236
+ size,
237
+ dataAccessTimestamp: timeZero,
238
+ dataModificationTimestamp: timeZero,
239
+ statusChangeTimestamp: timeZero,
240
+ };
241
+ }
242
+
243
+ statAt(_pathFlags, path) {
244
+ const entry = getChildEntry(this.#entry, path, {
245
+ create: false,
246
+ directory: false,
247
+ });
248
+ let type = 'unknown',
249
+ size = BigInt(0);
250
+ if (entry.source) {
251
+ type = 'regular-file';
252
+ const source = getSource(entry);
253
+ size = BigInt(source.byteLength);
254
+ } else if (entry.dir) {
255
+ type = 'directory';
256
+ }
257
+ return {
258
+ type,
259
+ linkCount: BigInt(0),
260
+ size,
261
+ dataAccessTimestamp: timeZero,
262
+ dataModificationTimestamp: timeZero,
263
+ statusChangeTimestamp: timeZero,
264
+ };
265
+ }
266
+
267
+ setTimesAt() {
268
+ console.log(`[filesystem] SET TIMES AT`);
269
+ }
270
+
271
+ linkAt() {
272
+ console.log(`[filesystem] LINK AT`);
273
+ }
274
+
275
+ openAt(_pathFlags, path, openFlags, _descriptorFlags, _modes) {
276
+ const childEntry = getChildEntry(this.#entry, path, openFlags);
277
+ return new Descriptor(childEntry);
278
+ }
279
+
280
+ readlinkAt() {
281
+ console.log(`[filesystem] READLINK AT`);
282
+ }
283
+
284
+ removeDirectoryAt() {
285
+ console.log(`[filesystem] REMOVE DIR AT`);
286
+ }
287
+
288
+ renameAt() {
289
+ console.log(`[filesystem] RENAME AT`);
290
+ }
291
+
292
+ symlinkAt() {
293
+ console.log(`[filesystem] SYMLINK AT`);
294
+ }
295
+
296
+ unlinkFileAt() {
297
+ console.log(`[filesystem] UNLINK FILE AT`);
298
+ }
299
+
300
+ isSameObject(other) {
301
+ return other === this;
302
+ }
303
+
304
+ metadataHash() {
305
+ let upper = BigInt(0);
306
+ upper += BigInt(this.#mtime);
307
+ return { upper, lower: BigInt(0) };
308
+ }
309
+
310
+ metadataHashAt(_pathFlags, _path) {
311
+ let upper = BigInt(0);
312
+ upper += BigInt(this.#mtime);
313
+ return { upper, lower: BigInt(0) };
314
+ }
269
315
  }
270
316
  const descriptorGetEntry = Descriptor.prototype._getEntry;
271
317
  delete Descriptor.prototype._getEntry;
272
318
 
273
- let _preopens = [[new Descriptor(_fileData), '/']], _rootPreopen = _preopens[0];
319
+ let _preopens = [[new Descriptor(_fileData), '/']],
320
+ _rootPreopen = _preopens[0];
274
321
 
275
322
  export const preopens = {
276
- getDirectories () {
277
- return _preopens;
278
- }
279
- }
323
+ getDirectories() {
324
+ return _preopens;
325
+ },
326
+ };
280
327
 
281
328
  export const types = {
282
- Descriptor,
283
- DirectoryEntryStream
329
+ Descriptor,
330
+ DirectoryEntryStream,
284
331
  };
285
332
 
286
- export { types as filesystemTypes }
333
+ export { types as filesystemTypes };