aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/shimmer.zig
blob: 3f61dad96f3a5f1c5d56c005abbc09a9c307f03d (plain) (blame)
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
const std = @import("std");
const StaticExport = @import("./static_export.zig");
const Sizes = @import("./sizes.zig");
const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false;

pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comptime Parent: type) type {
    return struct {
        pub const namespace = _namespace;
        pub const name = _name;

        fn toCppType(comptime FromType: ?type) ?type {
            return comptime brk: {
                var NewReturnType = FromType orelse c_void;

                if (NewReturnType == c_void) {
                    break :brk FromType;
                }

                var ReturnTypeInfo: std.builtin.TypeInfo = @typeInfo(FromType orelse c_void);

                if (ReturnTypeInfo == .Pointer and NewReturnType != *c_void) {
                    NewReturnType = ReturnTypeInfo.Pointer.child;
                    ReturnTypeInfo = @typeInfo(NewReturnType);
                }

                switch (ReturnTypeInfo) {
                    .Union,
                    .Struct,
                    .Enum,
                    => {
                        if (@hasDecl(ReturnTypeInfo, "Type")) {
                            break :brk NewReturnType;
                        }
                    },
                    else => {},
                }

                break :brk FromType;
            };
        }
        pub const align_of_symbol = std.fmt.comptimePrint("{s}__{s}_object_align_", .{ namespace, name });
        pub const size_of_symbol = std.fmt.comptimePrint("{s}__{s}_object_size_", .{ namespace, name });
        pub const byte_size = brk: {
            const identifier = std.fmt.comptimePrint("{s}__{s}", .{ namespace, name });
            const align_symbol = std.fmt.comptimePrint("{s}__{s}_align", .{ namespace, name });
            if (@hasDecl(Sizes, identifier)) {
                break :brk @field(Sizes, identifier); //+ @field(Sizes, align_symbol);
            } else {
                break :brk 0;
            }
        };
        pub const Bytes = [byte_size]u8;

        pub inline fn getConvertibleType(comptime ZigType: type) type {
            if (@typeInfo(ZigType) == .Struct) {
                const Struct: std.builtin.TypeInfo.Struct = ChildType.Struct;
                for (Struct.fields) |field| {
                    if (std.mem.eql(u8, field.name, "ref")) {
                        return field.field_type;
                    }
                }
            }

            return ZigType;
        }

        fn pointerChild(comptime Type: type) type {
            if (@typeInfo(Type) == .Pointer) {
                return @typeInfo(Type).Pointer.child_type;
            }

            return Type;
        }

        pub inline fn toZigType(comptime ZigType: type, comptime CppType: type, value: CppType) *ZigType {
            if (comptime hasRef(ZigType)) {
                // *WTF::String => Wtf.String{ = value}, via casting instead of copying
                if (comptime @typeInfo(CppType) == .Pointer and @typeInfo(ZigType) != .Pointer) {
                    return @bitCast(ZigType, @ptrToInt(value));
                }
            }

            return @as(ZigType, value);
        }

        pub fn symbolName(comptime typeName: []const u8) []const u8 {
            return comptime std.fmt.comptimePrint("{s}__{s}__{s}", .{ namespace, name, typeName });
        }

        pub fn exportFunctions(comptime Functions: anytype) [std.meta.fieldNames(@TypeOf(Functions)).len]StaticExport {
            const FunctionsType = @TypeOf(Functions);
            return comptime brk: {
                var functions: [std.meta.fieldNames(FunctionsType).len]StaticExport = undefined;
                for (std.meta.fieldNames(FunctionsType)) |fn_name, i| {
                    const Function = @TypeOf(@field(Functions, fn_name));
                    if (@typeInfo(Function) != .Fn) {
                        @compileError("Expected " ++ @typeName(Parent) ++ "." ++ @typeName(Function) ++ " to be a function but received " ++ @tagName(@typeInfo(Function)));
                    }
                    var Fn: std.builtin.TypeInfo.Fn = @typeInfo(Function).Fn;
                    if (Fn.calling_convention != .C) {
                        @compileError("Expected " ++ @typeName(Parent) ++ "." ++ @typeName(Function) ++ " to have a C Calling Convention.");
                    }

                    const export_name = symbolName(fn_name);
                    functions[i] = StaticExport{
                        .Type = Function,
                        .symbol_name = export_name,
                        .local_name = fn_name,
                        .Parent = Parent,
                    };
                }

                break :brk functions;
            };
        }

        pub inline fn zigFn(comptime typeName: []const u8, args: anytype) (@typeInfo(@TypeOf(@field(Parent, typeName))).Fn.return_type orelse void) {
            const identifier = symbolName(typeName);
            const func = comptime @typeInfo(Parent).Struct.fields[std.meta.fieldIndex(Parent, typeName)].field_type;
            const ReturnType = comptime @typeInfo(func).Fn.return_type orelse c_void;

            const Func: type = comptime brk: {
                var FuncType: std.builtin.TypeInfo = @typeInfo(@TypeOf(func));
                var Fn: std.builtin.TypeInfo.Fn = FuncType.Fn;

                Fn.calling_convention = std.builtin.CallingConvention.C;
                Fn.return_type = toCppType(Fn.return_type);

                const ArgsType = @TypeOf(args);
                for (std.meta.fieldNames(args)) |field, i| {
                    Fn.args[i] = std.builtin.TypeInfo.FnArg{
                        .is_generic = false,
                        .is_noalias = false,
                        .arg_type = @typeInfo(ArgsType).fields[i].field_type,
                    };
                }
                FuncType.Fn = Fn;
                break :brk @Type(FuncType);
            };

            comptime @export(Func, .{ .name = identifier });
            unreachable;
        }

        pub inline fn cppFn(comptime typeName: []const u8, args: anytype) (ret: {
            if (!@hasDecl(Parent, typeName)) {
                @compileError(@typeName(Parent) ++ " is missing cppFn: " ++ typeName);
            }
            break :ret std.meta.declarationInfo(Parent, typeName).data.Fn.return_type;
        }) {
            if (comptime is_bindgen) {
                unreachable;
            } else {
                const identifier = comptime std.fmt.comptimePrint("{s}__{s}__{s}", .{ namespace, name, typeName });
                const func = comptime @typeInfo(Parent).Struct.fields[std.meta.fieldIndex(Parent, typeName)].field_type;
                const ReturnType = comptime @typeInfo(func).Fn.return_type orelse c_void;

                const Func: type = comptime brk: {
                    var FuncType: std.builtin.TypeInfo = @typeInfo(@TypeOf(func));
                    var Fn: std.builtin.TypeInfo.Fn = FuncType.Fn;

                    Fn.calling_convention = std.builtin.CallingConvention.C;
                    Fn.return_type = toCppType(Fn.return_type);

                    const ArgsType = @TypeOf(args);
                    for (std.meta.fieldNames(args)) |field, i| {
                        Fn.args[i] = std.builtin.TypeInfo.FnArg{
                            .is_generic = false,
                            .is_noalias = false,
                            .arg_type = @typeInfo(ArgsType).fields[i].field_type,
                        };
                    }
                    FuncType.Fn = Fn;
                    break :brk @Type(FuncType);
                };
                const Outgoing = comptime @extern(Func, std.builtin.ExternOptions{ .name = identifier });

                return toZigType(
                    ReturnType,
                    @typeInfo(Func).Fn.return_type orelse void,
                    @call(.{}, Outgoing, args),
                );
            }
        }
    };
}