Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const assert = std.debug.assert; const print = std.debug.print; const WrappedFn = fn (?*c_void) callconv(.C) void; fn wrap(comptime Class: type, comptime func: anytype) WrappedFn { const Fn = @typeInfo(@TypeOf(func)).Fn; return struct { fn thunk(ptr: ?*c_void) callconv(.C) void { const instance = @ptrCast(*Class, ptr); const return_type = Fn.return_type.?; if (return_type != void) { @compileError("finalize must not return anything"); } const result = @call(.{ .modifier = .always_inline }, func, .{instance}); } }.thunk; } pub fn ForeignClass(name: []const u8, comptime Class: anytype) type { const ti = @typeInfo(Class); const meta = struct { pub fn isFinalize(comptime decl: std.builtin.TypeInfo.Declaration) bool { if (!std.mem.eql(u8, "finalize", decl.name)) return false; const fn_type_info = @typeInfo(decl.data.Fn.fn_type); if (fn_type_info.Fn.args.len != 1) return false; const arg1_info = @typeInfo(fn_type_info.Fn.args[0].arg_type orelse unreachable); if (arg1_info != .Pointer) return false; if (arg1_info.Pointer.child != Class) return false; const ret_info = @typeInfo(fn_type_info.Fn.return_type orelse unreachable); // No access to the VM, so no way to signal errors to it. if (ret_info != .Void) return false; return true; } }; return struct { pub fn printInfo() void { var finalizer: ?WrappedFn = undefined; inline for (ti.Struct.decls) |decl| { // comptime { switch (decl.data) { .Fn => { print("name = {s}\n", .{decl.name}); print(" function = {s}\n", .{@typeName(decl.data.Fn.fn_type)}); if (meta.isFinalize(decl)) { print(" found the finalize function!\n", .{}); finalizer = wrap(Class, @field(Class, decl.name)); } }, else => {}, } // } } } }; } const TestClass = struct { const Self = @This(); // Make the struct have a size > 0. data: [4]u8, pub fn initialize0(self: *Self) !void {} pub fn initialize3(self: *Self, x: i32, y: i32, z: i32) !void {} pub fn finalize(self: *Self) void {} pub fn call(self: *Self, str: []const u8) void {} }; pub fn main() !void { ForeignClass("TestClass", TestClass).printInfo(); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree