diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2012-02-18 14:08:52 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2012-02-18 14:08:52 +0100 |
commit | 076344633e7b2525cf48005f84f3935f324b60bf (patch) | |
tree | 2a013da0ba2cd4a9f7eaf073a5ba128723f13154 /src/tools.c | |
parent | b4582dd0bb65a916de7fa9612880b75a17c7ae7f (diff) | |
download | lanes-076344633e7b2525cf48005f84f3935f324b60bf.tar.gz lanes-076344633e7b2525cf48005f84f3935f324b60bf.tar.bz2 lanes-076344633e7b2525cf48005f84f3935f324b60bf.zip |
* changed lanes.configure signature to receive a table instead of individual parameters
* added support for an on_state_create callback called to load custom functions in a state in addition to the base libraries
Diffstat (limited to 'src/tools.c')
-rw-r--r-- | src/tools.c | 81 |
1 files changed, 47 insertions, 34 deletions
diff --git a/src/tools.c b/src/tools.c index 85672e2..212cf52 100644 --- a/src/tools.c +++ b/src/tools.c | |||
@@ -91,7 +91,7 @@ void luaG_dump( lua_State* L ) { | |||
91 | } | 91 | } |
92 | 92 | ||
93 | 93 | ||
94 | /*---=== luaG_openlibs ===---*/ | 94 | /*---=== luaG_newstate ===---*/ |
95 | 95 | ||
96 | static const luaL_Reg libs[] = { | 96 | static const luaL_Reg libs[] = { |
97 | { LUA_LOADLIBNAME, luaopen_package }, | 97 | { LUA_LOADLIBNAME, luaopen_package }, |
@@ -371,7 +371,7 @@ static void populate_func_lookup_table_recur( lua_State *L, int _ctx_base, int _ | |||
371 | } | 371 | } |
372 | 372 | ||
373 | /* | 373 | /* |
374 | * create a "fully.qualified.name" <-> function equivalence dabase | 374 | * create a "fully.qualified.name" <-> function equivalence database |
375 | */ | 375 | */ |
376 | void populate_func_lookup_table( lua_State *L, int _i, char const *_name) | 376 | void populate_func_lookup_table( lua_State *L, int _i, char const *_name) |
377 | { | 377 | { |
@@ -422,39 +422,52 @@ void populate_func_lookup_table( lua_State *L, int _i, char const *_name) | |||
422 | */ | 422 | */ |
423 | #define is_name_char(c) (isalpha(c) || (c)=='*') | 423 | #define is_name_char(c) (isalpha(c) || (c)=='*') |
424 | 424 | ||
425 | const char *luaG_openlibs( lua_State *L, const char *libs) | 425 | lua_State* luaG_newstate( char const* libs, lua_CFunction _on_state_create) |
426 | { | 426 | { |
427 | const char *p; | 427 | char const* p; |
428 | unsigned len; | 428 | unsigned int len; |
429 | lua_State* const L = luaL_newstate(); | ||
429 | 430 | ||
430 | if (!libs) return NULL; // no libs, not even 'base' | 431 | // no libs, or special init func (not even 'base') |
431 | 432 | if (libs || _on_state_create) | |
432 | // 'lua.c' stops GC during initialization so perhaps its a good idea. :) | ||
433 | // | ||
434 | lua_gc( L, LUA_GCSTOP, 0); | ||
435 | |||
436 | // Anything causes 'base' to be taken in | ||
437 | // | ||
438 | STACK_GROW(L,2); | ||
439 | STACK_CHECK(L) | ||
440 | lua_pushcfunction( L, luaopen_base); | ||
441 | lua_call( L, 0, 1); | ||
442 | // after opening base, register the functions they exported in our name<->function database | ||
443 | populate_func_lookup_table( L, LUA_GLOBALSINDEX, NULL); | ||
444 | lua_pop( L, 1); | ||
445 | STACK_MID( L, 0); | ||
446 | for( p= libs; *p; p+=len ) | ||
447 | { | 433 | { |
448 | len=0; | 434 | // 'lua.c' stops GC during initialization so perhaps its a good idea. :) |
449 | while (*p && !is_name_char(*p)) p++; // bypass delimiters | 435 | // |
450 | while (is_name_char(p[len])) len++; // bypass name | 436 | lua_gc( L, LUA_GCSTOP, 0); |
451 | if (len && (!openlib( L, p, len ))) | ||
452 | break; | ||
453 | } | ||
454 | STACK_END(L,0) | ||
455 | lua_gc(L, LUA_GCRESTART, 0); | ||
456 | 437 | ||
457 | return *p ? p : NULL; | 438 | // Anything causes 'base' to be taken in |
439 | // | ||
440 | STACK_GROW( L, 2); | ||
441 | STACK_CHECK( L) | ||
442 | if( _on_state_create) | ||
443 | { | ||
444 | lua_pushcfunction( L, _on_state_create); | ||
445 | lua_call( L, 0, 0); | ||
446 | } | ||
447 | if( libs) | ||
448 | { | ||
449 | lua_pushcfunction( L, luaopen_base); | ||
450 | lua_call( L, 0, 0); | ||
451 | } | ||
452 | // after opening base, register the functions it exported in our name<->function database | ||
453 | populate_func_lookup_table( L, LUA_GLOBALSINDEX, NULL); | ||
454 | STACK_MID( L, 0); | ||
455 | if( libs) | ||
456 | { | ||
457 | for( p = libs; *p; p += len) | ||
458 | { | ||
459 | len=0; | ||
460 | while (*p && !is_name_char(*p)) p++; // bypass delimiters | ||
461 | while (is_name_char(p[len])) len++; // bypass name | ||
462 | if (len && (!openlib( L, p, len ))) | ||
463 | break; | ||
464 | } | ||
465 | serialize_require( L); | ||
466 | } | ||
467 | STACK_END(L,0) | ||
468 | lua_gc( L, LUA_GCRESTART, 0); | ||
469 | } | ||
470 | return L; | ||
458 | } | 471 | } |
459 | 472 | ||
460 | 473 | ||
@@ -1317,7 +1330,7 @@ static void inter_copy_func( lua_State *L2, uint_t L2_cache_i, lua_State *L, uin | |||
1317 | else | 1330 | else |
1318 | { | 1331 | { |
1319 | if( !inter_copy_one_( L2, L2_cache_i, L, lua_gettop(L), VT_NORMAL)) | 1332 | if( !inter_copy_one_( L2, L2_cache_i, L, lua_gettop(L), VT_NORMAL)) |
1320 | luaL_error( L, "Cannot copy upvalue type '%s'", luaG_typename( L, -1)); | 1333 | luaL_error( L, "Cannot copy upvalue type '%s'", luaL_typename( L, -1)); |
1321 | } | 1334 | } |
1322 | lua_pop( L, 1); | 1335 | lua_pop( L, 1); |
1323 | } | 1336 | } |
@@ -1475,7 +1488,7 @@ static bool_t inter_copy_one_( lua_State *L2, uint_t L2_cache_i, lua_State *L, u | |||
1475 | lua_rawset( L2, -3 ); // add to table (pops key & val) | 1488 | lua_rawset( L2, -3 ); // add to table (pops key & val) |
1476 | } else { | 1489 | } else { |
1477 | luaL_error( L, "Unable to copy over type '%s' (in %s)", | 1490 | luaL_error( L, "Unable to copy over type '%s' (in %s)", |
1478 | luaG_typename(L,val_i), | 1491 | luaL_typename(L,val_i), |
1479 | vt==VT_NORMAL ? "table":"metatable" ); | 1492 | vt==VT_NORMAL ? "table":"metatable" ); |
1480 | } | 1493 | } |
1481 | } | 1494 | } |
@@ -1647,7 +1660,7 @@ static int new_require( lua_State *L) | |||
1647 | { | 1660 | { |
1648 | int rc, i; | 1661 | int rc, i; |
1649 | int args = lua_gettop( L); | 1662 | int args = lua_gettop( L); |
1650 | //char const *modname = luaL_checkstring( L, 1); | 1663 | //char const* modname = luaL_checkstring( L, 1); |
1651 | 1664 | ||
1652 | STACK_GROW( L, args + 1); | 1665 | STACK_GROW( L, args + 1); |
1653 | STACK_CHECK( L) | 1666 | STACK_CHECK( L) |