|
Virgil, thanks for giving me these informations, but something seems strange to me concerning the __newindex and __index metamethods. I thought it was designed for accessing and setting absent field in a table.
So i don't understand how it could be of any help since i don't want to catch absent field but fields which can be variables already present...
I should have misunderstood sthg...
Rgds.
----------------------------------------------------------------------
global variable conflictSee "sandboxing" in the list archives.
http://lua-users.org/cgi-bin/namazu.cgi?query=sandboxing&idxname=lua-l&max=2
0&result=normal&sort=date%3Alate
see also the "more modular scripts" thread from 2 days ago (July 13, 2004).
More simply since you have not mentioned being concerned about protecting
variables introduced by one script from overwriting by another script, but
only protecting specific variables introduced by the C code, simply
setting/using __newindex and __index metamethods on the globals table may
suffice. In this scheme's simplest form <AFAIK> you would NOT set these
protected values in the globals table, but rather add them to another table
and set that table as the __index member of the globals table's metatable.
The __newindex metamethod would simply need to check if any "new" global
variables already exist in the table of "protected globals", and if so issue
your warning, if not then it would go ahead and actually add the variable to
the globals table by way of a rawset operation.
Note that the above scheme will not protect "nested" members of tables, but
you could apply the method to each sub-table to be protected.
----
I haven't read it myself, but I'm fairly certain that Roberto's "Programming
in Lua" book covers this exact issue.
http://www.lua.org/pil/
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of BONNARD Cyril
Sent: Thursday, July 15, 2004 8:55 AM
To: lua@bazar2.conectiva.com.br
Subject: global variable conflict
Hello everybody,
i am new in Lua world, so i may ask some stupid question...
I am facing a problem with variable names. I have a set of global
variables defined in C and passed to the LUA stack via the C API. This stack
is left intact and reused for running some LUA script. The problem is that,
in the scripts, some variables could have the same name than variables
retreived from C code. This yield the erasement of the initial variables. Is
there any easy way to solve the problem, such as overloading the behaviour
of the assignment operator "=" to test if the current variable name is
already in use and warn the user of the conflict.
Any remark is obviously welcome!
Regards, cyril.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://bazar2.conectiva.com.br/mailman/private/lua/attachments/20040715/d2fb7fa1/attachment-0001.htm
------------------------------
Message: 2
Date: Thu, 15 Jul 2004 16:57:46 -0300
From: "Andre Carregal" <carregal@fabricadigital.com.br>
Subject: LuaUnit author(s)?
To: "Lua List (E-mail)" <lua@bazar2.conectiva.com.br>
Message-ID: <511BF1C97726D4119B44000629757A45D45E8C@BRAIN>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
I'm looking for the creator of LuaUnit
(http://www.gpgstudy.com/gpgiki/LuaUnit,
http://lua-users.org/wiki/UnitTesting).
Does anybody has any information on the author(s)?
Thanks in advance,
Andre Carregal
------------------------------
Message: 3
Date: Thu, 15 Jul 2004 22:25:14 -0600
From: Philip Bock <phil@flamewars.org>
Subject: string.dump problems
To: lua@bazar2.conectiva.com.br
Message-ID: <40F758AA.2010406@flamewars.org>
Content-Type: text/plain; charset="us-ascii"
I've written a simple preprocessor in Lua, to preprocess Lua scripts.
Basically, it implements #include and #define, so I don't have to define
a large number of global variables for commonly used constants. (By the
way, will I really save anything doing it this way? I assume numerical
constants are more efficient than global variables.)
Anyway, I decided to build it into a function I can include with my
program to preprocess and then byte-compile source files, so I don't
have to package a seperate luac binary. My problem is that string.dump()
doesn't seem to be generating valid byte-code. If I execute the function
returned from loadstring(), I get the expected results. If I dump the
code my preprocessor generates, and run it through either luac or
luac.lua, I get the expected results.
The byte-code string.dump() generates seems to contain a copy of the
text of the source passed to loadstring. I notice that neither luac or
luac.lua seem to do this.
The source is attached. To test it, call it like this:
lua preprocess.lua infile.lua outfile
My platform is win32, in case that matters.
Thanks, Philip Bock
-------------- next part --------------
--[[
This preprocessor was written for Lua scripts. It's not a C preprocessor,
because it only understands the #include, #define, and #undef directives.
No other features are supported, not even comments. No error checking
is performed, so incorrect directives will result in mangled output.
]]
-- Main parser
function preprocess(infile, outfile, macros)
for line in infile:lines() do
-- Perfrom macro substitution
for mname, mval in pairs(macros) do
line = string.gsub(line, "([^%w_])"..mname.."([^%w_])", function (x, y) return x..mval..y end)
line = string.gsub(line, "^"..mname.."$", mval)
line = string.gsub(line, "^"..mname.."[^%w_]", function (x) return mval..x end)
line = string.gsub(line, "[^%w_]"..mname.."$", function (x) return x..mval end)
end
-- Check for directives
local _, _, directive, arguments = string.find(line, "^%#(%w+)%s+(.+)$")
if (directive == nil) then -- No directives
outfile:write(line.."\n")
elseif (directive == "define") then
local _, _, macro_name, macro_val = string.find(arguments, "^(%S+)%s+(%S+)")
if (macro_val) then
macros[macro_name] = macro_val
else
macros[macro_name] = 0
end
elseif (directive == "undef") then
macros[arguments] = nil
elseif (directive == "include") then
local _, _, includefile = string.find(arguments, "^%p(.+)%p$")
preprocess(assert(io.open(includefile, "r"), "Error opening #include file "..includefile), outfile, macros)
else
error("Unknown directive "..directive)
end
end
end
function luac(infilename, outfilename)
local macros = {}
local infile = assert(io.open(infilename, "r"))
local outfile = assert(io.open(outfilename, "wb"))
local tempfile = io.tmpfile()
preprocess(infile, tempfile, macros)
tempfile:seek("set")
func = loadstring(tempfile:read("*a"))
func()
outfile:write(string.dump(func))
tempfile:close()
infile:close()
outfile:close()
end
luac(arg[1], arg[2])
------------------------------
Message: 4
Date: Fri, 16 Jul 2004 13:45:04 +0200 (BST)
From: Markus Huber <pulse@elmulab.de>
Subject: print does two things
To: lua@bazar2.conectiva.com.br
Message-ID: <Marcel-1.53-0716114504-0b0btQ3@markus.takeiteasy.de>
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
The follwoing line does two things:
myprint = print
1. does a real print
2. correct assignment of myprint
so it works but it prints too! And this is not allowed in my
circumstance. Thats why I would like to catch print.
???
--
Markus
------------------------------
Message: 5
Date: Fri, 16 Jul 2004 12:54:17 +0100
From: Rob Kendrick <lua-l@nun.org.uk>
Subject: Re: print does two things
To: Lua list <lua@bazar2.conectiva.com.br>
Message-ID: <20040716115417.GA23645@digital-scurf.org>
Content-Type: text/plain; charset=us-ascii
On Fri, Jul 16, 2004 at 01:45:04PM +0200, Markus Huber wrote:
> The follwoing line does two things:
>
> myprint = print
>
> 1. does a real print
> 2. correct assignment of myprint
>
> so it works but it prints too! And this is not allowed in my
> circumstance. Thats why I would like to catch print.
Am I reading this correctly? You want to make a copy of the reference
to 'print', but doing "foo = print" calls the print function? I didn't
know it did this, and I can't test it here. If it does however, have
you considered using rawget() instead?
--
Rob Kendrick, Pepperfish Ltd. +44 (0)845 226 4146 www.pepperfish.net
PGP signed or encrypted mail welcome Key ID: 3651D17A
------------------------------
Message: 6
Date: Fri, 16 Jul 2004 14:00:32 +0200
From: Michael Roth <mroth@nessie.de>
Subject: Re: print does two things
To: Lua list <lua@bazar2.conectiva.com.br>
Message-ID: <40F7C360.4090404@nessie.de>
Content-Type: text/plain; charset=us-ascii; format=flowed
Markus Huber wrote:
> The follwoing line does two things:
>
> myprint = print
>
> 1. does a real print
> 2. correct assignment of myprint
>
> so it works but it prints too! And this is not allowed in my
> circumstance. Thats why I would like to catch print.
Not confirmend. My lua does assign print to myprint only. All work
correctly. Nothing will printed by this statement:
$ lua -v
Lua 5.0.2 Copyright (C) 1994-2004 Tecgraf, PUC-Rio
$ echo "myprint = print" | lua -
$
------------------------------
Message: 7
Date: Fri, 16 Jul 2004 14:34:56 +0200 (BST)
From: Markus Huber <pulse@elmulab.de>
Subject: Re: print does two things - canceled
To: lua@bazar2.conectiva.com.br
Message-ID: <Marcel-1.53-0716123456-d07btQ3@markus.takeiteasy.de>
Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
Thank you. Its an specific bug in my program and has nothing to to with
the Lua.bin
--
Markus
End of Lua Digest, Vol 65, Issue 5
**********************************