Skip to content

Commit

Permalink
Merge pull request #105 from Raiden1411/human_v2
Browse files Browse the repository at this point in the history
human_readable: update parser usage and evm interpreter bug fixes
  • Loading branch information
Raiden1411 authored Oct 7, 2024
2 parents 79c77aa + 6f29e60 commit 72edbf5
Show file tree
Hide file tree
Showing 34 changed files with 4,286 additions and 1,308 deletions.
40 changes: 24 additions & 16 deletions bench/benchmark.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ pub fn main() !void {

var count: usize = 0;
while (count < opts.warmup_runs) : (count += 1) {
const abi = try zabi_root.human_readable.parsing.parseHumanReadable(zabi_root.abi.abitypes.Abi, allocator, constants.slice);
const abi = try zabi_root.human_readable.parsing.parseHumanReadable(allocator, constants.slice);
defer abi.deinit();
}

var timer = try std.time.Timer.start();
while (count < opts.runs) : (count += 1) {
const abi = try zabi_root.human_readable.parsing.parseHumanReadable(zabi_root.abi.abitypes.Abi, allocator, constants.slice);
const abi = try zabi_root.human_readable.parsing.parseHumanReadable(allocator, constants.slice);
defer abi.deinit();
}

Expand Down Expand Up @@ -327,16 +327,20 @@ pub fn decodingFunctions(allocator: Allocator, printer: *ColorWriter(@TypeOf(std

try printer.writer().writeAll("Abi Logs Decoding... ");
{
const event = try parseHumanReadable(
Event,
allocator,
"event Foo(uint indexed a, int indexed b, bool indexed c, bytes5 indexed d)",
);
defer event.deinit();
const event: Event = .{
.type = .event,
.name = "Foo",
.inputs = &.{
.{ .type = .{ .uint = 256 }, .indexed = true, .name = "a" },
.{ .type = .{ .int = 256 }, .indexed = true, .name = "b" },
.{ .type = .{ .bool = {} }, .indexed = true, .name = "c" },
.{ .type = .{ .fixedBytes = 5 }, .indexed = true, .name = "d" },
},
};

const encoded = try encodeLogTopics(
allocator,
event.value,
event,
.{ 69, -420, true, "01234" },
);
defer allocator.free(encoded);
Expand Down Expand Up @@ -408,16 +412,20 @@ pub fn encodingFunctions(allocator: Allocator, printer: *ColorWriter(@TypeOf(std

try printer.writer().writeAll("ABI Logs Encoding... ");
{
const event = try parseHumanReadable(
Event,
allocator,
"event Foo(uint indexed a, int indexed b, bool indexed c, bytes5 indexed d)",
);
defer event.deinit();
const event: Event = .{
.type = .event,
.name = "Foo",
.inputs = &.{
.{ .type = .{ .uint = 256 }, .indexed = true, .name = "a" },
.{ .type = .{ .int = 256 }, .indexed = true, .name = "b" },
.{ .type = .{ .bool = {} }, .indexed = true, .name = "c" },
.{ .type = .{ .fixedBytes = 5 }, .indexed = true, .name = "d" },
},
};

const result = try benchmark.benchmark(allocator, zabi_root.encoding.logs_encoding.encodeLogTopics, .{
allocator,
event.value,
event,
.{ 69, -420, true, "01234" },
}, .{ .warmup_runs = 5, .runs = 100 });
result.printSummary();
Expand Down
12 changes: 9 additions & 3 deletions build/docs_generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,16 @@ pub const DocsGenerator = struct {
.keyword_enum => try out_file.writeAll("enum {\n"),
.keyword_struct => try out_file.writeAll("struct {\n"),
.keyword_union => try out_file.writeAll("union(enum) {\n"),
else => std.debug.panic("Unexpected node token found: {s}", .{@tagName(self.tokens[container_token])}),
else => std.debug.panic("Unexpected token found: {s}", .{@tagName(self.tokens[container_token])}),
}

for (container.ast.members) |member| {
switch (self.nodes[member]) {
.container_field_init => try self.extractFromContainerField(out_file, member),
.fn_decl, .simple_var_decl => continue,
.fn_decl,
.simple_var_decl,
.@"comptime",
=> continue,
else => std.debug.panic("Unexpected node token found: {s}", .{@tagName(self.nodes[member])}),
}
}
Expand All @@ -217,7 +220,9 @@ pub const DocsGenerator = struct {
switch (self.nodes[member]) {
.fn_decl => try self.extractFromFnProto(member, out_file, duplicate),
.simple_var_decl => try self.extractFromSimpleVar(out_file, member, duplicate),
.container_field_init => continue,
.container_field_init,
.@"comptime",
=> continue,
else => std.debug.panic("Unexpected node token found: {s}", .{@tagName(self.nodes[member])}),
}
}
Expand Down Expand Up @@ -358,6 +363,7 @@ pub const DocsGenerator = struct {
.number_literal,
.sub,
.string_literal,
.@"comptime",
=> return,
else => std.debug.panic("Unexpected token found: {s}\n", .{@tagName(self.nodes[variable.ast.init_node])}),
}
Expand Down
16 changes: 16 additions & 0 deletions docs/pages/api/abi/param_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ union(enum) {
}
```

### FromHumanReadableTokenTag
Converts a human readable token into `ParamType`.

### Signature

```zig
pub fn fromHumanReadableTokenTag(tag: TokenTags) ?ParamType
```

### FreeArrayParamType
User must call this if the union type contains a fixedArray or dynamicArray field.
They create pointers so they must be destroyed after.
Expand Down Expand Up @@ -82,3 +91,10 @@ or call the destroy method on your allocator manually
pub fn typeToUnion(abitype: []const u8, alloc: Allocator) ParamErrors!ParamType
```

### TypeToUnionWithTag
### Signature

```zig
pub fn typeToUnionWithTag(allocator: Allocator, abitype: []const u8, token_tag: TokenTags) ParamErrors!ParamType
```

Loading

0 comments on commit 72edbf5

Please sign in to comment.