Skip to content

Commit

Permalink
reflect: remove comptime var pointer returns
Browse files Browse the repository at this point in the history
Following ziglang/zig#19414 and
https://ziggit.dev/t/comptime-mutable-memory-changes/3702 change in zig,
we cannot return `comptime var` pointer anymore.
  • Loading branch information
krichprollsch committed Jun 18, 2024
1 parent 47e1d88 commit 1ccf77b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 62 deletions.
13 changes: 7 additions & 6 deletions src/engines/v8/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn getNativeArg(

// JS object
const ptr = try getNativeObject(nat_ctx, T_refl, js_value.castTo(v8.Object));
if (arg_T.underPtr() != null) {
if (comptime arg_T.underPtr() != null) {
value = ptr;
} else {
value = ptr.*;
Expand Down Expand Up @@ -389,13 +389,14 @@ fn getArgs(

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

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

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

// call native func
const function = @field(T_refl.T, func.name);
const res_T = func.return_type.underErr() orelse func.return_type.T;
const res_T = comptime func.return_type.underErr() orelse func.return_type.T;
var res: res_T = undefined;
if (comptime @typeInfo(func.return_type.T) == .ErrorUnion) {
res = @call(.auto, function, args) catch |err| {
Expand All @@ -940,7 +941,7 @@ fn callFunc(
nat_ctx.alloc,
nat_ctx,
T_refl,
func.return_type.underT(),
comptime func.return_type.underT(),
res,
cbk_info.getThis(),
isolate,
Expand Down
9 changes: 5 additions & 4 deletions src/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ const loadFn = @import("private_api.zig").loadFn;
// reflect the user-defined types to obtain type information (T_refl)
// This function must be called at comptime by the root file of the project
// and stored in a constant named `Types`
pub fn reflect(comptime types: anytype) []refl.Struct {
pub fn reflect(comptime types: anytype) []const refl.Struct {
std.debug.assert(@inComptime());

// call types reflection
return refl.do(types) catch unreachable;
const structs: []const refl.Struct = refl.do(types) catch |e| @compileError(@errorName(e));
return structs;
}

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

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

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

var apis: [types.len]API = undefined;
Expand Down
Loading

0 comments on commit 1ccf77b

Please sign in to comment.