Skip to content

Commit 015eb4a

Browse files
committed
reflect: remove comptime var pointer returns
Following ziglang/zig#19414 and https://ziggit.dev/t/comptime-mutable-memory-changes/3702 change in zig, we cannot return `comptime var` pointer anymore.
1 parent 035e6ca commit 015eb4a

File tree

4 files changed

+128
-62
lines changed

4 files changed

+128
-62
lines changed

src/engines/v8/generate.zig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn getNativeArg(
153153

154154
// JS object
155155
const ptr = try getNativeObject(nat_ctx, T_refl, js_value.castTo(v8.Object));
156-
if (arg_T.underPtr() != null) {
156+
if (comptime arg_T.underPtr() != null) {
157157
value = ptr;
158158
} else {
159159
value = ptr.*;
@@ -373,13 +373,14 @@ fn getArgs(
373373

374374
fn freeArgs(alloc: std.mem.Allocator, comptime func: refl.Func, obj: anytype) !void {
375375
inline for (func.args) |arg_T| {
376+
const underT = comptime arg_T.underT();
376377

377378
// free char slices
378379
// the API functions will be responsible of copying the slice
379380
// in their implementations if they want to keep it afterwards
380-
if (arg_T.underT() == []u8 or arg_T.underT() == []const u8) {
381+
if (underT == []u8 or underT == []const u8) {
381382
const val = @field(obj, arg_T.name.?);
382-
if (arg_T.underOpt() != null) {
383+
if (comptime arg_T.underOpt() != null) {
383384
// free only if val is non-null
384385
if (val) |v| {
385386
alloc.free(v);
@@ -390,7 +391,7 @@ fn freeArgs(alloc: std.mem.Allocator, comptime func: refl.Func, obj: anytype) !v
390391
}
391392

392393
// free varidadic slices
393-
if (try refl.Type.variadic(arg_T.underT(), null) != null) {
394+
if (try refl.Type.variadic(underT, null) != null) {
394395
const val = @field(obj, arg_T.name.?).?;
395396
// NOTE: variadic are optional by design
396397
alloc.free(@field(val, "slice"));
@@ -885,7 +886,7 @@ fn callFunc(
885886

886887
// call native func
887888
const function = @field(T_refl.T, func.name);
888-
const res_T = func.return_type.underErr() orelse func.return_type.T;
889+
const res_T = comptime func.return_type.underErr() orelse func.return_type.T;
889890
var res: res_T = undefined;
890891
if (comptime @typeInfo(func.return_type.T) == .ErrorUnion) {
891892
res = @call(.auto, function, args) catch |err| {
@@ -912,7 +913,7 @@ fn callFunc(
912913
nat_ctx.alloc,
913914
nat_ctx,
914915
T_refl,
915-
func.return_type.underT(),
916+
comptime func.return_type.underT(),
916917
res,
917918
cbk_info.getThis(),
918919
isolate,

src/generate.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ const loadFn = @import("private_api.zig").loadFn;
2424
// reflect the user-defined types to obtain type information (T_refl)
2525
// This function must be called at comptime by the root file of the project
2626
// and stored in a constant named `Types`
27-
pub fn reflect(comptime types: anytype) []refl.Struct {
27+
pub fn reflect(comptime types: anytype) []const refl.Struct {
2828
std.debug.assert(@inComptime());
2929

3030
// call types reflection
31-
return refl.do(types) catch unreachable;
31+
const structs: []const refl.Struct = refl.do(types) catch |e| @compileError(@errorName(e));
32+
return structs;
3233
}
3334

3435
// Import user-defined types
35-
pub const Types: []refl.Struct = @import("root").Types;
36+
pub const Types: []const refl.Struct = @import("root").Types;
3637

3738
// retrieved the reflected type of a user-defined native type
3839
pub fn getType(comptime T: type) refl.Struct {
@@ -47,7 +48,7 @@ pub fn getType(comptime T: type) refl.Struct {
4748

4849
// generate APIs from reflected types
4950
// which can be later loaded in JS.
50-
fn generate(comptime types: []refl.Struct) []API {
51+
fn generate(comptime types: []const refl.Struct) []API {
5152
std.debug.assert(@inComptime());
5253

5354
var apis: [types.len]API = undefined;

0 commit comments

Comments
 (0)