Skip to content

Commit

Permalink
Update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter committed Jun 13, 2022
1 parent 4e58e68 commit 9b06b82
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,24 +861,25 @@ We've already seen examples of Lua code calling Terra functions. In general, you

More examples are in `tests/luabridge*.t`.

It is also possible to call Lua functions from Terra. Again, the translation from Terra objects to Lua uses LuaJITs conversion rules. Primitive types like `double` will be converted to their respective Lua type, while aggregate and derived types will be boxed in a LuaJIT `ctype` that can be modified from Lua:
It is also possible to call Lua functions from Terra. Again, the translation from Terra objects to Lua uses LuaJITs conversion rules. Primitive types like `double` will be converted to their respective Lua type, while aggregate and derived types will be boxed in a LuaJIT `ctype` that can be modified from Lua. Note that the function `add1` must be cast explicitly, as otherwise Terra cannot determine the type of its arguments.

function add1(a)
a.real = a.real + 1
end
struct Complex { real : double, imag : double }
tadd1 = terralib.cast({&Complex}->{},add1)
terra doit()
var a : Complex = {1,2}
add1(&a)
var a = Complex {1,2}
tadd1(&a)
return a
end
a = doit()
print(a.real,a.imag) -- 2 1
print(a.real,a.imag) -- 2 2
print(type(a)) -- cdata

The file `tests/terralua.t` includes more examples. The file `tests/terraluamethod.t` also demonstrate using Lua functions inside the method table of a terra object.

since we cannot determine the Terra types that function will return, Lua functions do not return values to Terra functions by default. To convert a Lua function into a Terra function that does return a value, you first need to `cast` it to a Terra function type:
The `cast` syntax can also be used to declare the return type of the Lua function, as shown below.

function luaadd(a,b) return a + b end
terraadd = terralib.cast( {int,int} -> int, luaadd)
Expand Down

0 comments on commit 9b06b82

Please sign in to comment.