@yowasp/clang 21.1.4-1 → 22.0.0-git20542-10
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 +37 -1
- package/gen/bundle.js +32 -7
- package/gen/llvm-resources.tar +0 -0
- package/gen/llvm.core.wasm +0 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,13 +3,49 @@ YoWASP Clang/LLD package
|
|
|
3
3
|
|
|
4
4
|
This package provides a complete [Clang/LLD][] toolchain built for [WebAssembly][] and targeting WebAssembly as well. See the [overview of the YoWASP project][yowasp] for details.
|
|
5
5
|
|
|
6
|
-
At the moment, this package only offers an API allowing to run Clang and LLD in a virtual filesystem; no executables are provided.
|
|
6
|
+
At the moment, this package only offers an API allowing to run Clang and LLD in a virtual filesystem; no executables are provided. Note that if you are importing `.../gen/bundle.js` directly, you must use it as a module.
|
|
7
7
|
|
|
8
8
|
[Clang/LLD]: https://llvm.org/
|
|
9
9
|
[WebAssembly]: https://webassembly.org/
|
|
10
10
|
[yowasp]: https://yowasp.github.io/
|
|
11
11
|
|
|
12
12
|
|
|
13
|
+
Examples
|
|
14
|
+
--------
|
|
15
|
+
|
|
16
|
+
All examples below are written for Node.js; to run them, install `@yowasp/clang` first. The C/C++ code can be compiled equally well in the browser and other runtimes, but WASI is unevenly supported.
|
|
17
|
+
|
|
18
|
+
### Hosted C++ executable
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { runClang } from '@yowasp/clang';
|
|
22
|
+
const { meow } = await runClang(['clang++', 'test.cc', '-o', 'meow'],
|
|
23
|
+
{"test.cc": `#include <iostream>\nint main() { std::cout << "meow++" << std::endl; }`});
|
|
24
|
+
|
|
25
|
+
import { WASI } from 'node:wasi';
|
|
26
|
+
const wasi = new WASI({ version: 'preview1' });
|
|
27
|
+
const module = await WebAssembly.compile(meow);
|
|
28
|
+
const instance = await WebAssembly.instantiate(module,
|
|
29
|
+
{wasi_snapshot_preview1: wasi.wasiImport});
|
|
30
|
+
wasi.start(instance);
|
|
31
|
+
// prints "meow++"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Freestanding C library
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import { runClang } from '@yowasp/clang';
|
|
39
|
+
const { 'a.out': wasm } = await runClang(['clang', '-nostdlib', '-Wl,--no-entry', 'test.c'], {
|
|
40
|
+
'test.c': 'int add(int a, int b) __attribute__((export_name("add"))) { return a + b; }'});
|
|
41
|
+
|
|
42
|
+
const module = await WebAssembly.compile(wasm);
|
|
43
|
+
const instance = await WebAssembly.instantiate(module);
|
|
44
|
+
console.log('add(1, 2) =', instance.exports.add(1, 2));
|
|
45
|
+
// prints "add(1, 2) = 3"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
|
|
13
49
|
API reference
|
|
14
50
|
-------------
|
|
15
51
|
|
package/gen/bundle.js
CHANGED
|
@@ -599,7 +599,7 @@ function parseTar(data, opts) {
|
|
|
599
599
|
const files = [];
|
|
600
600
|
let offset = 0;
|
|
601
601
|
while (offset < buffer.byteLength - 512) {
|
|
602
|
-
|
|
602
|
+
let name = _readString(buffer, offset, 100);
|
|
603
603
|
if (name.length === 0) {
|
|
604
604
|
break;
|
|
605
605
|
}
|
|
@@ -613,6 +613,7 @@ function parseTar(data, opts) {
|
|
|
613
613
|
const type = _type === TAR_TYPE_FILE ? "file" : _type === TAR_TYPE_DIR ? "directory" : _type;
|
|
614
614
|
const user = _readString(buffer, offset + 265, 32);
|
|
615
615
|
const group = _readString(buffer, offset + 297, 32);
|
|
616
|
+
name = _sanitizePath(name);
|
|
616
617
|
const meta = {
|
|
617
618
|
name,
|
|
618
619
|
type,
|
|
@@ -647,6 +648,29 @@ function parseTar(data, opts) {
|
|
|
647
648
|
}
|
|
648
649
|
return files;
|
|
649
650
|
}
|
|
651
|
+
function _sanitizePath(path) {
|
|
652
|
+
let normalized = path.replace(/\\/g, "/");
|
|
653
|
+
normalized = normalized.replace(/^[a-zA-Z]:\//, "");
|
|
654
|
+
normalized = normalized.replace(/^\/+/, "");
|
|
655
|
+
const hasLeadingDotSlash = normalized.startsWith("./");
|
|
656
|
+
const parts = normalized.split("/");
|
|
657
|
+
const resolved = [];
|
|
658
|
+
for (const part of parts) {
|
|
659
|
+
if (part === "..") {
|
|
660
|
+
resolved.pop();
|
|
661
|
+
} else if (part !== "." && part !== "") {
|
|
662
|
+
resolved.push(part);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
let result = resolved.join("/");
|
|
666
|
+
if (hasLeadingDotSlash && !result.startsWith("./")) {
|
|
667
|
+
result = "./" + result;
|
|
668
|
+
}
|
|
669
|
+
if (path.endsWith("/") && !result.endsWith("/")) {
|
|
670
|
+
result += "/";
|
|
671
|
+
}
|
|
672
|
+
return result;
|
|
673
|
+
}
|
|
650
674
|
function _readString(buffer, offset, size) {
|
|
651
675
|
const view = new Uint8Array(buffer, offset, size);
|
|
652
676
|
const i = view.indexOf(0);
|
|
@@ -696,7 +720,7 @@ var modules = async (fetch3) => ({
|
|
|
696
720
|
var filesystem = async (fetch3) => ({
|
|
697
721
|
"usr": await fetch3(new URL("./llvm-resources.tar", import.meta.url)).then((resp) => resp.arrayBuffer()).then(unpackTarFilesystem)
|
|
698
722
|
});
|
|
699
|
-
var totalSize =
|
|
723
|
+
var totalSize = 105241285;
|
|
700
724
|
|
|
701
725
|
// gen/llvm.js
|
|
702
726
|
function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.instantiate) {
|
|
@@ -7877,14 +7901,14 @@ function instantiate(getCoreModule, imports, instantiateCore = WebAssembly.insta
|
|
|
7877
7901
|
var llvm = new Application(llvm_resources_exports, instantiate, "yowasp-llvm");
|
|
7878
7902
|
var runLLVM = llvm.run.bind(llvm);
|
|
7879
7903
|
function subcommand(command, subcommandName) {
|
|
7880
|
-
return function(args, files, options) {
|
|
7881
|
-
if (args ===
|
|
7904
|
+
return function(args = null, files = {}, options = {}) {
|
|
7905
|
+
if (args === null)
|
|
7882
7906
|
return command(args, files, options);
|
|
7883
7907
|
return command([subcommandName, ...args], files, options);
|
|
7884
7908
|
};
|
|
7885
7909
|
}
|
|
7886
|
-
function runClang(args, files, options = {}) {
|
|
7887
|
-
if (args ===
|
|
7910
|
+
function runClang(args = null, files = {}, options = {}) {
|
|
7911
|
+
if (args === null)
|
|
7888
7912
|
return runLLVM(args, files, options);
|
|
7889
7913
|
if (args.includes("-###"))
|
|
7890
7914
|
return runLLVM(args, files, options);
|
|
@@ -8034,8 +8058,9 @@ var commands = {
|
|
|
8034
8058
|
"clang": subcommand(runClang, "clang"),
|
|
8035
8059
|
"clang++": subcommand(runClang, "clang++")
|
|
8036
8060
|
};
|
|
8037
|
-
var version = "
|
|
8061
|
+
var version = "22.0.0-git20542-10";
|
|
8038
8062
|
export {
|
|
8063
|
+
Exit,
|
|
8039
8064
|
commands,
|
|
8040
8065
|
runClang,
|
|
8041
8066
|
runLLVM,
|
package/gen/llvm-resources.tar
CHANGED
|
Binary file
|
package/gen/llvm.core.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yowasp/clang",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "22.0.0-git20542-10",
|
|
4
4
|
"description": "LLVM/Clang/LLD toolchain targeting WebAssembly",
|
|
5
5
|
"author": "Catherine <whitequark@whitequark.org>",
|
|
6
6
|
"license": "ISC",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"scripts": {
|
|
33
33
|
"transpile": "jco new ../llvm-build/bin/llvm --wasi-command --output llvm.wasm && jco transpile llvm.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/",
|
|
34
34
|
"pack": "yowasp-pack-resources gen/llvm-resources.js gen ../wasi-prefix/usr usr",
|
|
35
|
-
"build": "esbuild --bundle lib/api.js --outfile=gen/bundle.js --format=esm --platform=node --define:VERSION=\\\"
|
|
35
|
+
"build": "esbuild --bundle lib/api.js --outfile=gen/bundle.js --format=esm --platform=node --define:VERSION=\\\"22.0.0-git20542-10\\\"",
|
|
36
36
|
"all": "npm run transpile && npm run pack && npm run build"
|
|
37
37
|
}
|
|
38
38
|
}
|