tinyc-web 0.1.2 → 0.2.0

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.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/tinyc-web.js CHANGED
@@ -2247,6 +2247,12 @@ var TinycWeb = (() => {
2247
2247
  const nativePath = this._nativePath(path);
2248
2248
  const dir = nativePath.slice(0, nativePath.lastIndexOf("/")) || "/";
2249
2249
  if (typeof FS.mkdirTree === "function") FS.mkdirTree(dir);
2250
+ if (typeof FS.unlink === "function") {
2251
+ try {
2252
+ FS.unlink(nativePath);
2253
+ } catch {
2254
+ }
2255
+ }
2250
2256
  FS.writeFile(nativePath, bytes);
2251
2257
  }
2252
2258
  };
@@ -2768,10 +2774,16 @@ var TinycWeb = (() => {
2768
2774
  this.handle = handle;
2769
2775
  this._io = null;
2770
2776
  this._deleted = false;
2777
+ this._outputTypeSet = false;
2771
2778
  }
2772
2779
  _call(name, returnType, argTypes, args) {
2773
2780
  if (this._deleted) throw new TinycError("tinyc: state already deleted");
2774
- return this._bridge.call(name, returnType, argTypes, [this.handle, ...args]);
2781
+ return this._bridge.call(
2782
+ name,
2783
+ returnType,
2784
+ ["number", ...argTypes],
2785
+ [this.handle, ...args]
2786
+ );
2775
2787
  }
2776
2788
  _checkRc(name, rc) {
2777
2789
  if (typeof rc === "number" && rc < 0) {
@@ -2858,10 +2870,23 @@ var TinycWeb = (() => {
2858
2870
  return this;
2859
2871
  }
2860
2872
  setOutputType(type) {
2861
- return this._checkRc(
2873
+ const rc = this._checkRc(
2862
2874
  "tcc_set_output_type",
2863
2875
  this._call("tcc_set_output_type", "number", ["number"], [type])
2864
2876
  );
2877
+ this._outputTypeSet = true;
2878
+ return rc;
2879
+ }
2880
+ /* tcc_set_output_type is not a plain setter in libtcc: it also
2881
+ * creates the state's ELF sections (tccelf_new), and upstream's
2882
+ * contract is that it MUST be called before any compilation —
2883
+ * compiling without it runs tccelf_end_file over sections that
2884
+ * do not exist and traps inside put_elf_sym. Establish the
2885
+ * MEMORY default lazily, exactly once, at the first compiling
2886
+ * entry point; an explicit setOutputType beforehand suppresses
2887
+ * the default, and tccelf_new is never run twice on one state. */
2888
+ _ensureOutputType() {
2889
+ if (!this._outputTypeSet) this.setOutputType(TCC_OUTPUT.MEMORY);
2865
2890
  }
2866
2891
  /** Add a file (source, object or archive) by virtual path. The file
2867
2892
  * must exist in the instance's virtual FS; it is synced to the
@@ -2875,12 +2900,14 @@ var TinycWeb = (() => {
2875
2900
  throw new TinycError(`tinyc: addFile: ${p} is not in the virtual FS`);
2876
2901
  }
2877
2902
  this._instance.fs.syncFile(p);
2903
+ this._ensureOutputType();
2878
2904
  return this._checkRc(
2879
2905
  "tcc_add_file",
2880
2906
  this._call("tcc_add_file", "number", ["string"], [this._bridge.modulePath(p)])
2881
2907
  );
2882
2908
  }
2883
2909
  compileString(source) {
2910
+ this._ensureOutputType();
2884
2911
  return this._checkRc(
2885
2912
  "tcc_compile_string",
2886
2913
  this._call("tcc_compile_string", "number", ["string"], [source])
@@ -2897,6 +2924,7 @@ var TinycWeb = (() => {
2897
2924
  const src = this._instance.fs.readTextFile(p);
2898
2925
  const marked = `#line 1 "${p}"
2899
2926
  ${src}`;
2927
+ this._ensureOutputType();
2900
2928
  return this._checkRc(
2901
2929
  "tcc_compile_string",
2902
2930
  this._call("tcc_compile_string", "number", ["string"], [marked])
@@ -3138,14 +3166,6 @@ ${src}`;
3138
3166
  * read fails naming exportFileAsync as the alternative. */
3139
3167
  exportFile(path) {
3140
3168
  const p = normalizeVfsPath(path);
3141
- let cacheMiss = null;
3142
- if (this.fs.hasFile(p)) {
3143
- try {
3144
- return this.fs.readFile(p);
3145
- } catch (err) {
3146
- cacheMiss = err;
3147
- }
3148
- }
3149
3169
  const FS = this._bridge.native?.FS;
3150
3170
  if (FS && typeof FS.readFile === "function") {
3151
3171
  try {
@@ -3155,10 +3175,14 @@ ${src}`;
3155
3175
  } catch {
3156
3176
  }
3157
3177
  }
3158
- if (cacheMiss) {
3159
- throw new TinycError(
3160
- `tinyc: ${p} is persisted in the virtual FS store but is not in the in-memory cache; read it with exportFileAsync() instead`
3161
- );
3178
+ if (this.fs.hasFile(p)) {
3179
+ try {
3180
+ return this.fs.readFile(p);
3181
+ } catch {
3182
+ throw new TinycError(
3183
+ `tinyc: ${p} is persisted in the virtual FS store but is not in the in-memory cache; read it with exportFileAsync() instead`
3184
+ );
3185
+ }
3162
3186
  }
3163
3187
  throw new TinycError(`tinyc: no such file in virtual FS: ${p}`);
3164
3188
  }
@@ -3166,7 +3190,6 @@ ${src}`;
3166
3190
  * when it is not in the content cache. */
3167
3191
  async exportFileAsync(path) {
3168
3192
  const p = normalizeVfsPath(path);
3169
- if (this.fs.hasFile(p)) return this.fs.readFileAsync(p);
3170
3193
  const FS = this._bridge.native?.FS;
3171
3194
  if (FS && typeof FS.readFile === "function") {
3172
3195
  try {
@@ -3176,6 +3199,7 @@ ${src}`;
3176
3199
  } catch {
3177
3200
  }
3178
3201
  }
3202
+ if (this.fs.hasFile(p)) return this.fs.readFileAsync(p);
3179
3203
  throw new TinycError(`tinyc: no such file in virtual FS: ${p}`);
3180
3204
  }
3181
3205
  /** exportFile as a Blob, e.g. for a download link or a File
@@ -3335,6 +3359,9 @@ ${src}`;
3335
3359
  const state = new TinycState(this, handle ?? {});
3336
3360
  this._states.add(state);
3337
3361
  state._installErrorFunc();
3362
+ if (isRunnableTarget(this.target)) {
3363
+ state.setOptions("-nostdlib");
3364
+ }
3338
3365
  if (this._bridge.has("tcc_add_include_path")) {
3339
3366
  for (const dir of this.fs.getIncludeDirs()) state.addIncludePath(dir);
3340
3367
  }
@@ -3448,6 +3475,37 @@ ${src}`;
3448
3475
  }
3449
3476
  var OPFS_MOUNT_POINT = "/opfs";
3450
3477
  var opfsBackendByModule = /* @__PURE__ */ new WeakMap();
3478
+ var OPFS_PROBE_DIR = OPFS_MOUNT_POINT + "/.tinyc-probe";
3479
+ var OPFS_PROBE_FILE = OPFS_PROBE_DIR + "/probe";
3480
+ var OPFS_PROBE_BYTES = new Uint8Array([116, 105, 110, 121, 99]);
3481
+ function probeOpfsBackend(FS) {
3482
+ if (!FS || typeof FS.writeFile !== "function" || typeof FS.readFile !== "function") {
3483
+ return false;
3484
+ }
3485
+ try {
3486
+ if (typeof FS.mkdirTree === "function") FS.mkdirTree(OPFS_PROBE_DIR);
3487
+ FS.writeFile(OPFS_PROBE_FILE, OPFS_PROBE_BYTES);
3488
+ const back = FS.readFile(OPFS_PROBE_FILE);
3489
+ if (!back || back.length !== OPFS_PROBE_BYTES.length) return false;
3490
+ for (let i = 0; i < OPFS_PROBE_BYTES.length; i++) {
3491
+ if (back[i] !== OPFS_PROBE_BYTES[i]) return false;
3492
+ }
3493
+ return true;
3494
+ } catch {
3495
+ return false;
3496
+ } finally {
3497
+ try {
3498
+ FS.unlink(OPFS_PROBE_FILE);
3499
+ } catch {
3500
+ }
3501
+ if (typeof FS.rmdir === "function") {
3502
+ try {
3503
+ FS.rmdir(OPFS_PROBE_DIR);
3504
+ } catch {
3505
+ }
3506
+ }
3507
+ }
3508
+ }
3451
3509
  async function tryMountOpfsBackend(native, bridge) {
3452
3510
  const canMemo = native !== null && (typeof native === "object" || typeof native === "function");
3453
3511
  if (canMemo) {
@@ -3472,7 +3530,7 @@ ${src}`;
3472
3530
  [OPFS_MOUNT_POINT, 511, backend],
3473
3531
  { async: true }
3474
3532
  );
3475
- if (rc === 0) result = "opfs";
3533
+ if (rc === 0 && probeOpfsBackend(native.FS)) result = "opfs";
3476
3534
  }
3477
3535
  }
3478
3536
  } catch {