MSK Software Development

LuaInterface 0.3

by parc on Jul.20, 2008, under Smalltalk

Several APIs added to set and retrieve values within the LUA environment. Several tests added.

Here are some examples, which are also included within the tests:

The idea of the first example shows, how a LUA script is executed (actually it is created within Smalltalk) and then the interpreter is asked for the value of a variable changed in that script.

| src aNumber |
  src := WriteStream on: String new.
  aNumber := Time now asMilliseconds.
  "First we create LUA souce which looks like w = 137482374"
    nextPutAll: 'w = ' ; nextPutAll: aNumber printString ; cr.
  "then we execue it which sets w to an integer value"
    doString: src contents.
  "then we query the interpreter for the value of 'w'"
    getGlobalNamed: 'w'.
  "and we check if its an integer and we check the value"
    assert: ((lua isNumber: -1) ) ;
    assert: ((lua toInteger: -1) = aNumber).

The next script sets the value of a LUA variable BEFORE executing LUA script. The script itselfs just returns the value set before executing the script:

| src aNumber |

aNumber := Time now asMilliseconds.
src := WriteStream on: String new.
  nextPutAll: 'return w' ; cr.
"we set the value in the new interpreter environment"
  pushInteger: aNumber ;
  setGlobalNamed: 'w' ;
"then we execute the code created above - simply returning the value again"
  doString: src contents.
"... and checking the results"
self assert: ((lua isNumber: -1) ).
self assert: ((lua toInteger: -1) = aNumber).



:, ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...