Skip to content

Instantly share code, notes, and snippets.

@maxdemarzi
Forked from ihnorton/hello_julia.lua
Created April 7, 2022 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxdemarzi/8652c7cf3aedfad558c18aaa2a468242 to your computer and use it in GitHub Desktop.
Save maxdemarzi/8652c7cf3aedfad558c18aaa2a468242 to your computer and use it in GitHub Desktop.
Calling Julia from LuaJIT via a shared library
ffi = require("ffi")
julia = ffi.load("/Users/inorton/git/julia/usr/lib/libjulia.dylib", true)
ffi.cdef[[
void jl_eval_string(const char*);
void jl_init(const char*);
]]
julia.jl_init("/Users/inorton/git/julia/usr/lib/")
julia.jl_eval_string(" println(\"hello, world\") ")
-- should print "hello, world"
#include "julia.h"
int
loadjl(char* path)
{
jl_init(path);
JL_SET_STACK_BASE;
jl_eval_string("println(sqrt(2.0))");
return 0;
}
local ffi = require("ffi")
local test = ffi.load("./test.so", true) -- note the optional second argument means to load symbols globally
ffi.cdef[[
void loadjl(char*);
]]
local path = ffi.new("char[19]", "/cmn/jldev/usr/bin")
test.loadjl(path)
gcc -o test.so -I$JULIA_DIR/src -I$JULIA_DIR/src/support -I$JULIA_DIR/usr/include -L$JULIA_DIR/usr/lib -fPIC -shared test.c -ljulia-debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment