@@ -1040,13 +1040,62 @@ save(const char *key, const void * proto) {
1040
1040
return result ;
1041
1041
}
1042
1042
1043
+ #define CACHE_OFF 0
1044
+ #define CACHE_EXIST 1
1045
+ #define CACHE_ON 2
1046
+
1047
+ static int cache_key = 0 ;
1048
+
1049
+ static int cache_level (lua_State * L ) {
1050
+ int t = lua_rawgetp (L , LUA_REGISTRYINDEX , & cache_key );
1051
+ int r = lua_tointeger (L , -1 );
1052
+ lua_pop (L ,1 );
1053
+ if (t == LUA_TNUMBER ) {
1054
+ return r ;
1055
+ }
1056
+ return CACHE_ON ;
1057
+ }
1058
+
1059
+ static int cache_mode (lua_State * L ) {
1060
+ static const char * lst [] = {
1061
+ "OFF" ,
1062
+ "EXIST" ,
1063
+ "ON" ,
1064
+ NULL ,
1065
+ };
1066
+ if (lua_isnoneornil (L ,1 )) {
1067
+ int t = lua_rawgetp (L , LUA_REGISTRYINDEX , & cache_key );
1068
+ int r = lua_tointeger (L , -1 );
1069
+ if (t == LUA_TNUMBER ) {
1070
+ if (r < 0 || r >= CACHE_ON ) {
1071
+ r = CACHE_ON ;
1072
+ }
1073
+ } else {
1074
+ r = CACHE_ON ;
1075
+ }
1076
+ lua_pushstring (L , lst [r ]);
1077
+ return 1 ;
1078
+ }
1079
+ int t = luaL_checkoption (L , 1 , "OFF" , lst );
1080
+ lua_pushinteger (L , t );
1081
+ lua_rawsetp (L , LUA_REGISTRYINDEX , & cache_key );
1082
+ return 0 ;
1083
+ }
1084
+
1043
1085
LUALIB_API int luaL_loadfilex (lua_State * L , const char * filename ,
1044
1086
const char * mode ) {
1087
+ int level = cache_level (L );
1088
+ if (level == CACHE_OFF ) {
1089
+ return luaL_loadfilex_ (L , filename , mode );
1090
+ }
1045
1091
const void * proto = load (filename );
1046
1092
if (proto ) {
1047
1093
lua_clonefunction (L , proto );
1048
1094
return LUA_OK ;
1049
1095
}
1096
+ if (level == CACHE_EXIST ) {
1097
+ return luaL_loadfilex_ (L , filename , mode );
1098
+ }
1050
1099
lua_State * eL = luaL_newstate ();
1051
1100
if (eL == NULL ) {
1052
1101
lua_pushliteral (L , "New state failed" );
@@ -1083,6 +1132,7 @@ cache_clear(lua_State *L) {
1083
1132
LUAMOD_API int luaopen_cache (lua_State * L ) {
1084
1133
luaL_Reg l [] = {
1085
1134
{ "clear" , cache_clear },
1135
+ { "mode" , cache_mode },
1086
1136
{ NULL , NULL },
1087
1137
};
1088
1138
luaL_newlib (L ,l );
0 commit comments