Skip to content

Commit ab7f691

Browse files
Add JSValue.typeOf
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
1 parent 13260c4 commit ab7f691

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/api.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ pub const test_utils = @import("tests/test_utils.zig");
4141
// JS types
4242
// --------
4343

44+
pub const JSTypes = enum {
45+
object,
46+
function,
47+
string,
48+
number,
49+
boolean,
50+
bigint,
51+
null,
52+
undefined,
53+
};
54+
4455
const types = @import("types.zig");
4556
pub const i64Num = types.i64Num;
4657
pub const u64Num = types.u64Num;

src/engines/v8/v8.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,18 @@ pub const JSValue = struct {
426426
pub fn toString(self: JSValue, alloc: std.mem.Allocator, env: Env) anyerror![]const u8 {
427427
return valueToUtf8(alloc, self.value, env.isolate, env.js_ctx.?);
428428
}
429+
430+
pub fn typeOf(self: JSValue, env: Env) anyerror!public.JSTypes {
431+
var buf: [20]u8 = undefined;
432+
const str = try self.value.typeOf(env.isolate);
433+
const len = str.lenUtf8(env.isolate);
434+
const s = buf[0..len];
435+
_ = str.writeUtf8(env.isolate, s);
436+
return std.meta.stringToEnum(public.JSTypes, s) orelse {
437+
std.log.err("JSValueTypeNotHandled: {s}", .{s});
438+
return error.JSValueTypeNotHandled;
439+
};
440+
}
429441
};
430442

431443
pub const TryCatch = struct {

src/interfaces.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ pub fn JSValue(comptime T: type, env: type) void {
116116

117117
// toString()
118118
assertDecl(T, "toString", fn (self: T, alloc: std.mem.Allocator, env: env) anyerror![]const u8);
119+
120+
// typeOf()
121+
assertDecl(T, "typeOf", fn (self: T, env: env) anyerror!public.JSTypes);
119122
}
120123

121124
pub fn JSObjectID(comptime T: type) void {

vendor/zig-v8

0 commit comments

Comments
 (0)