| 416 | } |
| 417 | |
| 418 | void LuaScript::Create() |
| 419 | { |
| 420 | Destroy(); |
| 421 | |
| 422 | name = GetPrettyFilename().string(); |
| 423 | |
| 424 | // create lua environment |
| 425 | L = luaL_newstate(); |
| 426 | if (!L) { |
| 427 | description = "Could not initialize Lua state"; |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | bool loaded = false; |
| 432 | BOOST_SCOPE_EXIT_ALL(&) { if (!loaded) Destroy(); }; |
| 433 | LuaStackcheck stackcheck(L); |
| 434 | |
| 435 | // register standard libs |
| 436 | preload_modules(L); |
| 437 | stackcheck.check_stack(0); |
| 438 | |
| 439 | // dofile and loadfile are replaced with include |
| 440 | lua_pushnil(L); |
| 441 | lua_setglobal(L, "dofile"); |
| 442 | lua_pushnil(L); |
| 443 | lua_setglobal(L, "loadfile"); |
| 444 | push_value(L, exception_wrapper<LuaInclude>); |
| 445 | lua_setglobal(L, "include"); |
| 446 | |
| 447 | // Replace the default lua module loader with our unicode compatible |
| 448 | // one and set the module search path |
| 449 | if (!Install(L, include_path)) { |
| 450 | description = get_string_or_default(L, 1); |
| 451 | lua_pop(L, 1); |
| 452 | return; |
| 453 | } |
| 454 | stackcheck.check_stack(0); |
| 455 | |
| 456 | // prepare stuff in the registry |
| 457 | |
| 458 | // store the script's filename |
| 459 | push_value(L, GetFilename().stem()); |
| 460 | lua_setfield(L, LUA_REGISTRYINDEX, "filename"); |
| 461 | stackcheck.check_stack(0); |
| 462 | |
| 463 | // reference to the script object |
| 464 | push_value(L, this); |
| 465 | lua_setfield(L, LUA_REGISTRYINDEX, "aegisub"); |
| 466 | stackcheck.check_stack(0); |
| 467 | |
| 468 | // make "aegisub" table |
| 469 | lua_pushstring(L, "aegisub"); |
| 470 | lua_createtable(L, 0, 13); |
| 471 | |
| 472 | set_field<LuaCommand::LuaRegister>(L, "register_macro"); |
| 473 | set_field<LuaExportFilter::LuaRegister>(L, "register_filter"); |
| 474 | set_field<lua_text_textents>(L, "text_extents"); |
| 475 | set_field<frame_from_ms>(L, "frame_from_ms"); |
no test coverage detected