Skip to content

Commit

Permalink
refactor(leb128): Zig v0.11-dev syntax & build updates
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 28, 2023
1 parent 4a4359d commit cae6541
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
35 changes: 35 additions & 0 deletions packages/leb128/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const lib = b.addSharedLibrary(.{
.name = "leb128",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "zig/leb128.zig" },
.target = .{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
},
.optimize = .ReleaseSmall,
});
// Add all symbols to the dynamic symbol table
lib.rdynamic = true;

b.installArtifact(lib);

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "zig/leb128.zig" },
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});

const run_main_tests = b.addRunArtifact(main_tests);

// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
}
10 changes: 6 additions & 4 deletions packages/leb128/tools/build-binary.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/bin/sh
zig build-lib -target wasm32-freestanding -dynamic -O ReleaseSmall zig/leb128.zig
BUILD=zig-out/lib

zig build

# apply binaryen optimizer
wasm-opt leb128.wasm -o opt.wasm -Oz
wasm-opt $BUILD/leb128.wasm -o $BUILD/opt.wasm -Oz

# display as .wast text format
wasm-dis opt.wasm -o opt.wast
wasm-dis $BUILD/opt.wasm -o $BUILD/opt.wast

# encode as base64
cat << EOF > src/binary.ts
Expand All @@ -15,5 +17,5 @@ cat << EOF > src/binary.ts
*
* @internal
*/
export const BINARY = "$(base64 opt.wasm)";
export const BINARY = "$(base64 -i $BUILD/opt.wasm)";
EOF
24 changes: 12 additions & 12 deletions packages/leb128/zig/leb128.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export var buf: [10]u8 = undefined;
/// have at least 10 bytes capacity)
pub fn leb128EncodeU(src: u64, dest: []u8) u8 {
if (src < 0x80) {
dest[0] = @intCast(u8, src & 0x7f);
dest[0] = @intCast(src & 0x7f);
return 1;
}
var n: u8 = 0;
var x: u64 = src;
while (true) {
var byte: u8 = @intCast(u8, x & 0x7f);
var byte: u8 = @intCast(x & 0x7f);
x = x >> 7;
if (x != 0) {
byte |= 0x80;
Expand All @@ -30,7 +30,7 @@ pub fn leb128DecodeU(src: []u8, num: *u8) u64 {
var n: u8 = 0;
while (n < 10) {
var byte = src[n];
res |= @intCast(u64, byte & 0x7f) << shift;
res |= @as(u64, @intCast(byte & 0x7f)) << shift;
shift += 7;
n += 1;
if (byte < 0x80) {
Expand All @@ -45,15 +45,15 @@ pub fn leb128DecodeU(src: []u8, num: *u8) u64 {
pub fn leb128EncodeI(src: i64, dest: []u8) u8 {
const neg: bool = src < 0;
if (src >= -64 and src < 64) {
dest[0] = @intCast(u8, src & 0x3f) |
@intCast(u8, @boolToInt(neg)) << 6;
dest[0] = @as(u8, @intCast(src & 0x3f)) |
@as(u8, @intCast(@intFromBool(neg))) << 6;
return 1;
}
var n: u8 = 0;
var x: i64 = src;
var more: bool = true;
while (more) {
var byte: u8 = @intCast(u8, x & 0x7f);
var byte: u8 = @intCast(x & 0x7f);
var sign: bool = (byte & 0x40) > 0;
x >>= 7;
if ((x == 0 and !sign) or (x == -1 and sign)) {
Expand All @@ -75,15 +75,15 @@ pub fn leb128DecodeI(src: []u8, num: *u8) i64 {
var byte: u8 = 0;
while (true) {
byte = src[n];
res |= @intCast(i64, byte & 0x7f) << shift;
res |= @as(i64, @intCast(byte & 0x7f)) << shift;
shift += 7;
n += 1;
if ((byte & 0x80) == 0 or n > 9) {
break;
}
}
if (n < 10 and (byte & 0x40) > 0) {
res |= @intCast(i64, -1) << shift;
res |= @as(i64, @intCast(-1)) << shift;
}
num.* = n;
return res;
Expand All @@ -92,24 +92,24 @@ pub fn leb128DecodeI(src: []u8, num: *u8) i64 {
/// WASM only. JS interop to exchange data via f64 (for lack of i64/u64
/// on JS side). Writes results to exported `buf` array and returns
/// number of bytes used.
export fn leb128EncodeU64(x: u64) u8 {
pub export fn leb128EncodeU64(x: u64) u8 {
return leb128EncodeU(x, buf[0..]);
}

/// WASM only. JS interop to exchange data via f64 (for lack of i64/u64
/// on JS side). Consumes bytes from the exported `buf` array and returns
/// decoded value. Writes number of bytes consumed into `buf[0]`
export fn leb128DecodeU64() u64 {
pub export fn leb128DecodeU64() u64 {
return leb128DecodeU(buf[0..], &buf[0]);
}

/// See `leb128_encode_u_js`
export fn leb128EncodeI64(x: i64) u8 {
pub export fn leb128EncodeI64(x: i64) u8 {
return leb128EncodeI(x, buf[0..]);
}

/// See `leb128_decode_u_js`
export fn leb128DecodeI64() i64 {
pub export fn leb128DecodeI64() i64 {
return leb128DecodeI(buf[0..], &buf[0]);
}

Expand Down

0 comments on commit cae6541

Please sign in to comment.