lua scripts as string within another script

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

lua scripts as string within another script

aryajur
Hi,
        I have a lua script which has some scripts written inside string variables. Is it possible to have these scripts as compiled scripts so when I compile the main script using luac these scripts are not visible directly as text in the compiled file?
       I tried to have the strings as string.dump(load(scriptAsString)) but that still has the text of the script although I see some binary characters appended.

Thanks,
Milind


Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Rena
On Wed, Nov 12, 2014 at 10:33 PM, Milind Gupta <[hidden email]> wrote:

> Hi,
>         I have a lua script which has some scripts written inside string
> variables. Is it possible to have these scripts as compiled scripts so when
> I compile the main script using luac these scripts are not visible directly
> as text in the compiled file?
>        I tried to have the strings as string.dump(load(scriptAsString)) but
> that still has the text of the script although I see some binary characters
> appended.
>
> Thanks,
> Milind
>
>

string.dump() is for doing the opposite. Just do:

f = load("print('hello')")
f()

load() compiles Lua source and returns a function. string.dump() takes
a function and returns a binary string that can later be given to
load().

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Andrew Starks


On Wed, Nov 12, 2014 at 10:09 PM, Rena <[hidden email]> wrote:
On Wed, Nov 12, 2014 at 10:33 PM, Milind Gupta <[hidden email]> wrote:
> Hi,
>         I have a lua script which has some scripts written inside string
> variables. Is it possible to have these scripts as compiled scripts so when
> I compile the main script using luac these scripts are not visible directly
> as text in the compiled file?
>        I tried to have the strings as string.dump(load(scriptAsString)) but
> that still has the text of the script although I see some binary characters
> appended.
>
> Thanks,
> Milind
>
>

string.dump() is for doing the opposite. Just do:

f = load("print('hello')")
f()

load() compiles Lua source and returns a function. string.dump() takes
a function and returns a binary string that can later be given to
load().

--
Sent from my Game Boy.


Seems like that should be "string.load()" then, doesn't it? That is, string.dump and string.load would be symmetrical.

-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Tim Hill
In reply to this post by aryajur

> On Nov 12, 2014, at 7:33 PM, Milind Gupta <[hidden email]> wrote:
>
> Hi,
>         I have a lua script which has some scripts written inside string variables. Is it possible to have these scripts as compiled scripts so when I compile the main script using luac these scripts are not visible directly as text in the compiled file?
>        I tried to have the strings as string.dump(load(scriptAsString)) but that still has the text of the script although I see some binary characters appended.
>
> Thanks,
> Milind
>
>

string.dump() will just give you the byte codes, not the source code. Are you sure about your code? And yes, once you have the byte code you can use it as input to load() to avoid having the source code distributed. Note, however, that byte code is NOT portable and so may not load if the target platform differs from the one on which it was compiled.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Oliver Kroth
Hello,

string.dump() is the right function. However, it will not strip the
debugging information. At least not until Lua 5.3, where you can specify
this as optional parameter. The debugging information is what you see as
'source text'.

Possibly you could try to use the output of the standalone Lua compiler
luac as input for load().

--
Oliver

Am 13.11.2014 um 06:45 schrieb Tim Hill:

>> On Nov 12, 2014, at 7:33 PM, Milind Gupta <[hidden email]> wrote:
>>
>> Hi,
>>          I have a lua script which has some scripts written inside string variables. Is it possible to have these scripts as compiled scripts so when I compile the main script using luac these scripts are not visible directly as text in the compiled file?
>>         I tried to have the strings as string.dump(load(scriptAsString)) but that still has the text of the script although I see some binary characters appended.
>>
>> Thanks,
>> Milind
>>
>>
> string.dump() will just give you the byte codes, not the source code. Are you sure about your code? And yes, once you have the byte code you can use it as input to load() to avoid having the source code distributed. Note, however, that byte code is NOT portable and so may not load if the target platform differs from the one on which it was compiled.
>
> —Tim
>
>


Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

aryajur
In reply to this post by Tim Hill
Here is a sample output:

> print(string.dump(load("x=1\ny=2\nprint('hello',x,y)")))
←LuaR ☺♦ ↓ô

          @└@ü♠ A A@☺ å @ ╞Ç@ ↔@ ☻▼ Ç ♠   ♦☻   x ♥      ≡?♦☻   y ♥       @♦♠   p
rint ♦♠   hello     ☺   ☺ ←   x=1
y=2
print('hello',x,y)   ☺   ☻   ♥   ♥   ♥   ♥   ♥   ♥       ☺   ♣   _ENV
>

As seen the dump contains a copy of the original string. Oliver is right it is the debugging source information. If I do print(stirng.dump(load("x=1\ny=2\nprint('hello',x,y)","string"))) then the copy of the whole string is gone just left with 'string'

Is string.dump platform dependent like the bytecode?

Thanks,
Milind

On Wed, Nov 12, 2014 at 9:45 PM, Tim Hill <[hidden email]> wrote:

> On Nov 12, 2014, at 7:33 PM, Milind Gupta <[hidden email]> wrote:
>
> Hi,
>         I have a lua script which has some scripts written inside string variables. Is it possible to have these scripts as compiled scripts so when I compile the main script using luac these scripts are not visible directly as text in the compiled file?
>        I tried to have the strings as string.dump(load(scriptAsString)) but that still has the text of the script although I see some binary characters appended.
>
> Thanks,
> Milind
>
>

string.dump() will just give you the byte codes, not the source code. Are you sure about your code? And yes, once you have the byte code you can use it as input to load() to avoid having the source code distributed. Note, however, that byte code is NOT portable and so may not load if the target platform differs from the one on which it was compiled.

—Tim



Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Paul K-2
In reply to this post by aryajur
Hi Milind,

>         I have a lua script which has some scripts written inside string
> variables. Is it possible to have these scripts as compiled scripts so when
> I compile the main script using luac these scripts are not visible directly
> as text in the compiled file?

Just to make sure I understand what you are trying to do. You have a
script A, which you a compiling to bytecode, but you also have scripts
B and C embedded into A and you don't want the users to see B and C in
the compiled code?

If so, you can write a function that turns them into something else,
for example, set of numbers. This may look like this:

decode("my string I don't want to see")

and decode function checks if it gets a string, it simply returns it,
but if it gets an array, it converts the content of that array into a
string and returns it.

Then you write a pre-processor that finds all instances of
decode('some string') and convert them into something like
decode({f('s'), f('o'), f('m')....}), where f() is some function that
converts a character into a different form, for example, a number.

This is simple enough to not have a significant performance impact and
good enough to make messages not visible in the binary code. Usual
disclaimers about dedicated hackers apply...

Note that the bytecode Lua generates is platform-dependent (this is
not the case for LuaJIT, which generates version-dependent, but
platform independent bytecode).

Paul.

Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

aryajur
Thanks Paul,
     That is a good idea.

Milind


On Wed, Nov 12, 2014 at 11:09 PM, Paul K <[hidden email]> wrote:
Hi Milind,

>         I have a lua script which has some scripts written inside string
> variables. Is it possible to have these scripts as compiled scripts so when
> I compile the main script using luac these scripts are not visible directly
> as text in the compiled file?

Just to make sure I understand what you are trying to do. You have a
script A, which you a compiling to bytecode, but you also have scripts
B and C embedded into A and you don't want the users to see B and C in
the compiled code?

If so, you can write a function that turns them into something else,
for example, set of numbers. This may look like this:

decode("my string I don't want to see")

and decode function checks if it gets a string, it simply returns it,
but if it gets an array, it converts the content of that array into a
string and returns it.

Then you write a pre-processor that finds all instances of
decode('some string') and convert them into something like
decode({f('s'), f('o'), f('m')....}), where f() is some function that
converts a character into a different form, for example, a number.

This is simple enough to not have a significant performance impact and
good enough to make messages not visible in the binary code. Usual
disclaimers about dedicated hackers apply...

Note that the bytecode Lua generates is platform-dependent (this is
not the case for LuaJIT, which generates version-dependent, but
platform independent bytecode).

Paul.


Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Luiz Henrique de Figueiredo
In reply to this post by Andrew Starks
> Seems like that should be "string.load()" then, doesn't it? That is,
> string.dump and string.load would be symmetrical.

string.dump produces a string, load doesn't.

Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Luiz Henrique de Figueiredo
In reply to this post by aryajur
>        I tried to have the strings as string.dump(load(scriptAsString)) but
> that still has the text of the script

Try string.dump(load(scriptAsString,"=none"))

Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

aryajur


Try string.dump(load(scriptAsString,"=none"))

Yes that works.
Thanks,
Milind
Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Tim Hill
In reply to this post by Luiz Henrique de Figueiredo

> On Nov 13, 2014, at 2:26 AM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
>
>>       I tried to have the strings as string.dump(load(scriptAsString)) but
>> that still has the text of the script
>
> Try string.dump(load(scriptAsString,"=none"))
>

Is this documented anywhere? .. I was unaware of this trick.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Philipp Janda
Am 13.11.2014 um 22:02 schröbte Tim Hill:

>
>> On Nov 13, 2014, at 2:26 AM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
>>
>>>        I tried to have the strings as string.dump(load(scriptAsString)) but
>>> that still has the text of the script
>>
>> Try string.dump(load(scriptAsString,"=none"))
>>
>
> Is this documented anywhere? .. I was unaware of this trick.

In the manual entry for `load`[1] and `lua_Debug`[2] (more precisely the
`source` field).

>
> —Tim
>

Philipp

   [1]: http://www.lua.org/manual/5.2/manual.html#pdf-load
   [2]: http://www.lua.org/manual/5.2/manual.html#4.9



Reply | Threaded
Open this post in threaded view
|

Re: lua scripts as string within another script

Tim Hill

>> Is this documented anywhere? .. I was unaware of this trick.
>
> In the manual entry for `load`[1] and `lua_Debug`[2] (more precisely the `source` field).
>
>>
>> —Tim
>>
>
> Philipp
>

Ah yes I see, really what’s going on is using “=none” to over-ride the default behavior of using the the “ld” string as the “source” string. Makes sense.

—Tim