Hello,
I am embeding the Lua interpreter ocaml-lua into my application. I want to bind an ocaml function returning a Lwt
promise f : unit -> int Lwt.t
. The execution of the lua script will be made by Lwt_main.run
. My attempt was to bind the function
function () -> Lwt_main.run @@ f ()
as follows
val f : unit -> int Lwt.t
let g (lua : Lua.state) =
let n = Lwt_main.run (f ()) in
let () = Lua.pushinteger lua n in
1
let open_api (lua : Lua.state) =
let () = Lua.pushocamlfunction lua g in
let () = Lua.setglobal lua "f" in
()
let script = {eos|
x = f()
print(x)
|eos}
let run_script () =
let lua = LuaL.newstate () in
let () = LuaL.openlibs lua in
let () = open_api lua in
let _status = LuaL.loadbuffer lua script "my_script" in
match Lua.pcall lua 0 0 0 with
| Lua.LUA_OK -> ()
| _ -> let () = Lua.pop lua 1 in
failwith "ERROR"
But run_script
will be called in Lwt_main.run
and we will have nested calls to Lwt_main.run
, which is not allowed.
Any idea on how to bind the function f
? In the lua script, the expected behavior is that f()
waits until the promise is resolved.
Thank you!