Example #1
0
extern "C" int loader_XLua (lua_State *L) {
	const char *filename;
	const char *name = luaL_checkstring(L, 1);
	filename = x_findfile(L, name, "path");
	if (filename == NULL) return 1;  /* library not found in this path */

	//LuaInternalFile lif;
	DWORD dwsz;
	LPRDATA rdPtr = XLuaGlobal::Get().GetStateByState(L)->rdList.front()->rdPtr;
	HANDLE hFile = rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvOpenHFile(filename, &dwsz, 0);

	if (hFile == INVALID_HANDLE_VALUE) {
		loaderror(L, filename);
		return 0;
	}

	//LuaInternalFile lif;
	char* text = (char*)calloc(dwsz+1, sizeof(char));

	DWORD dwread;
	ReadFile(hFile, text, dwsz, &dwread, NULL);

	if (dwsz != dwread || luaL_loadbuffer(L, text, dwsz, name) != 0) {
		free(text);
		rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvCloseHFile(hFile);
		loaderror(L, filename);
		return 0;
	}

	free(text);
	rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvCloseHFile(hFile);

	luaJIT_setmode(L, -1, LUAJIT_MODE_FUNC|LUAJIT_MODE_OFF);
	return 1;
}
Example #2
0
static int setjitmode(lua_State *L, int mode)
{
  int idx = 0;
  if (L->base == L->top || tvisnil(L->base)) {  /* jit.on/off/flush([nil]) */
    mode |= LUAJIT_MODE_ENGINE;
  } else {
    /* jit.on/off/flush(func|proto, nil|true|false) */
    if (tvisfunc(L->base) || tvisproto(L->base))
      idx = 1;
    else if (!tvistrue(L->base))  /* jit.on/off/flush(true, nil|true|false) */
      goto err;
    if (L->base+1 < L->top && tvisbool(L->base+1))
      mode |= boolV(L->base+1) ? LUAJIT_MODE_ALLFUNC : LUAJIT_MODE_ALLSUBFUNC;
    else
      mode |= LUAJIT_MODE_FUNC;
  }
  if (luaJIT_setmode(L, idx, mode) != 1) {
  err:
#if LJ_HASJIT
    lj_err_arg(L, 1, LJ_ERR_NOLFUNC);
#else
    lj_err_caller(L, LJ_ERR_NOJIT);
#endif
  }
  return 0;
}
Example #3
0
ScriptApiBase::ScriptApiBase()
{
	#ifdef SCRIPTAPI_LOCK_DEBUG
	m_locked = false;
	#endif

	m_luastack = luaL_newstate();
	assert(m_luastack);

	// Add and save an error handler
	lua_pushcfunction(m_luastack, script_error_handler);
	m_errorhandler = lua_gettop(m_luastack);

	// Make the ScriptApiBase* accessible to ModApiBase
	lua_pushlightuserdata(m_luastack, this);
	lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");

	// If we are using LuaJIT add a C++ wrapper function to catch
	// exceptions thrown in Lua -> C++ calls
#if USE_LUAJIT
	lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
	luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
	lua_pop(m_luastack, 1);
#endif

	m_server = 0;
	m_environment = 0;
	m_guiengine = 0;
}
Example #4
0
/*
** Open JIT library
*/
LUALIB_API int luaopen_jit(lua_State *L)
{
  /* Add the core JIT library. */
  luaL_register(L, LUA_JITLIBNAME, jitlib);
  lua_pushliteral(L, LUAJIT_VERSION);
  lua_setfield(L, -2, "version");
  setintfield("version_num", LUAJIT_VERSION_NUM);
  lua_pushstring(L, luaJIT_arch);
  lua_setfield(L, -2, "arch");
  makepipeline(L);

  /* Add the utility JIT library. */
  luaL_register(L, LUA_JITLIBNAME ".util", jitutillib);
  makestatus(L, "status");
  makehints(L, hints_H, JIT_H_MAX, "hints");
  makehints(L, hints_FH, JIT_FH_MAX, "fhints");
  lua_pop(L, 1);

  /* Everything ok, so turn the JIT engine on. Vroooom! */
  if (luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON) <= 0) {
    /* Ouch. Someone screwed up DynASM or the JSUBs. Probably me. */
    /* But if you get 999999999, look at jit_consistency_check(). */
    return luaL_error(L, "JIT engine init failed (%d)",
	G(L)->jit_state->dasmstatus);
  }

  return 1;
}
Example #5
0
void CScriptEngine::init()
{
#ifdef USE_LUA_STUDIO
    bool lua_studio_connected = !!m_lua_studio_world;
    if (lua_studio_connected)
        m_lua_studio_world->remove		(lua());
#endif // #ifdef USE_LUA_STUDIO

    CScriptStorage::reinit();

#ifdef USE_LUA_STUDIO
    if (m_lua_studio_world || strstr(Core.Params, "-lua_studio")) {
        if (!lua_studio_connected)
            try_connect_to_debugger		();
        else {
#ifdef USE_LUAJIT_ONE
            jit_command					(lua(), "debug=2");
            jit_command					(lua(), "off");
#else
            luaJIT_setmode(lua(), 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);
#endif
            m_lua_studio_world->add		(lua());
        }
    }
#endif // #ifdef USE_LUA_STUDIO

    luabind::open(lua());
    setup_callbacks();
    export_classes(lua());
    setup_auto_load();

#ifdef DEBUG
    m_stack_is_ready					= true;
#endif

#ifndef USE_LUA_STUDIO
#	ifdef DEBUG
#		if defined(USE_DEBUGGER) && !defined(USE_LUA_STUDIO)
    if( !debugger() || !debugger()->Active()  )
#		endif // #if defined(USE_DEBUGGER) && !defined(USE_LUA_STUDIO)
        lua_sethook					(lua(),lua_hook_call,	LUA_MASKLINE|LUA_MASKCALL|LUA_MASKRET,	0);
#	endif // #ifdef DEBUG
#endif // #ifndef USE_LUA_STUDIO
    //	lua_sethook							(lua(), lua_hook_call,	LUA_MASKLINE|LUA_MASKCALL|LUA_MASKRET,	0);

    bool								save = m_reload_modules;
    m_reload_modules = true;
    process_file_if_exists("_G", false);
    m_reload_modules = save;

    register_script_classes();
    object_factory().register_script();

#ifdef XRGAME_EXPORTS
    load_common_scripts();
#endif
    m_stack_level = lua_gettop(lua());
}
 void lualambda_master::start(size_t nworkers) {
   clients.resize(nworkers);
   for (size_t i = 0;i < clients.size(); ++i) {
     auto luastate = new lua::State(true);
     clients[i].reset(luastate);
     luaJIT_setmode(luastate->getState().get(), 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON);
     worker_queue.push(i);
   }
 }
Example #7
0
static int loader_Lua (lua_State *L) {
  const char *filename;
  const char *name = luaL_checkstring(L, 1);
  filename = findfile(L, name, "path");
  if (filename == NULL) return 1;  /* library not found in this path */
  if (luaL_loadfile(L, filename) != 0)
    loaderror(L, filename);
  /* not useful to JIT compile main chunk of a module */
  luaJIT_setmode(L, -1, LUAJIT_MODE_FUNC|LUAJIT_MODE_OFF);
  return 1;  /* library loaded successfully */
}
Example #8
0
void initWrappedExceptions(lua_State* L) {
  Dl_info thisLib;
  int r = dladdr(reinterpret_cast<void*>(&wrapExceptions), &thisLib);
  CHECK(r != 0 && thisLib.dli_fname);

  void* leakedHandle = dlopen(thisLib.dli_fname, RTLD_NODELETE | RTLD_NOW);
  CHECK(leakedHandle);

  lua_pushlightuserdata(L, reinterpret_cast<void*>(&wrapExceptions));
  luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
  lua_pop(L, 1);
}
Example #9
0
/* Set JIT mode for the engine or a closure and/or its subroutines. */
static int setmode(lua_State *L, int mode)
{
  int idx = 0;
  switch (lua_type(L, 1)) {
  case LUA_TNONE:	/* jit.on/off() */
  case LUA_TNIL:	/* jit.on/off(nil) */
    luaJIT_setmode(L, 0, mode | LUAJIT_MODE_ENGINE);
    break;
  case LUA_TFUNCTION:	/* jit.on/off(func, nil|true|false) */
    idx = 1;
  case LUA_TBOOLEAN:	/* jit.on/off(true, nil|true|false) (parent frame) */
    if (lua_isboolean(L, 2))
      mode |= lua_toboolean(L, 2)?LUAJIT_MODE_ALLFUNC:LUAJIT_MODE_ALLSUBFUNC;
    else
      mode |= LUAJIT_MODE_FUNC;
    if (luaJIT_setmode(L, idx, mode) == 1)  /* Ok? */
      break;
  default:
    luaL_argerror(L, 1, "Lua function expected");
    break;
  }
  return 0;
}
Example #10
0
ScriptApiBase::ScriptApiBase()
{
	#ifdef SCRIPTAPI_LOCK_DEBUG
	m_locked = false;
	#endif

	m_luastack = luaL_newstate();
	FATAL_ERROR_IF(!m_luastack, "luaL_newstate() failed");

	luaL_openlibs(m_luastack);

	// Add and save an error handler
	lua_pushcfunction(m_luastack, script_error_handler);
	m_errorhandler = lua_gettop(m_luastack);

	// Make the ScriptApiBase* accessible to ModApiBase
	lua_pushlightuserdata(m_luastack, this);
	lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");

	// If we are using LuaJIT add a C++ wrapper function to catch
	// exceptions thrown in Lua -> C++ calls
#if USE_LUAJIT
	lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
	luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
	lua_pop(m_luastack, 1);
#endif

	// Add basic globals
	lua_newtable(m_luastack);
	lua_setglobal(m_luastack, "core");

	lua_pushstring(m_luastack, DIR_DELIM);
	lua_setglobal(m_luastack, "DIR_DELIM");

	lua_pushstring(m_luastack, porting::getPlatformName());
	lua_setglobal(m_luastack, "PLATFORM");

	// m_secure gets set to true inside
	// ScriptApiSecurity::initializeSecurity(), if neccessary.
	// Default to false otherwise
	m_secure = false;

	m_server = NULL;
	m_environment = NULL;
	m_guiengine = NULL;
}
Example #11
0
static void initialize_lua_studio	( lua_State* state, cs::lua_studio::world*& world, lua_studio_engine*& engine)
{
    engine							= 0;
    world							= 0;

    u32 const old_error_mode		= SetErrorMode(SEM_FAILCRITICALERRORS);
    s_script_debugger_handle		= LoadLibrary(CS_LUA_STUDIO_BACKEND_FILE_NAME);
    SetErrorMode					(old_error_mode);
    if (!s_script_debugger_handle) {
        Msg							("! cannot load %s dynamic library", CS_LUA_STUDIO_BACKEND_FILE_NAME);
        return;
    }

    R_ASSERT2						(s_script_debugger_handle, "can't load script debugger library");

    s_create_world					= (create_world_function_type)
        GetProcAddress(
        s_script_debugger_handle,
        "_cs_lua_studio_backend_create_world@12"
        );
    R_ASSERT2						(s_create_world, "can't find function \"cs_lua_studio_backend_create_world\"");

    s_destroy_world					= (destroy_world_function_type)
        GetProcAddress(
        s_script_debugger_handle,
        "_cs_lua_studio_backend_destroy_world@4"
        );
    R_ASSERT2						(s_destroy_world, "can't find function \"cs_lua_studio_backend_destroy_world\" in the library");

    engine							= xr_new<lua_studio_engine>();
    world							= s_create_world( *engine, false, false );
    VERIFY							(world);

    s_old_log_callback				= SetLogCB(&log_callback);

#ifdef USE_LUAJIT_ONE
    jit_command						(state, "debug=2");
    jit_command						(state, "off");
#else
    luaJIT_setmode(lua(), 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);
#endif

    world->add						(state);
}
Example #12
0
    bool Environment::run(const char *file) {
#ifdef EXCEPTIONS_ENABLED
        lua_pushlightuserdata(L, (void *) wrap_exceptions);
        luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
        pop(1);
#endif

        pushTrace();
        int error = luaL_loadfile(L, findfile(file, "r")) || lua_pcall(L, 0, 0, 1);
        remove(1);

        if (error) {

            const char *errmsg = to(-1, wrapType<const char *>());

            conoutf(CON_ERROR, "Could not execute file (%s):\n%s", file, errmsg);
            pop(1);

            return false;
        }

        return true;
    }
Example #13
0
void lutro_init()
{
   L = luaL_newstate();
   luaL_openlibs(L);

#ifdef HAVE_JIT
   luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
#endif

   lutro_checked_stack_begin();

   init_settings(L);

   lutro_preload(L, lutro_core_preload, "lutro");
   lutro_preload(L, lutro_graphics_preload, "lutro.graphics");
   lutro_preload(L, lutro_audio_preload, "lutro.audio");
   lutro_preload(L, lutro_input_preload, "lutro.input");
   lutro_preload(L, lutro_filesystem_preload, "lutro.filesystem");
   lutro_preload(L, lutro_timer_preload, "lutro.timer");
#ifdef HAVE_INOTIFY
   lutro_preload(L, lutro_live_preload, "lutro.live");
#endif

   // if any of these requires fail, the checked stack assertion at the end will
   // be triggered. remember that assertions are only avaialable in debug mode.
   lutro_require(L, "lutro", 1);
   lutro_require(L, "lutro.graphics", 1);
   lutro_require(L, "lutro.audio", 1);
   lutro_require(L, "lutro.input", 1);
   lutro_require(L, "lutro.filesystem", 1);
   lutro_require(L, "lutro.timer", 1);
#ifdef HAVE_INOTIFY
   lutro_require(L, "lutro.live", 1);
#endif

   lutro_checked_stack_assert(0);
}
Example #14
0
TEST(cmsgpack, checkLua) {
    int rc;

    lua_State *L = lua_open();
    luaL_openlibs(L);

    rc = luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
    EXPECT_EQ(1, rc);

    std::string buf = "x = 10";
    rc = luaL_loadbuffer(L, buf.c_str(), buf.length(), "test");
    ASSERT_EQ(0, rc);

    rc = lua_pcall(L, 0, 0, 0);
    LUA_ASSERT_PCALL(L, rc);

    lua_getglobal(L, "x");
    auto num = lua_tonumber(L, -1);
    lua_pop(L, 1);

    EXPECT_DOUBLE_EQ(10, num);

    lua_close(L);
}
Example #15
0
/* Set JIT debug level. Defaults to maximum level for use with -j. */
static int j_debug(lua_State *L)
{
  luaJIT_setmode(L, luaL_optinteger(L, 1, 100), LUAJIT_MODE_DEBUG);
  return 0;
}
Example #16
0
void LSLuaState::open()
{
    assert(!L);

    L = lua_newstate(lsLuaAlloc, NULL);
    //L = lua_open();
    toLuaState.insert(L, this);

    luaopen_base(L);
    luaopen_table(L);
    luaopen_string(L);
    luaopen_math(L);
    luaL_openlibs(L);

    // TODO: turn this back on when it doesn't fail on the testWhile unit test
    // update luajit and test again
    luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);

    // open the lua debug library
    luaopen_debug(L);

    // open socket library
    luaopen_socket_core(L);

    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXCLASSES);

    lua_newtable(L);
    lua_setglobal(L, "__ls_nativeclasses");

    lua_pushcfunction(L, traceback);
    lua_setglobal(L, "__ls_traceback");
    _tracemessage[0] = 0;

    // entry -> version
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDVERSION);

    // entry -> native user data
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDUSERDATA);

    // native user data -> script instance
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDNATIVESCRIPT);

    // native delegate table
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXNATIVEDELEGATES);

    // interned field name lookup
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMEMBERINFONAME);

    // typeid -> type*
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSASSEMBLYLOOKUP);

    // lua/luacfunction -> MethodBase* lookups
    lua_newtable(L);

    // weak key metatable
    lua_newtable(L);
    lua_pushstring(L, "k");
    lua_setfield(L, -2, "__mode");
    lua_setmetatable(L, -2);

    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMETHODLOOKUP);

    lsr_instanceregister(L);

    NativeInterface::registerNativeTypes(L);
}
Example #17
0
static int runlove(int argc, char **argv)
{
#ifdef LOVE_LEGENDARY_APP_ARGV_HACK
	int hack_argc = 0;
	char **hack_argv = 0;
	get_app_arguments(argc, argv, hack_argc, hack_argv);
	argc = hack_argc;
	argv = hack_argv;
#endif // LOVE_LEGENDARY_APP_ARGV_HACK

	// Oh, you just want the version? Okay!
	if (argc > 1 && strcmp(argv[1], "--version") == 0)
	{
		printf("LOVE %s (%s)\n", love_version(), love_codename());
		return 0;
	}

	// Create the virtual machine.
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

#ifdef LOVE_ANDROID
	luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE| LUAJIT_MODE_OFF);
	lua_register(L, "print", l_print_sdl_log);
#endif

	// Add love to package.preload for easy requiring.
	love_preload(L, luaopen_love, "love");

	// Add command line arguments to global arg (like stand-alone Lua).
	{
		lua_newtable(L);

		if (argc > 0)
		{
			lua_pushstring(L, argv[0]);
			lua_rawseti(L, -2, -2);
		}

		lua_pushstring(L, "embedded boot.lua");
		lua_rawseti(L, -2, -1);

		for (int i = 1; i < argc; i++)
		{
			lua_pushstring(L, argv[i]);
			lua_rawseti(L, -2, i);
		}

		lua_setglobal(L, "arg");
	}

	// require "love"
	lua_getglobal(L, "require");
	lua_pushstring(L, "love");
	lua_call(L, 1, 1); // leave the returned table on the stack.

	// Add love._exe = true.
	// This indicates that we're running the standalone version of love, and not
	// the library version.
	{
		lua_pushboolean(L, 1);
		lua_setfield(L, -2, "_exe");
	}

	// Pop the love table returned by require "love".
	lua_pop(L, 1);

	// require "love.boot" (preloaded when love was required.)
	lua_getglobal(L, "require");
	lua_pushstring(L, "love.boot");
	lua_call(L, 1, 1);

	// Call the returned boot function.
	lua_call(L, 0, 1);

	int retval = 0;
	if (lua_isnumber(L, -1))
		retval = (int) lua_tonumber(L, -1);

	lua_close(L);

#if defined(LOVE_LEGENDARY_APP_ARGV_HACK) && !defined(LOVE_IOS)
	if (hack_argv)
	{
		for (int i = 0; i<hack_argc; ++i)
			delete [] hack_argv[i];
		delete [] hack_argv;
	}
#endif // LOVE_LEGENDARY_APP_ARGV_HACK

	return retval;
}
Example #18
0
int main(int argc, char **argv)
{
	int retval = 0;

#ifdef LOVE_IOS
	int orig_argc = argc;
	char **orig_argv = argv;

	// on iOS we should never programmatically exit the app, so we'll just
	// "restart" when that is attempted. Games which use threads might cause
	// some issues if the threads aren't cleaned up properly...
	while (true)
	{
		argc = orig_argc;
		argv = orig_argv;
#endif

#ifdef LOVE_LEGENDARY_APP_ARGV_HACK
	int hack_argc = 0;
	char **hack_argv = 0;
	get_app_arguments(argc, argv, hack_argc, hack_argv);
	argc = hack_argc;
	argv = hack_argv;
#endif // LOVE_LEGENDARY_APP_ARGV_HACK

	if (strcmp(LOVE_VERSION_STRING, love_version()) != 0)
	{
		printf("Version mismatch detected!\nLOVE binary is version %s\n"
				"LOVE library is version %s\n", LOVE_VERSION_STRING, love_version());
		return 1;
	}

	// Oh, you just want the version? Okay!
	if (argc > 1 && strcmp(argv[1], "--version") == 0)
	{
		printf("LOVE %s (%s)\n", love_version(), love_codename());
		return 0;
	}

	// Create the virtual machine.
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

#ifdef LOVE_ANDROID
	luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE| LUAJIT_MODE_OFF);
	lua_register(L, "print", l_print_sdl_log);
#endif

	// Add love to package.preload for easy requiring.
	love_preload(L, luaopen_love, "love");

	// Add command line arguments to global arg (like stand-alone Lua).
	{
		lua_newtable(L);

		if (argc > 0)
		{
			lua_pushstring(L, argv[0]);
			lua_rawseti(L, -2, -2);
		}

		lua_pushstring(L, "embedded boot.lua");
		lua_rawseti(L, -2, -1);

		for (int i = 1; i < argc; i++)
		{
			lua_pushstring(L, argv[i]);
			lua_rawseti(L, -2, i);
		}

		lua_setglobal(L, "arg");
	}

	// require "love"
	lua_getglobal(L, "require");
	lua_pushstring(L, "love");
	lua_call(L, 1, 1); // leave the returned table on the stack.

	// Add love._exe = true.
	// This indicates that we're running the standalone version of love, and not
	// the library version.
	{
		lua_pushboolean(L, 1);
		lua_setfield(L, -2, "_exe");
	}

	// Pop the love table returned by require "love".
	lua_pop(L, 1);

	// require "love.boot" (preloaded when love was required.)
	lua_getglobal(L, "require");
	lua_pushstring(L, "love.boot");
	lua_call(L, 1, 1);

	// Call the returned boot function.
	lua_call(L, 0, 1);

	if (lua_isnumber(L, -1))
		retval = (int) lua_tonumber(L, -1);

	lua_close(L);

#if defined(LOVE_LEGENDARY_APP_ARGV_HACK)
	if (hack_argv)
	{
		for (int i = 0; i<hack_argc; ++i)
			delete [] hack_argv[i];
		delete [] hack_argv;
	}
#endif // LOVE_LEGENDARY_APP_ARGV_HACK

#ifdef LOVE_IOS
	} // while (true)
#endif

#ifdef LOVE_ANDROID
	SDL_Quit();
#endif

	return retval;
}
Example #19
0
int colony_runtime_open ()
{
  lua_State* L = tm_lua_state = luaL_newstate ();
  if (L == NULL) {
    tm_logf(SYS_ERR, "Error creating Lua state.\n");
    return 255;
  }
  lua_atpanic(L, &runtime_panic);

  // lua_gc(L, LUA_GCSETPAUSE, 90);
  // lua_gc(L, LUA_GCSETSTEPMUL, 200);

  // Open libraries.
  luaL_openlibs(L);

#if COLONY_JIT
  // Enable JIT.
  luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON);
#endif

  // Type of build.
#ifdef COLONY_EMBED
  lua_pushboolean(L, 1);
#else
  lua_pushboolean(L, 0);
#endif
  lua_setglobal(L, "COLONY_EMBED");

#ifndef COLONY_EMBED
#ifdef COLONY_COMPILER_PATH
  lua_pushliteral(L, colony_runtime_xstr(COLONY_COMPILER_PATH));
  lua_setglobal(L, "COLONY_COMPILER_PATH");
#endif
#endif

  // Preload Lua modules.
  lua_getglobal(L, "package");
  lua_getfield(L, -1, "preload");
  lua_remove(L, -2);
  // tm
  lua_pushcfunction(L, luaopen_tm);
  lua_setfield(L, -2, "tm");
  // http_parser
  lua_pushcfunction(L, luaopen_http_parser);
  lua_setfield(L, -2, "http_parser_lua");
  // hsregex
  lua_pushcfunction(L, luaopen_hsregex);
  lua_setfield(L, -2, "hsregex");
  // rapidjson
  lua_pushcfunction(L, lua_open_rapidjson);
  lua_setfield(L, -2, "rapidjson");
  // Load lib/*.lua files into memory.
  for (int i = 0; dir_runtime_lib[i].path != NULL; i++) {
    lua_pushlstring(L, dir_runtime_lib[i].path, strchr(dir_runtime_lib[i].path, '.') - dir_runtime_lib[i].path);
    int res = luaL_loadbuffer(L, (const char *) dir_runtime_lib[i].src, dir_runtime_lib[i].len, dir_runtime_lib[i].path);
    if (res != 0) {
      printf("Error in runtime lib %s: %d\n", dir_runtime_lib[i].path, res);
      report(L, res);
      exit(1);
    }
    lua_settable(L, -3);
  }
  // Done with preload
  lua_pop(L, 1);

  // Given the index of a builtin file to load, this function loads it.
  lua_pushcfunction(L, builtin_loader);
  lua_setglobal(L, "_builtin_load");

  // Adds builtin files to an array _builtin.
  lua_newtable(L);
  for (int i = 0; dir_builtin[i].path != NULL; i++) {
    lua_pushlstring(L, dir_builtin[i].path, strchr(dir_builtin[i].path, '.') - dir_builtin[i].path);
    lua_pushnumber(L, i);
    lua_settable(L, -3);
  }
  lua_setglobal(L, "_builtin");

#if !COLONY_JIT
  // Load bit module on Lua VM.
  lua_pushcfunction(L, luaopen_bit);
  lua_call(L, 0, 1);
  lua_setglobal(L, "bit");
#endif

  // Initialize runtime semantics.
  colony_init(L);

  return tm_eval_lua(L, "require('preload');");
}