lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Sep 08, 2001 at 10:05:12AM -0400, Matt Holmes wrote:
> You could do this a couple of ways, but the easiest way would be to create a
> static string right in the C source which represents the Lua byte code, link
> the lua library, and on main just do lua_dobuffer on the array. You could
> even write a system that does this for you, but the system you run it on
> would need a C compiler to actually make the exe. I actually toyed around
> with adding something like this to my Lua IDE: I am still toying ;)

Here's a much, much easier way to do this.  Make an executable linked with
lua and whatever other libs you need (or just modify the existing
standalone lua interpreter).  The main() should at least initialize lua and
register your other functions with it, then do this:

<c-code>
     // Writing this on the fly; don't sue me if it doesn't compile.
	  // Error-checking omitted for brevity...

     int bytecodesize;
	  char *bytecode;
	  FILE *executable = fopen(argv[0], "r");
	  fseek(executable, sizeof(int), SEEK_END);
	  fread(&bytecodesize, sizeof(int), 1, executable);

     bytecode = (char *) malloc(sizeof(char) * bytecodesize);
	  fseek(executable, bytecodesize + sizeof(int), SEEK_END)
	  fread(bytecode, bytecodesize, 1, executable);
	  lua_dobuffer(bytecode);
	  free(bytecode);
</c-code>


Now all you have to do to make an executable out of your Lua script is
append the bytecode to the end of this executable, followed by the size of
the bytecode.  Et voila!  You can now take this single executable file
anywhere and run it (well, as long as it's the same OS/architecture.  =| ).

This trick is used all the time; WinZip self-extractors, self-playing
Powerpoint presentations, etc.  I believe perl, python, Visual Basic,
etc. all do this the same way.

If you append a 0 to this first proto-executable, you can make new Lua
executables from any existing one...  For example, if in main() you find
you have been given '--make-new-lua-exe filename.lua' on the command-line,
open argv[0], read in everything from the start of the file to the start of
the appended bytecode (which will be everything if this is the
proto-executable), then write it out to a new file.  Append the contents of
the argument filename and its size to this new file, call it 'filename.exe'
and you've cloned the engine with new bytecode attached.  The file argument
can even be an uncompiled .lua script that gets compiled along the way.

This can take you pretty far...  You can even add a way to undump or
decompile existing appended bytecode to a file for editing.  Now your
executable is a minimal development environment... "Oops, I have a
bug... Oh well, give it the arg to just undump its own bytecode to
foo.lua... Edit foo.lua... Run the executable but tell it make a new one
with the new bytecode... Test... Ah, there, now it works."

In fact, the existing standalong Lua interpreter has 90% of this already
present (well it would with a bit of luac mixed in).  I'd like to propose
that the makefile that produces lua.exe appends a zero onto the end, and
that these args/functionality get added to its main().  Forget any
lua2exe.exe... lua.exe is all you know and all ye need know... Standalone
interpreter, proto-executable, compiler, decompiler, etc.


Bret

PS: lash authors, are you listening?



> Matt "Kerion" Holmes
> Lead Developer
> Lua Studio - The Lua IDE
> http://www.sourceforge.net/projects/visuallua/
> 
> ----- Original Message -----
> From: <sessile@in-gen.net>
> To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
> Sent: Saturday, September 08, 2001 9:42 AM
> Subject: Single file executable distribution?
> 
> 
> > How would one go about embedding a Lua script file into the Lua
> > standalone interpeter so that the script could be distributed
> > (and run!) on machines without Lua being installed separately?
> > Any Lua tools similar to Python's py2exe available, perhaps?
> >
> > Regards,
> > Dean.
> >
> 

-- 
Bret Mogilefsky * Mgr. SCEA Developer Support * mogul@gelatinous.com