Example #1
0
struct lua_debugst* dbg_lua_load(bstring name)
{
    bstring path, modtype;
    struct lua_debugst* ds;
    int module;

    // Calculate full path to file.
    path = osutil_getmodulepath();
    bconchar(path, '/');
    bconcat(path, name);

    // Create the new lua preprocessor structure.
    ds = malloc(sizeof(struct lua_debugst));
    ds->state = lua_open();
    assert(ds->state != NULL);
    luaL_openlibs(ds->state);
    luaX_loadexpressionlib(ds->state);

    // Load globals.
    dcpu_lua_set_constants(ds->state);

    // Execute the code in the new Lua context.
    if (luaL_dofile(ds->state, path->data) != 0)
    {
        printd(LEVEL_ERROR, "lua error was %s.\n", lua_tostring(ds->state, -1));

        // Return NULL.
        lua_close(ds->state);
        free(ds);
        bdestroy(path);
        return NULL;
    }

    // Load tables.
    lua_getglobal(ds->state, "MODULE");
    module = lua_gettop(ds->state);

    // Ensure module table was provided.
    if (lua_isnoneornil(ds->state, module))
    {
        printd(LEVEL_ERROR, "failed to load debugger module from %s.\n", path->data);

        // Return NULL.
        lua_close(ds->state);
        free(ds);
        bdestroy(path);
        return NULL;
    }

    // Check to see whether the module is
    // a preprocessor module.
    lua_getfield(ds->state, module, "Type");
    modtype = bfromcstr(lua_tostring(ds->state, -1));
    if (!biseqcstrcaseless(modtype, "Debugger"))
    {
        // Return NULL.
        lua_pop(ds->state, 1);
        lua_close(ds->state);
        free(ds);
        bdestroy(modtype);
        bdestroy(path);
        return NULL;
    }
    lua_pop(ds->state, 1);
    bdestroy(modtype);

    // Create the handler tables.
    lua_newtable(ds->state);
    lua_setglobal(ds->state, HANDLER_TABLE_COMMANDS_NAME);
    lua_newtable(ds->state);
    lua_setglobal(ds->state, HANDLER_TABLE_HOOKS_NAME);
    lua_newtable(ds->state);
    lua_setglobal(ds->state, HANDLER_TABLE_SYMBOLS_NAME);

    // Set the global functions.
    lua_pushcfunction(ds->state, &dbg_lua_add_command);
    lua_setglobal(ds->state, "add_command");
    lua_pushcfunction(ds->state, &dbg_lua_add_hook);
    lua_setglobal(ds->state, "add_hook");
    lua_pushcfunction(ds->state, &dbg_lua_add_symbol_hook);
    lua_setglobal(ds->state, "add_symbol_hook");

    // Run the setup function.
    lua_getglobal(ds->state, "setup");
    if (lua_pcall(ds->state, 0, 0, 0) != 0)
    {
        printd(LEVEL_ERROR, "failed to run setup() in debugger module from %s.\n", path->data);
        printd(LEVEL_ERROR, "lua error was %s.\n", lua_tostring(ds->state, -1));
    }

    // Unset the global functions.
    lua_pushnil(ds->state);
    lua_setglobal(ds->state, "add_command");
    lua_pushnil(ds->state);
    lua_setglobal(ds->state, "add_hook");
    lua_pushnil(ds->state);
    lua_setglobal(ds->state, "add_symbol_hook");

    // Pop tables from stack.
    lua_pop(ds->state, 2);

    // Return new debugger module.
    return ds;
}
Example #2
0
struct lua_hardware* vm_hw_lua_load(vm_t* vm, bstring name)
{
	bstring path, modtype;
	struct lua_hardware* hw;
	int module, hwinfo;

	// Calculate full path to file.
	path = osutil_getarg0path();
#ifdef _WIN32
	bcatcstr(path, "modules\\");
#else
	bcatcstr(path, "/modules/");
#endif
	bconcat(path, name);

	// Create the new lua hardware structure.
	hw = malloc(sizeof(struct lua_hardware));
	hw->vm = vm;
	hw->state = lua_open();
	assert(hw->state != NULL);
	luaL_openlibs(hw->state);
	luaX_loadexpressionlib(hw->state);

	// Execute the code in the new Lua context.
	if (luaL_dofile(hw->state, path->data) != 0)
	{
		printf("lua error was %s.\n", lua_tostring(hw->state, -1));

		// Return NULL.
		lua_close(hw->state);
		free(hw);
		bdestroy(path);
		return NULL;
	}

	// Load tables.
	lua_getglobal(hw->state, "MODULE");
	module = lua_gettop(hw->state);
	lua_getglobal(hw->state, "HARDWARE");
	hwinfo = lua_gettop(hw->state);

	// Ensure both tables were provided.
	if (lua_isnoneornil(hw->state, module) || lua_isnoneornil(hw->state, hwinfo))
	{
		printf("failed to load hardware from %s.\n", path->data);

		// Return NULL.
		lua_close(hw->state);
		free(hw);
		bdestroy(path);
		return NULL;
	}

	// Check to see whether the module is
	// a hardware module.
	lua_getfield(hw->state, module, "Type");
	modtype = bfromcstr(lua_tostring(hw->state, -1));
	if (!biseqcstrcaseless(modtype, "Hardware"))
	{
		// Return NULL.
		lua_pop(hw->state, 1);
		lua_close(hw->state);
		free(hw);
		bdestroy(modtype);
		bdestroy(path);
		return NULL;
	}
	lua_pop(hw->state, 1);
	bdestroy(modtype);

	// Store information into the Lua
	// hardware structure.
	lua_getfield(hw->state, hwinfo, "ID");
	lua_getfield(hw->state, hwinfo, "Version");
	lua_getfield(hw->state, hwinfo, "Manufacturer");
	hw->device.id = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 2);
	hw->device.version = (uint16_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 1);
	hw->device.manufacturer = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state));
	lua_pop(hw->state, 3);

	printf("hardware loaded ID: %u, Version: %u, Manufacturer: %u.\n", hw->device.id, hw->device.version, hw->device.manufacturer);

	// Register the hardware.
	hw->device.handler = &vm_hw_lua_interrupt;
	hw->device.userdata = hw;
	vm_hw_register(vm, hw->device);

	// Register the hooks.
	hw->cycle_id = vm_hook_register(vm, &vm_hw_lua_cycle, HOOK_ON_POST_CYCLE, hw);
	hw->write_id = vm_hook_register(vm, &vm_hw_lua_write, HOOK_ON_WRITE, hw);

	// Pop tables from stack.
	lua_pop(hw->state, 2);

	// Return new hardware.
	return hw;
}
Example #3
0
struct lua_preproc* pp_lua_load(bstring name)
{
	bstring path, modtype;
	struct lua_preproc* pp;
	int module;

	// Calculate full path to file.
	path = osutil_getmodulepath();
	bconchar(path, '/');
	bconcat(path, name);

	// Create the new lua preprocessor structure.
	pp = malloc(sizeof(struct lua_preproc));
	pp->state = lua_open();
	assert(pp->state != NULL);
	luaL_openlibs(pp->state);
	luaX_loadexpressionlib(pp->state);

	// Execute the code in the new Lua context.
	if (luaL_dofile(pp->state, path->data) != 0)
	{
		printd(LEVEL_ERROR, "lua error was %s.\n", lua_tostring(pp->state, -1));

		// Return NULL.
		lua_close(pp->state);
		free(pp);
		bdestroy(path);
		return NULL;
	}

	// Load tables.
	lua_getglobal(pp->state, "MODULE");
	module = lua_gettop(pp->state);

	// Ensure module table was provided.
	if (lua_isnoneornil(pp->state, module))
	{
		printd(LEVEL_ERROR, "failed to load preprocessor module from %s.\n", path->data);

		// Return NULL.
		lua_close(pp->state);
		free(pp);
		bdestroy(path);
		return NULL;
	}

	// Check to see whether the module is
	// a preprocessor module.
	lua_getfield(pp->state, module, "Type");
	modtype = bfromcstr(lua_tostring(pp->state, -1));
	if (!biseqcstrcaseless(modtype, "Preprocessor"))
	{
		// Return NULL.
		lua_pop(pp->state, 1);
		lua_close(pp->state);
		free(pp);
		bdestroy(modtype);
		bdestroy(path);
		return NULL;
	}
	lua_pop(pp->state, 1);
	bdestroy(modtype);

	// Create the handler table.
	lua_newtable(pp->state);
	lua_setglobal(pp->state, HANDLER_TABLE_NAME);

	// Set the global add_preprocessor_directive function.
	lua_pushcfunction(pp->state, &pp_lua_add_preprocessor_directive);
	lua_setglobal(pp->state, "add_preprocessor_directive");

	// Run the setup function.
	lua_getglobal(pp->state, "setup");
	if (lua_pcall(pp->state, 0, 0, 0) != 0)
	{
		printd(LEVEL_ERROR, "failed to run setup() in preprocessor module from %s.\n", path->data);
	}

	// Unset the add_preprocessor_directive function.
	lua_pushnil(pp->state);
	lua_setglobal(pp->state, "add_preprocessor_directive");

	// Pop tables from stack.
	lua_pop(pp->state, 2);

	// Return new preprocessor module.
	return pp;
}