all: move standalone libraries to libs/ subdirectory
The root dir of our repository has grown quite a lot the past few months.
I'd like to make it more clear where the bulk of the engine lives (`src/`) and
also make it more clear which Mach libraries are consumable as standalone projects.
As for the name of this directory, `libs` was my first choice but there's a bit of
a convention of that being external libraries in Zig projects _today_, while these
are libraries maintained as part of Mach in this repository - not external ones.
We will name this directory `libs`, and if we have a need for external libraries
we will use `external` or `deps` for that directory name. I considered other names
such as `components`, `systems`, `modules` (which are bad as they overlap with
major ECS / engine concepts), and it seems likely the official Zig package manager
will break the convention of using a `libs` dir anyway.
Performed via:
```sh
mkdir libs/
git mv freetype libs/
git mv basisu libs/
git mv gamemode libs/
git mv glfw libs/
git mv gpu libs/
git mv gpu-dawn libs/
git mv sysaudio libs/
git mv sysjs libs/
git mv ecs libs/
```
git-subtree-dir: glfw
git-subtree-mainline: 0d5b853443
git-subtree-split: 572d1144f11b353abdb64fff828b25a4f0fbb7ca
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
git mv ecs libs/
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
79ec61396f
commit
0645429df9
240 changed files with 6 additions and 6 deletions
|
|
@ -1,270 +0,0 @@
|
|||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const js = struct {
|
||||
extern fn zigCreateMap() u32;
|
||||
extern fn zigCreateArray() u32;
|
||||
extern fn zigCreateString(str: [*]const u8, len: u32) u32;
|
||||
extern fn zigCreateFunction(id: *const anyopaque, captures: [*]Value, len: u32) u32;
|
||||
extern fn zigGetAttributeCount(id: u64) u32;
|
||||
extern fn zigGetProperty(id: u64, name: [*]const u8, len: u32, ret_ptr: *anyopaque) void;
|
||||
extern fn zigSetProperty(id: u64, name: [*]const u8, len: u32, set_ptr: *const anyopaque) void;
|
||||
extern fn zigDeleteProperty(id: u64, name: [*]const u8, len: u32) void;
|
||||
extern fn zigGetIndex(id: u64, index: u32, ret_ptr: *anyopaque) void;
|
||||
extern fn zigSetIndex(id: u64, index: u32, set_ptr: *const anyopaque) void;
|
||||
extern fn zigGetString(val_id: u64, ptr: [*]const u8) void;
|
||||
extern fn zigGetStringLength(val_id: u64) u32;
|
||||
extern fn zigValueEqual(val: *const anyopaque, other: *const anyopaque) bool;
|
||||
extern fn zigValueInstanceOf(val: *const anyopaque, other: *const anyopaque) bool;
|
||||
extern fn zigDeleteIndex(id: u64, index: u32) void;
|
||||
extern fn zigFunctionCall(id: u64, name: [*]const u8, len: u32, args: ?*const anyopaque, args_len: u32, ret_ptr: *anyopaque) void;
|
||||
extern fn zigFunctionInvoke(id: u64, args: ?*const anyopaque, args_len: u32, ret_ptr: *anyopaque) void;
|
||||
extern fn zigGetParamCount(id: u64) u32;
|
||||
extern fn zigConstructType(id: u64, args: ?*const anyopaque, args_len: u32) u32;
|
||||
extern fn zigCleanupObject(id: u64) void;
|
||||
};
|
||||
|
||||
pub const Value = extern struct {
|
||||
tag: Tag,
|
||||
val: extern union {
|
||||
ref: u64,
|
||||
num: f64,
|
||||
bool: bool,
|
||||
},
|
||||
|
||||
pub const Tag = enum(u8) {
|
||||
object,
|
||||
num,
|
||||
bool,
|
||||
str,
|
||||
nulled,
|
||||
undef,
|
||||
func,
|
||||
};
|
||||
|
||||
pub fn is(val: *const Value, comptime tag: Tag) bool {
|
||||
return val.tag == tag;
|
||||
}
|
||||
|
||||
pub fn view(val: *const Value, comptime tag: Tag) switch (tag) {
|
||||
.object => Object,
|
||||
.num => f64,
|
||||
.bool => bool,
|
||||
.str => String,
|
||||
.func => Function,
|
||||
.nulled, .undef => @compileError("Cannot get null or undefined as a value"),
|
||||
} {
|
||||
return switch (tag) {
|
||||
.object => Object{ .ref = val.val.ref },
|
||||
.num => val.val.num,
|
||||
.bool => val.val.bool,
|
||||
.str => String{ .ref = val.val.ref },
|
||||
.func => Function{ .ref = val.val.ref },
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn eql(val: *const Value, other: Value) bool {
|
||||
if (val.tag != other.tag)
|
||||
return false;
|
||||
|
||||
return switch (val.tag) {
|
||||
.num => val.val.num == other.val.num,
|
||||
.bool => val.val.bool == other.val.bool,
|
||||
// Using JS equality (===) is better here since lets say a ref can be dangling
|
||||
else => js.zigValueEqual(val, &other),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn instanceOf(val: *const Value, other: Value) bool {
|
||||
return js.zigValueInstanceOf(val, &other);
|
||||
}
|
||||
|
||||
pub fn format(val: *const Value, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
|
||||
switch (val.tag) {
|
||||
.object => try writer.print("Object@{}({})", .{ val.val.ref, val.view(.object).attributeCount() }),
|
||||
.num => if (val.val.num > 10000)
|
||||
try writer.print("{e}", .{val.val.num})
|
||||
else
|
||||
try writer.print("{d}", .{val.val.num}),
|
||||
.bool => try writer.writeAll(if (val.view(.bool)) "true" else "false"),
|
||||
.str => try writer.print("String@{}({})", .{ val.val.ref, val.view(.str).getLength() }),
|
||||
.func => try writer.print("Function@{}({})", .{ val.val.ref, val.view(.func).paramCount() }),
|
||||
.nulled => try writer.writeAll("null"),
|
||||
.undef => try writer.writeAll("undefined"),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const Object = struct {
|
||||
ref: u64,
|
||||
|
||||
pub fn deinit(obj: *const Object) void {
|
||||
js.zigCleanupObject(obj.ref);
|
||||
}
|
||||
|
||||
pub fn toValue(obj: *const Object) Value {
|
||||
return .{ .tag = .object, .val = .{ .ref = obj.ref } };
|
||||
}
|
||||
|
||||
pub fn attributeCount(obj: *const Object) usize {
|
||||
return js.zigGetAttributeCount(obj.ref);
|
||||
}
|
||||
|
||||
pub fn get(obj: *const Object, prop: []const u8) Value {
|
||||
var ret: Value = undefined;
|
||||
js.zigGetProperty(obj.ref, prop.ptr, @intCast(u32, prop.len), &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
pub fn set(obj: *const Object, prop: []const u8, value: Value) void {
|
||||
js.zigSetProperty(obj.ref, prop.ptr, @intCast(u32, prop.len), &value);
|
||||
}
|
||||
|
||||
pub fn delete(obj: *const Object, prop: []const u8) void {
|
||||
js.zigDeleteProperty(obj.ref, prop.ptr, @intCast(u32, prop.len));
|
||||
}
|
||||
|
||||
pub fn getIndex(obj: *const Object, index: u32) Value {
|
||||
var ret: Value = undefined;
|
||||
js.zigGetIndex(obj.ref, index, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
pub fn setIndex(obj: *const Object, index: u32, value: Value) void {
|
||||
js.zigSetIndex(obj.ref, index, &value);
|
||||
}
|
||||
|
||||
pub fn deleteIndex(obj: *const Object, index: u32) void {
|
||||
js.zigDeleteIndex(obj.ref, index);
|
||||
}
|
||||
|
||||
pub fn call(obj: *const Object, fun: []const u8, args: []const Value) Value {
|
||||
var ret: Value = undefined;
|
||||
js.zigFunctionCall(obj.ref, fun.ptr, @intCast(u32, fun.len), args.ptr, @intCast(u32, args.len), &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
pub const Function = struct {
|
||||
ref: u64,
|
||||
|
||||
pub fn deinit(func: *const Function) void {
|
||||
js.zigCleanupObject(func.ref);
|
||||
}
|
||||
|
||||
pub fn toValue(func: *const Function) Value {
|
||||
return .{ .tag = .func, .val = .{ .ref = func.ref } };
|
||||
}
|
||||
|
||||
pub fn paramCount(func: *const Function) usize {
|
||||
// FIXME: native functions would always return 0
|
||||
return js.zigGetParamCount(func.ref);
|
||||
}
|
||||
|
||||
pub fn construct(func: *const Function, args: []const Value) Object {
|
||||
return .{ .ref = js.zigConstructType(func.ref, args.ptr, @intCast(u32, args.len)) };
|
||||
}
|
||||
|
||||
pub fn invoke(func: *const Function, args: []const Value) Value {
|
||||
var ret: Value = undefined;
|
||||
js.zigFunctionInvoke(func.ref, args.ptr, @intCast(u32, args.len), &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
pub const String = struct {
|
||||
ref: u64,
|
||||
|
||||
pub fn deinit(string: *const String) void {
|
||||
js.zigCleanupObject(string.ref);
|
||||
}
|
||||
|
||||
pub fn toValue(string: *const String) Value {
|
||||
return .{ .tag = .str, .val = .{ .ref = string.ref } };
|
||||
}
|
||||
|
||||
pub fn getLength(string: *const String) usize {
|
||||
return js.zigGetStringLength(string.ref);
|
||||
}
|
||||
|
||||
pub fn getOwnedSlice(string: *const String, allocator: std.mem.Allocator) ![]const u8 {
|
||||
var slice = try allocator.alloc(u8, string.getLength());
|
||||
errdefer allocator.free(slice);
|
||||
js.zigGetString(string.ref, slice.ptr);
|
||||
return slice;
|
||||
}
|
||||
};
|
||||
|
||||
export fn wasmCallFunction(id: *anyopaque, args: u32, len: u32, captures: [*]Value, captures_len: u32) void {
|
||||
var captures_slice: []Value = undefined;
|
||||
captures_slice.ptr = captures;
|
||||
captures_slice.len = captures_len;
|
||||
|
||||
const obj = Object{ .ref = args };
|
||||
if (builtin.zig_backend == .stage1) {
|
||||
obj.set("return_value", functions.items[@ptrToInt(id) - 1](obj, len, captures_slice));
|
||||
} else {
|
||||
var func = @ptrCast(*FunType, @alignCast(std.meta.alignment(*FunType), id));
|
||||
obj.set("return_value", func(obj, len, captures_slice));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn global() Object {
|
||||
return Object{ .ref = 0 };
|
||||
}
|
||||
|
||||
pub fn createMap() Object {
|
||||
return .{ .ref = js.zigCreateMap() };
|
||||
}
|
||||
|
||||
pub fn createArray() Object {
|
||||
return .{ .ref = js.zigCreateArray() };
|
||||
}
|
||||
|
||||
pub fn createString(string: []const u8) String {
|
||||
return .{ .ref = js.zigCreateString(string.ptr, @intCast(u32, string.len)) };
|
||||
}
|
||||
|
||||
pub fn createNumber(num: f64) Value {
|
||||
return .{ .tag = .num, .val = .{ .num = num } };
|
||||
}
|
||||
|
||||
pub fn createBool(val: bool) Value {
|
||||
return .{ .tag = .bool, .val = .{ .bool = val } };
|
||||
}
|
||||
|
||||
pub fn createNull() Value {
|
||||
return .{ .tag = .nulled, .val = undefined };
|
||||
}
|
||||
|
||||
pub fn createUndefined() Value {
|
||||
return .{ .tag = .undef, .val = undefined };
|
||||
}
|
||||
|
||||
const FunType = if (@import("builtin").zig_backend == .stage1)
|
||||
fn (args: Object, args_len: u32, captures: []Value) Value
|
||||
else
|
||||
*const fn (args: Object, args_len: u32, captures: []Value) Value;
|
||||
|
||||
var functions: std.ArrayListUnmanaged(FunType) = .{};
|
||||
|
||||
pub fn createFunction(fun: FunType, captures: []Value) Function {
|
||||
if (builtin.zig_backend == .stage1) {
|
||||
functions.append(std.heap.page_allocator, fun) catch unreachable;
|
||||
return .{ .ref = js.zigCreateFunction(@intToPtr(*anyopaque, functions.items.len), captures.ptr, @intCast(u32, captures.len)) };
|
||||
}
|
||||
return .{ .ref = js.zigCreateFunction(&fun, captures.ptr, captures.len) };
|
||||
}
|
||||
|
||||
pub fn constructType(t: []const u8, args: []const Value) Object {
|
||||
const constructor = global().get(t).view(.func);
|
||||
defer constructor.deinit();
|
||||
|
||||
return constructor.construct(args);
|
||||
}
|
||||
|
||||
test {
|
||||
std.testing.refAllDeclsRecursive(@This());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue