Jump to content




Compile And Run Lua

lua

  • You cannot reply to this topic
51 replies to this topic

#1 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 19 May 2013 - 03:28 AM

Intro:
Many people do not realise Lua is actually a compiled language because of ComputerCraft's open source ethic.
As I am big supporter of the ability to have closed source and compile programs, have spent the last 30 minutes writing a very basic compiler for lua code. As the default shell cannot outright run compiled lua because of the way it's set up, I also created a basic way to run the compiled code.
Really they are both more like wrappers for existing functions, but nonetheless these are the first compile-run programs for CC that I am aware of. Anyway, for those interested, I am submitting these programs (and their included functions) for public use.

Limits:

You may use the code provided as is or you can extract the individual 'compile' and 'run' functions for use, or even rewrite them to your own suiting, but you should give credit by providing the header provided in both files when you use the code in other files or projects.
It would also be appreciated if any inspired works mention this post or link to it or the files.

The Files:
Compile.lua: http://pastebin.com/S520NYMV
Run.lua: http://pastebin.com/xjBgXmfy

Usage:
Compile.lua inFile outFIle
Run outFile

Afternote:
Feel free too integrate the system into the shell, providing you leave credit..
I recommend making the shell test a file for the 'LuaQ' header and making it choose which loadfile function to use based on that.

Versions:
Version 1.0.1 - First working versions
Version 1.0.2 - Bug Fix.Added sleep to fix yielding. Credits to pingoleon60.
Version 1.0.3 - Speed Enhancement. Replaced sleep with queue event. Credits to theoriginalbit
Version 1.0.4 - Speed Enhancement. Added if statement to only yield when required.


Spoiler

Edited by Pharap, 21 May 2013 - 09:22 PM.


#2 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 19 May 2013 - 03:33 AM

I was planning on something like this but didn't know how I would do it but you did it so I don't need to anymore! Now to make it run without a run program...

#3 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 19 May 2013 - 03:38 AM

Well, closed source just doesn't fit into computercraft. There are two advantages of closed source: the first is that people can't steal your work and the second is that you can't sell open-source programs. To the first: in computercraft every program is in this forum, which is good moderated, so clones of programs get deleted very fast. And about the second one no one would pay for a simple cc program
Edit: and of course it's possible to decompile compiled programs

#4 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 05:57 AM

Great work man ! (Under the circumstances you've been shockingly nice, but we didn't want our freedom ^^)

View PostKingOfNoobs, on 19 May 2013 - 03:38 AM, said:

And about the second one no one would pay for a simple cc program

And if you want to sell your programs ingame, versus diamonds or something else ?

#5 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 19 May 2013 - 05:59 AM

View Postpingoleon60, on 19 May 2013 - 05:57 AM, said:

Great work man !

View PostKingOfNoobs, on 19 May 2013 - 03:38 AM, said:

And about the second one no one would pay for a simple cc program

And if you want to sell your programs ingame, versus diamonds or something else ?
But that would mean that you're only publishing your program on one single server

#6 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 06:00 AM

Why ? Oh I see.
You're right, but it's not very serious. If you want to publish your programs only on one server, you can.
And if you play on more than one server...

Oh, and I've an error during compile. Add a sleep(0) in the for loop, it's slower but it doesn't crash, I think.

#7 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 19 May 2013 - 07:46 AM

View PostKingOfNoobs, on 19 May 2013 - 03:38 AM, said:

Well, closed source just doesn't fit into computercraft. There are two advantages of closed source: the first is that people can't steal your work and the second is that you can't sell open-source programs. To the first: in computercraft every program is in this forum, which is good moderated, so clones of programs get deleted very fast. And about the second one no one would pay for a simple cc program
Edit: and of course it's possible to decompile compiled programs

This is an opinion, not a fact: "closed source just doesn't fit into computercraft"
You forgot to mention compiling speeds up loading time because it means the Lua VM doesn't have to compile the code itself because it's loading it pre-compiled.
It also stops people pointing out annoying flaws in your code or panicking because your code looks horrible.


View Postsuperaxander, on 19 May 2013 - 03:33 AM, said:

I was planning on something like this but didn't know how I would do it but you did it so I don't need to anymore! Now to make it run without a run program...

To make your job easier: Run.lua will run non-compiled programs. Now all you have to do is dive into the shell and swap out/rework a couple functions.
Glad my 'compiler' is appreciated.


View Postpingoleon60, on 19 May 2013 - 05:57 AM, said:

Great work man ! (Under the circumstances you've been shockingly nice, but we didn't want our freedom ^^)

And if you want to sell your programs ingame, versus diamonds or something else ?

yay, people read random postscript spoilers.
Also, that second point is completely valid, I approve of that idea :D


View Postpingoleon60, on 19 May 2013 - 06:00 AM, said:

Why ? Oh I see.
You're right, but it's not very serious. If you want to publish your programs only on one server, you can.
And if you play on more than one server...

Oh, and I've an error during compile. Add a sleep(0) in the for loop, it's slower but it doesn't crash, I think.

Oversight on my part. I didn't think anyone would compile anything large enough to take 5 seconds lol
I'll add that in with a comment and credit :D

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 May 2013 - 07:50 AM

Firstly nice script :) its simple, yet elegant ;)

View PostPharap, on 19 May 2013 - 07:46 AM, said:

View Postpingoleon60, on 19 May 2013 - 06:00 AM, said:

Why ? Oh I see.
You're right, but it's not very serious. If you want to publish your programs only on one server, you can.
And if you play on more than one server...

Oh, and I've an error during compile. Add a sleep(0) in the for loop, it's slower but it doesn't crash, I think.

Oversight on my part. I didn't think anyone would compile anything large enough to take 5 seconds lol
I'll add that in with a comment and credit :D
Secondly if speed is what you're after, and you don't mind about the program being un-terminatable while its running, I would suggest
os.queueEvent('bogus')
coroutine.yield()
its much faster than sleep(0)

#9 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 07:51 AM

For the crash without sleep(0), my program was over 600 lines, so it's maybe for that.
But, if you have to compile a program, it's often a big program.

EDIT : TOB you're a ninja xD
Yeah, it's better than sleep(0), I forgot this.

And I think it's not useful with the runner, because I change it with the compiler, but I hadn't to do this with the runner because it work.

#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 May 2013 - 07:59 AM

Oh, also just curious... in Compile, why do you have the level on your errors as 2? because that will throw it back to the bios. Did you mean to have 0 so that no file or line number is shown?

#11 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 08:02 AM

View PostPharap, on 19 May 2013 - 07:46 AM, said:

You forgot to mention compiling speeds up loading time because it means the Lua VM doesn't have to compile the code itself because it's loading it pre-compiled.

And you also forgot that a program (in this case, a program with 522 lines) who is 19 Kbytes weight become 12 Kbytes weight. Yeah, it's little, but if you use the HTTP Api to send the program, it's better for the server connexion.

EDIT : Oh, and I'd like to create a GUI for the compiler (I like create GUIs for command-types already made programs). Can I ?

#12 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 19 May 2013 - 08:27 AM

View Posttheoriginalbit, on 19 May 2013 - 07:50 AM, said:

Firstly nice script :) its simple, yet elegant ;)

Secondly if speed is what you're after, and you don't mind about the program being un-terminatable while its running, I would suggest
os.queueEvent('bogus')
coroutine.yield()
its much faster than sleep(0)

Thanks :D
I may consider rewriting it to do that later.
I'm not fussed about the compiler speed yet since technically you don't have to compile a program to run it, the compilation is more of a post-creation packaging step. I will get round to it at some point though.

View Postpingoleon60, on 19 May 2013 - 07:51 AM, said:

And I think it's not useful with the runner, because I change it with the compiler, but I hadn't to do this with the runner because it work.

Future Proofing~ :D

View Posttheoriginalbit, on 19 May 2013 - 07:59 AM, said:

Oh, also just curious... in Compile, why do you have the level on your errors as 2? because that will throw it back to the bios. Did you mean to have 0 so that no file or line number is shown?

Force of habit lol

View Postpingoleon60, on 19 May 2013 - 08:02 AM, said:

And you also forgot that a program (in this case, a program with 522 lines) who is 19 Kbytes weight become 12 Kbytes weight. Yeah, it's little, but if you use the HTTP Api to send the program, it's better for the server connexion.

Haven't tried the http thing, but it should work. Something worth noting, for some reason if you try to write dumped functions as strings the string formatting messes them up and makes them not runnable. I think it's because the formatting deletes carriage returns for OS compatibility or something like that. It's worth knowing if you plan to write something that takes the sent program/function and stores it.

The size thing on the other hand is also very true, especially if the Lua VM does optimisations like Tail Calls. Not to mention complicated maths like 8 * (10 + 5), which since lua is stack based will be shrunk like mad. And all those many many lost whitespace chars and comments that become irrelevant.

I have sometimes wished Javascript and/or HTML were compiled. It would reduce site bandwidth, speed up browser loading time, speed up browser execution time and free up a load of space on thousands of servers. Web developers would complain (especially the ones I know), but I'm going to be harsh and say it will be a good change for them to experience what it's like being a traditional no-internets-attached programmer where libraries are all compiled, and you can't just rip apart programs without permission. A lot of developers obfuscate their code anyway to prevent copying (this site is definitely no exception: http://www.computerc....css?ipbv=32006) and compiled Javascript libraries would speed up library load times. My webdev friend is library obsessed, he never makes his own, always using about 10-15 libraries written by other people: bootstrap, YUI, googlemaps API, node.js, Sinatra (I know it's ruby, but I'm making a point) and countless others. Yet he has never made his own library. Think of how much faster that would all load if it was compiled.

I'll stop going on now, This is turning into a wall of text.

#13 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 19 May 2013 - 11:40 AM

This is pretty cool. I have seen some dude(Can't remember his name, it starts with a 'U' IIRC) back on CCNet create one of these. But you can just easily decompile it with a tool such as this http://sourceforge.n...rojects/unluac/

#14 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 02:18 PM

View PostSammich Lord, on 19 May 2013 - 11:40 AM, said:

But you can just easily decompile it with a tool such as this http://sourceforge.n...rojects/unluac/

I have tried, but I haven't succeeded yet. I saw on internet that if you put a special part of bytecode on the top of the file, it's automatically crash the decompiler. It's what happened to me. Anyone knows things about this ?

#15 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 19 May 2013 - 04:10 PM

"Supporter of Closed Source"? *pukes*


Nice and compact code tho...

#16 Nina

  • Members
  • 19 posts

Posted 19 May 2013 - 04:52 PM

There are quite some problems with this...
  • Closed-source/proprietary software (well except ComputerCraft (why is it closed-source anyway?)) really has no place here. Isn't this a community after all?
  • The sleep(0.1) trick is bad. Take any large program (I tried with LuaIDE) and attempt to compile and run it. Running LuaIDE like this would take 5954.9 seconds. That's around 1 hour and 40 minutes!
  • It is impossible to pass any arguments to programs executed with the supplied "Run" program.
  • Having a "Run" program in the first place is stupid -- you make programs compiled with this dependent on your "Run" program, while they might as well have the code that does this by themselves.


#17 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 04:58 PM

View PostNina, on 19 May 2013 - 04:52 PM, said:

There are quite some problems with this...
  • Closed-source/proprietary software (well except ComputerCraft (why is it closed-source anyway?)) really has no place here. Isn't this a community after all?
  • The sleep(0.1) trick is bad. Take any large program (I tried with LuaIDE) and attempt to compile and run it. Running LuaIDE like this would take 5954.9 seconds. That's around 1 hour and 40 minutes!
  • It is impossible to pass any arguments to programs executed with the supplied "Run" program.
  • Having a "Run" program in the first place is stupid -- you make programs compiled with this dependent on your "Run" program, while they might as well have the code that does this by themselves.


View PostNina, on 19 May 2013 - 04:52 PM, said:

  • Closed-source/proprietary software (well except ComputerCraft (why is it closed-source anyway?)) really has no place here. Isn't this a community after all?

You haven't to use it specially for here, but on a server where you sell your programs in example.


View PostNina, on 19 May 2013 - 04:52 PM, said:

  • The sleep(0.1) trick is bad. Take any large program (I tried with LuaIDE) and attempt to compile and run it. Running LuaIDE like this would take 5954.9 seconds. That's around 1 hour and 40 minutes!

Thanks to read the topic.


View PostNina, on 19 May 2013 - 04:52 PM, said:

  • It is impossible to pass any arguments to programs executed with the supplied "Run" program.

True.


View PostNina, on 19 May 2013 - 04:52 PM, said:

  • Having a "Run" program in the first place is stupid -- you make programs compiled with this dependent on your "Run" program, while they might as well have the code that does this by themselves.

Easy to do yourself, it's just a basic program to introduce an idea. And see the main post.

#18 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 19 May 2013 - 05:24 PM

Instead of
sleep(0.1)
use
sleep(0.05)
or
os.queueEvent("derp")
os.pullEvent("derp")


#19 JustPingo

  • Members
  • 108 posts
  • LocationFrance

Posted 19 May 2013 - 07:50 PM

View Posttheoriginalbit, on 19 May 2013 - 07:50 AM, said:

os.queueEvent('bogus')
coroutine.yield()
its much faster than sleep(0)


#20 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 May 2013 - 08:46 PM

View Postpingoleon60, on 19 May 2013 - 07:50 PM, said:

View Posttheoriginalbit, on 19 May 2013 - 07:50 AM, said:

os.queueEvent('bogus')
coroutine.yield()
its much faster than sleep(0)
It's also much faster than

View Postjesusthekiller, on 19 May 2013 - 05:24 PM, said:

or
os.queueEvent("derp")
os.pullEvent("derp")






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users