Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding Lua in OCaml

Tags:

embed

ocaml

lua

Could you, please, give a code snippet showing how to use Lua embedded in OCaml?

A simple example could be a "Hello, World" variant. Have OCaml prompt the user for a name. Then pass that name to a Lua function. Have Lua print a greeting and return the length of the name. Then have OCaml print a message about the length of the name.

Example:

user@desktop:~$ ./hello.opt

Name? User

Hello, User.

Your name is 4 letters long.

user@desktop:~$

[Edit]

As a non-C programmer, could I implement this without having to write an intermediary C program to pass the data between Lua and OCaml?

Following is a theoretical idea of what I would like to try. Unfortunately, line 3 of ocaml_hello.ml would need to know how to call the function defined in lua_hello.lua in order for the code to be valid.

lua_hello.lua Defines lua_hello, which prints an argument and returns its length.

1  function lua_hello (name)
2    print ("Hello, "..name..".")
3    return (string.len (name))
4  end

ocaml_hello.ml OCaml prompts for a name, calls the Lua function, and prints the return value.

1  let () = print_string "Name? "; flush stdout in
2  let name = input_line stdin in
3  let len  = Lua_hello.lua_hello name in
4    Printf.printf "Your name is %d letters long." len; flush stdout;;
like image 490
ldfritz Avatar asked Jan 02 '09 04:01

ldfritz


1 Answers

I'm not aware of a mature set of bindings for embedding the C implementation of Lua into OCaml. An immature set of bindings was posted on the Caml mailing list in 2004.

If you want to use the ML implementation you can find some examples in a paper called ML Module Mania. The ML implementation, unlike the C implementation, guarantees type safety, but to do so it uses some very scurvy tricks in the ML module system. If you are asking basic questions, you probably want to avoid this.

In your example it's a little hard to guess where you want the function to come from. I suggest you either ask for a C example or give people a C example and ask how it could be realized in OCaml (though I think bindings are going to be a problem).


Edit

In response to the revised question, it's pretty complicated. The usual model is that you would put Lua in charge, and you would call Objective Caml code from Lua. You're putting Caml in charge, which makes things more complicated. Here's a rough sketch of what things might look like:

let lua = Lua.new()  (* create Lua interpreter *)
let chunk = LuaL.loadfile lua "hello.lua" (* load and compile the file hello.lua *)
let _ = Lua.call lua 0 0 (* run the code to create the hello function *)
let lua_len s =
  (* push the function; push the arg; call; grab the result; pop it; return *)
  let _ = Lua.getglobal lua "lua_hello" in
  let _ = Lua.pushstring lua s in
  let _ = Lua.call lua 1 1 in
  let len = Lua.tointeger lua (-1) in
  let _ = Lua.pop lua 1 in
  len

let () = print_string "Name? "; flush stdout
let name = input_line stdin
let len  = lua_len name
Printf.printf "Your name is %d letters long." len; flush stdout;;

Again, I don't know where you'll get the bindings for the Lua and LuaL modules.

like image 54
Norman Ramsey Avatar answered Nov 15 '22 05:11

Norman Ramsey