1399 } |
1399 } |
1400 lua_pop (L, 1); |
1400 lua_pop (L, 1); |
1401 } |
1401 } |
1402 |
1402 |
1403 return ret; |
1403 return ret; |
|
1404 } |
|
1405 |
|
1406 /// main.hook_run |
|
1407 /// Runs handlers for given hook with supplied arguments. |
|
1408 /// A: string (hook name), table (hook arguments) |
|
1409 /// R: return enum field (hook handler result) |
|
1410 static int lua_main_hook_run ( lua_State *L ) |
|
1411 { |
|
1412 const char * hook_name = luaL_checkstring ( L, 1 ); |
|
1413 int num = 1; |
|
1414 hk_arg_t * args; |
|
1415 guint ret; |
|
1416 lua_settop ( L, 2 ); // 1 name, 2 args_in |
|
1417 luaL_argcheck ( L, lua_type ( L, 2 ) == LUA_TTABLE, 2, "table with hook arguments expected" ); |
|
1418 // count fields, build temporary stringified table |
|
1419 lua_newtable ( L ); // 3 args |
|
1420 lua_pushnil ( L ); // 4 key |
|
1421 while ( lua_next ( L, 2 ) != 0 ) { // 5 value |
|
1422 int ktype = lua_type ( L, 4 ); |
|
1423 if ( ktype == LUA_TSTRING || ktype == LUA_TNUMBER ) { |
|
1424 int vtype = lua_type ( L, 5 ); |
|
1425 if ( vtype == LUA_TSTRING || vtype == LUA_TNUMBER ) { |
|
1426 lua_pushvalue ( L, 4 ); // 6 key copy |
|
1427 lua_pushvalue ( L, 5 ); // 7 value copy |
|
1428 lua_tostring ( L, 6 ); |
|
1429 lua_tostring ( L, 7 ); |
|
1430 lua_settable ( L, 3 ); // 5 value |
|
1431 num ++; |
|
1432 } |
|
1433 } |
|
1434 lua_pop ( L, 1 ); // 4 key |
|
1435 } |
|
1436 // alloc args, populate from temporary table |
|
1437 args = luaL_malloc ( L, num * sizeof ( hk_arg_t ) ); |
|
1438 num = 0; |
|
1439 lua_pushnil ( L ); // 4 key |
|
1440 while ( lua_next ( L, 3 ) != 0 ) { // 5 value |
|
1441 ( args + num ) -> name = lua_tostring ( L, 4 ); |
|
1442 ( args + num ) -> value = lua_tostring ( L, 5 ); |
|
1443 num ++; |
|
1444 lua_pop ( L, 1 ); // 4 key |
|
1445 } |
|
1446 ( args + num ) -> name = NULL; |
|
1447 ( args + num ) -> value = NULL; |
|
1448 // run hook |
|
1449 ret = hk_run_handlers ( hook_name, args ); |
|
1450 luaL_free ( L, args ); |
|
1451 luaL_pushenum ( L, ret, lua_hook_handler_result ); |
|
1452 return 1; |
1404 } |
1453 } |
1405 |
1454 |
1406 /// main.hook |
1455 /// main.hook |
1407 /// Installs hook handler, returns an object, that you need to keep until |
1456 /// Installs hook handler, returns an object, that you need to keep until |
1408 /// hook handling is no more needed. |
1457 /// hook handling is no more needed. |