33 lines
798 B
Zig
33 lines
798 B
Zig
const std = @import("std");
|
|
const cjit = @import("cjit");
|
|
|
|
fn add(a: i32, b: i32) callconv(cjit.call) i32 {
|
|
return a + b;
|
|
}
|
|
|
|
test {
|
|
var rt: cjit.Runtime = try .init(std.testing.allocator);
|
|
defer rt.deinit();
|
|
|
|
try rt.compile("test.c",
|
|
\\int add(int a, int b);
|
|
\\
|
|
\\int add_one(int x)
|
|
\\{
|
|
\\ return add(x, 1);
|
|
\\}
|
|
);
|
|
|
|
try rt.setSymbol(fn (i32, i32) callconv(cjit.call) i32, "add", &add);
|
|
try rt.link();
|
|
|
|
const add_one = rt.getSymbol(fn (i32) callconv(cjit.call) i32, "add_one").?;
|
|
|
|
try std.testing.expectEqual(-9, add_one(-10));
|
|
try std.testing.expectEqual(11, add_one(10));
|
|
|
|
const add_ptr = rt.getSymbol(fn (i32, i32) callconv(cjit.call) i32, "add").?;
|
|
|
|
try std.testing.expectEqual(add_ptr, &add);
|
|
}
|