Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: ExtendeD on June 25, 2011, 05:56:31 am

Title: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: ExtendeD on June 25, 2011, 05:56:31 am
It appears that during its function cleanup of the TI-Nspire implementation, TI fortunately kept the standard Lua function loadstring() that allows dynamic loading of Lua code snippets.

Here is a quick and dirty example of a command prompt to execute Lua on-liners:
Code: [Select]
function on.create()
editor = D2Editor.newRichText()
editor:move(0,150)
editor:resize(250,50)
end

function on.charIn(char)
editor:setText(editor:getText() .. char)
end
 
function on.enterKey()
local chunk, errc = loadstring(editor:getText())
if (not chunk) then
print(errc)
else
local status, err = pcall(chunk)
if (not status) then
print(err)
end
end
editor:setText("")
end

function on.backspaceKey()
editor:setText(editor:getText():sub(1, editor:getText():len() - 1))
end

Now imagine this combined with calls to var.store() and var.recall(), and you have a on-calc Lua code compiler :)
Unfortunately I have too many simultaneous projects to invest more on this, but I'm sure some of you will be able to build interesting tools from this.
Title: Re: Loading Lua code dynamically for fun and profit
Post by: Munchor on June 25, 2011, 06:05:52 am
This seems just like Jim Bauwen's Lua Console. Yours reads lines like his, but his also reads files, as you suggested in the end.

Not sure if yours has any improvement, but it's funny because both of you got to the same conclusion.

However, I hope TI doesn't remove those functions, a dynamic interpreter is very cool :) Nice job ExtendeD!
Title: Re: Loading Lua code dynamically for fun and profit
Post by: ExtendeD on June 25, 2011, 06:09:56 am
jimbauwens, ephan pointed me to this (http://ourl.ca/11333/215527). Does the console really read files, bypassing TI's filters?
Title: Re: Loading Lua code dynamically for fun and profit
Post by: Ashbad on June 25, 2011, 07:37:25 am
If they take it away though, would it really be that hard to make your own loadstring() function?  It doesnt seems as though it would.
Title: Re: Loading Lua code dynamically for fun and profit
Post by: fb39ca4 on June 25, 2011, 08:00:38 am
If it has to compile the code, then yes it'll be hard.
But this has some interesting possibilities. Self modifying code!
Title: Re: Loading Lua code dynamically for fun and profit
Post by: Jim Bauwens on June 25, 2011, 10:53:15 am
ExtendeD, it allows loading variables in the current document, or variables in libraries (not in the local document).

In case you want to try my lua console, you can find the current version here: http://bwns.be/jim/term.lua . It has basic history support and runs everything in a coroutine, which makes that I can have functions such as pause. I have added these functions: print, pause, sleep, readinp, load and save. load("varname") will load and execute the content of varname. save("data","varname") will save data in varname. I'm currently planning an oncalc Lua programmer that utilizes some of these functions.
Title: Re: Loading Lua code dynamically for fun and profit
Post by: ExtendeD on June 26, 2011, 03:02:44 am
Nice :)
Title: Re: Loading Lua code dynamically for fun and profit
Post by: Munchor on June 26, 2011, 06:25:40 am
When Ndless 3 comes, I think a C Text Editor that zipped TNSs like Lua>TNS Converters do won't be too hard, right?
Title: Re: Loading Lua code dynamically for fun and profit
Post by: Lionel Debroux on June 26, 2011, 07:37:15 am
Well, no.
A C text editor that reads and writes plain text documents (which cannot be opened with the built-in document viewer/editor) is significantly easier than a C text editor that reads and writes normal TNS documents that are made up by using TIXC (TI XML Compression, proprietary and patented) and currently unspecified data structures, Zlib and OpenSSL-based triple DES ECB ;)

The method used by the third-party Lua->TNS converters wouldn't help - for a start, it doesn't work on OS 3.0.2 anyway.
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: ExtendeD on July 03, 2011, 04:04:16 am
Here is a first shot at an on-calc Lua editor, oclua.
The standard Notes editor is used to type in Lua code, then the code just needs to be pasted to be run.

The file is built for OS 3.0.1, it needs to be rebuilt with TI's official tools for OS 3.0.2 compatibility (or sent to an OS 3.0.1 then exported).

(sorry for the lines on the screenshot, it may be because I'm using an unregistered version of Camtasia Studio)
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Lionel Debroux on July 03, 2011, 04:13:34 am
Great, congratulations once again :)
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Jim Bauwens on July 03, 2011, 04:14:05 am
Thats looks pretty good! I didn't think of using copy/paste for that.
Good work!
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: apcalc on July 03, 2011, 08:49:43 am
Wow, this looks very useful!

If you would like, I will try to post an encrypted version of this later today! :)
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: critor on July 03, 2011, 12:53:34 pm
I've resaved the oclua.tns file with the 3.0.1 software.
http://ti.bank.free.fr/index.php?mod=archives&ac=voir&id=3606

So now, everybody with an OS/software 3.0.1, 3.0.2 or above should be able to use Oclua directly. :)
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: pianoman on July 03, 2011, 12:55:15 pm
Do you have any idea how much I love you guys?
Great job!
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Chockosta on July 05, 2011, 02:01:35 pm
I love you.
This was the only which lacked to the TI-Nspire
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: DJ Omnimaga on July 06, 2011, 01:03:59 pm
Darn this is epic ExtendeD. A lot of people liked to program on-calc when they did 83+ stuff. I bet this will be appreciated by many Nspire owners too. O.O
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: BrownyTCat on July 06, 2011, 05:54:57 pm
I hope if ndless 3 comes out we can have a robust editor with a compiler.
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Levak on July 06, 2011, 08:03:37 pm
with a compiler.

A what ? for Lua ?
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Chockosta on July 07, 2011, 10:08:11 am
Even if it's not a compiler, it looks like one...
How should we call that ? an "encryptor" ?
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Levak on July 07, 2011, 11:17:41 am
Even if it's not a compiler, it looks like one...
How should we call that ? an "encryptor" ?
Well .. a shell ?
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Munchor on July 07, 2011, 11:20:38 am
I think he means that if Ndless 3 comes it'd be good to have an on-calc text editor with a LUA>TNS Converter. Keep it simple :)
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Chockosta on July 07, 2011, 11:25:37 am
I was talking of the tool which produces TNS files from LUA code.
I don't really know what a shell is, but I think it's something different. (The interpretor is a shell, isn't it ?)
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Levak on July 07, 2011, 11:48:59 am
I think he means that if Ndless 3 comes it'd be good to have an on-calc text editor with a LUA>TNS Converter. Keep it simple :)
^this
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Matrefeytontias on September 29, 2012, 10:24:37 am
Huge up, sorry :/

I'm having trouble with OCLua : why can't I use a for loop which runs more than 128 times ?

This code brings the calc into an infinite loop :
Code: [Select]
for i = 0, 128 do
end
And this works fine :
Code: [Select]
for i = 0, 127 do
end
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Jim Bauwens on September 29, 2012, 10:35:13 am
I haven't tried your code, but I assume you put that in on.paint .
Probably what is happening is that on.paint get's called again (sometimes it does this when execution time is a bit too long), causing it too loop constantly.
But that's just a theory.
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Adriweb on September 29, 2012, 10:51:49 am
Huge up, sorry :/

I'm having trouble with OCLua : why can't I use a for loop which runs more than 128 times ?

This code brings the calc into an infinite loop :
Code: [Select]
for i = 0, 128 do
end
And this works fine :
Code: [Select]
for i = 0, 127 do
end


Could you post more of the code ?

Also, I'm not sure about Jim's theory - well I haven't found myself in such case before. but maybe with timers and stuff it may happen... weird though...
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Matrefeytontias on September 29, 2012, 11:30:53 am
Here is the code which doesn't work for me.

In fact, once I try the code I just posted, it works ??? but just try the one I joined (it's the page 3).

Also, do you know a way to get text from a *.tns ?
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Lionel Debroux on September 29, 2012, 11:56:50 am
Copying and pasting some text might work, as would accessing TI-BASIC variables... but without external help, TI's proprietary Lua, which does not have io.* and os.* functions (i.e., among other limitations and incompatibilities with standard Lua, no external file access) cannot get text from a .tns.
Title: Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
Post by: Adriweb on September 29, 2012, 12:13:47 pm
Copying and pasting some text might work, as would accessing TI-BASIC variables... but without external help, TI's proprietary Lua, which does not have io.* and os.* functions (i.e., among other limitations and incompatibilities with standard Lua, no external file access) cannot get text from a .tns.
I believe he's talking about the OcLua's text in page 2 which is a Notes page. So not-related with TI's Nspire-Lua. But still, it's a closed format.

The "only" easy way is to open it with the computer software. Then copy/paste it wherever you want it.


Anyway, about your script : no problems for me for any of your codes...
(Also, remember to put the least possible code (only the final graphics/screen drawing -related things) in the paint method since it's going to get called quite often and you don't want it to slow the rest :)