@spade-lang/spade 0.21.0-dev1.1.e1834a-e1834a → 0.21.0-dev1.1.e6d3f2-e6d3f2

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.
package/gen/bundle.js CHANGED
@@ -121,8 +121,13 @@ var TerminalInput = class {
121
121
  };
122
122
  var TerminalOutput = class {
123
123
  };
124
+ var nextFilesystemId = /* @__PURE__ */ function() {
125
+ let id = 0;
126
+ return () => id++;
127
+ }();
124
128
  var File = class {
125
129
  constructor(data = "") {
130
+ this.id = nextFilesystemId();
126
131
  if (data instanceof Uint8Array) {
127
132
  this.data = data;
128
133
  } else if (typeof data === "string") {
@@ -135,40 +140,15 @@ var File = class {
135
140
  return this.data.length;
136
141
  }
137
142
  };
138
- var ReadStream = class extends InputStream {
139
- constructor(file, offset) {
140
- super();
141
- this.file = file;
142
- this.offset = offset;
143
- }
144
- read(len) {
145
- const data = this.file.data.subarray(Number(this.offset), Number(this.offset + len));
146
- this.offset += len;
147
- return data;
148
- }
149
- };
150
- var WriteStream = class extends OutputStream {
151
- constructor(file, offset) {
152
- super();
153
- this.file = file;
154
- this.offset = offset;
155
- }
156
- write(contents) {
157
- const newData = new Uint8Array(this.file.data.length + contents.length);
158
- newData.set(this.file.data);
159
- newData.subarray(Number(this.offset)).set(contents);
160
- this.file.data = newData;
161
- this.offset += BigInt(contents.length);
162
- }
163
- };
164
143
  var Directory = class _Directory {
165
144
  constructor(files = {}) {
145
+ this.id = nextFilesystemId();
166
146
  this.files = files;
167
147
  }
168
148
  get size() {
169
149
  return Object.keys(this.files).length;
170
150
  }
171
- traverse(path, flags = { create: false, remove: false }) {
151
+ traverse(path, { create = null, remove = false } = {}) {
172
152
  let entry = this;
173
153
  let separatorAt = -1;
174
154
  do {
@@ -177,7 +157,7 @@ var Directory = class _Directory {
177
157
  const files = entry.files;
178
158
  separatorAt = path.indexOf("/");
179
159
  const segment = separatorAt === -1 ? path : path.substring(0, separatorAt);
180
- if (separatorAt === -1 && flags.remove)
160
+ if (separatorAt === -1 && remove)
181
161
  delete files[segment];
182
162
  else if (segment === "" || segment === ".")
183
163
  ;
@@ -185,10 +165,12 @@ var Directory = class _Directory {
185
165
  ;
186
166
  else if (Object.hasOwn(files, segment))
187
167
  entry = files[segment];
188
- else if (flags.create === "directory" || flags.create === "file" && separatorAt !== -1)
168
+ else if (create === "directory" || create !== null && separatorAt !== -1)
189
169
  entry = files[segment] = new _Directory({});
190
- else if (flags.create === "file")
170
+ else if (create === "file")
191
171
  entry = files[segment] = new File(new Uint8Array());
172
+ else if (create instanceof File || create instanceof _Directory)
173
+ entry = files[segment] = create;
192
174
  else
193
175
  throw "no-entry";
194
176
  path = path.substring(separatorAt + 1);
@@ -196,6 +178,33 @@ var Directory = class _Directory {
196
178
  return entry;
197
179
  }
198
180
  };
181
+ var ReadStream = class extends InputStream {
182
+ constructor(file, offset) {
183
+ super();
184
+ this.file = file;
185
+ this.offset = offset;
186
+ }
187
+ read(len) {
188
+ const data = this.file.data.subarray(Number(this.offset), Number(this.offset + len));
189
+ this.offset += len;
190
+ return data;
191
+ }
192
+ };
193
+ var WriteStream = class extends OutputStream {
194
+ constructor(file, offset) {
195
+ super();
196
+ this.file = file;
197
+ this.offset = offset;
198
+ }
199
+ write(contents) {
200
+ const offset = Number(this.offset);
201
+ const newData = new Uint8Array(Math.max(this.file.data.length, offset + contents.length));
202
+ newData.set(this.file.data);
203
+ newData.subarray(offset).set(contents);
204
+ this.file.data = newData;
205
+ this.offset += BigInt(contents.length);
206
+ }
207
+ };
199
208
  var Descriptor = class _Descriptor {
200
209
  constructor(entry) {
201
210
  this.entry = entry;
@@ -210,7 +219,7 @@ var Descriptor = class _Descriptor {
210
219
  return {};
211
220
  }
212
221
  metadataHash() {
213
- return { upper: 0, lower: 0 };
222
+ return { upper: 0, lower: this.entry.id };
214
223
  }
215
224
  metadataHashAt(_pathFlags, path) {
216
225
  if (!(this.entry instanceof Directory))
@@ -242,7 +251,7 @@ var Descriptor = class _Descriptor {
242
251
  openAt(_pathFlags, path, openFlags, _descriptorFlags) {
243
252
  if (!(this.entry instanceof Directory))
244
253
  throw "invalid";
245
- const openEntry = this.entry.traverse(path, { create: openFlags.create ? "file" : false });
254
+ const openEntry = this.entry.traverse(path, openFlags.create ? { create: "file" } : {});
246
255
  if (openFlags.directory) {
247
256
  if (!(openEntry instanceof Directory))
248
257
  throw "not-directory";
@@ -272,6 +281,18 @@ var Descriptor = class _Descriptor {
272
281
  writeViaStream(offset) {
273
282
  return new WriteStream(this.entry, offset);
274
283
  }
284
+ setSize(size) {
285
+ if (this.entry instanceof Directory)
286
+ throw "is-directory";
287
+ size = Number(size);
288
+ if (size > this.entry.data.length) {
289
+ const newData = new Uint8Array(size);
290
+ newData.set(this.entry.data);
291
+ this.entry.data = newData;
292
+ } else if (size < this.entry.data.length) {
293
+ this.entry.data = this.entry.data.subarray(0, size);
294
+ }
295
+ }
275
296
  readDirectory() {
276
297
  return new DirectoryEntryStream(this.entry);
277
298
  }
@@ -281,15 +302,28 @@ var Descriptor = class _Descriptor {
281
302
  unlinkFileAt(path) {
282
303
  const pathEntry = this.entry.traverse(path);
283
304
  if (pathEntry instanceof Directory)
284
- return "is-directory";
305
+ throw "is-directory";
285
306
  this.entry.traverse(path, { remove: true });
286
307
  }
287
308
  removeDirectoryAt(path) {
288
309
  const pathEntry = this.entry.traverse(path);
289
310
  if (!(pathEntry instanceof Directory))
290
- return "not-directory";
311
+ throw "not-directory";
291
312
  this.entry.traverse(path, { remove: true });
292
313
  }
314
+ readlinkAt(path) {
315
+ const _pathEntry = this.entry.traverse(path);
316
+ throw "invalid";
317
+ }
318
+ renameAt(oldPath, newDescriptor, newPath) {
319
+ if (!(this.entry instanceof Directory))
320
+ throw "not-directory";
321
+ if (!(newDescriptor.entry instanceof Directory))
322
+ throw "not-directory";
323
+ const oldEntry = this.entry.traverse(oldPath);
324
+ this.entry.traverse(newPath, { create: oldEntry });
325
+ this.entry.traverse(oldPath, { remove: true });
326
+ }
293
327
  };
294
328
  var DirectoryEntryStream = class {
295
329
  constructor(directory) {
@@ -466,7 +500,11 @@ async function fetchObject(obj, fetchFn) {
466
500
  return obj;
467
501
  }
468
502
  function fetchWebAssembly(url) {
469
- return fetch_default(url).then(WebAssembly.compileStreaming);
503
+ if (WebAssembly.compileStreaming !== void 0) {
504
+ return fetch_default(url).then(WebAssembly.compileStreaming);
505
+ } else {
506
+ return fetch_default(url).then((resp) => resp.arrayBuffer()).then(WebAssembly.compile);
507
+ }
470
508
  }
471
509
  function fetchUint8Array(url) {
472
510
  return fetch_default(url).then((resp) => resp.arrayBuffer()).then((buf) => new Uint8Array(buf));
@@ -486,53 +524,56 @@ var Application = class {
486
524
  this.instantiate = instantiate2;
487
525
  this.argv0 = argv0;
488
526
  }
489
- async preload() {
490
- if (this.resourceData === null)
491
- this.resourceData = await this.resources().then(fetchResources);
492
- }
493
527
  // The `printLine` option is deprecated and not documented but still accepted for compatibility.
494
- execute(args, files = {}, { stdin, stdout, stderr, decodeASCII = true, printLine } = {}) {
528
+ run(args = null, files = {}, options = {}) {
529
+ if (this.resourceData === null) {
530
+ if (options.synchronously)
531
+ throw new Error("Cannot run application synchronously unless resources are prefetched first; use `await run()` to do so");
532
+ return this.resources().then(fetchResources).then((resourceData) => {
533
+ this.resourceData = resourceData;
534
+ return this.run(args, files, options);
535
+ });
536
+ }
537
+ if (args === null)
538
+ return;
495
539
  const environment = new Environment();
496
540
  environment.args = [this.argv0].concat(args);
497
541
  environment.root = directoryFromTree(files);
498
542
  for (const [dirName, dirContents] of Object.entries(this.resourceData.filesystem))
499
543
  environment.root.files[dirName] = directoryFromTree(dirContents);
500
- const lineBufferedConsole = lineBuffered(printLine ?? console.log);
501
- environment.stdin = stdin === void 0 ? null : stdin;
502
- environment.stdout = stdout === void 0 ? lineBufferedConsole : stdout;
503
- environment.stderr = stderr === void 0 ? lineBufferedConsole : stderr;
504
- const wasmCommand = this.instantiate(
505
- (filename) => this.resourceData.modules[filename],
506
- { runtime: environment.exports },
507
- // workaround for bytecodealliance/jco#374
508
- (module, imports) => new WebAssembly.Instance(module, imports)
509
- );
510
- let error = null;
511
- try {
512
- wasmCommand.run.run();
513
- } catch (e) {
514
- if (!(e instanceof Exit))
515
- throw e;
516
- if (e instanceof Exit && e.code !== 0)
517
- error = e;
518
- }
519
- for (const dirName of Object.keys(this.resourceData.filesystem))
520
- delete environment.root.files[dirName];
521
- files = directoryIntoTree(environment.root, { decodeASCII });
522
- if (error !== null) {
523
- error.files = files;
524
- throw error;
544
+ const lineBufferedConsole = lineBuffered(options.printLine ?? console.log);
545
+ environment.stdin = options.stdin === void 0 ? null : options.stdin;
546
+ environment.stdout = options.stdout === void 0 ? lineBufferedConsole : options.stdout;
547
+ environment.stderr = options.stderr === void 0 ? lineBufferedConsole : options.stderr;
548
+ const runCommand = (wasmCommand) => {
549
+ let error = null;
550
+ try {
551
+ wasmCommand.run.run();
552
+ } catch (e) {
553
+ if (!(e instanceof Exit))
554
+ throw e;
555
+ if (e instanceof Exit && e.code !== 0)
556
+ error = e;
557
+ }
558
+ for (const dirName of Object.keys(this.resourceData.filesystem))
559
+ delete environment.root.files[dirName];
560
+ files = directoryIntoTree(environment.root, { decodeASCII: options.decodeASCII ?? true });
561
+ if (error !== null) {
562
+ error.files = files;
563
+ throw error;
564
+ } else {
565
+ return files;
566
+ }
567
+ };
568
+ const getCoreModule = (filename) => this.resourceData.modules[filename];
569
+ const imports = { runtime: environment.exports };
570
+ if (options.synchronously) {
571
+ const instantiateCore = (module, imports2) => new WebAssembly.Instance(module, imports2);
572
+ return runCommand(this.instantiate(getCoreModule, imports, instantiateCore));
525
573
  } else {
526
- return files;
574
+ return this.instantiate(getCoreModule, imports).then(runCommand);
527
575
  }
528
576
  }
529
- run(args = null, files = {}, options = {}) {
530
- if (this.resourceData === null)
531
- return this.preload().then((_) => this.run(args, files, options));
532
- if (args === null)
533
- return;
534
- return this.execute(args, files, options);
535
- }
536
577
  };
537
578
 
538
579
  // gen/spade.js
@@ -582,7 +623,7 @@ function utf8Encode(s, realloc, memory) {
582
623
  utf8EncodedLen = writtenTotal;
583
624
  return ptr;
584
625
  }
585
- function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.Instance) {
626
+ async function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.instantiate) {
586
627
  const module0 = getCoreModule("spade.core.wasm");
587
628
  const module1 = getCoreModule("spade.core2.wasm");
588
629
  const module2 = getCoreModule("spade.core3.wasm");
@@ -3005,8 +3046,10 @@ function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.Insta
3005
3046
  handleEntry.rep[symbolDispose]();
3006
3047
  }
3007
3048
  }
3008
- ({ exports: exports0 } = instantiateCore(module2));
3009
- ({ exports: exports1 } = instantiateCore(module0, {
3049
+ Promise.all([module0, module1, module2, module3]).catch(() => {
3050
+ });
3051
+ ({ exports: exports0 } = await instantiateCore(await module2));
3052
+ ({ exports: exports1 } = await instantiateCore(await module0, {
3010
3053
  wasi_snapshot_preview1: {
3011
3054
  args_get: exports0["23"],
3012
3055
  args_sizes_get: exports0["24"],
@@ -3027,7 +3070,7 @@ function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.Insta
3027
3070
  sched_yield: exports0["31"]
3028
3071
  }
3029
3072
  }));
3030
- ({ exports: exports2 } = instantiateCore(module1, {
3073
+ ({ exports: exports2 } = await instantiateCore(await module1, {
3031
3074
  __main_module__: {
3032
3075
  _start: exports1._start
3033
3076
  },
@@ -3106,7 +3149,7 @@ function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.Insta
3106
3149
  }));
3107
3150
  memory0 = exports1.memory;
3108
3151
  realloc0 = exports2.cabi_import_realloc;
3109
- ({ exports: exports3 } = instantiateCore(module3, {
3152
+ ({ exports: exports3 } = await instantiateCore(await module3, {
3110
3153
  "": {
3111
3154
  $imports: exports0.$imports,
3112
3155
  "0": trampoline12,
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spade-lang/spade",
3
- "version": "0.21.0-dev1.1.e1834a-e1834a",
3
+ "version": "0.21.0-dev1.1.e6d3f2-e6d3f2",
4
4
  "description": "Spade HDL (YoWASP package)",
5
5
  "author": "Frans Skarman <frans.skarman@liu.se>",
6
6
  "license": "ISC",
@@ -27,11 +27,11 @@
27
27
  "types": "./lib/api.d.ts",
28
28
  "devDependencies": {
29
29
  "@bytecodealliance/jco": "1.0.0",
30
- "@yowasp/runtime": "8.0.40",
30
+ "@yowasp/runtime": "9.0.58",
31
31
  "esbuild": "^0.19.8"
32
32
  },
33
33
  "scripts": {
34
- "transpile": "jco new ${CARGO_TARGET_DIR:-../../target}/wasm32-wasip1/release/spade.wasm --wasi-command --output spade.wasm && jco transpile spade.wasm --instantiation sync --no-typescript --no-namespaced-exports --map 'wasi:io/*=runtime#io' --map 'wasi:cli/*=runtime#cli' --map 'wasi:clocks/*=runtime#*' --map 'wasi:filesystem/*=runtime#fs' --map 'wasi:random/*=runtime#random' --out-dir gen/",
34
+ "transpile": "jco new ${CARGO_TARGET_DIR:-../../target}/wasm32-wasip1/release/spade.wasm --wasi-command --output spade.wasm && jco transpile spade.wasm --instantiation async --no-typescript --no-namespaced-exports --map 'wasi:io/*=runtime#io' --map 'wasi:cli/*=runtime#cli' --map 'wasi:clocks/*=runtime#*' --map 'wasi:filesystem/*=runtime#fs' --map 'wasi:random/*=runtime#random' --out-dir gen/",
35
35
  "pack": "yowasp-pack-resources gen/resources-spade.js gen",
36
36
  "build": "esbuild --bundle lib/api.js --outfile=gen/bundle.js --format=esm --platform=node --external:./resources-*.js",
37
37
  "all": "npm run transpile && npm run pack && npm run build"