1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
const builtin = @import("builtin");
const std = @import("std");
const z = @import("z");
pub const panic = z.panic;
pub const std_options: std.Options = .{
.logFn = z.log.default,
};
pub const app: z.App(.{
.name = "Memora",
.globals = &.{
z.Object.global(.window),
z.Object.global(.Object),
},
}) = .init;
fn run_wrapped() !void {
var env: z.Env = .init(std.heap.wasm_allocator);
defer env.deinit();
// Set and delete properties
_ = app.globals.window.set(&env, "my_prop", 45);
_ = app.globals.window.delete(&env, "my_prop");
// Get properties
const console = try app.globals.window.get(&env, "console").await(&env, .object);
const location = try app.globals.window.get(&env, "location").await(&env, .object);
// Create new object
const obj = app.globals.Object.new(&env);
// Get string, needs to be freed.
const hostname = try location.get(&env, "hostname").await(&env, .string);
defer std.heap.wasm_allocator.free(hostname);
// Call method of an object
try console.call(
&env,
.log,
.{
"hello, wasm!",
try obj.await(&env),
hostname,
},
).await(&env, .undefined);
// Unlink Object from global store.
_ = env.free(console);
}
fn run() callconv(.c) void {
run_wrapped() catch |e| {
std.log.err("{}", .{e});
};
}
comptime {
if (builtin.target.cpu.arch.isWasm()) {
@export(&run, .{ .name = "run" });
}
}
|