Loading ...
Sorry, an error occurred while loading the content.

LUA interface giving me trouble with garbage collection patch

Expand Messages
  • Benjamin Fritz
    I want to finish up this patch to fix a crash in Vim: https://groups.google.com/d/topic/vim_dev/dnN58kO5Vg4/discussion I changed luaV_setref() to return a
    Message 1 of 5 , Jul 6, 2014
    • 0 Attachment
      I want to finish up this patch to fix a crash in Vim:
      https://groups.google.com/d/topic/vim_dev/dnN58kO5Vg4/discussion

      I changed luaV_setref() to return a value if garbage collection cannot
      safely proceed.

      But, I do not know how to get that return value back to the code
      calling it from eval.c, via set_ref_in_lua(). Can someone please
      explain briefly how the function calls in the LUA interface work? I
      cannot figure out how to get a return value back from lua_call(), in
      the C code.

      --
      --
      You received this message from the "vim_dev" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/d/optout.
    • Yukihiro Nakadaira
      On Mon, Jul 7, 2014 at 2:36 PM, Benjamin Fritz ... Here is sample code. #include #include #include /*
      Message 2 of 5 , Jul 9, 2014
      • 0 Attachment
        On Mon, Jul 7, 2014 at 2:36 PM, Benjamin Fritz <fritzophrenic@...> wrote:
        I want to finish up this patch to fix a crash in Vim:
        https://groups.google.com/d/topic/vim_dev/dnN58kO5Vg4/discussion

        I changed luaV_setref() to return a value if garbage collection cannot
        safely proceed.

        But, I do not know how to get that return value back to the code
        calling it from eval.c, via set_ref_in_lua(). Can someone please
        explain briefly how the function calls in the LUA interface work? I
        cannot figure out how to get a return value back from lua_call(), in
        the C code.

        Here is sample code.

        #include <lua.h>
        #include <lauxlib.h>
        #include <stdio.h>

        /* int add(int x, int y) */
        int add(lua_State *L)
        {
            int x = lua_tointeger(L, 1);
            int y = lua_tointeger(L, 2);
            /* push result to stack */
            lua_pushinteger(L, x + y);
            /* return number of results */
            return 1;
        }

        int main()
        {
            lua_State *L;
            int r;

            L = luaL_newstate();

            /* r = add(111, 222) */
            lua_pushcclosure(L, add, 0);
            lua_pushinteger(L, 111);
            lua_pushinteger(L, 222);
            lua_call(L, 2 /* nargs */, 1 /* nresults */);
            /* get results */
            r = lua_tointeger(L, -1);
            /* remove results from stack */
            lua_pop(L, 1);

            printf("add(111, 222) => %d\n", r);

            lua_close(L);

            return 0;
        }

        --
        Yukihiro Nakadaira - yukihiro.nakadaira@...

        --
        --
        You received this message from the "vim_dev" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_dev" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        For more options, visit https://groups.google.com/d/optout.
      • Ben Fritz
        ... Thanks! I ll give this a try later. What s the -1 for in lua_tointeger()? Is there a good way to test this when I m done? E.g. are there LUA tests in Vim s
        Message 3 of 5 , Jul 10, 2014
        • 0 Attachment
          On Wednesday, July 9, 2014 11:24:50 PM UTC-5, Yukihiro Nakadaira wrote:
          >
          > Here is sample code.
          >
          > [edited]
          >
          >     L = luaL_newstate();
          >
          >     lua_pushcclosure(L, add, 0);
          >     lua_pushinteger(L, 111);
          >     lua_pushinteger(L, 222);
          >     lua_call(L, 2 /* nargs */, 1 /* nresults */);
          >     r = lua_tointeger(L, -1);
          >     lua_pop(L, 1);
          >     lua_close(L);

          Thanks! I'll give this a try later. What's the -1 for in lua_tointeger()?

          Is there a good way to test this when I'm done? E.g. are there LUA tests
          in Vim's test suite that will exercise the garbage collector? Or maybe a
          toy script using the LUA interface I could run myself?

          --
          --
          You received this message from the "vim_dev" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_dev" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
          For more options, visit https://groups.google.com/d/optout.
        • Павлов Николай Алекса
          ... Hash: SHA512 ... Almost all lua_* functions operate with stack. -1 is stack index. Like indexes in lua itself 1 means first value in the stack, 2 means
          Message 4 of 5 , Jul 10, 2014
          • 0 Attachment
            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA512

            On July 10, 2014 7:49:02 PM GMT+03:00, Ben Fritz <fritzophrenic@...> wrote:
            >On Wednesday, July 9, 2014 11:24:50 PM UTC-5, Yukihiro Nakadaira wrote:
            >>
            >> Here is sample code.
            >>
            >> [edited]
            >>
            >>     L = luaL_newstate();
            >>
            >>     lua_pushcclosure(L, add, 0);
            >>     lua_pushinteger(L, 111);
            >>     lua_pushinteger(L, 222);
            >>     lua_call(L, 2 /* nargs */, 1 /* nresults */);
            >>     r = lua_tointeger(L, -1);
            >>     lua_pop(L, 1);
            >>     lua_close(L);
            >
            >Thanks! I'll give this a try later. What's the -1 for in
            >lua_tointeger()?

            Almost all lua_* functions operate with stack. -1 is stack index. Like indexes in lua itself 1 means first value in the stack, 2 means second, ..., -1 means last, -2 means last but one, ...

            In lua_pop you though specify how many values to pop, not which element to pop.

            >
            >Is there a good way to test this when I'm done? E.g. are there LUA
            >tests
            >in Vim's test suite that will exercise the garbage collector? Or maybe
            >a
            >toy script using the LUA interface I could run myself?

            -----BEGIN PGP SIGNATURE-----
            Version: APG v1.1.1

            iQI1BAEBCgAfBQJTvrb/GBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
            HhHSvmnCD/9oKtyxddAIENOS3Yj+18oqxBAfgNFKjNiToD7tY87rx999YYFevnIc
            KhgHaoiqcyw4cmTLWnBduoA1VmtUGt8iwxT9VXoOpw6HfbWqiuaqAsoIKrLIoLZp
            H7zcLZIQX763eimUY/PsUgzDVShR73OsjHl8x0PcoYXsh80KcW42Fa5xNoV7O4lr
            eqnG5V3OsQ6zcOz+SjUN6J3yHITDOpLGe0YNBuSctPqRtHooNR191rzfylQAJna9
            5Mx5r05rXAg++HKF6sb7btf0lWMaUmYNJvTweRaxd6mSywxgwh9M+jZ7yiGXE9r3
            thD7Be3Tk7woQT0ZdgkoioH6/efZNovoVf2NB/eOU17w3Dxm0doHfyH/s4xH5Dqq
            4m2wJMtwlV2R0SYVYjzSOUXBVRxunUlU9m0K4CV2FUUwabLGaEJvmfegUt25sUlL
            pcO1OFaJNLjGyHM96xbMwv1GpjRgUAfSjVbbDHxjeV/GeanemLlp8etYw1CTP9zs
            CNNlVMzoHMSvYC8dGO4lqv7fdXNSFzvfUT/lL6i4trxVupgCvZ3oxWX4VUBTHRd4
            Zzjr20iVLe4TGJTlSyCScvpMSZGdNiEEBhYhUpZGE20J5On7w5y2XHryrsNn+dOP
            ZLNVywtzroQxKaLK79aOf4UDJhH2eKTGpuOiKQCnTk7/TxUXlPadTw==
            =UYLM
            -----END PGP SIGNATURE-----

            --
            --
            You received this message from the "vim_dev" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_dev" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
            For more options, visit https://groups.google.com/d/optout.
          • Yukihiro Nakadaira
            ... I have no idea. -- Yukihiro Nakadaira - yukihiro.nakadaira@gmail.com -- -- You received this message from the vim_dev maillist. Do not top-post! Type
            Message 5 of 5 , Jul 11, 2014
            • 0 Attachment
              On Fri, Jul 11, 2014 at 12:49 AM, Ben Fritz <fritzophrenic@...> wrote:
              On Wednesday, July 9, 2014 11:24:50 PM UTC-5, Yukihiro Nakadaira wrote:
              >
              > Here is sample code.
              >
              > [edited]
              >
              >     L = luaL_newstate();
              >
              >     lua_pushcclosure(L, add, 0);
              >     lua_pushinteger(L, 111);
              >     lua_pushinteger(L, 222);
              >     lua_call(L, 2 /* nargs */, 1 /* nresults */);
              >     r = lua_tointeger(L, -1);
              >     lua_pop(L, 1);
              >     lua_close(L);

              Thanks! I'll give this a try later. What's the -1 for in lua_tointeger()?

              Is there a good way to test this when I'm done? E.g. are there LUA tests
              in Vim's test suite that will exercise the garbage collector? Or maybe a
              toy script using the LUA interface I could run myself?

              I have no idea.

              --
              Yukihiro Nakadaira - yukihiro.nakadaira@...

              --
              --
              You received this message from the "vim_dev" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_dev" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/d/optout.
            Your message has been successfully submitted and would be delivered to recipients shortly.