Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Alert! New version of Lua is being developed (Lua 5.2)

Alert! New version of Lua is being developed (Lua 5.2)

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #15 on Wed 10 Feb 2010 10:48 PM (UTC)
Message
In order to emulate what existing behaviour?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #16 on Wed 10 Feb 2010 10:50 PM (UTC)
Message
This:

foo = loadfile("filename.lua")
setfenv(foo, env)
foo()

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #17 on Wed 10 Feb 2010 10:51 PM (UTC)
Message
I can't get loadstring to work at all as I would expect, but since we have loadin that isn't a big deal.

One example of many I tried:


local arg = " a = 1; b = 2 "

-- need these inside the local environment
local print = print
local loadstring = loadstring
local assert = assert

local t = {}
in t do
  local f = assert (loadstring (arg))
  f ()
  print (a, b)
end -- in

print (t.a, t.b)


Prints:


nil     nil
nil     nil


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #18 on Wed 10 Feb 2010 10:56 PM (UTC)
Message
Twisol said:

This:

foo = loadfile("filename.lua")
setfenv(foo, env)
foo()



Well you have got me on that one. I would have assumed:


in env do
  foo = loadfile("filename.lua")
  foo()
end


But since my loadstring doesn't work, I guess that won't either.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #19 on Wed 10 Feb 2010 10:58 PM (UTC)
Message
Nick Gammon said:

I can't get loadstring to work at all as I would expect, but since we have loadin that isn't a big deal.

One example of many I tried:

...


Notice that if you change the final "print (t.a, t.b)" to "print (a, b)", it prints "1 2", confirming what I said before about loadstring (and loadfile by extension) loading in to their own environments, not the caller's environment.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #20 on Wed 10 Feb 2010 11:01 PM (UTC)

Amended on Wed 10 Feb 2010 11:02 PM (UTC) by Twisol

Message
Nick Gammon said:

Twisol said:

This:

foo = loadfile("filename.lua")
setfenv(foo, env)
foo()



Well you have got me on that one. I would have assumed:


in env do
  foo = loadfile("filename.lua")
  foo()
end


But since my loadstring doesn't work, I guess that won't either.


loadstring itselfs returns a compiled chunk, and loadstring's environment is the one it was defined in (i.e. the original) rather than the one in the 'in' statement. Hence, the chunk's environment is also the original one. The only way to get around this is to get the contents of the file itself and pass that in to loadin(). This applies equally to loadfile - the only difference is where the chunk's code is obtained.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #21 on Wed 10 Feb 2010 11:11 PM (UTC)
Message
You seem to be quite correct on both counts. Browsing the Lua list, they appear to suggest at one point as a replacement for your loadfile problem:


file = assert (io.open (path))
source = file:read ("*all")
file:close ()
func = loadin (env, source, "@"..path)


However this means making io.open available, and unless you play around a bit, it makes io.open for *output* available, which is a bit of a security hole, letting plugins write arbitrary files to your hard disk. Even reading them is bad enough. For example, a plugin might open all your world files, looking for player names and passwords, and then secretly pass them on using various mechanisms to the author.

At least loadfile would only load Lua code so its use for general reading of passwords was quite limited, and you couldn't use it to write to disk.

I don't know the answer to this ... do we need loadfile to be used with an environment? I admit I suggest it here:

Template:post=6294 Please see the forum thread: http://gammon.com.au/forum/?id=6294.


But would it be generally used?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #22 on Wed 10 Feb 2010 11:20 PM (UTC)
Message
Nick Gammon said:
But would it be generally used?


Generally? Well, considering the usual complexity of a typical plugin - from what I've seen, not very - I doubt it. But I'm working a bit on a small library to complement my 'structured plugin' idiom, which does use this technique a bit. I call it Plugger, and it's something of a lightweight 'plugin framework'. I'm using addxml as the standard means of defining aliases, triggers, etc - which I collectively call 'reflexes' since they cause something to happen when an certain event occurs - and it's much simpler and pleasing to be able to write a file that looks like this:

alias {
  match = [[^\s*test\s+alias\s*$]]
  send = [[
    "stand"
  ]]
}

trigger {
  -- etc
}

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #23 on Wed 10 Feb 2010 11:22 PM (UTC)
Message
I've posted a summary of this problem on the Lua list, maybe someone will suggest something. Probably they will suggest I make io.open available.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #24 on Wed 10 Feb 2010 11:31 PM (UTC)

Amended on Thu 11 Feb 2010 12:20 AM (UTC) by Twisol

Message
Or you could write your own loadfile() wrapper. Loadfile only takes one argument currently, it shouldn't be so bad if you write a wrapper.

The below code is Lua, but you'd probably actually implement it in C. (lua_setfenv is still valid in the C API, as far as I know)


function loadfile(env, filename)
  -- support loadfile(filename) form
  if filename == nil then
    filename = env
    env = nil
  end
  
  local chunk = loadfile(filename)
  if env then
    setfenv(chunk, env)
  end
  
  return chunk
end


Then, users simply use "t = {}; loadfile(t, filename)", using the same argument order as loadin.

EDIT: Belatedly, I forgot the last 'end'. Added.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #25 on Thu 11 Feb 2010 01:40 AM (UTC)

Amended on Thu 11 Feb 2010 01:41 AM (UTC) by Twisol

Message
*laughs* The guy who responded on the mailing list basically suggested the same thing I just did. ^_^ He wrote his version in backwards-compatible Lua, whereas I'd suggest implementing it in C, but it's the same idea.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #26 on Thu 11 Feb 2010 02:25 AM (UTC)
Message
Seems to me this is throwing functionality out the window. After all, the original loadfile (and setfenv) were core Lua functions. Using io.read means you have to incorporate the io library, something you may conceivably not want to do.

Your suggestion of a workaround function is OK (I mean, it can be solved one way or the other), but I think I'll plug at the Lua developers a bit more. After all, a new release shouldn't remove core functionality without giving a reasonable workaround (short of everyone furiously putting things back through custom functions).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #27 on Thu 11 Feb 2010 02:28 AM (UTC)
Message
I fully agree. :)

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


77,797 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.