Skip to content

Commit

Permalink
Fix test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Jan 7, 2024
1 parent 752b1ec commit cd53c7c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v2
- name: Install npm
run: sudo apt-get install -y curl unzip && curl -fsSL https://fnm.vercel.app/install | bash && export PATH="/$HOME/.local/share/fnm:$PATH" && eval "`fnm env`" && fnm install v20.6.1 && fnm use v20.6.1 && npm -g i npm@latest
run: sudo apt-get install -y curl unzip && curl -fsSL https://fnm.vercel.app/install | bash && export PATH="/$HOME/.local/share/fnm:$PATH" && eval "`fnm env`" && fnm install v20.10.0 && fnm use v20.10.0 && npm -g i npm@latest
- name: Install Deno and Bun
run: npm install -g npm@latest; npm install -g bun deno-bin
- name: Run tests
Expand Down
17 changes: 9 additions & 8 deletions src/core/Kivi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mem_allocator: std.mem.Allocator,
const Kivi = @This();

inline fn stringcpy(dest: []u8, src: []const u8) void {
@memcpy(dest.ptr[0..src.len], src.ptr[0..src.len]);
@memcpy(dest[0..src.len], src);
}

pub fn init(self: *Kivi, config: *const Config) !usize {
Expand All @@ -35,19 +35,20 @@ pub fn init(self: *Kivi, config: *const Config) !usize {
pub fn reserve_key(self: *Kivi, size: usize) ![]u8 {
return self.mem_allocator.alloc(u8, size);
}
pub fn reserve(self: *Kivi, key: []u8, size: usize) ![]u8 {
const value = try self.mem_allocator.alloc(u8, size);
try self.entries.put(self.mem_allocator, key, Entry{ .key = key, .value = value });

return value;
pub fn reserve_value(self: *Kivi, size: usize) ![]u8 {
return try self.mem_allocator.alloc(u8, size);
}
pub fn putEntry(self: *Kivi, key: []u8, value: []u8) !void {
return self.entries.put(self.mem_allocator, key, Entry{ .key = key, .value = value });
}
pub fn set(self: *Kivi, key: []const u8, value: []const u8) !usize {
const key_slice = try self.reserve_key(key.len);
const value_slice = try self.reserve(key_slice, value.len);

const value_slice = try self.reserve_value(value.len);
stringcpy(key_slice, key);
stringcpy(value_slice, value);

try self.putEntry(key_slice, value_slice);

return value_slice.len;
}

Expand Down
4 changes: 1 addition & 3 deletions src/core/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pub export fn kivi_deinit(self: *Kivi) void {
self.deinit();
}
pub export fn kivi_get(self: *const Kivi, key: [*]const u8, key_len: usize, val: ?[*]u8, val_len: usize) usize {
return self.get(key[0..key_len], if (val) |v| v[0..val_len] else null) catch {
return 0;
};
return self.get(key[0..key_len], if (val) |v| v[0..val_len] else null) catch 0;
}
pub export fn kivi_set(self: *Kivi, key: [*]const u8, key_len: usize, val: [*]const u8, val_len: usize) usize {
return self.set(key[0..key_len], val[0..val_len]) catch 0;
Expand Down
6 changes: 5 additions & 1 deletion src/drivers/js/nodejs/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,18 @@ pub export fn kivi_set_js(env: ntypes.napi_env, info: ntypes.napi_callback_info)
if (value_len == 0) {
return new_unint(env, 0);
}
const value_buf = self.reserve(key_buf, value_len) catch {
const value_buf = self.reserve_value(value_len) catch {
return new_unint(env, 0);
};
const written_value_len = string_to_buffer(env, args[2], value_buf);
if (written_value_len == 0) {
return new_unint(env, 0);
}

self.putEntry(key_buf, value_buf) catch {
return new_unint(env, 0);
};

return new_unint(env, @intCast(value_len));
}
pub export fn kivi_del_js(env: ntypes.napi_env, info: ntypes.napi_callback_info) ntypes.napi_value {
Expand Down

0 comments on commit cd53c7c

Please sign in to comment.