diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2011-02-18 20:33:43 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2011-02-18 20:33:43 +0100 |
commit | 1760eafa1d2ebce8f07e11414a53d4a251af5b8e (patch) | |
tree | 18f2fcd400bb35528212930c4e61be04be31bea0 /src/tools.c | |
parent | ab233d0c9a1edc34836e2249c1eb6d714f1066b5 (diff) | |
download | lanes-1760eafa1d2ebce8f07e11414a53d4a251af5b8e.tar.gz lanes-1760eafa1d2ebce8f07e11414a53d4a251af5b8e.tar.bz2 lanes-1760eafa1d2ebce8f07e11414a53d4a251af5b8e.zip |
* keeper.lua is now embedded in text form instead of bytecode to improve LuaJIT2-compatibility (but this is not enough).
* moved keeper-related code in a separate source file
Diffstat (limited to 'src/tools.c')
-rw-r--r-- | src/tools.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/tools.c b/src/tools.c index 29959a8..d475fc0 100644 --- a/src/tools.c +++ b/src/tools.c | |||
@@ -1278,3 +1278,72 @@ int luaG_inter_move( lua_State* L, lua_State *L2, uint_t n ) | |||
1278 | lua_pop( L, (int) n); | 1278 | lua_pop( L, (int) n); |
1279 | return ret; | 1279 | return ret; |
1280 | } | 1280 | } |
1281 | |||
1282 | /*---=== Serialize require ===--- | ||
1283 | */ | ||
1284 | |||
1285 | MUTEX_T require_cs; | ||
1286 | |||
1287 | //--- | ||
1288 | // [val]= new_require( ... ) | ||
1289 | // | ||
1290 | // Call 'old_require' but only one lane at a time. | ||
1291 | // | ||
1292 | // Upvalues: [1]: original 'require' function | ||
1293 | // | ||
1294 | static int new_require( lua_State *L ) | ||
1295 | { | ||
1296 | int rc; | ||
1297 | int args= lua_gettop(L); | ||
1298 | |||
1299 | STACK_GROW(L,1); | ||
1300 | STACK_CHECK(L) | ||
1301 | |||
1302 | // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would | ||
1303 | // leave us locked, blocking any future 'require' calls from other lanes. | ||
1304 | // | ||
1305 | MUTEX_LOCK( &require_cs); | ||
1306 | { | ||
1307 | lua_pushvalue( L, lua_upvalueindex(1) ); | ||
1308 | lua_insert( L, 1 ); | ||
1309 | |||
1310 | rc= lua_pcall( L, args, 1 /*retvals*/, 0 /*errfunc*/ ); | ||
1311 | // | ||
1312 | // LUA_ERRRUN / LUA_ERRMEM | ||
1313 | } | ||
1314 | MUTEX_UNLOCK( &require_cs); | ||
1315 | |||
1316 | if (rc) | ||
1317 | lua_error(L); // error message already at [-1] | ||
1318 | |||
1319 | STACK_END(L,0) | ||
1320 | return 1; | ||
1321 | } | ||
1322 | |||
1323 | /* | ||
1324 | * Serialize calls to 'require', if it exists | ||
1325 | */ | ||
1326 | void serialize_require( lua_State *L ) | ||
1327 | { | ||
1328 | STACK_GROW(L,1); | ||
1329 | STACK_CHECK(L) | ||
1330 | |||
1331 | // Check 'require' is there; if not, do nothing | ||
1332 | // | ||
1333 | lua_getglobal( L, "require" ); | ||
1334 | if (lua_isfunction( L, -1 )) | ||
1335 | { | ||
1336 | // [-1]: original 'require' function | ||
1337 | |||
1338 | lua_pushcclosure( L, new_require, 1 /*upvalues*/ ); | ||
1339 | lua_setglobal( L, "require" ); | ||
1340 | |||
1341 | } | ||
1342 | else | ||
1343 | { | ||
1344 | // [-1]: nil | ||
1345 | lua_pop(L,1); | ||
1346 | } | ||
1347 | |||
1348 | STACK_END(L,0) | ||
1349 | } | ||