fix up zig code

This commit is contained in:
Folkert 2021-03-03 20:05:02 +01:00
parent 9873eb358b
commit ca8c23e448
2 changed files with 44 additions and 8 deletions

View file

@ -95,6 +95,7 @@ pub const RocList = extern struct {
const dest_ptr = first_slot; const dest_ptr = first_slot;
@memcpy(dest_ptr, source_ptr, old_length * element_width); @memcpy(dest_ptr, source_ptr, old_length * element_width);
@memset(dest_ptr + old_length * element_width, 0, delta_length * element_width);
} }
// NOTE the newly added elements are left uninitialized // NOTE the newly added elements are left uninitialized
@ -246,19 +247,35 @@ pub fn listWalk(list: RocList, stepper: Opaque, stepper_caller: Caller2, accum:
return; return;
} }
if (list.isEmpty()) {
@memcpy(output orelse unreachable, accum orelse unreachable, accum_width); @memcpy(output orelse unreachable, accum orelse unreachable, accum_width);
return;
}
const alloc: [*]u8 = @ptrCast([*]u8, std.heap.c_allocator.alloc(u8, accum_width) catch unreachable);
var b1 = output orelse unreachable;
var b2 = alloc;
@memcpy(b2, accum orelse unreachable, accum_width);
if (list.bytes) |source_ptr| { if (list.bytes) |source_ptr| {
var i: usize = 0; var i: usize = 0;
const size = list.len(); const size = list.len();
while (i < size) : (i += 1) { while (i < size) : (i += 1) {
const element = source_ptr + i * element_width; const element = source_ptr + i * element_width;
stepper_caller(stepper, element, output, output); stepper_caller(stepper, element, b2, b1);
const temp = b1;
b2 = b1;
b1 = temp;
} }
}
@memcpy(output orelse unreachable, b2, accum_width);
std.heap.c_allocator.free(alloc[0..accum_width]);
const data_bytes = list.len() * element_width; const data_bytes = list.len() * element_width;
utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes); utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes);
}
} }
pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2, accum: Opaque, alignment: usize, element_width: usize, accum_width: usize, output: Opaque) callconv(.C) void { pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2, accum: Opaque, alignment: usize, element_width: usize, accum_width: usize, output: Opaque) callconv(.C) void {
@ -266,7 +283,16 @@ pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2
return; return;
} }
if (list.isEmpty()) {
@memcpy(output orelse unreachable, accum orelse unreachable, accum_width); @memcpy(output orelse unreachable, accum orelse unreachable, accum_width);
return;
}
const alloc: [*]u8 = @ptrCast([*]u8, std.heap.c_allocator.alloc(u8, accum_width) catch unreachable);
var b1 = output orelse unreachable;
var b2 = alloc;
@memcpy(b2, accum orelse unreachable, accum_width);
if (list.bytes) |source_ptr| { if (list.bytes) |source_ptr| {
const size = list.len(); const size = list.len();
@ -275,11 +301,21 @@ pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2
i -= 1; i -= 1;
const element = source_ptr + i * element_width; const element = source_ptr + i * element_width;
stepper_caller(stepper, element, output, output); stepper_caller(stepper, element, output, output);
const temp = b1;
b2 = b1;
b1 = temp;
} }
const data_bytes = list.len() * element_width; const data_bytes = list.len() * element_width;
utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes); utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes);
} }
@memcpy(output orelse unreachable, b2, accum_width);
std.heap.c_allocator.free(alloc[0..accum_width]);
const data_bytes = list.len() * element_width;
utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes);
} }
// List.contains : List k, k -> Bool // List.contains : List k, k -> Bool

View file

@ -1104,7 +1104,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let tag_field_layouts = &fields[*tag_id as usize]; let tag_field_layouts = &fields[*tag_id as usize];
for (field_symbol, tag_field_layout) in arguments.iter().zip(tag_field_layouts.iter()) { for (field_symbol, tag_field_layout) in arguments.iter().zip(tag_field_layouts.iter()) {
let (val, val_layout) = load_symbol_and_layout(scope, field_symbol); let (val, _val_layout) = load_symbol_and_layout(scope, field_symbol);
// Zero-sized fields have no runtime representation. // Zero-sized fields have no runtime representation.
// The layout of the struct expects them to be dropped! // The layout of the struct expects them to be dropped!
@ -1127,7 +1127,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
field_vals.push(ptr); field_vals.push(ptr);
} else { } else {
// this check fails for recursive tag unions, but can be helpful while debugging // this check fails for recursive tag unions, but can be helpful while debugging
debug_assert_eq!(tag_field_layout, val_layout); // debug_assert_eq!(tag_field_layout, val_layout);
field_vals.push(val); field_vals.push(val);
} }