Changeset a2b666a8ab8d7131bde0c3d600c5f4b8d7fa2709
- Timestamp:
- 06/06/08 03:32:29
(5 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1212723149 +0000
- git-parent:
[fc9516a2d8a7585aaaffbaab77009199ca907c90]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1212723149 +0000
- Message:
make modules actually be lua modules
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| rfc9516a |
ra2b666a |
|
| 83 | 83 | } |
|---|
| 84 | 84 | |
|---|
| | 85 | static void |
|---|
| | 86 | noit_lua_pushmodule(lua_State *L, const char *m) { |
|---|
| | 87 | int stack_pos = LUA_GLOBALSINDEX; |
|---|
| | 88 | char *copy, *part, *brkt; |
|---|
| | 89 | copy = alloca(strlen(m)+1); |
|---|
| | 90 | assert(copy); |
|---|
| | 91 | memcpy(copy,m,strlen(m)+1); |
|---|
| | 92 | |
|---|
| | 93 | for(part = strtok_r(copy, ".", &brkt); |
|---|
| | 94 | part; |
|---|
| | 95 | part = strtok_r(NULL, ".", &brkt)) { |
|---|
| | 96 | lua_getfield(L, stack_pos, part); |
|---|
| | 97 | if(stack_pos == -1) lua_remove(L, -2); |
|---|
| | 98 | else stack_pos = -1; |
|---|
| | 99 | } |
|---|
| | 100 | } |
|---|
| 85 | 101 | static void |
|---|
| 86 | 102 | noit_lua_hash_to_table(lua_State *L, |
|---|
| … | … | |
| 314 | 330 | static int |
|---|
| 315 | 331 | noit_lua_module_onload(noit_image_t *i) { |
|---|
| | 332 | int rv; |
|---|
| 316 | 333 | lua_State *L; |
|---|
| 317 | 334 | lua_module_closure_t *lmc; |
|---|
| | 335 | |
|---|
| 318 | 336 | lmc = noit_image_get_userdata(i); |
|---|
| 319 | 337 | L = lmc->lua_state; |
|---|
| 320 | | if(luaL_dofile(L, lmc->script)) { |
|---|
| | 338 | lua_getglobal(L, "require"); |
|---|
| | 339 | lua_pushstring(L, lmc->object); |
|---|
| | 340 | rv = lua_pcall(L, 1, 1, 0); |
|---|
| | 341 | if(rv) { |
|---|
| 321 | 342 | int i; |
|---|
| 322 | 343 | noitL(nlerr, "lua: %s.onload failed\n", lmc->object); |
|---|
| … | … | |
| 330 | 351 | } |
|---|
| 331 | 352 | } |
|---|
| | 353 | lua_pop(L,i); |
|---|
| 332 | 354 | return -1; |
|---|
| 333 | 355 | } |
|---|
| 334 | | |
|---|
| 335 | | lua_getfield(L, LUA_GLOBALSINDEX, lmc->object); |
|---|
| | 356 | lua_pop(L, lua_gettop(L)); |
|---|
| | 357 | |
|---|
| | 358 | noit_lua_pushmodule(L, lmc->object); |
|---|
| 336 | 359 | if(lua_isnil(L, -1)) { |
|---|
| 337 | 360 | lua_pop(L, 1); |
|---|
| … | … | |
| 366 | 389 | L = lmc->lua_state |
|---|
| 367 | 390 | #define SETUP_CALL(L, func, failure) do { \ |
|---|
| 368 | | lua_getfield(L, LUA_GLOBALSINDEX, lmc->object); \ |
|---|
| | 391 | noit_lua_pushmodule(L, lmc->object); \ |
|---|
| 369 | 392 | lua_getfield(L, -1, func); \ |
|---|
| 370 | 393 | lua_remove(L, -2); \ |
|---|
| … | … | |
| 375 | 398 | } while(0) |
|---|
| 376 | 399 | #define RETURN_INT(L, func) do { \ |
|---|
| | 400 | int base = lua_gettop(L); \ |
|---|
| | 401 | assert(base == 1); \ |
|---|
| 377 | 402 | if(lua_isnumber(L, -1)) { \ |
|---|
| 378 | 403 | int rv; \ |
|---|
| … | … | |
| 463 | 488 | * to wake up the coro... |
|---|
| 464 | 489 | */ |
|---|
| 465 | | lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0); |
|---|
| 466 | 490 | goto done; |
|---|
| 467 | 491 | default: /* Errors */ |
|---|
| … | … | |
| 486 | 510 | lua_getglobal(ci->lmc->lua_state, "noit_coros"); |
|---|
| 487 | 511 | luaL_unref(ci->lmc->lua_state, -1, ci->coro_state_ref); |
|---|
| 488 | | lua_pop(ci->lmc->lua_state, 2); |
|---|
| | 512 | lua_pop(ci->lmc->lua_state, 1); |
|---|
| 489 | 513 | |
|---|
| 490 | 514 | done: |
|---|
| … | … | |
| 507 | 531 | lua_getglobal(ci->lmc->lua_state, "noit_coros"); |
|---|
| 508 | 532 | luaL_unref(ci->lmc->lua_state, -1, ci->coro_state_ref); |
|---|
| 509 | | lua_pop(ci->lmc->lua_state, 2); |
|---|
| | 533 | lua_pop(ci->lmc->lua_state, 1); |
|---|
| 510 | 534 | lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0); |
|---|
| 511 | 535 | assert(2 == noit_lua_resume(ci, 0)); |
|---|
| … | … | |
| 549 | 573 | eventer_add(e); |
|---|
| 550 | 574 | |
|---|
| | 575 | noitL(nlerr, "initiate gettop => %d\n", lua_gettop(L)); |
|---|
| 551 | 576 | ci->lmc = lmc; |
|---|
| 552 | 577 | lua_getglobal(L, "noit_coros"); |
|---|
| 553 | 578 | ci->coro_state = lua_newthread(L); |
|---|
| | 579 | ci->coro_state_ref = luaL_ref(L, -2); |
|---|
| | 580 | lua_pop(L, 1); /* pops noit_coros */ |
|---|
| 554 | 581 | noit_hash_store(&noit_coros, |
|---|
| 555 | 582 | (const char *)&ci->coro_state, sizeof(ci->coro_state), |
|---|
| 556 | 583 | ci); |
|---|
| 557 | | ci->coro_state_ref = luaL_ref(L, -2); |
|---|
| 558 | | lua_pop(L, 1); /* pops noit_coros */ |
|---|
| 559 | 584 | |
|---|
| 560 | 585 | SETUP_CALL(ci->coro_state, "initiate", goto fail); |
|---|
| … | … | |
| 586 | 611 | lua_State *L; |
|---|
| 587 | 612 | lua_module_closure_t *lmc; |
|---|
| 588 | | char *script; |
|---|
| 589 | 613 | char *object; |
|---|
| 590 | | char scriptfile[MAXPATHLEN]; |
|---|
| 591 | 614 | |
|---|
| 592 | 615 | noitL(nldeb, "Loading lua module: %s\n", module_name); |
|---|
| … | … | |
| 595 | 618 | return NULL; |
|---|
| 596 | 619 | } |
|---|
| 597 | | if(noit_conf_get_string(section, "@script", &script) == 0) { |
|---|
| 598 | | noitL(nlerr, "Lua module %s require script attribute.\n", module_name); |
|---|
| 599 | | free(object); |
|---|
| 600 | | return NULL; |
|---|
| 601 | | } |
|---|
| 602 | | snprintf(scriptfile, sizeof(scriptfile), "%s/%s", |
|---|
| 603 | | noit_lua_loader_get_directory(loader), script); |
|---|
| 604 | | free(script); |
|---|
| 605 | 620 | |
|---|
| 606 | 621 | m = noit_blank_module(); |
|---|
| … | … | |
| 612 | 627 | lmc = calloc(1, sizeof(*lmc)); |
|---|
| 613 | 628 | lmc->object = object; |
|---|
| 614 | | lmc->script = strdup(scriptfile); |
|---|
| 615 | 629 | |
|---|
| 616 | 630 | L = lmc->lua_state = lua_open(); |
|---|
| | 631 | |
|---|
| | 632 | |
|---|
| 617 | 633 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
|---|
| 618 | 634 | luaL_openlibs(L); /* open libraries */ |
|---|
| … | … | |
| 620 | 636 | lua_newtable(L); |
|---|
| 621 | 637 | lua_setglobal(L, "noit_coros"); |
|---|
| | 638 | |
|---|
| | 639 | lua_getfield(L, LUA_GLOBALSINDEX, "package"); |
|---|
| | 640 | lua_pushfstring(L, "%s", noit_lua_loader_get_directory(loader)); |
|---|
| | 641 | lua_setfield(L, -2, "path"); |
|---|
| | 642 | lua_pop(L, 1); |
|---|
| | 643 | |
|---|
| 622 | 644 | lua_gc(L, LUA_GCRESTART, 0); |
|---|
| 623 | 645 | |
|---|
| … | … | |
| 627 | 649 | free(m->hdr.description); |
|---|
| 628 | 650 | free(lmc->object); |
|---|
| 629 | | free(lmc->script); |
|---|
| 630 | 651 | free(lmc); |
|---|
| 631 | 652 | /* FIXME: We leak the opaque_handler in the module here... */ |
|---|
| rdb9f2be |
ra2b666a |
|
| 23 | 23 | typedef struct lua_module_closure { |
|---|
| 24 | 24 | char *object; |
|---|
| 25 | | char *script; |
|---|
| 26 | 25 | lua_State *lua_state; |
|---|
| | 26 | int object_ref; |
|---|
| 27 | 27 | } lua_module_closure_t; |
|---|
| 28 | 28 | |
|---|