emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/beardbolt d257e51fce 244/323: starters/zig: Automatical


From: ELPA Syncer
Subject: [elpa] externals/beardbolt d257e51fce 244/323: starters/zig: Automatically export functions
Date: Thu, 9 Mar 2023 10:58:35 -0500 (EST)

branch: externals/beardbolt
commit d257e51fce1bc6796be93854048bdcab65106ecc
Author: Erik Arvstedt <erik.arvstedt@gmail.com>
Commit: Erik Arvstedt <erik.arvstedt@gmail.com>

    starters/zig: Automatically export functions
    
    This is makes experimenting in the starter file much more convenient.
    Previously, each Zig fn had to be manually exported by referencing it in
    the separate `export` fn.
---
 starters/rmsbolt.zig | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/starters/rmsbolt.zig b/starters/rmsbolt.zig
index ace2786ec6..4f4ad1c52e 100644
--- a/starters/rmsbolt.zig
+++ b/starters/rmsbolt.zig
@@ -16,20 +16,33 @@ export fn isRMS(a: u8) u8 {
     };
 }
 
-// Functions marked with `export` use the C calling convention, so its 
parameters and
-// return value can only have C types.
-// To export a native Zig fn, use the following pattern:
-fn zigFn(xs: []u8) []u8 {
+// Exported by `exportFns` below
+pub fn zigFn(xs: []u8) []u8 {
     for (xs) |*x| {
         x.* *= 2;
     }
     return xs;
 }
 
-export fn exportZigFn() usize {
-    return @ptrToInt(zigFn);
+// Export all public, non-generic functions in this file.
+// This is needed because functions that accept or return Zig-specific types 
can't be marked
+// with `export`.
+// `export` is limited to functions that only accept or return C types, which 
makes them
+// compatible with the C calling convention.
+export fn exportPubFns() usize {
+    var fns: usize = 0;
+    inline for (@typeInfo((@This())).Struct.decls) |decl| {
+        if (!decl.is_pub) continue;
+        const field = @field(@This(), decl.name);
+        const info = @typeInfo(@TypeOf(field));
+        if (info == .Fn and !info.Fn.is_generic) {
+            fns += @ptrToInt(field);
+        }
+    }
+    return fns;
 }
 
+
 // In some cases, Zig embeds a panic handler that prints stack traces, causing 
a
 // disassembly much larger than normal.
 // You can optionally place this function in files you disassemble to make 
them easier to digest.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]