Author Topic: Loading Lua code dynamically for fun and profit (on-calc Lua editor)  (Read 14537 times)

0 Members and 1 Guest are viewing this topic.

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
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.
« Last Edit: July 03, 2011, 03:13:33 am by ExtendeD »
Ndless.me with the finest TI-Nspire programs

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Loading Lua code dynamically for fun and profit
« Reply #1 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!

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Loading Lua code dynamically for fun and profit
« Reply #2 on: June 25, 2011, 06:09:56 am »
jimbauwens, ephan pointed me to this. Does the console really read files, bypassing TI's filters?
Ndless.me with the finest TI-Nspire programs

Ashbad

  • Guest
Re: Loading Lua code dynamically for fun and profit
« Reply #3 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.
« Last Edit: June 25, 2011, 07:37:51 am by Ashbad »

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Loading Lua code dynamically for fun and profit
« Reply #4 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!

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Loading Lua code dynamically for fun and profit
« Reply #5 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.

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Loading Lua code dynamically for fun and profit
« Reply #6 on: June 26, 2011, 03:02:44 am »
Nice :)
Ndless.me with the finest TI-Nspire programs

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Loading Lua code dynamically for fun and profit
« Reply #7 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?

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Loading Lua code dynamically for fun and profit
« Reply #8 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.
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
« Reply #9 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)
Ndless.me with the finest TI-Nspire programs

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
« Reply #10 on: July 03, 2011, 04:13:34 am »
Great, congratulations once again :)
« Last Edit: July 03, 2011, 04:14:16 am by Lionel Debroux »
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
« Reply #11 on: July 03, 2011, 04:14:05 am »
Thats looks pretty good! I didn't think of using copy/paste for that.
Good work!

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
« Reply #12 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! :)


Offline critor

  • Editor
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2079
  • Rating: +439/-13
    • View Profile
    • TI-Planet
Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
« Reply #13 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. :)
TI-Planet co-admin.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Loading Lua code dynamically for fun and profit (on-calc Lua editor)
« Reply #14 on: July 03, 2011, 12:55:15 pm »
Do you have any idea how much I love you guys?
Great job!