Here is the Smalltalk version of Calling Lua Functions.
We assume that the following code is located in an external file:
function f (x, y)
return (x^2 * math.sin(y))/(1 - x)
end
and here is the Smalltalk code to call the Lua function with parameters from Smalltalk – directly via C-API:
anInterface := MSKLuaLibrary newLua.
anInterface doFile: 'd:\Example02.lua'.
"Opens system libraries"
anInterface
openLibs.
anInterface
"function to be called"
getGlobalNamed: 'f' ;
"parameter value for 'x'"
pushNumber: 4.0 ;
"parameter value for 'y'"
pushNumber: 2.0 ;
"call function with parameters"
pCall: 2 results: 1 entryPoint: 0.
"Is top of stack a number -> should be"
(anInterface isNumber: -1)
ifTrue:[
| z |
"get top of stack and convert it to number"
z := anInterface toNumberX: -1.
"clean up stack"
anInterface pop: 1.
z ]
ifFalse:[ 'Error !' ]