How to locate the implementation of a specific function, e.g. string.byte?

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

How to locate the implementation of a specific function, e.g. string.byte?

孙世龙 sunshilong
Hi,

How to locate the implementation of a specific function, e.g. string.byte?
I hope to dig deep in the related code snippet.
I would be grateful to have some help with this question.

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: How to locate the implementation of a specific function, e.g. string.byte?

Luiz Henrique de Figueiredo
> How to locate the implementation of a specific function, e.g. string.byte?

https://www.lua.org/source/5.4/lstrlib.c.html#str_byte
Reply | Threaded
Open this post in threaded view
|

Re: How to locate the implementation of a specific function, e.g. string.byte?

Francisco Olarte
In reply to this post by 孙世龙 sunshilong
Hi Sunshilong:
On Sat, Oct 10, 2020 at 9:47 AM 孙世龙 sunshilong <[hidden email]> wrote:
> How to locate the implementation of a specific function, e.g. string.byte?
> I hope to dig deep in the related code snippet.
> I would be grateful to have some help with this question.

Source code is pretty easy to follow. Normally functions are
registered  in src/l*lib.c. You can start by grepping for
luaL_setfuncs, which is the function most normally used to register
them, and find things like ( in 5.4.0 sources )

src/lstrlib.c
1786:  luaL_setfuncs(L, stringmetamethods, 0);

Then you can go there and see:

   1761 static const luaL_Reg strlib[] = {
   1762   {"byte", str_byte},
   1763   {"char", str_char},

And from these is just a matter of searching for it:

    176 static int str_byte (lua_State *L) {
    177   size_t l;
    178   const char *s = luaL_checklstring(L, 1, &l);

This method works generally for me when I'm curious on how a lib
function is done.

Francisco Olarte.
Reply | Threaded
Open this post in threaded view
|

Re: How to locate the implementation of a specific function, e.g. string.byte?

孙世龙 sunshilong
Hi, Francisco Olarte

Thank you for your clarification.

>Source code is pretty easy to follow. Normally functions are
>registered  in src/l*lib.c.
If I understand you correctly, the functions that provided by the sources that
named as src/l*.c which are without "lib" characters are used internally by Lua,
not for the users?

Am I right?
Best regards
Sunshilong

On Sat, Oct 10, 2020 at 7:11 PM Francisco Olarte <[hidden email]> wrote:

>
> Hi Sunshilong:
> On Sat, Oct 10, 2020 at 9:47 AM 孙世龙 sunshilong <[hidden email]> wrote:
> > How to locate the implementation of a specific function, e.g. string.byte?
> > I hope to dig deep in the related code snippet.
> > I would be grateful to have some help with this question.
>
> Source code is pretty easy to follow. Normally functions are
> registered  in src/l*lib.c. You can start by grepping for
> luaL_setfuncs, which is the function most normally used to register
> them, and find things like ( in 5.4.0 sources )
>
> src/lstrlib.c
> 1786:  luaL_setfuncs(L, stringmetamethods, 0);
>
> Then you can go there and see:
>
>    1761 static const luaL_Reg strlib[] = {
>    1762   {"byte", str_byte},
>    1763   {"char", str_char},
>
> And from these is just a matter of searching for it:
>
>     176 static int str_byte (lua_State *L) {
>     177   size_t l;
>     178   const char *s = luaL_checklstring(L, 1, &l);
>
> This method works generally for me when I'm curious on how a lib
> function is done.
>
> Francisco Olarte.