2 if string.sub(_VERSION,1,7)=='Lua 5.0' then
3 -- lua5.0 doesnt have a nice way to do this
4 lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
14 -- Now call our C function
16 print("Trying some C functions")
19 print(" add(a,b) =", example.add(a,b))
20 print(" sub(a,b) =", example.sub(a,b))
21 print(" mul(a,b) =", example.mul(a,b))
23 print("Calling them using the my_func()")
24 print(" add(a,b) =", example.callback(a,b,example.add))
25 print(" sub(a,b) =", example.callback(a,b,example.sub))
26 print(" mul(a,b) =", example.callback(a,b,example.mul))
28 print("Now let us write our own function")
29 function foo(a,b) return 101 end
30 print(" foo(a,b) =", example.callback(a,b,foo))
32 print("Now let us try something that will fail")
33 local ok,c=pcall(example.callback,a,b,print)
35 print("this failed as expected, error:",c)
37 print("oops, that worked! result:",c)
41 -- part2 stored function
42 print("trying a stored fn")
43 print("the_func=",example.the_func)
44 print("setting to print")
45 example.the_func=print
46 print("the_func=",example.the_func)
47 print("call_the_func(5)")
48 example.call_the_func(5)
50 function bar(i) print("bar",i) end
51 print("setting to bar")
53 print("call_the_func(5)")
54 example.call_the_func(5)