aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app/root.zig42
-rw-r--r--src/z/App/main.js9
-rw-r--r--src/z/App/root.zig26
-rw-r--r--src/z/Env/root.zig7
-rw-r--r--src/z/Env/value.zig2
-rw-r--r--src/z/Object.zig (renamed from src/z/Env/Object.zig)8
-rw-r--r--src/z/log/js.zig (renamed from src/z/env/console.zig)0
-rw-r--r--src/z/log/root.zig (renamed from src/z/env/root.zig)18
-rw-r--r--src/z/root.zig33
9 files changed, 64 insertions, 81 deletions
diff --git a/src/app/root.zig b/src/app/root.zig
index dcba16f..6de9216 100644
--- a/src/app/root.zig
+++ b/src/app/root.zig
@@ -2,36 +2,40 @@ const builtin = @import("builtin");
const std = @import("std");
const z = @import("z");
-pub const panic = z.env.panic;
+pub const panic = z.panic;
pub const std_options: std.Options = .{
- .logFn = z.env.logFn,
+ .logFn = z.log.default,
};
-pub const app: z.App(.{ .name = "Memora" }, .{
- .window = *z.Env.Object,
- .Object = *z.Env.Object,
+pub const app: z.App(.{
+ .name = "Memora",
+ .globals = &.{
+ z.Object.global(.window),
+ z.Object.global(.Object),
+ },
}) = .init;
-const js = struct {
- const window = app.globals.window;
- const Object = app.globals.Object;
-};
-
fn run_wrapped() !void {
var env: z.Env = .init(std.heap.wasm_allocator);
defer env.deinit();
- _ = js.window.set(&env, "my_prop", 45);
- _ = js.window.delete(&env, "my_prop");
+ // 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);
- const call = console.call(
+ // Call method of an object
+ try console.call(
&env,
.log,
.{
@@ -39,21 +43,15 @@ fn run_wrapped() !void {
try obj.await(&env),
hostname,
},
- );
-
- try call.await(&env, .undefined);
+ ).await(&env, .undefined);
+ // Unlink Object from global store.
_ = env.free(console);
}
fn run() callconv(.c) void {
run_wrapped() catch |e| {
- var buffer: [1024]u8 = undefined;
- var writer: std.Io.Writer = .fixed(&buffer);
-
- writer.print("{}", .{e}) catch unreachable;
-
- z.env.console.log(writer.buffered());
+ std.log.err("{}", .{e});
};
}
diff --git a/src/z/App/main.js b/src/z/App/main.js
index 6251a47..a875c1d 100644
--- a/src/z/App/main.js
+++ b/src/z/App/main.js
@@ -20,18 +20,9 @@ const imports = {
WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then(
({ instance, module }) => {
- const is64 = new Uint8Array(
- instance.exports.memory.buffer,
- instance.exports.bit_length,
- 1,
- )[0] == 8
-
window.wasm = {
exports: instance.exports,
memory: instance.exports.memory,
- is64: is64,
- UsizeArray: is64 ? BigUint64Array : Uint32Array,
- IsizeArray: is64 ? BigInt64Array : Int32Array,
}
wasm.exports.run();
diff --git a/src/z/App/root.zig b/src/z/App/root.zig
index d684248..326f3c0 100644
--- a/src/z/App/root.zig
+++ b/src/z/App/root.zig
@@ -2,19 +2,25 @@ const builtin = @import("builtin");
const std = @import("std");
const h = @import("html");
-pub const Options = struct { name: []const u8 };
+pub const Options = struct {
+ name: []const u8,
+ globals: []const Global,
-pub fn App(comptime options: Options, globals: anytype) type {
- const info = @typeInfo(@TypeOf(globals)).@"struct";
+ pub const Global = struct {
+ name: []const u8,
+ type: type,
+ };
+};
+pub fn App(comptime options: Options) type {
if (builtin.target.cpu.arch.isWasm()) {
- var names: [info.fields.len][]const u8 = undefined;
- var types: [info.fields.len]type = undefined;
- var attrs: [info.fields.len]std.builtin.Type.StructField.Attributes = undefined;
+ var names: [options.globals.len][]const u8 = undefined;
+ var types: [options.globals.len]type = undefined;
+ var attrs: [options.globals.len]std.builtin.Type.StructField.Attributes = undefined;
- for (info.fields, 0..) |field, index| {
+ for (options.globals, 0..) |field, index| {
names[index] = field.name;
- types[index] = @field(globals, field.name);
+ types[index] = field.type;
attrs[index] = .{};
}
@@ -23,7 +29,7 @@ pub fn App(comptime options: Options, globals: anytype) type {
globals: G = g: {
var gl: G = undefined;
- for (info.fields, 1..) |field, id| {
+ for (options.globals, 1..) |field, id| {
@field(gl, field.name) = @ptrFromInt(id);
}
@@ -47,7 +53,7 @@ pub fn App(comptime options: Options, globals: anytype) type {
.native(.script, .{}, .content(reg: {
var register_code: []const u8 = "";
- for (info.fields, 1..) |field, id| {
+ for (options.globals, 1..) |field, id| {
register_code = register_code ++ std.fmt.comptimePrint(
"ObjectStore.registerObject({}, {s});",
.{ id, field.name },
diff --git a/src/z/Env/root.zig b/src/z/Env/root.zig
index 5a08362..6cc5ae3 100644
--- a/src/z/Env/root.zig
+++ b/src/z/Env/root.zig
@@ -1,15 +1,16 @@
const std = @import("std");
-const js = @import("js.zig");
const Self = @This();
-pub const Call = @import("call.zig").Call;
+const js = @import("js.zig");
const future = @import("future.zig");
+const Object = @import("../Object.zig");
+
+pub const Call = @import("call.zig").Call;
pub const AnyFuture = future.AnyFuture;
pub const Future = future.Future;
pub const Value = @import("value.zig").Value;
-pub const Object = @import("Object.zig");
calls: std.ArrayList(Call) = .empty,
values: std.ArrayList(Value) = .empty,
diff --git a/src/z/Env/value.zig b/src/z/Env/value.zig
index b077fb2..59b788f 100644
--- a/src/z/Env/value.zig
+++ b/src/z/Env/value.zig
@@ -1,5 +1,5 @@
const std = @import("std");
-const Object = @import("root.zig").Object;
+const Object = @import("../Object.zig");
pub fn isString(comptime T: type) bool {
return comptime blk: {
diff --git a/src/z/Env/Object.zig b/src/z/Object.zig
index 77afa1f..4e66e5b 100644
--- a/src/z/Env/Object.zig
+++ b/src/z/Object.zig
@@ -1,4 +1,6 @@
-const Env = @import("root.zig");
+const mod = @import("root.zig");
+const Options = mod.Options;
+const Env = mod.Env;
const AnyFuture = Env.AnyFuture;
const Future = Env.Future;
@@ -27,3 +29,7 @@ pub inline fn free(self: *Self, env: *Env) Future(.undefined) {
pub fn new(self: *Self, env: *Env) Future(.object) {
return env.new(self, .{});
}
+
+pub fn global(tag: @EnumLiteral()) Options.Global {
+ return .{ .name = @tagName(tag), .type = *Self };
+}
diff --git a/src/z/env/console.zig b/src/z/log/js.zig
index 16f562c..16f562c 100644
--- a/src/z/env/console.zig
+++ b/src/z/log/js.zig
diff --git a/src/z/env/root.zig b/src/z/log/root.zig
index 3ceac84..ba9141e 100644
--- a/src/z/env/root.zig
+++ b/src/z/log/root.zig
@@ -1,8 +1,8 @@
const std = @import("std");
-pub const console = @import("console.zig");
+pub const js = @import("js.zig");
-pub fn logFn(
+pub fn default(
comptime level: std.log.Level,
comptime scope: @EnumLiteral(),
comptime format: []const u8,
@@ -12,10 +12,10 @@ pub fn logFn(
var writer: std.Io.Writer = .fixed(&buffer);
const func = switch (level) {
- .err => console.err,
- .warn => console.warn,
- .info => console.log,
- .debug => console.debug,
+ .err => js.err,
+ .warn => js.warn,
+ .info => js.log,
+ .debug => js.debug,
};
if (scope != .default) {
@@ -26,9 +26,3 @@ pub fn logFn(
func(writer.buffered());
}
-fn defaultPanic(msg: []const u8, _: ?usize) noreturn {
- console.err(msg);
- @trap();
-}
-
-pub const panic = std.debug.FullPanic(defaultPanic);
diff --git a/src/z/root.zig b/src/z/root.zig
index fc42acd..e18193b 100644
--- a/src/z/root.zig
+++ b/src/z/root.zig
@@ -1,29 +1,16 @@
const builtin = @import("builtin");
const std = @import("std");
-const wasm = wasm_struct: {
- if (builtin.target.cpu.arch.isWasm()) {
- break :wasm_struct struct {
- pub const env = @import("env/root.zig");
- pub const Env = @import("Env/root.zig");
-
- export const bit_length: u8 = @sizeOf(usize);
- };
- }
-
- break :wasm_struct struct {
- pub const Env = struct {
- pub const Object = struct {};
- };
- };
-};
-
-pub const env = wasm.env;
-pub const Env = wasm.Env;
+pub const log = @import("log/root.zig");
+pub const Env = @import("Env/root.zig");
+pub const Object = @import("Object.zig");
pub const App = @import("App/root.zig").App;
+pub const Options = @import("App/root.zig").Options;
+
+fn defaultPanic(msg: []const u8, _: ?usize) noreturn {
+ log.js.err(msg);
+ @trap();
+}
-pub const allocator: std.mem.Allocator = if (builtin.target.cpu.arch.isWasm()) .{
- .ptr = undefined,
- .vtable = &std.heap.BrkAllocator.vtable,
-} else .failing;
+pub const panic = std.debug.FullPanic(defaultPanic);