lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello, 

Sorry. There is a typo in the code. The example code is as the following (hello.lua):

#!/usr/bin/lua

print('c')


The test.c file is as the following:


#include <stdio.h>

#include<lua.h>
#include<lualib.h>
#include<lauxlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


int main(int argc, char* argv[]) {
    int error;
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    if (argc < 2) {
      printf("error the arg at least two\n");
      return 0;
    }
    char *f = argv[1];
    FILE *E = fopen(f, "rb+");
    if (E == NULL) {
       printf("cannot open file\n");
       return 0;
    }
    fseek(E, 0, SEEK_END);
    size_t size = ftell(E);
    printf("buffer size is %d\n", size);
    fseek(E, 0, SEEK_SET);

    char *buffer = (char *)malloc(size);
    if (!buffer)
    {
        printf("malloc buffer error\n");
        fclose(E);
        return 0;
    }
    if (fread(buffer, size, 1, E) != 1)
    {
        fclose(E);
        return 0;
    }


    fclose(E);  
    
    int e = luaL_loadbuffer(L, buffer, size, "line");
    if (e == 0) {
      e = lua_pcall(L, 0, 0, 0);
    }
    else {
      printf("loadbuffer error\n");
    }
   
    lua_close(L);

    return 0;
}

I compile the code as the following:

gcc -o test test.c -I ../src -lm -llua


Then run the code:

./test hello.lua


It prints

"loadbuffer error"




szbnwer@gmail.com <szbnwer@gmail.com> 于2019年5月22日周三 下午12:07写道:
ahh, just seen that in the title u wrote it just the right way, so i
dunno if there is a typo in ur actual code or just in ur example,
however in the worst case, u can still manually trim the shebang line
from ur scripts, but i think if lua can handle shebangs then it can do
so via the c api...

2019-05-22 3:59 GMT, szbnwer@gmail.com <szbnwer@gmail.com>:
> hi there! :)
>
> i think u need this:
>
> #!/usr/bin/lua
> (note the "!")
>
> otherwise ive never played with the c api, but for a 2nd shot i'd try
> the same without the whole 1st line. if none will do the trick, then
> others will help :D
>
> good luck and the like! :)
>
> 2019-05-22 1:29 GMT, Baozeng <sploving1@gmail.com>:
>> Hello all,
>>   The last email is not show normally. so I send again.
>> I have a simple Lua file, for example, hello.lua. The contents of the
>> file
>> is as the following:
>>
>> #/usr/bin/lua
>> print('hello')
>>
>>  I first read the file content into a buffer. And then use
>> luaL_loadbuffer
>> to load the buffer and finally use lua_pcall to call the function.
>> However,
>> I have found that luaL_loadbuffer cannot return 0 because it does not
>> deal
>> with this case.
>>
>> Do you think this is a feature missing? If it does, I would like to
>> commit
>> the code to deal with that.
>> Thank you.
>>
>> Baozeng <sploving1@gmail.com> 于2019年5月22日周三 上午9:19写道:
>>
>>> Hello all,
>>>     I have a simple Lua file, for example, hello.lua. The contents of
>>> the
>>> file is as the following:
>>>
>>> #/usr/bin/lua
>>> print('hello')
>>>
>>>  I first read the file content into a buffer. And then use
>>> luaL_loadbuffer
>>> to load the buffer and finally use lua_pcall to call the function.
>>> However,
>>> I have found that luaL_loadbuffer cannot return 0 because it does not
>>> deal
>>> with this case.
>>>
>>> Do you think this is a feature missing? If it does, I would like to
>>> commit
>>> the code to deal with that.
>>> Thank you.
>>>
>>> --
>>>      Best Regards,
>>>      Baozeng Ding
>>>
>>>
>>
>>
>> --
>>      Best Regards,
>>      Baozeng Ding
>>
>



--
     Best Regards,
     Baozeng Ding