reverse-ffi IO
This commit is contained in:
+23
-22
@@ -21,7 +21,8 @@ endif
|
|||||||
|
|
||||||
$(OUT_DIR)/main: main.c lake | $(OUT_DIR)
|
$(OUT_DIR)/main: main.c lake | $(OUT_DIR)
|
||||||
# Add library paths for Lake package and for Lean itself
|
# Add library paths for Lake package and for Lean itself
|
||||||
emcc -o $@ $< -I $(LEAN_SYSROOT)/include -L $(LEAN_LIBDIR) lib/build/ir/RFFI.c -lInit -lLean -lleancpp -lleanrt $(LINK_FLAGS)
|
emcc -o $@ $< -I $(LEAN_SYSROOT)/include -L $(LEAN_LIBDIR) lib/build/ir/RFFI.c -lInit -lLean -lleancpp -lleanrt $(LINK_FLAGS) \
|
||||||
|
-sFORCE_FILESYSTEM -lnodefs.js -s EXIT_RUNTIME=1 -s MAIN_MODULE=1 -s LINKABLE=1 -s EXPORT_ALL=1 -s ALLOW_MEMORY_GROWTH=1 -fwasm-exceptions -pthread -flto
|
||||||
|
|
||||||
run: $(OUT_DIR)/main
|
run: $(OUT_DIR)/main
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
@@ -34,26 +35,26 @@ endif
|
|||||||
# Alternatively, we can copy all shared lib dependencies to the current directory
|
# Alternatively, we can copy all shared lib dependencies to the current directory
|
||||||
# in order to avoid path set up and obtain a more portable executable
|
# in order to avoid path set up and obtain a more portable executable
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
# ifeq ($(OS),Windows_NT)
|
||||||
SHLIB_PREFIX :=
|
# SHLIB_PREFIX :=
|
||||||
SHLIB_EXT := dll
|
# SHLIB_EXT := dll
|
||||||
LEAN_SHLIB := $(LEAN_SYSROOT)/bin/libleanshared.$(SHLIB_EXT)
|
# LEAN_SHLIB := $(LEAN_SYSROOT)/bin/libleanshared.$(SHLIB_EXT)
|
||||||
else
|
# else
|
||||||
SHLIB_PREFIX := lib
|
# SHLIB_PREFIX := lib
|
||||||
# Add current directory to loader path (default on Windows)
|
# # Add current directory to loader path (default on Windows)
|
||||||
ifeq ($(shell uname -s),Darwin)
|
# ifeq ($(shell uname -s),Darwin)
|
||||||
LINK_FLAGS_LOCAL := -Wl,-rpath,@executable_path
|
# LINK_FLAGS_LOCAL := -Wl,-rpath,@executable_path
|
||||||
SHLIB_EXT := dylib
|
# SHLIB_EXT := dylib
|
||||||
else
|
# else
|
||||||
LINK_FLAGS_LOCAL := -Wl,-rpath,'$${ORIGIN}'
|
# LINK_FLAGS_LOCAL := -Wl,-rpath,'$${ORIGIN}'
|
||||||
SHLIB_EXT := so
|
# SHLIB_EXT := so
|
||||||
endif
|
# endif
|
||||||
LEAN_SHLIB=$(LEAN_LIBDIR)/libleanshared.$(SHLIB_EXT)
|
# LEAN_SHLIB=$(LEAN_LIBDIR)/libleanshared.$(SHLIB_EXT)
|
||||||
endif
|
# endif
|
||||||
|
|
||||||
$(OUT_DIR)/main-local: main.c lake | $(OUT_DIR)
|
# $(OUT_DIR)/main-local: main.c lake | $(OUT_DIR)
|
||||||
cp -f $(LEAN_SHLIB) lib/build/lib/$(SHLIB_PREFIX)RFFI.$(SHLIB_EXT) $(OUT_DIR)
|
# cp -f $(LEAN_SHLIB) lib/build/lib/$(SHLIB_PREFIX)RFFI.$(SHLIB_EXT) $(OUT_DIR)
|
||||||
cc -o $@ $< -I $(LEAN_SYSROOT)/include -L $(OUT_DIR) -lRFFI -lleanshared $(LINK_FLAGS_LOCAL)
|
# cc -o $@ $< -I $(LEAN_SYSROOT)/include -L $(OUT_DIR) -lRFFI -lleanshared $(LINK_FLAGS_LOCAL)
|
||||||
|
|
||||||
run-local: $(OUT_DIR)/main-local
|
# run-local: $(OUT_DIR)/main-local
|
||||||
$(OUT_DIR)/main-local
|
# $(OUT_DIR)/main-local
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import Lean
|
||||||
|
|
||||||
@[export my_length]
|
@[export my_length]
|
||||||
def myLength (s : String) : UInt64 :=
|
def myLength (s : String) : IO Unit := do
|
||||||
s.length.toUInt64
|
IO.println "hello"
|
||||||
|
return () -- IO.println Lean.origin
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <lean/lean.h>
|
#include <lean/lean.h>
|
||||||
|
|
||||||
extern uint64_t my_length(lean_obj_arg);
|
extern lean_object* my_length(lean_object*, lean_object*);
|
||||||
|
|
||||||
// see https://leanprover.github.io/lean4/doc/dev/ffi.html#initialization
|
// see https://leanprover.github.io/lean4/doc/dev/ffi.html#initialization
|
||||||
extern void lean_initialize_runtime_module();
|
extern void lean_initialize_runtime_module();
|
||||||
@@ -10,11 +10,13 @@ extern void lean_io_mark_end_initialization();
|
|||||||
extern lean_object * initialize_RFFI(uint8_t builtin, lean_object *);
|
extern lean_object * initialize_RFFI(uint8_t builtin, lean_object *);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
lean_initialize();
|
||||||
lean_initialize_runtime_module();
|
lean_initialize_runtime_module();
|
||||||
lean_object * res;
|
lean_object * res;
|
||||||
// use same default as for Lean executables
|
// use same default as for Lean executables
|
||||||
uint8_t builtin = 1;
|
uint8_t builtin = 1;
|
||||||
res = initialize_RFFI(builtin, lean_io_mk_world());
|
lean_object * io_world = lean_io_mk_world();
|
||||||
|
res = initialize_RFFI(builtin, io_world);
|
||||||
if (lean_io_result_is_ok(res)) {
|
if (lean_io_result_is_ok(res)) {
|
||||||
lean_dec_ref(res);
|
lean_dec_ref(res);
|
||||||
} else {
|
} else {
|
||||||
@@ -27,6 +29,5 @@ int main() {
|
|||||||
// actual program
|
// actual program
|
||||||
|
|
||||||
lean_object * s = lean_mk_string("hello!");
|
lean_object * s = lean_mk_string("hello!");
|
||||||
uint64_t l = my_length(s);
|
my_length(s, lean_io_mk_world());
|
||||||
printf("output: %ld\n", l);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,4 +3,3 @@ set -ex
|
|||||||
./clean.sh
|
./clean.sh
|
||||||
|
|
||||||
make run
|
make run
|
||||||
make run-local
|
|
||||||
|
|||||||
Reference in New Issue
Block a user