-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
const std = @import("std");
const ApiSpec = @import("./ApiSpec.zig");
const public_api = @import("./api.zig").public_api;
fn CSetterValue(comptime T: type) type {
return switch (@typeInfo(T)) {
.pointer => |pointer| switch (pointer.size) {
.slice => if (pointer.is_const) [*:0]const pointer.child else [*:0]pointer.child,
else => @compileError("Non-slice pointer is not supported."),
},
.optional => |optional| CSetterValue(optional.child),
else => T,
};
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var args = std.process.ArgIterator.init();
defer args.deinit();
_ = args.skip();
const output = args.next() orelse {
return error.NotEnoughArgument;
};
if (args.next()) |_| {
return error.TooManyArgument;
}
const items = try allocator.alloc(ApiSpec.Item, public_api.len);
errdefer allocator.free(items);
inline for (public_api, 0..) |Api, i| {
switch (@typeInfo(Api)) {
.@"enum" => |info| {
const fields = try allocator.alloc(ApiSpec.EnumField, info.fields.len);
inline for (info.fields, 0..) |field, j| {
fields[j] = .{
.name = try std.ascii.allocUpperString(allocator, field.name),
.value = field.value,
};
}
items[i] = .{ .@"enum" = .{
.cname = Api.cname,
.fields = fields,
} };
},
.@"struct" => |_| {
const self_pointer: ApiSpec.CType = .{
.pointer = .{
.@"const" = false,
.type = .{ .custom = Api.Data.cname },
},
};
const ref: ApiSpec.Function = .{
.name = "retain",
.parameters = &.{.{ .type = self_pointer }},
.return_type = self_pointer,
};
const unref: ApiSpec.Function = .{
.name = "release",
.parameters = &.{.{ .type = self_pointer }},
.return_type = .{ .value = .void },
};
const info = @typeInfo(Api.Data).@"struct";
if (info.layout == .@"extern") {
// Plain struct or Deserializable
const fields = try allocator.alloc(ApiSpec.StructField, info.fields.len);
inline for (info.fields, 0..) |field, j| {
fields[j] = .{
.name = field.name,
.type = .fromZigType(field.type),
};
}
const from_json: ApiSpec.Function = .{
.name = "from_json",
.parameters = &.{
.{ .type = .{
.pointer = .{
.@"const" = true,
.type = .char,
},
} },
},
.return_type = self_pointer,
};
items[i] = .{
.@"struct" = .{
.cname = Api.Data.cname,
.fields = fields,
.new_function = if (@hasDecl(Api.Data, "deserializable") and Api.Data.deserializable) from_json else null,
.member_functions = &.{ ref, unref },
},
};
continue;
}
// Serializable
const init = @typeInfo(@TypeOf(Api.Data.init)).@"fn";
const init_params = try allocator.alloc(ApiSpec.FunctionParameter, init.params.len);
inline for (init.params, 0..) |param, j| {
init_params[j] = .{
.type = .fromZigType(param.type.?),
};
}
const methods = try allocator.alloc(ApiSpec.Function, info.fields.len + 3);
methods[info.fields.len + 0] = ref;
methods[info.fields.len + 1] = unref;
methods[info.fields.len + 2] = .{
.name = "to_json",
.parameters = &.{.{ .type = self_pointer }},
.return_type = .fromZigType([*:0]const u8),
};
inline for (info.fields, 0..) |field, j| {
if (field.type == [][]const u8) {
methods[j] = .{
.name = std.fmt.comptimePrint("append_{s}", .{field.name}),
.parameters = &.{
.{ .type = self_pointer },
.{ .type = .fromZigType([*:0]const u8) },
},
.return_type = .{ .value = .void },
};
} else {
methods[j] = .{
.name = std.fmt.comptimePrint("set_{s}", .{field.name}),
.parameters = &.{
.{ .type = self_pointer },
.{ .type = .fromZigType(CSetterValue(field.type)) },
},
.return_type = .{ .value = .void },
};
}
}
items[i] = .{
.@"opaque" = .{
.cname = Api.Data.cname,
.new_function = .{
.name = "new",
.parameters = init_params,
.return_type = self_pointer,
},
.member_functions = methods,
},
};
},
else => {
@compileError(std.fmt.comptimePrint("Cannot export {s}", .{@typeName(Api)}));
},
}
}
var output_file = try std.fs.cwd().createFile(output, .{});
defer output_file.close();
const writer = output_file.writer();
try std.json.stringify(ApiSpec{ .items = items }, .{}, writer);
return std.process.cleanExit();
}