// 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; }
// Lua: flush() static int file_flush( lua_State* L ) { if((FS_OPEN_OK - 1)==file_fd) return luaL_error(L, "open a file first"); if(fs_flush(file_fd) == 0) lua_pushboolean(L, 1); else lua_pushnil(L); return 1; }
static int f_flush (lua_State *L) { return pushresult(L, fs_flush(tofile(L)) == 0, NULL); }
static int io_flush (lua_State *L) { return pushresult(L, fs_flush(getiofile(L, IO_OUTPUT)) == 0, NULL); }