lua_getglobal function argument

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

lua_getglobal function argument

saito yutaka
Hello .
I am trying to read lua.c to want know how lua works.
And I want to ask about lua_getglobal function.

I find a this code .
  lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");

I know that this code means get a value PROMPT("> ") or PROMPT2(">> ") .
But why does it use underscore [ _ ] front of global names ?

Thanks.
Yutaka

http://saitolog.blogspot.com/

Reply | Threaded
Open this post in threaded view
|

RE: lua_getglobal function argument

Thomas Buergel
> I find a this code .
>   lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
>
> I know that this code means get a value PROMPT("> ") or PROMPT2(">> ") .
> But why does it use underscore [ _ ] front of global names ?

Look at the rest of the function:

  static const char *get_prompt (lua_State *L, int firstline) {
    const char *p;
    lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
    p = lua_tostring(L, -1);
    if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
    lua_pop(L, 1);  /* remove global */
    return p;
  }

It attempts to read the globals _PROMPT or _PROMPT2, and if they don't exist, it uses the defaults defined in C (LUA_PROMPT and LUA_PROMPT2).

This is also defined in the Lua manual (at least in 5.1) [1]:

  The primary prompt is the value of the global variable _PROMPT,
  if this value is a string; otherwise, the default prompt is used.
  Similarly, the secondary prompt is the value of the global variable _PROMPT2.

You can try this easily yourself:

  Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
  > _PROMPT="first: "
  first: _PROMPT2="next: "
  first:
  first: for i=1,2 do
  next: print(i)
  next: end
  1
  2
  first:


[1]: http://www.lua.org/manual/5.1/lua.html



Reply | Threaded
Open this post in threaded view
|

Re: lua_getglobal function argument

Duncan Cross
In reply to this post by saito yutaka
On Wed, Feb 8, 2012 at 10:35 AM, saito yutaka <[hidden email]> wrote:

> Hello .
> I am trying to read lua.c to want know how lua works.
> And I want to ask about lua_getglobal function.
>
> I find a this code .
>  lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
>
> I know that this code means get a value PROMPT("> ") or PROMPT2(">> ") .
> But why does it use underscore [ _ ] front of global names ?
>
> Thanks.
> Yutaka

Starting with one underscore is just a convention to indicate
"internal" global variables that relate to the host
application/current context, rather than the specific program being
run. For example _VERSION[1]. It is recommended that a piece of pure
Lua code should avoid using this convention when defining its own
global variables.

[1] http://www.lua.org/manual/5.1/manual.html#pdf-_VERSION

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: lua_getglobal function argument

saito yutaka
Thanks for your help.

Regards

2012/2/8 Duncan Cross <[hidden email]>:

> On Wed, Feb 8, 2012 at 10:35 AM, saito yutaka <[hidden email]> wrote:
>> Hello .
>> I am trying to read lua.c to want know how lua works.
>> And I want to ask about lua_getglobal function.
>>
>> I find a this code .
>>  lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
>>
>> I know that this code means get a value PROMPT("> ") or PROMPT2(">> ") .
>> But why does it use underscore [ _ ] front of global names ?
>>
>> Thanks.
>> Yutaka
>
> Starting with one underscore is just a convention to indicate
> "internal" global variables that relate to the host
> application/current context, rather than the specific program being
> run. For example _VERSION[1]. It is recommended that a piece of pure
> Lua code should avoid using this convention when defining its own
> global variables.
>
> [1] http://www.lua.org/manual/5.1/manual.html#pdf-_VERSION
>
> -Duncan
>