Lua

29 Nov 2014

I was just looking into methods of scripting inside of my mbed application, and decided I'd give Lua a shot to see how well it works. This is not eLua but just the Lua interpreter and language embedded inside my app.

To build Lua it required a handful of tweaks to the source to exclude some functions that are not available (feof, time). The entire thing uses 105k of flash. That includes all of the debugging, file IO, and parser / compiler. It should be possible to trim that down to about half that size, especially if the parser is not needed and it will just execute existing bytecode.

Performance-wise I was able to call my C defined LED routine twice (on then off) 1,000,000 times in 19.156 seconds. That's 52khz. That includes all the overhead of calling from Lua to my C routine.

Here's the test lua code:

for i = 0, 1000000, 1 do
     mbed.LED(true)
     mbed.LED(false)
end

For comparison, the C version of that executes in 0.29 seconds:

for (int i=0; i<1000000; i++) {
     myled=true;
     myled=false;
}

Lua can count to 1,000,000 on my device (F401RE, stock clock speed) in 10.239 seconds (not calling back into the C code).

I can post my C code that sets up and calls into Lua if anyone is interested. It's very easy to use.

29 Nov 2014

I was loading the base, string, table and math libraries. If none of those are loaded then my executable flash size drops to 73k. I believe it would be around 50k for just the interpreter without the parser / compiler. Other routines can surely be removed that do not apply if one really wishes to trim it down further.

17 Dec 2014

Dan,

I posted a Lua project yesterday - didn't see your work before I posted. I had trouble with fgets working...how did you solve this problem?

Scott

19 Dec 2014

I haven't made use of liolib to read from files, and the other use of fgets is in a debugging routine that I've also not used. Is fgets buggy in the mbed libraries or something?

08 Mar 2019

Hi Dan,

I am currently looking to use Lua on MBedOS to run some scripts to control cellular modems. I saw from the comments above that you had managed to get Lua ported onto MBed OS. I tried to find the port on MBed but couldnt't find it. Do you mind sharing your port for me to take a look.