2

Possible Duplicate:
Redirecting/redefining print() for embedded Lua

I am new to Lua and rather confused. I have seen this but Im not sure it works for 5.2.

I have read about _ENV table(?) but again not sure if this has anything to do with it.

So on to the question: How do I, in C/C++, redirect the in-built print function of Lua 5.2 to call my own C/C++ function?

Community
  • 1
  • 1
alfred
  • 639
  • 1
  • 6
  • 11
  • 2
    Doesn't the top-rated answer in the linked question work for you? – Some programmer dude Sep 07 '12 at 08:00
  • I can not test it as its incomplete - it assumes you know how to fill in the rest of the code needed - which I dont... yet... And I dont think in 5.2 the global space is called "_G" anymore (I could be wrong) – alfred Sep 07 '12 at 08:32
  • 1
    @OlegV.Volkov OP mentionned this question and said he was not sure about his case. – Eregrith Sep 07 '12 at 09:21
  • @Eregrith, being closed as duplicate is good reassurance that it is, indeed, exactly same question. – Oleg V. Volkov Sep 07 '12 at 09:25
  • @OlegV.Volkov The question here is more "Does this work in 5.2" more than "How do you do this" I think. So it's not exactly the same question. – Eregrith Sep 07 '12 at 09:31
  • @Eregrith, `print 2+2` used to work in Perl 5.8, will it work in Perl 5.10? Sorry, but this question talks about very basic use of language that will unlikely to ever change between versions and shows complete lack of even trying to check already suitable answer. – Oleg V. Volkov Sep 07 '12 at 10:32
  • @OlegV.Volkov - not true; The way globals work in Lua has changed significantly between 5.1 and 5.2. – finnw Sep 07 '12 at 11:01
  • @finnw, you can no longer do `print = function() --[[whatever]] end`? Oh wow, you learn something new every day! – Oleg V. Volkov Sep 07 '12 at 11:02
  • 1
    @OlegV.Volkov: In simple cases you can. In more subtle cases it might not affect code in previously-loaded chunks (whilst it would have done so in Lua 5.1.) The OP is right to be concerned about `_ENV`, and I think that makes this question different from the other one. – finnw Sep 07 '12 at 11:15
  • @finnw, ability to have different global environment for arbitrary chunk/function is not something new to 5.2. `setfenv` was in Lua way before that. – Oleg V. Volkov Sep 07 '12 at 11:26

1 Answers1

2

From Lua 5.2 manual:

Lua keeps a distinguished environment called the global environment.
This value is kept at a special index in the C registry (see §4.5).
In Lua, the variable _G is initialized with this same value.

So this answer to the question you linked will work if you replace the calls to luaL_register to calls to luaL_setfuncs.

Community
  • 1
  • 1
Eregrith
  • 4,263
  • 18
  • 39
  • Nice find - thank you. Only problem is that 5.2 doesnt have luaL_register call anymore. So either the "duplicate" needs to be updated to reflect 5.2 changes or this is not a duplicate question? – alfred Sep 07 '12 at 12:52
  • Maybe the answer is in **Resolve module system and module function issues** section of [this page](http://lua-users.org/wiki/LuaFiveTwo): `The module/luaL_register functions are deprecated and replaced by luaL_newlib and luaL_setfuncs. ` – Eregrith Sep 07 '12 at 12:58
  • Thx @Eregrith: both of your suggestions helped out, which let me work it out: https://gist.github.com/3671566 And no the other _duplicate_ does **NOT** work with 5.2 - you guys are too quick with the duplicate button :P (it would be good to reopen this so I can put the code up for someone else to use). – alfred Sep 08 '12 at 04:26
  • I don't know how to do this :/ – Eregrith Sep 10 '12 at 08:49
  • Ehm ? Why did you unaccept this ? – Eregrith Sep 10 '12 at 13:00
  • I initially accepted this as I thought people were right in the fact that I did not understand things properly and it was a duplicate. I then realised that not to be the case so I unaccepted it. As the answer was meant to be about how to do it using Lua's 5.2 version of functions. – alfred Sep 10 '12 at 14:00
  • Well, I thought the answer's comment about replacing calls to `luaL_register` by calls to `luaL_setfuncs` did the trick ? – Eregrith Sep 10 '12 at 16:00
  • [Here is a more concise way to do this](http://pastebin.com/2kAt4fKq) – alfred Sep 11 '12 at 01:20