Omnimaga

General Discussion => Technology and Development => Web Programming and Design => Topic started by: Juju on February 17, 2013, 09:30:52 pm

Title: Lua/HTML5 Web Interpreter
Post by: Juju on February 17, 2013, 09:30:52 pm
Was bored today, so I made this. It's a Lua interpreter (based on lua.js (https://github.com/mherkender/lua.js)) with a library that works with a HTML5 canvas. Some functions are implemented and I'm slowly implementing more. At the end, it should support both Nspire Lua and LuaZM graphic functions. (Obviously, suggestions are open :))

Documentation
dofile(url) - Asynchronously executes a remote file. Ex.: Try dofile("http://juju2143.ca/test.lua") :P
gc.drawLine(x1, y1, x2, y2)
gc.drawRect(x, y, w, h)
gc.fillRect(x, y, w, h)
gc.setColorRGB(r, g, b) - Same as their Nspire counterparts
zmg.time()
zmg.ticks() - Same as their LuaZM counterparts

Known issues
- Backspace might not work as expected in some browsers, for some reason.
- dofile() is asynchonous and doesn't lock the terminal while the AJAX thing runs, so print() might print in the input.
- Infinite loops will crash your browser

http://juju2143.ca/lua
Title: Re: Lua/HTML5 Web Interpreter
Post by: Sorunome on February 17, 2013, 10:46:28 pm
nice so far!
print("THE GAME")
Title: Re: Lua/HTML5 Web Interpreter
Post by: Spenceboy98 on February 17, 2013, 11:07:47 pm
You should make it support LuaZM so I can test my programs. :P

It works nicely, but as you said, Backspace doesn't work.

/me lost.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 17, 2013, 11:18:29 pm
Backspace works now. Apparently the compacted version of termlib.js is broke, but not the non-compacted version.

And yeah good idea it should also support LuaZM.

EDIT: Every Nspire Lua and LuaZM graphics function are implemented as stubs. Unless I actually implemented them.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 01:02:42 pm
Update bump. Added an editor (type run() in the terminal to run the code, if you add an argument it runs like dofile(file)) and a windowing system. Resizing windows doesn't work though, so don't try.

I guess I could rename this to "Lua/HTML5 Web IDE" as I now aim for an IDE-like functionality, now that I added an editor.
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on February 25, 2013, 01:13:25 pm
This is very interesting. Do you plan much calculator support such as for LuaZM, Nspire Lua and maybe CPLua? Something that would be cool in long terms if you do is the option to develop for the first two at once so it's easier to modify files to port them over the other calc.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 01:18:45 pm
I'm aiming for a complete LuaZM simulator with this (as at least 2 people requested), then I could also add Nspire Lua support (with the help of Jim Bauwens). For CPLua, well I wasn't even aware it existed and I have no ClassPad, so I can't really tell. Same for LuaFX, support for it would also be nice.

Oh, I forgot, you can resize the canvas with canvas.mode(mode) or canvas.mode(width, height) where mode is a constant ("nspire", "prizm" or "big").
Title: Re: Lua/HTML5 Web Interpreter
Post by: Adriweb on February 25, 2013, 01:23:18 pm
Very nice.
I hope you can work with Jim so that it'd be a great all-in-oen tool for Nspire-Lua too.

At one point, can you consider making an API or something so that it can be integrated on other websites (thinking of TI-Planet :P), etc. ?


Also, can you implement by defauls 'class' ? ( http://wiki.inspired-lua.org/class() (http://wiki.inspired-lua.org/class()) )
And I've noticed that when there is an error, it doesnt show the line, can you make it do that ?
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 01:29:38 pm
Yeah, an integration API will be a pretty good idea.

class can be implemented by default kinda easily (next update?) and AFAIK it actually shows where is the error when there's one. Well, at least on Google Chrome. (What do you mean?)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Adriweb on February 25, 2013, 02:03:25 pm
Cool :)

I meant that it doesnt show (for me, on Chrome, too), at what line of the text the error is (here, 5) :
(http://i.imgur.com/8xQhMCw.png)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 02:13:12 pm
Ah, yeah, you're right. But it will tell the line number if you made a syntax error (try removing a parenthesis).
Title: Re: Lua/HTML5 Web Interpreter
Post by: Sorunome on February 25, 2013, 07:04:23 pm
Mhmm, will you be able to import stuff like luasockets and run IRC bots? XD
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 08:10:39 pm
Probably not. I guess.

Also you'll like the new sample preloaded in the editor.
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on February 25, 2013, 08:22:00 pm
Will we see screenshots of game running soon? :P
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 08:34:24 pm
I guess, yeah. Only clipRect and key* are left to implement in the zmg library, then you'll be able to run all your favorite LuaZM games.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Sorunome on February 25, 2013, 09:49:40 pm
wait, could you put reubenquest on it? (so does it have löve)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 25, 2013, 09:50:50 pm
Might be possible, but not planned. Another project already does this, actually.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Sorunome on February 25, 2013, 09:56:14 pm
DERPY!!!!!!!!!!!
And with parenthesis highlighting: i wouldn't only put a box around the other parenthesis but also around the current one.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Spenceboy98 on February 25, 2013, 10:18:41 pm
How long will it take you to do the keys for LuaZM? So far it looks great. :D
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on February 27, 2013, 03:40:03 pm
The best thing will be if we can upload LuaZM files and Nspire Lua documents so we can quickly test games. :P
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 27, 2013, 03:54:26 pm
The best thing will be if we can upload LuaZM files and Nspire Lua documents so we can quickly test games. :P
I'm planning a save/load feature :P
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on February 27, 2013, 03:59:37 pm
Cool to hear :). I thought it would be nice that we don't have to copy the code by hand, which is hard with Lua tns files that lacks the source if you don't know what tool to use to extract it.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 27, 2013, 07:12:25 pm
UPDATE :o

- loadfile(url) - Loads a remote file in the editor
- New open file dialog! (Doesn't work yet! Might support tns files! If possible!) EDIT: It works!
Title: Re: Lua/HTML5 Web Interpreter
Post by: Adriweb on February 28, 2013, 04:53:42 am
It looks even more awesome now :D

(How do you load a .tns file btw? When I try, it just show the content (compressed, encoded) of the .tns file itself and not the script within the document, if that's what to be expected)
Title: Re: Lua/HTML5 Web Interpreter
Post by: flyingfisch on February 28, 2013, 11:47:04 am
could you make the error messages include a line number? :)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on February 28, 2013, 12:20:40 pm
(How do you load a .tns file btw? When I try, it just show the content (compressed, encoded) of the .tns file itself and not the script within the document, if that's what to be expected)
It doesn't work yet. Ideally it should detect if it's a tns or not, if it is, extract the xml file inside and parse it.

could you make the error messages include a line number? :)
This is definitely something I should look at. (The change would be made upstream in the lua.js code though.)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Adriweb on February 28, 2013, 01:22:01 pm
Ideally it should detect if it's a tns or not, if it is, extract the xml file inside and parse it.

Good luck :D
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on March 02, 2013, 02:00:04 am
I tried opening Aspirin for the PRIZM and nothing would happen, then I tried Run in case it actually loaded without notifying me, but it made my browser go incredibly slow until I close the tab D:
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 02, 2013, 10:13:30 am
That probably worked. File>Open loads a file inside the editor (Window>Editor) (maybe I should open the windows by default? What do you think?) and while loops still crashes the browser.
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on March 04, 2013, 04:09:11 am
Yeah I think it should open the window by default, like most Windows programs. And I see, thanks for the info. :P
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 28, 2013, 02:15:29 pm
Bump! Added a save file dialog! You Nspire-owners should test it out...



Edit: Oh and I forgot. https://github.com/juju2143/LuaIDE
Title: Re: Lua/HTML5 Web Interpreter
Post by: Spenceboy98 on March 28, 2013, 03:06:01 pm
Have you done the keys for luazm yet? Can't wait until I can test my programs. :)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 28, 2013, 03:07:49 pm
I began doing this. Should finish it.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Adriweb on March 28, 2013, 03:13:06 pm
Nice :)

Feedback : print("hello") gets in the tns as :    print(%22hello%22)  so it errors   :P
Title: Re: Lua/HTML5 Web Interpreter
Post by: flyingfisch on March 28, 2013, 03:32:12 pm
Still no infinite loop support?
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 28, 2013, 03:45:42 pm
Nice :)

Feedback : print("hello") gets in the tns as :    print(%22hello%22)  so it errors   :P

Thank you, this has been fixed.
Still no infinite loop support?
No, not yet :/
Have you done the keys for luazm yet? Can't wait until I can test my programs. :)
Actually maybe this has been done already. So now you might want to poke me about infinite loop support.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Adriweb on March 28, 2013, 08:56:27 pm
Btw Juju, on the source code, I see at https://github.com/juju2143/LuaIDE/blob/master/luna.php#L10 :
Code: [Select]

Do you check/sanitize/whatever the input ?

It wouldn't be good if the php process were to execute some things like "shell_exec('luna '. 'test.lua' . ' ' . 'test.tns ; rm -Rf /');" :D
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 28, 2013, 09:51:16 pm
Btw Juju, on the source code, I see at https://github.com/juju2143/LuaIDE/blob/master/luna.php#L10 :
Code: [Select]

Do you check/sanitize/whatever the input ?

It wouldn't be good if the php process were to execute some things like "shell_exec('luna '. 'test.lua' . ' ' . 'test.tns ; rm -Rf /');" :D

Yes, actually at line 2 and 3 PHP generate a random filename (something like /tmp/lunaAsdfasdf), so there's no chance the user could put a rm -rf / in there. Then at line 14 a header with an actual file name is sent (which have no incidence on the filesystem).
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 29, 2013, 06:22:33 pm
Well. After the The Fun Has Been Doubled Update, (yeah, codenaming updates sure is fun, I should do that more often) we now have the long-awaited... The Infinite Loop Update!

That is, infinite loops work! Try it out with this sample code:

Code: [Select]
i = 0
zmg.clear()
while true do
zmg.drawText(0,0,i,zmg.makeColor("black"),zmg.makeColor("white"))
zmg.fastCopy()
time = zmg.ticks()
while time+128 >= zmg.ticks() do end
i = i+1
end

then select Tools > Run (threaded). You should put a sleep loop (line 6 and 7) in there or else the browser crashes. So yeah, the Lua code is now ran in a web worker, that is, a different thread from the UI one. All the recent major browsers (including IE10) should support it (or else it would tell you). Have fun, and, as usual, report all the bugs here :)
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on March 30, 2013, 01:03:45 am
Glad you managed to fix them. :D How well would the typical Nspire Lua and PRIZM LuaZM game work for now? Would Aspirin run?

It seems like the IDE doesn't work at all in Opera. If I click Run or Run Threaded, nothing happens and the browser just starts being laggy.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 30, 2013, 01:11:52 am
Nspire Lua: not supported yet. Nor are keys in threaded mode. Yet. Aspirin: I dunno I think there's a syntax error somewhere. Also tested Bloxorz: works fine. But as I said, I still have to work on keys. Maybe test moar stuff too. Such as Opera. And other games. Did you tried the default program?
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on March 30, 2013, 01:23:42 am
Yep and nothing at all happened either other than browser lag almost freezing Opera.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 30, 2013, 01:28:31 am
That's something I should look at, then.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on March 30, 2013, 01:21:29 pm
It works for me in Opera 12.14, I can't move the windows though.

Also added a sample browser and some small fixes. (Hope you don't mind I've put your ball program in there Spenceboy98.)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Spenceboy98 on March 30, 2013, 06:48:09 pm
It works for me in Opera 12.14, I can't move the windows though.

Also added a sample browser and some small fixes. (Hope you don't mind I've put your ball program in there Spenceboy98.)

I don't mind. :) Looks good so far. :D
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on April 07, 2013, 12:31:31 pm
Update!

- Compile with luac (experimental feature)
- A skin! (toggle in Tools > Skin, you can click on it but it does nothing right now)

Now how I should do keys. It's more complicated than I thought since the lua code runs in a separate thread.
Title: Re: Lua/HTML5 Web Interpreter
Post by: DJ Omnimaga on April 14, 2013, 03:04:21 am
Do you plan to remove the blur from the fonts like in this screenshot (by Flyingfisch)?

(http://img.removedfromgame.com/imgs/15-screenshot.png)
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on April 14, 2013, 02:54:48 pm
No, actually I never found a way, it's probably due to antialiasing and the fact it's not a true bitmap font. Until I find a way it's for me pretty much a minor detail I should fix.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Spenceboy98 on July 26, 2013, 01:36:45 am
None of the sample programs seem to be working. They keep putting this instead:
Code: [Select]
<!DOCTYPE HTML>
<html>
<head>
<title>Four oh three.</title>
<style>
body
{
background: #000000;
color: #00FFFF;
font-family: monospace;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<img src="http://julosoft.net/ponies/Derpy/derpy_hoverupsidedown_right.gif" />
<br/><br/>Derpy did not knew what went wrong.
<br/><br/>She looked for /lua/p?f=http://juju2143.ca/lua/ball.lua&_termlib_reqid=1374816950165_23013 and found it...
<br/><br/>But the binder was locked. She looked for the key by sea and land but found nothing.
<br/><br/>Maybe she misplaced that key, accidentally destroyed it, or maybe it just never existed.
<br/><br/>Derpy says she's deeply sorry.
<br/><br/><a href="/">Return to main page</a>
<!--br/><br/>-->
</body>
</html>
Title: Re: Lua/HTML5 Web Interpreter
Post by: Sorunome on July 26, 2013, 04:07:27 am
Um, the default test thingy is working over here ???
Maybe you didn't copy everything over to my server juju?
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on July 26, 2013, 10:18:54 am
The file does exist, but the proxy script seems to throw a 403 since I moved over to Sorunome's. I'll check this out when I'll have some time.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Spenceboy98 on July 26, 2013, 02:35:50 pm
Um, the default test thingy is working over here ???
Maybe you didn't copy everything over to my server juju?

The default loads fine at startup, but when I go to click a different sample it gives me the HTML code.
Title: Re: Lua/HTML5 Web Interpreter
Post by: Juju on July 27, 2013, 12:25:59 pm
It should now work, there was a bug in the nginx configuration.