Upstream version 1.3.40
[profile/ivi/swig.git] / Examples / lua / funcptr3 / runme.lua
1 ---- importing ----
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')
5         assert(lib)()
6 else
7         -- lua 5.1 does
8         require('example')
9 end
10
11 a = 37
12 b = 42
13
14 -- Now call our C function
15
16 print("Trying some C functions")
17 print("    a        =", a)
18 print("    b        =", b)
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))
22
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))
27
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))
31
32 print("Now let us try something that will fail")
33 local ok,c=pcall(example.callback,a,b,print)
34 if ok==false then
35         print("this failed as expected, error:",c)
36 else
37         print("oops, that worked! result:",c)
38 end
39
40
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)
49
50 function bar(i) print("bar",i) end
51 print("setting to bar")
52 example.the_func=bar
53 print("call_the_func(5)")
54 example.call_the_func(5)