0

I am wondering what I am missing in using a custom c function for loadfile in Lua.

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

static void panic(char* error)
{
  fprintf(stderr, "panic: %s\n", error);
  exit(1);
}

static int _custom_loadfile(lua_State* L)
{
  const char* rfile = luaL_checkstring(L, 1);
  FILE* rfile_fp = fopen(rfile, "r");
  if (rfile_fp == NULL) panic("failed to open rfile.");
  if (fseek(rfile_fp, 0, SEEK_END) != 0) panic("failed to seek to eof.");
  size_t buffersize = ftell(rfile_fp) + 1 * sizeof(char);
  char* buffer = (char*) malloc(buffersize);
  if (buffer == NULL) lua_error(L);
  if (fseek(rfile_fp, 0, SEEK_SET) != 0) panic("failed to seek to start.");
  if (fread(buffer, 1, buffersize, rfile_fp) != buffersize) panic("failed to read file");
  int error = luaL_loadbuffer(L, buffer, bufferfilesize, rfile);
  if (error) lua_error(...);
  return 1; // one value
}

...

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

  lua_pushcfunction(L, _custom_loadfile);
  lua_setglobal(L, "loadfile");
  ...
}

example.lua

...
custom_table = loadfile('custom_table')
print(custom_table)
...

custom_table.lua

return {
  name = 'custom_table',
  other = 'lots of stuff',
  table = {},
}

Output

[string "custom_table":165: '<eof>' expected

I have also tried a lua_tostring(L); after the loadbuffer, but that gives me a different error as the data is not a string.

Tippecanoe
  • 188
  • 2
  • 10
  • Aside: Casting the return value of `malloc()` is redundant and merely servers to clutter one's code. You should instead check `malloc()`'s return value and compare it with `NULL` to see if it failed to allocate memory. And `sizeof(char)` is defined by the standard to be 1, so it can be left out. – Harith Aug 28 '23 at 21:10
  • The [docs](https://www.lua.org/pil/26.1.html) indicate the function should return an `int` not `void`. – Ben Voigt Aug 28 '23 at 21:10
  • 1
    The message `[string "custom_table":165: '' expected` means Lua parser found syntax error in your Lua script (was it loaded from `magic_file.txt`?) – ESkri Aug 28 '23 at 22:46
  • 1
    Please post a complete [mre]. `fp` is entirely unused. `magicfilesize` and `bufferfilesize` are undefined. `...` is (obviously) a syntax error. It is unclear how you arrive at a point where there is data in the buffer. Do not omit things for brevity at the sake of clarity. If your example is too long, reconstruct it until it is minimal but still able to be compiled (or otherwise fails compilation, but the diagnostic produced is the focus of the question). – Oka Aug 29 '23 at 12:02
  • I have updated it from the comment feedback, I simplified the code to remove some of the complexity and focus on just getting the luaL_loadbuffer working – Tippecanoe Aug 29 '23 at 19:03
  • 1
    What are the contents of the file `custom_table`? – Oka Aug 29 '23 at 20:42
  • As @ESkri commented earlier, it would seem that the problem is in the `custom_table` file itself. That's what that parser output is telling you. – Mike Andrews Aug 29 '23 at 20:42
  • Note: using `ftell` to find the "size of a file" is fraught with peril - see this [excellent answer](https://stackoverflow.com/a/49122325/2505965). Judging purely by your pointer style and casting of the return of `malloc`, I would hazard a guess that you are using a C++ compiler/IDE - possibly MSVC, in which case the point about `ftell` and *text* files (`fopen(..., "r")`) on Windows is extremely relevant. – Oka Aug 29 '23 at 21:04

1 Answers1

0

This isn't a problem with your C code at all. This output is the key to solving your problem.

[string "custom_table":165: '<eof>' expected

The 165 in there is a line number. This error indicates that the Lua interpreter expected the program code to end, but additional unexpected content is present. For example, here's your provided custom_table file with "blah" at the end of it:

return {
  name = 'custom_table',
  other = 'lots of stuff',
  table = {},
}

blah

And here's what Lua says about it.

% lua /tmp/custom_table.lua
lua: /tmp/custom_table.lua:7: <eof> expected near 'blah'

Review the contents of your custom_table file. You will likely find an extra closing curly brace in the file on or before line 165.

Mike Andrews
  • 3,045
  • 18
  • 28