Skip to content

Commit 1ccf77b

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 47e1d88 commit 1ccf77b

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
@@ -168,7 +168,7 @@ fn getNativeArg(
168168

169169
// JS object
170170
const ptr = try getNativeObject(nat_ctx, T_refl, js_value.castTo(v8.Object));
171-
if (arg_T.underPtr() != null) {
171+
if (comptime arg_T.underPtr() != null) {
172172
value = ptr;
173173
} else {
174174
value = ptr.*;
@@ -389,13 +389,14 @@ fn getArgs(
389389

390390
fn freeArgs(alloc: std.mem.Allocator, comptime func: refl.Func, obj: anytype) !void {
391391
inline for (func.args) |arg_T| {
392+
const underT = comptime arg_T.underT();
392393

393394
// free char slices
394395
// the API functions will be responsible of copying the slice
395396
// in their implementations if they want to keep it afterwards
396-
if (arg_T.underT() == []u8 or arg_T.underT() == []const u8) {
397+
if (underT == []u8 or underT == []const u8) {
397398
const val = @field(obj, arg_T.name.?);
398-
if (arg_T.underOpt() != null) {
399+
if (comptime arg_T.underOpt() != null) {
399400
// free only if val is non-null
400401
if (val) |v| {
401402
alloc.free(v);
@@ -406,7 +407,7 @@ fn freeArgs(alloc: std.mem.Allocator, comptime func: refl.Func, obj: anytype) !v
406407
}
407408

408409
// free varidadic slices
409-
if (try refl.Type.variadic(arg_T.underT(), null) != null) {
410+
if (try refl.Type.variadic(underT, null) != null) {
410411
const val = @field(obj, arg_T.name.?).?;
411412
// NOTE: variadic are optional by design
412413
alloc.free(@field(val, "slice"));
@@ -913,7 +914,7 @@ fn callFunc(
913914

914915
// call native func
915916
const function = @field(T_refl.T, func.name);
916-
const res_T = func.return_type.underErr() orelse func.return_type.T;
917+
const res_T = comptime func.return_type.underErr() orelse func.return_type.T;
917918
var res: res_T = undefined;
918919
if (comptime @typeInfo(func.return_type.T) == .ErrorUnion) {
919920
res = @call(.auto, function, args) catch |err| {
@@ -940,7 +941,7 @@ fn callFunc(
940941
nat_ctx.alloc,
941942
nat_ctx,
942943
T_refl,
943-
func.return_type.underT(),
944+
comptime func.return_type.underT(),
944945
res,
945946
cbk_info.getThis(),
946947
isolate,

src/generate.zig

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

4444
// call types reflection
45-
return refl.do(types) catch unreachable;
45+
const structs: []const refl.Struct = refl.do(types) catch |e| @compileError(@errorName(e));
46+
return structs;
4647
}
4748

4849
// Import user-defined types
49-
pub const Types: []refl.Struct = @import("root").Types;
50+
pub const Types: []const refl.Struct = @import("root").Types;
5051

5152
// retrieved the reflected type of a user-defined native type
5253
pub fn getType(comptime T: type) refl.Struct {
@@ -61,7 +62,7 @@ pub fn getType(comptime T: type) refl.Struct {
6162

6263
// generate APIs from reflected types
6364
// which can be later loaded in JS.
64-
fn generate(comptime types: []refl.Struct) []API {
65+
fn generate(comptime types: []const refl.Struct) []API {
6566
std.debug.assert(@inComptime());
6667

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

0 commit comments

Comments
 (0)