Last active
November 18, 2018 05:27
-
-
Save daurnimator/c8359e540988e859eea1897b4c3d4c45 to your computer and use it in GitHub Desktop.
Attempt at using zig's export to C feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef THING_H | |
#define THING_H | |
#include <stdint.h> | |
#ifdef __cplusplus | |
#define THING_EXTERN_C extern "C" | |
#else | |
#define THING_EXTERN_C | |
#endif | |
#if defined(_WIN32) | |
#define THING_EXPORT THING_EXTERN_C __declspec(dllimport) | |
#else | |
#define THING_EXPORT THING_EXTERN_C __attribute__((visibility ("default"))) | |
#endif | |
struct Node; | |
struct makeThing(u32); | |
THING_EXPORT void Init(struct makeThing(u32) * ptr, uint32_t x); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Attempt at using zig's export to C feature | |
// Compile with `zig build-lib --output-h thing.h thing.zig` | |
const std = @import("std"); | |
const List = std.LinkedList(void); | |
fn makeThing(comptime member:type) type { | |
return struct { | |
const Self = @This(); | |
// intrusive LinkedList | |
node: List.Node, | |
/// initialize Thing structure | |
pub fn init(x:member) Self { | |
return Self { | |
.node = List.Node.init(undefined), | |
.x = x, | |
}; | |
} | |
x: member, | |
pub fn get() member { | |
return x; | |
} | |
}; | |
} | |
const u32Thing = makeThing(u32); | |
export fn InitializeThing(ptr: *u32Thing, x:u32) void { | |
ptr.* = u32Thing.init(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment