File: runme.lua

package info (click to toggle)
swig 4.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 42,876 kB
  • sloc: cpp: 61,013; ansic: 27,612; java: 14,670; python: 10,632; cs: 8,103; makefile: 6,287; yacc: 6,197; sh: 5,247; ruby: 5,172; perl: 3,541; php: 2,069; ml: 2,066; lisp: 1,894; javascript: 1,300; tcl: 1,091; xml: 115
file content (54 lines) | stat: -rw-r--r-- 1,429 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---- importing ----
if string.sub(_VERSION,1,7)=='Lua 5.0' then
	-- lua5.0 doesn't have a nice way to do this
	lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
	assert(lib)()
else
	-- lua 5.1 does
	require('example')
end

a = 37
b = 42

-- Now call our C function

print("Trying some C functions")
print("    a        =", a)
print("    b        =", b)
print("    add(a,b) =", example.add(a,b))
print("    sub(a,b) =", example.sub(a,b))
print("    mul(a,b) =", example.mul(a,b))

print("Calling them using the my_func()")
print("    add(a,b) =", example.callback(a,b,example.add))
print("    sub(a,b) =", example.callback(a,b,example.sub))
print("    mul(a,b) =", example.callback(a,b,example.mul))

print("Now let us write our own function")
function foo(a,b) return 101 end
print("    foo(a,b) =", example.callback(a,b,foo))

print("Now let us try something that will fail")
local ok,c=pcall(example.callback,a,b,print)
if ok==false then
	print("this failed as expected, error:",c)
else
	print("oops, that worked! result:",c)
end


-- part2 stored function
print("trying a stored fn")
print("the_func=",example.the_func)
print("setting to print")
example.the_func=print
print("the_func=",example.the_func)
print("call_the_func(5)")
example.call_the_func(5)

function bar(i) print("bar",i) end
print("setting to bar")
example.the_func=bar
print("call_the_func(5)")
example.call_the_func(5)