Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is a little bit revamped version of your code and it's adapted to zig v0.13 #9

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.idea/
.zig-cache/
zig-out/

node_modules/

*.o
zig-cache/zigdom.h
zigdom.o
Expand Down
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

An example demonstrating Zig interacting with the DOM via JS.

Compile with `zig build-lib zigdom.zig -target wasm32-freestanding -dynamic -OReleaseSmall`
Forked from https://github.com/shritesh/zig-wasm-dom

Compile with `zig build`
30 changes: 30 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const std = @import("std");

const number_of_pages = 1;
const max_number_of_pages = 2;

pub fn build(b: *std.Build) void {
const target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});

const exe = b.addExecutable(.{
.name = "zigdom",
.root_source_file = b.path("src/zigdom.zig"),
.target = target,
.optimize = .ReleaseSmall,
});

// <https://github.com/ziglang/zig/issues/8633>
exe.global_base = 0;
exe.entry = .disabled;
exe.rdynamic = true;
// exe.import_memory = true;
exe.stack_size = std.wasm.page_size / 2;

exe.initial_memory = std.wasm.page_size * number_of_pages;
exe.max_memory = std.wasm.page_size * max_number_of_pages;

b.installArtifact(exe);
}
6 changes: 0 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@

<body>
<script src="zigdom.js"></script>
<script>
fetch('zigdom.wasm')
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, zigdom.imports))
.then(zigdom.launch);
</script>
</body>

</html>
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "zig-wasm-dom",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"prettier": "^3.3.3"
}
}
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 15 additions & 12 deletions zigdom.zig → src/zigdom.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// https://github.com/shritesh/zig-wasm-dom
extern "document" fn query_selector(selector_ptr: [*]const u8, selector_len: usize) usize;
extern "document" fn create_element(tag_name_ptr: [*]const u8, tag_name_len: usize) usize;
extern "document" fn create_text_node(data_ptr: [*]const u8, data_len: usize) usize;
extern "element" fn set_attribute(element_id: usize, name_ptr: [*]const u8, name_len: usize, value_ptr: [*]const u8, value_len: usize) void;
extern "element" fn get_attribute(element_id: usize, name_ptr: [*]const u8, name_len: usize, value_ptr: *[*]u8, value_len: *usize) bool;
extern "event_target" fn add_event_listener(event_target_id: usize, event_ptr: [*]const u8, event_len: usize, event_id: usize) void;
extern "window" fn alert(msg_ptr: [*]const u8, msg_len: usize) void;
const string = [*]const u8;

extern "document" fn query_selector(selector_ptr: string, selector_len: usize) usize;
extern "document" fn create_element(tag_name_ptr: string, tag_name_len: usize) usize;
extern "document" fn create_text_node(data_ptr: string, data_len: usize) usize;
extern "element" fn set_attribute(element_id: usize, name_ptr: string, name_len: usize, value_ptr: string, value_len: usize) void;
extern "element" fn get_attribute(element_id: usize, name_ptr: string, name_len: usize, value_ptr: *[*]u8, value_len: *usize) bool;
extern "event_target" fn add_event_listener(event_target_id: usize, event_ptr: string, event_len: usize, event_id: usize) void;
extern "window" fn alert(msg_ptr: string, msg_len: usize) void;
extern "node" fn append_child(node_id: usize, child_id: usize) usize;
extern "zig" fn release_object(object_id: usize) void;

Expand Down Expand Up @@ -112,11 +114,12 @@ fn launch() !void {
}

fn attach_listener(node: usize, event_name: []const u8, event_id: eventId) void {
add_event_listener(node, event_name.ptr, event_name.len, @enumToInt(event_id));
add_event_listener(node, event_name.ptr, event_name.len, @intFromEnum(event_id));
}

export fn dispatchEvent(id: u32) void {
switch (@intToEnum(eventId, id)) {
export fn dispatch_event(id: u32) void {
const e: eventId = @enumFromInt(id);
switch (e) {
eventId.Submit => on_submit_event(),
eventId.Clear => on_clear_event(),
}
Expand Down Expand Up @@ -151,8 +154,8 @@ export fn launch_export() bool {
}

export fn _wasm_alloc(len: usize) u32 {
var buf = std.heap.page_allocator.alloc(u8, len) catch {
const buf = std.heap.page_allocator.alloc(u8, len) catch {
return 0;
};
return @ptrToInt(buf.ptr);
return @intFromPtr(buf.ptr);
}
Binary file added zig-out/bin/zigdom.wasm
Binary file not shown.
Loading