Ejemplo n.º 1
0
static int dofile (lua_State *L, const char *name) {
#if defined( LUA_USES_LOADF )
  return dochunk(L, luaL_loadfile(L, name));
#else
  return dochunk(L, luaL_loadfsfile(L, name));
#endif
}
Ejemplo n.º 2
0
static int luaB_loadfile (lua_State *L) {
  const char *fname = luaL_optstring(L, 1, NULL);
#if 0
  return load_aux(L, luaL_loadfile(L, fname));
#else
  return load_aux(L, luaL_loadfsfile(L, fname));
#endif
}
Ejemplo n.º 3
0
// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
static int node_compile( lua_State* L )
{
  Proto* f;
  int file_fd = FS_OPEN_OK - 1;
  size_t len;
  const char *fname = luaL_checklstring( L, 1, &len );
  if ( len > FS_NAME_MAX_LENGTH )
    return luaL_error(L, "filename too long");

  char output[FS_NAME_MAX_LENGTH];
  c_strcpy(output, fname);
  // check here that filename end with ".lua".
  if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) )
    return luaL_error(L, "not a .lua file");

  output[c_strlen(output) - 2] = 'c';
  output[c_strlen(output) - 1] = '\0';
  NODE_DBG(output);
  NODE_DBG("\n");
  if (luaL_loadfsfile(L, fname) != 0) {
    return luaL_error(L, lua_tostring(L, -1));
  }

  f = toproto(L, -1);

  int stripping = 1;      /* strip debug information? */

  file_fd = fs_open(output, fs_mode2flag("w+"));
  if (file_fd < FS_OPEN_OK)
  {
    return luaL_error(L, "cannot open/write to file");
  }

  lua_lock(L);
  int result = luaU_dump(L, f, writer, &file_fd, stripping);
  lua_unlock(L);

  if (fs_flush(file_fd) < 0) {   // result codes aren't propagated by flash_fs.h
    // overwrite Lua error, like writer() does in case of a file io error
    result = 1;
  }
  fs_close(file_fd);
  file_fd = FS_OPEN_OK - 1;

  if (result == LUA_ERR_CC_INTOVERFLOW) {
    return luaL_error(L, "value too big or small for target integer type");
  }
  if (result == LUA_ERR_CC_NOTINTEGER) {
    return luaL_error(L, "target lua_Number is integral but fractional value found");
  }
  if (result == 1) {    // result status generated by writer() or fs_flush() fail
    return luaL_error(L, "writing to file failed");
  }

  return 0;
}
Ejemplo n.º 4
0
static int luaB_dofile (lua_State *L) {
  const char *fname = luaL_optstring(L, 1, NULL);
  int n = lua_gettop(L);
#if 0
  if (luaL_loadfile(L, fname) != 0) lua_error(L);
#else
  if (luaL_loadfsfile(L, fname) != 0) lua_error(L);
#endif
  lua_call(L, 0, LUA_MULTRET);
  return lua_gettop(L) - n;
}
Ejemplo n.º 5
0
static int luaB_dofile (lua_State *L) {
  const char *fname = luaL_optstring(L, 1, NULL);
  lua_settop(L, 1);
  #if defined( LUA_USES_LOADF )
  if (luaL_loadfile(L, fname) != LUA_OK)
    return lua_error(L);
  #else
  if (luaL_loadfsfile(L, fname) != LUA_OK)
    return lua_error(L);
  #endif
  lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
  return dofilecont(L, 0, 0);
}
Ejemplo n.º 6
0
static int handle_script (lua_State *L, char **argv) {
  int status;
  const char *fname = argv[0];
  if (c_strcmp(fname, "-") == 0 && c_strcmp(argv[-1], "--") != 0)
    fname = NULL;  /* stdin */
#if defined( LUA_USES_LOADF )
  status = luaL_loadfile(L, fname);
#else
  status = luaL_loadfsfile(L, fname);
#endif
  if (status == LUA_OK) {
    int n = pushargs(L);  /* push arguments to script */
    status = docall(L, n, LUA_MULTRET);
  }
  return report(L, status);
}