Skip to content

Commit

Permalink
feat: Threads
Browse files Browse the repository at this point in the history
closes #176
  • Loading branch information
giann committed Sep 18, 2023
1 parent fa9cfb5 commit 3fe8019
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
- `std.serialize` takes any buzz value and return a serializable version of it (objects become maps, etc.) provided the data is has no circular reference and does not contain not serializable values (functions, fibers, etc.)
- UTF8 helpers: `str.utf8Len`, `str.utf8Codepoints`, `str.utf8Valid`
- New integer literal for single chars: `'A' == 65`
- `thread` (https://github.com/buzz-language/buzz/issues/176) std library with `Thread`, `Mutext` and `Semaphore`

## Changed

- `json` lib is renamed `serialize`
- `Json` now returns a `Boxed` object (which can be reused in other contexts than JSON)
- Identifiers can now have `_` since pattern delimiters have changed
- Changed pattern delimiters (https://github.com/buzz-language/buzz/issues/165)
- `assert` message parameter can be omitted

## Fixed

Expand Down
3 changes: 3 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ pub fn build(b: *Build) !void {
"src/lib/buzz_http.zig",
"src/lib/buzz_ffi.zig",
"src/lib/buzz_serialize.zig",
"src/lib/buzz_thread.zig",
};
// Zig only libs
const lib_names = [_][]const u8{
Expand All @@ -383,6 +384,7 @@ pub fn build(b: *Build) !void {
"http",
"ffi",
"serialize",
"thread",
};
const all_lib_names = [_][]const u8{
"std",
Expand All @@ -398,6 +400,7 @@ pub fn build(b: *Build) !void {
"errors",
"ffi",
"serialize",
"thread",
};

// TODO: this section is slow. Modifying Buzz parser shouldn't trigger recompile of all buzz dynamic libraries
Expand Down
76 changes: 31 additions & 45 deletions src/builtin/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,16 @@ const SortContext = struct {
};

fn lessThan(context: SortContext, lhs: Value, rhs: Value) bool {
var args = std.ArrayList(*const Value).init(context.ctx.vm.gc.allocator);
defer args.deinit();

// TODO: handle error
args.append(&lhs) catch unreachable;
args.append(&rhs) catch unreachable;
var args = [_]Value{
lhs,
rhs,
};

buzz_api.bz_call(
context.ctx.vm,
context.sort_closure,
@as([*]const *const Value, @ptrCast(args.items)),
@as(u8, @intCast(args.items.len)),
@ptrCast(args[0..]),
@intCast(args.len),
null,
);

Expand Down Expand Up @@ -271,19 +269,16 @@ pub fn forEach(ctx: *NativeCtx) c_int {
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;

for (list.items.items, 0..) |item, index| {
var args = std.ArrayList(*const Value).init(ctx.vm.gc.allocator);
defer args.deinit();

// TODO: handle error
const index_value = Value.fromInteger(@as(i32, @intCast(index)));
args.append(&index_value) catch unreachable;
args.append(&item) catch unreachable;
var args = [_]Value{
Value.fromInteger(@as(i32, @intCast(index))),
item,
};

buzz_api.bz_call(
ctx.vm,
closure,
@as([*]const *const Value, @ptrCast(args.items)),
@as(u8, @intCast(args.items.len)),
@ptrCast(args[0..]),
@intCast(args.len),
null,
);
}
Expand All @@ -297,20 +292,17 @@ pub fn reduce(ctx: *NativeCtx) c_int {
var accumulator = ctx.vm.peek(0);

for (list.items.items, 0..) |item, index| {
var args = std.ArrayList(*const Value).init(ctx.vm.gc.allocator);
defer args.deinit();

// TODO: handle error
const index_value = Value.fromInteger(@as(i32, @intCast(index)));
args.append(&index_value) catch unreachable;
args.append(&item) catch unreachable;
args.append(&accumulator) catch unreachable;
var args = [_]Value{
Value.fromInteger(@as(i32, @intCast(index))),
item,
accumulator,
};

buzz_api.bz_call(
ctx.vm,
closure,
@as([*]const *const Value, @ptrCast(args.items)),
@as(u8, @intCast(args.items.len)),
@ptrCast(args[0..]),
@intCast(args.len),
null,
);

Expand All @@ -335,19 +327,16 @@ pub fn filter(ctx: *NativeCtx) c_int {
) catch unreachable; // TODO: handle error

for (list.items.items, 0..) |item, index| {
var args = std.ArrayList(*const Value).init(ctx.vm.gc.allocator);
defer args.deinit();

// TODO: handle error
const index_value = Value.fromInteger(@as(i32, @intCast(index)));
args.append(&index_value) catch unreachable;
args.append(&item) catch unreachable;
var args = [_]Value{
Value.fromInteger(@as(i32, @intCast(index))),
item,
};

buzz_api.bz_call(
ctx.vm,
closure,
@as([*]const *const Value, @ptrCast(args.items)),
@as(u8, @intCast(args.items.len)),
@ptrCast(args[0..]),
@intCast(args.len),
null,
);

Expand Down Expand Up @@ -376,19 +365,16 @@ pub fn map(ctx: *NativeCtx) c_int {
) catch unreachable; // TODO: handle error

for (list.items.items, 0..) |item, index| {
var args = std.ArrayList(*const Value).init(ctx.vm.gc.allocator);
defer args.deinit();

// TODO: handle error
const index_value = Value.fromInteger(@as(i32, @intCast(index)));
args.append(&index_value) catch unreachable;
args.append(&item) catch unreachable;
var args = [_]Value{
Value.fromInteger(@as(i32, @intCast(index))),
item,
};

buzz_api.bz_call(
ctx.vm,
closure,
@as([*]const *const Value, @ptrCast(args.items)),
@as(u8, @intCast(args.items.len)),
@ptrCast(args[0..]),
@intCast(args.len),
null,
);

Expand Down
25 changes: 21 additions & 4 deletions src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ export fn bz_listSet(vm: *VM, self: Value, index: usize, value: Value) void {
) catch @panic("Could not set element in list");
}

export fn bz_listPtr(self: *ObjList) [*]Value {
return self.items.items.ptr;
}

export fn bz_listLen(self: *ObjList) usize {
return self.items.items.len;
}
Expand Down Expand Up @@ -602,7 +606,7 @@ export fn bz_newVM(self: *VM) ?*VM {
var gc = self.gc.allocator.create(GarbageCollector) catch {
return null;
};
// FIXME: should share strings between gc

gc.* = GarbageCollector.init(self.gc.allocator);
gc.type_registry = TypeRegistry{
.gc = gc,
Expand All @@ -617,7 +621,20 @@ export fn bz_newVM(self: *VM) ?*VM {
return vm;
}

export fn bz_startVM(self: *VM) void {
self.current_fiber.* = _vm.Fiber.init(
self.gc.allocator,
null, // parent fiber
null, // stack_slice
.OP_CALL, // call_type
1, // arg_count
false, // catch_count
null, // method/member
) catch @panic("Out of memory");
}

export fn bz_deinitVM(_: *VM) void {
// FIXME
// self.deinit();
}

Expand Down Expand Up @@ -719,14 +736,14 @@ pub export fn bz_invoke(
pub export fn bz_call(
self: *VM,
closure: *ObjClosure,
arguments: ?[*]const *const Value,
arguments: ?[*]const Value,
len: u8,
catch_value: ?*Value,
catch_value: ?*const Value,
) void {
self.push(closure.toValue());
var i: usize = 0;
while (i < len) : (i += 1) {
self.push(arguments.?[i].*);
self.push(arguments.?[i]);
}

// TODO: catch properly
Expand Down
8 changes: 5 additions & 3 deletions src/lib/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub const ZigType = opaque {
pub const VM = opaque {
pub const allocator = @import("../buzz_api.zig").allocator;

pub extern fn bz_newVM(self: *VM) *VM;
pub extern fn bz_newVM(self: *VM) ?*VM;
pub extern fn bz_startVM(self: *VM) void;
pub extern fn bz_deinitVM(self: *VM) void;
pub extern fn bz_compile(
self: *VM,
Expand All @@ -50,9 +51,9 @@ pub const VM = opaque {
pub extern fn bz_call(
self: *VM,
closure: *ObjClosure,
arguments: ?[*]const *const Value,
arguments: ?[*]const Value,
len: usize,
catch_value: ?*Value,
catch_value: ?*const Value,
) void;
pub extern fn bz_push(self: *VM, value: Value) void;
pub extern fn bz_pop(self: *VM) Value;
Expand Down Expand Up @@ -311,6 +312,7 @@ pub const ObjList = opaque {
pub extern fn bz_listConcat(vm: *VM, list: Value, other_list: Value) Value;
pub extern fn bz_getListField(vm: *VM, list_value: Value, field_name_value: Value, bind: bool) Value;
pub extern fn bz_listNext(vm: *VM, list_value: Value, index: *Value) Value;
pub extern fn bz_listPtr(self: *ObjList) [*]Value;
};

pub const ObjMap = opaque {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/buzz_io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export fn runFile(ctx: *api.NativeCtx) c_int {
defer api.VM.allocator.free(source);

// Init new VM
var vm = ctx.vm.bz_newVM();
var vm = ctx.vm.bz_newVM() orelse @panic("Out of memory");
defer vm.bz_deinitVM();

// Compile
Expand Down
Loading

0 comments on commit 3fe8019

Please sign in to comment.