Read only variables

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

Read only variables

Chris Percival
If I create a variable using (for example):

lua_pushstring(L, "Hello world");
lua_setglobal(L, "MyHelloString");

Is there any way to make that variable read only (or 'constant') in 
the script, ie any attempt to change their value results in an error?

Chris


Reply | Threaded
Open this post in threaded view
|

Re: Read only variables

Peter Shook-3
aircooledspeed wrote:
> 
> If I create a variable using (for example):
> 
> lua_pushstring(L, "Hello world");
> lua_setglobal(L, "MyHelloString");
> 
> Is there any way to make that variable read only (or 'constant') in
> the script, ie any attempt to change their value results in an error?

You can make the value a userdata, and use the setglobal tag method to
protect it.  But this only works for globals.  It won't work on local
variables.

#include "lua.h"

static int const_tag;

static int my_const (lua_State *L) {
  lua_settop(L,1);
  lua_pushusertag(L, (void*)lua_ref(L,1), const_tag );
  return 1;
}

static int get_const (lua_State *L) {
  lua_getref(L, (int)lua_touserdata(L,2) );
  return 1;
}

static int set_const (lua_State *L) {
  lua_settop(L,1);
  lua_pushstring(L, " is a constant so I'm not going to let you change
it" );
  lua_concat(L,2);
  lua_error(L, lua_tostring(L,-1) );
  return 0;
}

void lua_const_open (lua_State *L) {
  const_tag = lua_newtag(L);
  lua_register(L, "const", my_const );
  lua_pushcfunction(L, get_const );
  lua_settagmethod(L, const_tag, "getglobal");
  lua_pushcfunction(L, set_const );
  lua_settagmethod(L, const_tag, "setglobal");
}

Here is an example:

$ ./lua
Lua 4.0  Copyright (C) 1994-2000 TeCGraf, PUC-Rio
> x = const(44)
> print(x)
44
> y = const"this is a test"
> print(y)
this is a test
> y=55
error: y is a constant so I'm not going to let you change it
stack traceback:
   1:  `setglobal' tag method [C]
   2:  main of string "y=55" at line 1
> x = 2
error: x is a constant so I'm not going to let you change it
stack traceback:
   1:  `setglobal' tag method [C]
   2:  main of string "x = 2" at line 1
> 

Cheers,

- Peter

Reply | Threaded
Open this post in threaded view
|

RE: Read only variables

Chris Percival
Wonderful, thankyou..  That should give me something to work with..

Chris Percival
Software Engineer
Interaxis Computing Ltd.
DDI:  +44 (0)1249 700072
http://www.interaxis.co.uk/

> -----Original Message-----
> From: [hidden email]
> [[hidden email] Behalf Of Peter Shook
> Sent: 31 January 2002 05:02
> To: Multiple recipients of list
> Subject: Re: Read only variables
> 
> 
> aircooledspeed wrote:
> > 
> > If I create a variable using (for example):
> > 
> > lua_pushstring(L, "Hello world");
> > lua_setglobal(L, "MyHelloString");
> > 
> > Is there any way to make that variable read only (or 'constant') in
> > the script, ie any attempt to change their value results in an error?
> 
> You can make the value a userdata, and use the setglobal tag method to
> protect it.  But this only works for globals.  It won't work on local
> variables.
<snip>

Reply | Threaded
Open this post in threaded view
|

Multiline comments

Chris Percival
In reply to this post by Peter Shook-3
I remember seeing on lua.org a while ago, an add-on for multiline comments.
I can't seem to find it now.  Can anyone give me any pointers as to where it
might be?  Did it work for v4.0?

Thanks,

Chris Percival
Software Engineer
Interaxis Computing Ltd.
DDI:  +44 (0)1249 700072
http://www.interaxis.co.uk/


Reply | Threaded
Open this post in threaded view
|

RE: Multiline comments

Nick Trout-4
> I remember seeing on lua.org a while ago, an add-on for 
> multiline comments.
> I can't seem to find it now.  Can anyone give me any pointers 
> as to where it
> might be?  Did it work for v4.0?

First stop for any questions like this is: http://lua-users.org/wiki/

Patch for block comments for v4.0 is by ET:
http://lua-users.org/wiki/LuaPowerPatches

Although you just do:

_=[[
This is a multiline
block
comment
]]
_=nil  --optionally getrid of it

Regards,
Nick

Reply | Threaded
Open this post in threaded view
|

RE: Multiline comments

Chris Percival
In reply to this post by Chris Percival
Ah sorry, found it under http://lua-users.org/wiki/LuaPowerPatches .

Chris Percival
Software Engineer
Interaxis Computing Ltd.
DDI:  +44 (0)1249 700072
http://www.interaxis.co.uk/

> -----Original Message-----
> From: [hidden email]
> [[hidden email] Behalf Of Chris Percival
> Sent: 31 January 2002 09:23
> To: Multiple recipients of list
> Subject: Multiline comments
> 
> 
> I remember seeing on lua.org a while ago, an add-on for multiline 
> comments.
> I can't seem to find it now.  Can anyone give me any pointers as 
> to where it
> might be?  Did it work for v4.0?
> 
> Thanks,
> 
> Chris Percival
> Software Engineer
> Interaxis Computing Ltd.
> DDI:  +44 (0)1249 700072
> http://www.interaxis.co.uk/

Reply | Threaded
Open this post in threaded view
|

RE: Multiline comments

Luiz Henrique de Figueiredo
In reply to this post by Chris Percival
>Although you just do:
>
>_=[[
>This is a multiline
>block
>comment
>]]

A more flexible idiom is

 comment = [[
 This is a multiline
 block
 comment
 --]]

This allows block comments to be used for commenting out blocks of code.
The block can be made active again by simply adding "--" to "comment =".
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Read only variables

Luiz Henrique de Figueiredo
In reply to this post by Chris Percival
>Is there any way to make that variable read only (or 'constant') in 
>the script, ie any attempt to change their value results in an error?

See FAQ 3.3.
--lhf

Reply | Threaded
Open this post in threaded view
|

RE: Read only variables

Chris Percival
Again I am aware of this section of the FAQ.  Using the code that Peter
Shook posted, I have tried to add a tag method in the following way:

static int MyFunc(lua_State *L)
	{
	lua_error(L, "setglobal");
	return 0;
	}


int const_tag = lua_newtag(L);
lua_pushcfunction(L, MyFunc);
lua_settagmethod(L, const_tag, "setglobal");

This way I should be able to use my function to check the name of the
function and not allow it to be written to, but again my function is never
called.  I guess this is a basic tag method, but the documentation is a bit
sparse around this.  I cannot find 1 example of how to do it...

Chris Percival
Software Engineer
Interaxis Computing Ltd.
DDI:  +44 (0)1249 700072
http://www.interaxis.co.uk/

> -----Original Message-----
> From: [hidden email]
> [[hidden email] Behalf Of Luiz Henrique de
> Figueiredo
> Sent: 31 January 2002 11:52
> To: Multiple recipients of list
> Subject: Re: Read only variables
>
>
> >Is there any way to make that variable read only (or 'constant') in
> >the script, ie any attempt to change their value results in an error?
>
> See FAQ 3.3.
> --lhf


Reply | Threaded
Open this post in threaded view
|

RE: Read only variables

Luiz Henrique de Figueiredo
In reply to this post by Chris Percival
>int const_tag = lua_newtag(L);
>lua_pushcfunction(L, MyFunc);
>lua_settagmethod(L, const_tag, "setglobal");
>
>This way I should be able to use my function to check the name of the
>function and not allow it to be written to, but again my function is never
>called.

Your function will only be called for globals whose values have tag const_tag.
So, you must explicitly set this tag for the globals you want to protect.
--lhf