js-runtime: Add Value.eql function, equivalent to Js's === operator

This commit is contained in:
iddev5 2022-07-07 23:35:18 +05:30 committed by Stephen Gutekanst
parent 8ec61055e3
commit e67fdd75e3
2 changed files with 20 additions and 0 deletions

View file

@ -268,6 +268,13 @@ const zig = {
memory.setString(ptr, values[value_map[val_id]]);
},
zigValueEqual(val, other) {
let memory = new MemoryBlock(zig.wasm.exports.memory.buffer);
const val_js = zig.readObject(memory.slice(val), memory);
const other_js = zig.readObject(memory.slice(other), memory);
return val_js === other_js;
},
functionCall(func, this_param, args, args_len, ret_ptr) {
let memory = new MemoryBlock(zig.wasm.exports.memory.buffer);
let argv = [];

View file

@ -14,6 +14,7 @@ const js = struct {
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 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;
@ -79,6 +80,18 @@ pub const Value = extern struct {
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 const Object = struct {