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.
- package/README.md +13 -0
- package/README.zh-CN.md +11 -0
- package/index.mjs +116 -20
- package/package.json +1 -44
- package/runtime-shims.mjs +1004 -0
- package/tcc-aarch64-linux-android.wasm +0 -0
- package/tcc-aarch64-linux-gnu.wasm +0 -0
- package/tcc-arm64-apple-darwin.wasm +0 -0
- package/tcc-riscv64-linux-gnu.wasm +0 -0
- package/tcc-wasm32-wasi.wasm +0 -0
- package/tcc-x86_64-apple-darwin.wasm +0 -0
- package/tcc-x86_64-linux-android.wasm +0 -0
- package/tcc-x86_64-linux-gnu.wasm +0 -0
- package/tcc-x86_64-windows.wasm +0 -0
- package/tinyc-web.js +74 -16
package/README.md
CHANGED
|
@@ -249,6 +249,19 @@ const worker = new TinycWorker({ shimModules: ['./my-shims.mjs'] });
|
|
|
249
249
|
// my-shims.mjs: export default { host_log: (ctx, ptr) => { ... } };
|
|
250
250
|
```
|
|
251
251
|
|
|
252
|
+
## SIMD intrinsics
|
|
253
|
+
|
|
254
|
+
Each target's sysroot carries that target's SIMD headers in its
|
|
255
|
+
default include set. The native targets carry the same headers
|
|
256
|
+
as the other packages (`arm_neon.h` for AArch64, the
|
|
257
|
+
`immintrin.h` set for x86-64, `riscv_vector.h` for riscv64).
|
|
258
|
+
The `wasm32-wasi` target carries `#include <wasm_simd128.h>` —
|
|
259
|
+
the WebAssembly SIMD128 intrinsic set — plus wasm forms of
|
|
260
|
+
`arm_neon.h`, `xmmintrin.h` and `emmintrin.h`: subsets of the
|
|
261
|
+
NEON and SSE/SSE2 surfaces mapped onto v128 operations. See the
|
|
262
|
+
root README's "SIMD intrinsics" section for the per-header
|
|
263
|
+
breakdown.
|
|
264
|
+
|
|
252
265
|
## Tests
|
|
253
266
|
|
|
254
267
|
```
|
package/README.zh-CN.md
CHANGED
|
@@ -249,6 +249,17 @@ const worker = new TinycWorker({ shimModules: ['./my-shims.mjs'] });
|
|
|
249
249
|
// my-shims.mjs: export default { host_log: (ctx, ptr) => { ... } };
|
|
250
250
|
```
|
|
251
251
|
|
|
252
|
+
## SIMD 内建函数
|
|
253
|
+
|
|
254
|
+
每个目标的 sysroot 在其默认头文件集里带该目标的 SIMD 头。
|
|
255
|
+
原生目标带与其它包相同的头(AArch64 用 `arm_neon.h`、x86-64
|
|
256
|
+
用 `immintrin.h` 一组、riscv64 用 `riscv_vector.h`)。
|
|
257
|
+
`wasm32-wasi` 目标带 `#include <wasm_simd128.h>`——
|
|
258
|
+
WebAssembly SIMD128 内建集——另带 wasm 形态的 `arm_neon.h`、
|
|
259
|
+
`xmmintrin.h`、`emmintrin.h`:NEON 与 SSE/SSE2 表面中映射到
|
|
260
|
+
v128 操作的子集。逐个头的覆盖明细见根 README 的「SIMD 内建
|
|
261
|
+
函数」一节。
|
|
262
|
+
|
|
252
263
|
## 测试
|
|
253
264
|
|
|
254
265
|
```
|
package/index.mjs
CHANGED
|
@@ -666,6 +666,14 @@ export class VirtualFS {
|
|
|
666
666
|
const nativePath = this._nativePath(path);
|
|
667
667
|
const dir = nativePath.slice(0, nativePath.lastIndexOf('/')) || '/';
|
|
668
668
|
if (typeof FS.mkdirTree === 'function') FS.mkdirTree(dir);
|
|
669
|
+
// The mirror write is a replace: the native copy must end up
|
|
670
|
+
// holding exactly `bytes`. The WasmFS FS.writeFile appends to a
|
|
671
|
+
// file that already exists (it writes at the file's current
|
|
672
|
+
// size), so an existing native file is unlinked first and the
|
|
673
|
+
// write lands as a fresh file with exactly the mirrored content.
|
|
674
|
+
if (typeof FS.unlink === 'function') {
|
|
675
|
+
try { FS.unlink(nativePath); } catch { /* not present natively */ }
|
|
676
|
+
}
|
|
669
677
|
FS.writeFile(nativePath, bytes);
|
|
670
678
|
}
|
|
671
679
|
}
|
|
@@ -1393,11 +1401,19 @@ export class TinycState {
|
|
|
1393
1401
|
this.handle = handle;
|
|
1394
1402
|
this._io = null;
|
|
1395
1403
|
this._deleted = false;
|
|
1404
|
+
this._outputTypeSet = false;
|
|
1396
1405
|
}
|
|
1397
1406
|
|
|
1398
1407
|
_call(name, returnType, argTypes, args) {
|
|
1399
1408
|
if (this._deleted) throw new TinycError('tinyc: state already deleted');
|
|
1400
|
-
|
|
1409
|
+
// The handle goes in as the first argument AND as a leading
|
|
1410
|
+
// 'number' in argTypes: the bridge converts args[i] through
|
|
1411
|
+
// argTypes[i], so prepending the handle to the values alone
|
|
1412
|
+
// shifts every converter one slot left (a 'string' converter
|
|
1413
|
+
// lands on the numeric handle, and the real string argument
|
|
1414
|
+
// reaches the module unconverted, as 0).
|
|
1415
|
+
return this._bridge.call(name, returnType,
|
|
1416
|
+
['number', ...argTypes], [this.handle, ...args]);
|
|
1401
1417
|
}
|
|
1402
1418
|
|
|
1403
1419
|
_checkRc(name, rc) {
|
|
@@ -1485,8 +1501,22 @@ export class TinycState {
|
|
|
1485
1501
|
}
|
|
1486
1502
|
|
|
1487
1503
|
setOutputType(type) {
|
|
1488
|
-
|
|
1504
|
+
const rc = this._checkRc('tcc_set_output_type',
|
|
1489
1505
|
this._call('tcc_set_output_type', 'number', ['number'], [type]));
|
|
1506
|
+
this._outputTypeSet = true;
|
|
1507
|
+
return rc;
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
/* tcc_set_output_type is not a plain setter in libtcc: it also
|
|
1511
|
+
* creates the state's ELF sections (tccelf_new), and upstream's
|
|
1512
|
+
* contract is that it MUST be called before any compilation —
|
|
1513
|
+
* compiling without it runs tccelf_end_file over sections that
|
|
1514
|
+
* do not exist and traps inside put_elf_sym. Establish the
|
|
1515
|
+
* MEMORY default lazily, exactly once, at the first compiling
|
|
1516
|
+
* entry point; an explicit setOutputType beforehand suppresses
|
|
1517
|
+
* the default, and tccelf_new is never run twice on one state. */
|
|
1518
|
+
_ensureOutputType() {
|
|
1519
|
+
if (!this._outputTypeSet) this.setOutputType(TCC_OUTPUT.MEMORY);
|
|
1490
1520
|
}
|
|
1491
1521
|
|
|
1492
1522
|
/** Add a file (source, object or archive) by virtual path. The file
|
|
@@ -1501,11 +1531,13 @@ export class TinycState {
|
|
|
1501
1531
|
throw new TinycError(`tinyc: addFile: ${p} is not in the virtual FS`);
|
|
1502
1532
|
}
|
|
1503
1533
|
this._instance.fs.syncFile(p);
|
|
1534
|
+
this._ensureOutputType();
|
|
1504
1535
|
return this._checkRc('tcc_add_file',
|
|
1505
1536
|
this._call('tcc_add_file', 'number', ['string'], [this._bridge.modulePath(p)]));
|
|
1506
1537
|
}
|
|
1507
1538
|
|
|
1508
1539
|
compileString(source) {
|
|
1540
|
+
this._ensureOutputType();
|
|
1509
1541
|
return this._checkRc('tcc_compile_string',
|
|
1510
1542
|
this._call('tcc_compile_string', 'number', ['string'], [source]));
|
|
1511
1543
|
}
|
|
@@ -1520,6 +1552,7 @@ export class TinycState {
|
|
|
1520
1552
|
const p = normalizeVfsPath(path);
|
|
1521
1553
|
const src = this._instance.fs.readTextFile(p);
|
|
1522
1554
|
const marked = `#line 1 "${p}"\n${src}`;
|
|
1555
|
+
this._ensureOutputType();
|
|
1523
1556
|
return this._checkRc('tcc_compile_string',
|
|
1524
1557
|
this._call('tcc_compile_string', 'number', ['string'], [marked]));
|
|
1525
1558
|
}
|
|
@@ -1735,14 +1768,11 @@ export class Tinyc {
|
|
|
1735
1768
|
* read fails naming exportFileAsync as the alternative. */
|
|
1736
1769
|
exportFile(path) {
|
|
1737
1770
|
const p = normalizeVfsPath(path);
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
cacheMiss = err;
|
|
1744
|
-
}
|
|
1745
|
-
}
|
|
1771
|
+
// The module FS copy is authoritative: it is where TCC writes,
|
|
1772
|
+
// and a compile can rewrite a path the virtual FS already holds
|
|
1773
|
+
// a mirror of (TinycState.run writes the fixed DEFAULT_RUN_OUTPUT
|
|
1774
|
+
// on every call). Read it first and refresh the mirror; only a
|
|
1775
|
+
// path the module FS does not have falls back to the mirror.
|
|
1746
1776
|
const FS = this._bridge.native?.FS;
|
|
1747
1777
|
if (FS && typeof FS.readFile === 'function') {
|
|
1748
1778
|
try {
|
|
@@ -1750,14 +1780,18 @@ export class Tinyc {
|
|
|
1750
1780
|
this.fs.addFileSync(p, bytes);
|
|
1751
1781
|
return this.fs.readFile(p);
|
|
1752
1782
|
} catch {
|
|
1753
|
-
// fall through to the
|
|
1783
|
+
// not in the module FS: fall through to the virtual FS copy
|
|
1754
1784
|
}
|
|
1755
1785
|
}
|
|
1756
|
-
if (
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1786
|
+
if (this.fs.hasFile(p)) {
|
|
1787
|
+
try {
|
|
1788
|
+
return this.fs.readFile(p);
|
|
1789
|
+
} catch {
|
|
1790
|
+
throw new TinycError(
|
|
1791
|
+
`tinyc: ${p} is persisted in the virtual FS store but is not in ` +
|
|
1792
|
+
`the in-memory cache; read it with exportFileAsync() instead`
|
|
1793
|
+
);
|
|
1794
|
+
}
|
|
1761
1795
|
}
|
|
1762
1796
|
throw new TinycError(`tinyc: no such file in virtual FS: ${p}`);
|
|
1763
1797
|
}
|
|
@@ -1766,7 +1800,6 @@ export class Tinyc {
|
|
|
1766
1800
|
* when it is not in the content cache. */
|
|
1767
1801
|
async exportFileAsync(path) {
|
|
1768
1802
|
const p = normalizeVfsPath(path);
|
|
1769
|
-
if (this.fs.hasFile(p)) return this.fs.readFileAsync(p);
|
|
1770
1803
|
const FS = this._bridge.native?.FS;
|
|
1771
1804
|
if (FS && typeof FS.readFile === 'function') {
|
|
1772
1805
|
try {
|
|
@@ -1774,9 +1807,10 @@ export class Tinyc {
|
|
|
1774
1807
|
this.fs.addFileSync(p, bytes);
|
|
1775
1808
|
return bytes;
|
|
1776
1809
|
} catch {
|
|
1777
|
-
// fall through to the
|
|
1810
|
+
// not in the module FS: fall through to the virtual FS copy
|
|
1778
1811
|
}
|
|
1779
1812
|
}
|
|
1813
|
+
if (this.fs.hasFile(p)) return this.fs.readFileAsync(p);
|
|
1780
1814
|
throw new TinycError(`tinyc: no such file in virtual FS: ${p}`);
|
|
1781
1815
|
}
|
|
1782
1816
|
|
|
@@ -1943,6 +1977,19 @@ export class Tinyc {
|
|
|
1943
1977
|
const state = new TinycState(this, handle ?? {});
|
|
1944
1978
|
this._states.add(state);
|
|
1945
1979
|
state._installErrorFunc();
|
|
1980
|
+
if (isRunnableTarget(this.target)) {
|
|
1981
|
+
// The wasm target is freestanding: no crt1.o/crti.o exist for
|
|
1982
|
+
// it anywhere, and this package's run model enters the product
|
|
1983
|
+
// through a named export, never through a C runtime startup.
|
|
1984
|
+
// Without -nostdlib, tcc_set_output_type for any non-MEMORY
|
|
1985
|
+
// type fails inside tccelf_add_crtbegin ('crt1.o not found')
|
|
1986
|
+
// and poisons the state's error count, which also makes the
|
|
1987
|
+
// next compile report failure. The backend's own test suite
|
|
1988
|
+
// drives the target with -nostdlib throughout; mirror that as
|
|
1989
|
+
// the state default here. Other targets keep the hosted
|
|
1990
|
+
// default: their EXE output does link the C runtime.
|
|
1991
|
+
state.setOptions('-nostdlib');
|
|
1992
|
+
}
|
|
1946
1993
|
if (this._bridge.has('tcc_add_include_path')) {
|
|
1947
1994
|
for (const dir of this.fs.getIncludeDirs()) state.addIncludePath(dir);
|
|
1948
1995
|
}
|
|
@@ -2111,11 +2158,59 @@ async function loadSysroot(instance, sysrootOpt) {
|
|
|
2111
2158
|
// backend in place; loadTinyc succeeds either way. The outcome is
|
|
2112
2159
|
// memoized per native module (repeat loads of a cached module do
|
|
2113
2160
|
// not mount twice) and reported as instance.fsBackend.
|
|
2161
|
+
//
|
|
2162
|
+
// A successful mount alone does not prove the backend works: in a
|
|
2163
|
+
// build without pthreads the backend's file operations are async
|
|
2164
|
+
// JS, and without Asyncify/JSPI to suspend into, the first real
|
|
2165
|
+
// operation traps — after wasmfs_create_directory has already
|
|
2166
|
+
// reported success. The mount is therefore followed by a
|
|
2167
|
+
// write/read probe through the same JS FS operations the
|
|
2168
|
+
// virtual-FS mirror uses (probeOpfsBackend below); only a probe
|
|
2169
|
+
// whose bytes read back intact lets the instance report 'opfs'.
|
|
2170
|
+
// A failed probe leaves the mount point in place but unused: the
|
|
2171
|
+
// instance reports 'memory' and no path is ever rooted under it.
|
|
2114
2172
|
// ---------------------------------------------------------------------
|
|
2115
2173
|
|
|
2116
2174
|
const OPFS_MOUNT_POINT = '/opfs';
|
|
2117
2175
|
const opfsBackendByModule = new WeakMap();
|
|
2118
2176
|
|
|
2177
|
+
const OPFS_PROBE_DIR = OPFS_MOUNT_POINT + '/.tinyc-probe';
|
|
2178
|
+
const OPFS_PROBE_FILE = OPFS_PROBE_DIR + '/probe';
|
|
2179
|
+
const OPFS_PROBE_BYTES = new Uint8Array([0x74, 0x69, 0x6e, 0x79, 0x63]); // "tinyc"
|
|
2180
|
+
|
|
2181
|
+
/** First-use probe of a freshly mounted OPFS backend: create a
|
|
2182
|
+
* directory, write a small file into it and read the bytes back,
|
|
2183
|
+
* through the JS FS API the virtual-FS mirror drives. Returns
|
|
2184
|
+
* true only when every step succeeds and the bytes match. Any
|
|
2185
|
+
* throw — the trap a non-suspending async backend produces on
|
|
2186
|
+
* first use is a catchable RuntimeError at this boundary — and
|
|
2187
|
+
* any mismatch means the backend cannot serve the working set.
|
|
2188
|
+
* The probe directory is removed again on every path, so a
|
|
2189
|
+
* working backend keeps no trace of it. */
|
|
2190
|
+
function probeOpfsBackend(FS) {
|
|
2191
|
+
if (!FS || typeof FS.writeFile !== 'function' ||
|
|
2192
|
+
typeof FS.readFile !== 'function') {
|
|
2193
|
+
return false;
|
|
2194
|
+
}
|
|
2195
|
+
try {
|
|
2196
|
+
if (typeof FS.mkdirTree === 'function') FS.mkdirTree(OPFS_PROBE_DIR);
|
|
2197
|
+
FS.writeFile(OPFS_PROBE_FILE, OPFS_PROBE_BYTES);
|
|
2198
|
+
const back = FS.readFile(OPFS_PROBE_FILE);
|
|
2199
|
+
if (!back || back.length !== OPFS_PROBE_BYTES.length) return false;
|
|
2200
|
+
for (let i = 0; i < OPFS_PROBE_BYTES.length; i++) {
|
|
2201
|
+
if (back[i] !== OPFS_PROBE_BYTES[i]) return false;
|
|
2202
|
+
}
|
|
2203
|
+
return true;
|
|
2204
|
+
} catch {
|
|
2205
|
+
return false;
|
|
2206
|
+
} finally {
|
|
2207
|
+
try { FS.unlink(OPFS_PROBE_FILE); } catch { /* not there */ }
|
|
2208
|
+
if (typeof FS.rmdir === 'function') {
|
|
2209
|
+
try { FS.rmdir(OPFS_PROBE_DIR); } catch { /* not there */ }
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
|
|
2119
2214
|
async function tryMountOpfsBackend(native, bridge) {
|
|
2120
2215
|
const canMemo =
|
|
2121
2216
|
native !== null &&
|
|
@@ -2146,8 +2241,9 @@ async function tryMountOpfsBackend(native, bridge) {
|
|
|
2146
2241
|
{ async: true }
|
|
2147
2242
|
);
|
|
2148
2243
|
// wasmfs_create_directory returns 0 on success and a
|
|
2149
|
-
// negative errno on failure, like mkdir.
|
|
2150
|
-
|
|
2244
|
+
// negative errno on failure, like mkdir. A mounted backend
|
|
2245
|
+
// still has to survive the probe before it is reported.
|
|
2246
|
+
if (rc === 0 && probeOpfsBackend(native.FS)) result = 'opfs';
|
|
2151
2247
|
}
|
|
2152
2248
|
}
|
|
2153
2249
|
} catch {
|
package/package.json
CHANGED
|
@@ -1,44 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"browser": "./index.mjs",
|
|
3
|
-
"description": "TinyC (TCC) in the browser: the compiler built to WebAssembly, one module per code generation target (wasm32-wasi by default), with a JS glue layer, a virtual filesystem and a Web Worker interface.",
|
|
4
|
-
"exports": {
|
|
5
|
-
".": "./index.mjs",
|
|
6
|
-
"./package.json": "./package.json",
|
|
7
|
-
"./sysroot-manifest": "./sysroot-manifest.json",
|
|
8
|
-
"./sysroot-manifests/*": "./sysroot-manifests/*",
|
|
9
|
-
"./worker": "./worker.mjs"
|
|
10
|
-
},
|
|
11
|
-
"files": [
|
|
12
|
-
"README.md",
|
|
13
|
-
"README.zh-CN.md",
|
|
14
|
-
"index.mjs",
|
|
15
|
-
"vfs-store.mjs",
|
|
16
|
-
"tinyc-web.js",
|
|
17
|
-
"worker.mjs",
|
|
18
|
-
"sysroot-manifest.json",
|
|
19
|
-
"sysroot-manifests",
|
|
20
|
-
"tcc-*.mjs",
|
|
21
|
-
"tcc-*.wasm"
|
|
22
|
-
],
|
|
23
|
-
"keywords": [
|
|
24
|
-
"tcc",
|
|
25
|
-
"tinycc",
|
|
26
|
-
"compiler",
|
|
27
|
-
"wasm",
|
|
28
|
-
"webassembly",
|
|
29
|
-
"browser",
|
|
30
|
-
"cross-compiler"
|
|
31
|
-
],
|
|
32
|
-
"license": "LGPL-2.1-or-later",
|
|
33
|
-
"main": "./index.mjs",
|
|
34
|
-
"module": "./index.mjs",
|
|
35
|
-
"name": "tinyc-web",
|
|
36
|
-
"scripts": {
|
|
37
|
-
"build": "./build.sh",
|
|
38
|
-
"build:web": "node build-web-bundle.mjs",
|
|
39
|
-
"test": "node --test test.mjs",
|
|
40
|
-
"test:web": "node --test test-web-bundle.mjs"
|
|
41
|
-
},
|
|
42
|
-
"type": "module",
|
|
43
|
-
"version": "0.1.2"
|
|
44
|
-
}
|
|
1
|
+
{"browser":"./index.mjs","description":"TinyC (TCC) in the browser: the compiler built to WebAssembly, one module per code generation target (wasm32-wasi by default), with a JS glue layer, a virtual filesystem and a Web Worker interface.","exports":{".":"./index.mjs","./package.json":"./package.json","./sysroot-manifest":"./sysroot-manifest.json","./sysroot-manifests/*":"./sysroot-manifests/*","./worker":"./worker.mjs"},"files":["README.md","README.zh-CN.md","index.mjs","runtime-shims.mjs","vfs-store.mjs","tinyc-web.js","worker.mjs","sysroot-manifest.json","sysroot-manifests","tcc-*.mjs","tcc-*.wasm"],"keywords":["tcc","tinycc","compiler","wasm","webassembly","browser","cross-compiler"],"license":"LGPL-2.1-or-later","main":"./index.mjs","module":"./index.mjs","name":"tinyc-web","scripts":{"build":"./build.sh","build:web":"node build-web-bundle.mjs","test":"node --test test.mjs","test:web":"node --test test-web-bundle.mjs"},"type":"module","version":"0.2.0"}
|