VASmalltalk – Call a Lua function with parameters from Smalltalk

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)

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"

  "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) 
    | z |
    "get top of stack and convert it to number"
    z := anInterface toNumberX: -1.
    "clean up stack"
    anInterface pop: 1.
    z ]
  ifFalse:[ 'Error !' ]
This entry was posted in Smalltalk and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s