@bytecodealliance/preview2-shim 0.17.2 → 0.17.4

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