Skip to content

Commit 5b5706b

Browse files
authored
Merge pull request #145 from Raiden1411/fmt_part2
ast: extend support for the solidity formatter
2 parents 58da61c + 1e90e49 commit 5b5706b

File tree

13 files changed

+802
-390
lines changed

13 files changed

+802
-390
lines changed

docs/package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "docs",
3-
"version": "0.0.0",
4-
"type": "module",
5-
"scripts": {
6-
"dev": "vocs dev",
7-
"build": "vocs build",
8-
"preview": "vocs preview"
9-
},
10-
"dependencies": {
11-
"@types/react": "latest",
12-
"react": "latest",
13-
"react-dom": "latest",
14-
"typescript": "latest",
15-
"vocs": "latest"
16-
}
2+
"name": "docs",
3+
"version": "0.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vocs dev",
7+
"build": "vocs build",
8+
"preview": "vocs preview"
9+
},
10+
"dependencies": {
11+
"@types/react": "latest",
12+
"react": "latest",
13+
"react-dom": "latest",
14+
"typescript": "latest",
15+
"vocs": "latest"
16+
}
1717
}

examples/wasm/index_format.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Zig WASM Test</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<style type="text/css">
8+
body {
9+
font-family: system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
10+
color: #000000;
11+
}
12+
13+
.hidden {
14+
display: none;
15+
}
16+
17+
.run {
18+
display: flex;
19+
flex-direction: column;
20+
gap: 8px;
21+
}
22+
23+
.contract_code_area {
24+
display: flex;
25+
flex-direction: column;
26+
gap: 8px;
27+
}
28+
29+
label {
30+
font-size: 14px;
31+
font-weight: bold;
32+
}
33+
34+
textarea {
35+
width: 50%;
36+
padding: 10px;
37+
font-size: 16px;
38+
border: 1px solid #ccc;
39+
border-radius: 4px;
40+
resize: vertical;
41+
}
42+
43+
@media (prefers-color-scheme: dark) {
44+
body {
45+
background-color: #111;
46+
color: #bbb;
47+
}
48+
}
49+
</style>
50+
</head>
51+
<title>ZIG WASM Formatter</title>
52+
<body>
53+
<h1 class="interpreter">Interpreter</h1>
54+
<div class="contract_code_area">
55+
<label for="code_label">Source Code:</label>
56+
<textarea id="code" name="message" rows="8"></textarea>
57+
</div>
58+
<br>
59+
<button type="button" id="run">Run</button>
60+
<h2>Output:</h2>
61+
<textarea id="result" name="message" rows="8"></textarea>
62+
<script src="./main_format.js"></script>
63+
</body>
64+
</html>

examples/wasm/main_format.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
(function () {
2+
const code = document.getElementById("code");
3+
4+
const run = document.getElementById("run");
5+
run.onclick = formatCode;
6+
7+
let wasm_promise = fetch("./zabi_wasm.wasm");
8+
var wasm_exports = null;
9+
10+
const text_decoder = new TextDecoder();
11+
const text_encoder = new TextEncoder();
12+
13+
// Convenience function to prepare a typed byte array
14+
// from a pointer and a length into WASM memory.
15+
function getView(ptr, len) {
16+
return new Uint8Array(wasm_exports.memory.buffer, ptr, len);
17+
}
18+
19+
// JS strings are UTF-16 and have to be encoded into an
20+
// UTF-8 typed byte array in WASM memory.
21+
function encodeString(str) {
22+
const capacity = str.length * 2 + 5; // As per MDN
23+
const ptr = wasm_exports.malloc(capacity);
24+
const { written } = text_encoder.encodeInto(str, getView(ptr, capacity));
25+
return [ptr, written, capacity];
26+
}
27+
28+
// Decodes the string type produced from zig.
29+
function decodeString(ptr, len) {
30+
if (len === 0) return "";
31+
return text_decoder.decode(
32+
new Uint8Array(wasm_exports.memory.buffer, ptr, len),
33+
);
34+
}
35+
36+
// Unwraps the string type produced from zig.
37+
function unwrapString(bigint) {
38+
const ptr = Number(bigint & 0xffffffffn);
39+
const len = Number(bigint >> 32n);
40+
41+
return decodeString(ptr, len);
42+
}
43+
44+
function formatCode() {
45+
const source = code.value;
46+
const [ptr, len] = encodeString(source);
47+
48+
const result = wasm_exports.formatSolidity(ptr, len);
49+
50+
const str = unwrapString(result);
51+
document.getElementById("result").textContent = str;
52+
}
53+
54+
// Instantiate WASM module and run our test code.
55+
WebAssembly.instantiateStreaming(wasm_promise, {
56+
env: {
57+
// We export this function to WASM land.
58+
log: (ptr, len) => {
59+
const msg = decodeString(ptr, len);
60+
console.log(msg);
61+
},
62+
panic: function (ptr, len) {
63+
const msg = decodeString(ptr, len);
64+
throw new Error("panic: " + msg);
65+
},
66+
},
67+
}).then((wasm_binary) => {
68+
wasm_exports = wasm_binary.instance.exports;
69+
window.wasm = wasm_binary; // for debugging
70+
});
71+
})();

pkg/blst/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn buildBlst(b: *std.Build, upstream: *std.Build.Dependency, target: std.Build.R
2323
var flags = std.ArrayList([]const u8).init(b.allocator);
2424
defer flags.deinit();
2525

26-
if (!target.result.isDarwin()) {
26+
if (!target.result.isDarwinLibC()) {
2727
try flags.appendSlice(&.{"-D__BLST_PORTABLE__"});
2828
}
2929

src/ast/Ast.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ pub fn parse(
100100
.errors = try parser.errors.toOwnedSlice(allocator),
101101
};
102102
}
103+
/// Returns a slice of all of the root nodes of the AST
104+
pub fn rootDecls(self: Ast) []const Node.Index {
105+
std.debug.assert(self.nodes.len > 0);
106+
const root = self.nodes.items(.data)[0];
107+
108+
return self.extra_data[root.lhs..root.rhs];
109+
}
103110
/// Ast representation of a `array_type` node.
104111
pub fn arrayType(
105112
self: Ast,
@@ -660,7 +667,7 @@ pub fn importDeclPath(self: Ast, node: Node.Index) ast.ImportDecl {
660667
.ast = .{
661668
.symbols = null,
662669
},
663-
.name = if (data.rhs == 0) null else data.lhs,
670+
.name = if (data.rhs == 0) null else data.rhs,
664671
.path = data.lhs,
665672
.main_token = main,
666673
.from = null,

src/ast/Parser.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2840,7 +2840,10 @@ pub fn parsePragmaDirective(self: *Parser) ParserErrors!Node.Index {
28402840
return self.addNode(.{
28412841
.tag = .pragma_directive,
28422842
.main_token = pragma,
2843-
.data = .{ .lhs = start, .rhs = end },
2843+
.data = .{
2844+
.lhs = start,
2845+
.rhs = end,
2846+
},
28442847
});
28452848
}
28462849
/// Parses the pragma version. Breaks instantly if version is just the number literal.

0 commit comments

Comments
 (0)