Murga-Projects Forums
MurgaLua Compiler - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: MurgaLua Compiler (/showthread.php?tid=273)


MurgaLua Compiler - iGame3D - 01-31-2008 09:54 PM

Testing Mariels Bezier script I get a

Code:
[string "--[[..."]:23: module 'lib/utils' not found:
no field package.preload['lib/utils']
       no file './lib/utils.lua"
etc ..... etc
[C]: in function 'require'
        [string "--[[..."]:23: in function 'newFunc'
        [string "murgaLua"]:268: in function 'decompileMurgaLua'
        [string ""]:1: in main chunk
        [C]: ?


Pasting the required scripts into the main script gets the compiled version mostly working.
I think its missing an image now.

Yep the event detection script can't find its image as well.

How should we handle that?

lfs.currentdir() returns the User folder by default.

How to get a binary to report its own location?


RE: MurgaLua Compiler - mikshaw - 02-01-2008 12:00 AM

I'm not sure about a binary, but you can use arg[0] to get the full path to a script. This path can be chopped if you need the directory name:
string.gsub(arg[0],"(.*)/.*","%1")

I'm kinda surprised that FLTK has fl_filename_name (equivalent of basename), but no equivalent of dirname


RE: MurgaLua Compiler - JohnMurga - 02-01-2008 02:45 AM

Try running the "about.lua" script from the latest snapshot.

murgaLua_ExePath is what the runtime uses to find itself ...

And you can use mikshaw's code to chop it up ... On Linux/Mac ...
On Windows you'll have the slashes the other way.

arg[0] "should" work, but it is the C equivalent and won't always point to the right place.

Cheers
JohnM


RE: MurgaLua Compiler - JohnMurga - 02-01-2008 02:50 AM

iGame3D Wrote:
I think its missing an image now.

How should we handle that?

Two ways to that (sub-optimal at present).

Embed it ... By using the new picture and hex functions to dump as a hex string, and then reload/decode it as hex string at runtime.

Or, get the executable to load it from a location relative to it's own (see previous post).

I will knock up a demo of the first method this week.

Cheers
JohnM


RE: MurgaLua Compiler - iGame3D - 02-01-2008 08:05 AM

Code:
-- path to compiled binary
pathToMurgaLua = string.gsub(murgaLua_ExePath,"(.*)/.*","%1")
appImage = pathToMurgaLua.."/background.gif"

-- will return correct path if cd to scripts path prior to running script
CurDirImage = lfs.currentdir() .."/background.gif"

fltk.fl_register_images()
-- if the file exists at same path as binary
if lfs.attributes(appImage) ~= nil then  img_back = Fl_Shared_Image.get(appImage) end

-- if the file exists at same path as script
if lfs.attributes(CurDirImage) ~= nil  then img_back = Fl_Shared_Image.get(CurDirImage) end


Using this I can get a script and/or its compiled version to find the image.
I'll try something similiar with the bezier example, it uses a subdirectory.
I don't understand require so I'll cheat it again.

-- Groovy, Bezier Example compiled with images


RE: MurgaLua Compiler - Juergen - 02-01-2008 09:08 AM

JohnMurga Wrote:
Embed it ... By using the new picture and hex functions to dump as a hex string, and then reload/decode it as hex string at runtime.


Ouch, I was asking myself, why you introduced those two functions. The functionality (base64 en/decoding) is already available in murgaLua and is actually superior.

Code:
if arg[1]==nil then print("You should provide a filename!") return 1 end
binary_name=io.open(arg[1],"rb")
if not binary_name then print("You should provide a valid filename!") return 1 end
print("Load and summing binary data")
binary_data=binary_name:read("*a")
binary_name:close()
binary_size=#binary_data
binary_md5sum=md5.sum(binary_data)
print("encoding, decoding and summing b64 data")
b64_string=(mime.b64(binary_data,nil))
b64_string_size=#b64_string
binary_data_b64=(mime.unb64(b64_string,nil))
binary_b64_md5sum=md5.sum(binary_data_b64)
binary_data_b64=nil
b64_string=nil
collectgarbage()
print("encoding, decoding and summing hex data")
hex_string=murgaLua.encodeDataAsHex(binary_data)
hex_string_size=#hex_string
binary_data_hex=murgaLua.decodeDataFromHex(hex_string)
binary_hex_md5sum=md5.sum(binary_data_hex)
hex_string=nil
binary_data_hex=nil
binary_data=nil
collectgarbage()
if binary_md5sum~=binary_b64_md5sum then print("Something is wrong with the b64 function!") return 1 end
if binary_md5sum~=binary_hex_md5sum then print("Something is wrong with the hex function!") return 1 end
print("The original file size was "..binary_size.." bytes long")
print(string.format("The b64 encoded data is %d bytes long, which is %.2f%% more than the original data",b64_string_size,b64_string_size/binary_size*100-100))
print(string.format("The hex encoded data is %d bytes long, which is %.2f%% more than the original data",hex_string_size,hex_string_size/binary_size*100-100))
print(string.format("The hex encoded string is %.2f%% bigger than the b64 encoded one",hex_string_size/b64_string_size*100-100))


If you test it with a big file you can see that the hex functions are also much slower.

I think it would be a good idea to either remove the two functions or to call mime de/encoding internally.

Juergen


RE: MurgaLua Compiler - JohnMurga - 02-01-2008 09:38 AM

Juergen Wrote:
Ouch, I was asking myself, why you introduced those two functions. The functionality (base64 en/decoding) is already available in murgaLua and is actually superior.


You are right in that embedding the image using base64 would be a MUCH better solution ... However, the hex functions where there for some hex conversions I needed to perform, so they where the first thing that sprung to mind.

They are slow because they are currently coded in Lua, I'll change that at a future date, however the functionality is useful :-)

Cheers
JohnM


RE: MurgaLua Compiler - iGame3D - 02-01-2008 03:26 PM

MurgaLua compiler is cool.

I took Marielle's old Applescript Bundle for running murgaLua with
an included script and modified so it only need run the new compiled binary.

I dropped any needed files or directories into "bundle.app/Contents/MacOS/"
where the compiled binary resides and everything is working like a charm.
Even mikshaws widget demo is running self contained.

Here's ten demo stand alone bundles for Mac OS X
The scripts for most of the demos are included, else they
are already in the released examples folder.

Oh this is whats inside "calculator, circledraw, console, photo_gallery, widgets, bezieranim, scripteditor, step7murgaLua, eventdetection, mondrian"


RE: MurgaLua Compiler - Juergen - 02-01-2008 09:08 PM

JohnMurga Wrote:
They are slow because they are currently coded in Lua, I'll change that at a future date, however the functionality is useful :-)


I guessed something like that already from the "stellar" performance ;-)

But I don't get it, why to bother to implement something like that in Lua when the C implementation is trivial and much simpler then the Lua implementation. BTW aren't Lua strings immutabe? Then this could bring noMMU system instantly down.

Juergen

P.S.: An example for a C implementation:

bin2hex:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
unsigned char byte;
char lookup[]="0123456789ABCDEF";

while (fread((char *) &byte,1,1,stdin)==1) {
printf("%c%c",lookup[(byte>>4)%16],lookup[byte%16]);
}
return 0;
}


hex2bin:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
unsigned char byte,i,j,obyte=0;
unsigned char lookup[]="0123456789ABCDEF";

j=0;
while (fread((char *) &byte,1,1,stdin)==1) {
  for(i=0;i<16;i++) {
    if (byte==lookup[i]) {
       obyte+=(j==0)?i<<4:i;
       if (j==1) {printf("%c",obyte); obyte=0;}
       j^=1;
       break;
     }
  }
}
return 0;
}




RE: MurgaLua Compiler - JohnMurga - 02-01-2008 11:47 PM

Juergen Wrote:
But I don't get it, why to bother to implement something like that in Lua when the C implementation is trivial and much simpler then the Lua implementation. BTW aren't Lua strings immutabe? Then this could bring noMMU system instantly down.


With some of these things I enjoy trying to write it in Lua ... And as it is I just knocked it as quickly as I could for simple 16 byte values, so it didn't matter much.

The Lua implementation is pretty trivial too :-)

PHP Code:
function decodeDataFromHex (myString)
    
local myFunction = function (someData)
        return 
base.string.char (base.tonumber(someData16 ))
    
end
    
return base.string.gsub(myString'(%x%x)'myFunction)
end

function encodeDataAsHex (myString)
    
local myFunction = function (someData)
        return (
'%02x'):format(someData:byte())
    
end
    
return base.string.gsub(myString'.'myFunction )
end 


But I'll change it to "C" for the next release.

Cheers
JohnM


RE: MurgaLua Compiler - Juergen - 02-06-2008 04:33 AM

JohnMurga Wrote:
With some of these things I enjoy trying to write it in Lua ... And as it is I just knocked it as quickly as I could for simple 16 byte values, so it didn't matter much.

The Lua implementation is pretty trivial too :-)

PHP Code:
function decodeDataFromHex (myString)
    
local myFunction = function (someData)
        return 
base.string.char (base.tonumber(someData16 ))
    
end
    
return base.string.gsub(myString'(%x%x)'myFunction)
end

function encodeDataAsHex (myString)
    
local myFunction = function (someData)
        return (
'%02x'):format(someData:byte())
    
end
    
return base.string.gsub(myString'.'myFunction )
end 


But I'll change it to "C" for the next release.

Cheers
JohnM


Yes it looks pretty trivial, but it hides a lot of complexity. (and it sure isn't PHP ;-)

My I suggest the following implementations:

The bin2hex function cal also called with a optinal second string argument, that produces a lower case hex string when it is set to "l".

Code:
static int bin2hex (lua_State *L) {
  size_t l1; //,i=0;
  unsigned char ll[]="0123456789abcdef";
  unsigned char lu[]="0123456789ABCDEF";
  unsigned char *lookup=lu;
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  if (lua_isstring(L, 2)) lookup=((char ) *lua_tolstring(L,2,NULL)=='l')?ll:lu;
  luaL_buffinit(L, &b);
  while (l1--) {
    luaL_putchar(&b, lookup[((unsigned char) (*s1)>>4)%16]);
    luaL_putchar(&b, lookup[(unsigned char)(*s1++)%16]);
  }
  luaL_pushresult(&b);
  return 1;
}


The hex2bin function works with a upper or lower case string, but not with a mixed case string.

Code:
static int hex2bin(lua_State *L) {
  unsigned char switch_case=0,i,j=0,obyte=0;
  size_t l1;
  unsigned char ll[]="0123456789abcdef";
  unsigned char lu[]="0123456789ABCDEF";
  unsigned char *lookup=lu;
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while (l1--) {
    switch_case|=1; // bit 1 -> switch, bit 2 -> already switched
    for(i=0;i<16;i++) {
       if ((unsigned char) *s1==lookup[i]) {
         obyte+=(j==0)?i<<4:i;
         if (j==1) { luaL_putchar(&b, (char) obyte); obyte=0; }
         j^=1;
         s1++;
         switch_case&=2; // no we do not have to switch the case
         break;
       }
    }
    if (switch_case&1) { // did we switch the case?
      if (switch_case&2) { //has the case been already been switched?
        lua_pushstring(L,"This isn't a valid hex string!");
        lua_error(L);
      }
      lookup=ll;
      l1++;
      switch_case|= 2; //we have switched the case
    }
  }
  luaL_pushresult(&b);
  return 1;
}


I'd like to ask the Kepler guys, if they include it in the md5 library, since they have the bin2hex version already (also in Lua). If they take it, it would make sense to use the version from the md5 library. Since the functionality is new, I think there aren't many users.

Juergen


RE: MurgaLua Compiler - JohnMurga - 02-06-2008 08:21 AM

Juergen Wrote:
I'd like to ask the Kepler guys, if they include it in the md5 library, since they have the bin2hex version already (also in Lua). If they take it, it would make sense to use the version from the md5 library. Since the functionality is new, I think there aren't many users.


Thanks :-)

I'll try it out and replace my methods with this in the next build (along with an acknowledgment).

Cheers
JohnM


MurgaLua Compiler Platforms - iGame3D - 02-06-2008 09:24 AM

Can I compile for windows from a Mac?
I haven't tried yet.


RE: MurgaLua Compiler - Juergen - 02-06-2008 09:29 AM

JohnMurga Wrote:

Juergen Wrote:
I'd like to ask the Kepler guys, if they include it in the md5 library, since they have the bin2hex version already (also in Lua). If they take it, it would make sense to use the version from the md5 library. Since the functionality is new, I think there aren't many users.


Thanks :-)

I'll try it out and replace my methods with this in the next build (along with an acknowledgment).

Cheers
JohnM


I improved the hex2bin function a little. The hex2bin function now accepts every even numbered string, that contains valid characters ([0123456789aAbBcCdDeEfF]).

Juergen

Code:
static int hex2bin(lua_State *L) {
  unsigned char switch_case=0,i,j=0,obyte=0;
  size_t l1;
  unsigned char ll[]="0123456789abcdef";
  unsigned char lu[]="0123456789ABCDEF";
  unsigned char *lookup=lu;
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  lua_pushstring(L,"This isn't a valid hex string!");
  if (l1%2!=0) lua_error(L);
  while (l1--) {
    switch_case++;
    for(i=0;i<16;i++) {
       if ((unsigned char) *s1==lookup[i]) {
         obyte+=(j==0)?i<<4:i;
         if (j==1) { luaL_putchar(&b, (char) obyte); obyte=0; }
         j^=1;
         s1++;
         switch_case=0; // everything is ok
         break;
       }
    }
    if (switch_case>0) { // we haven't found our letter
      if (switch_case>1) { //switching was fruitless -> exit
        lua_error(L);
      }

    lookup=(lookup==ll)?lu:ll; //switch lookup table
    l1++; //
    switch_case++; //try again
    }
  }
  luaL_pushresult(&b);
  return 1;
}




RE: MurgaLua Compiler - Juergen - 02-06-2008 08:14 PM

Juergen Wrote:
I improved the hex2bin function a little. The hex2bin function now accepts every even numbered string, that contains valid characters ([0123456789aAbBcCdDeEfF]).

Juergen

Another change. It looks now much simpler. Although it might be a little bit slower in some cases (lower case hex) and faster when used with upper case letters. But it is much more readable and also smaller.

Juergen

Code:
static int hex2bin(lua_State *L) {
  unsigned char found=0,i,j=0,obyte=0;
  size_t l1;
  char lookup[]="0123456789ABCDEF0123456789abcdef";
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  lua_pushstring(L,"This isn't a valid hex string!");
  if (l1%2!=0) lua_error(L);
  while (l1--) {
     found++;
     for(i=0;i<32;i++) {
       if (*s1==lookup[i]) {
         if (j==0) obyte=(i%16)<<4;
           else { obyte+=i%16; luaL_putchar(&b,(char)obyte); }
         j^=1;
         s1++;
         found--;
         break;
       }
    }
    if (found==1) lua_error(L);
  }
  luaL_pushresult(&b);
  return 1;
}