예제 #1
0
파일: mini.c 프로젝트: chelmuth/lua
int main(void)
{
	lua_State *L = lua_open();

	lua_register(L,"print",print);

	if (luaL_dostring(L, exec_string) != 0)
		printf("%s\n", lua_tostring(L,-1));

	lua_close(L);
	return 0;
}
예제 #2
0
void
sys_exec_string(const char *str)
{
	int success;
	
	log_print(str);
	log_print("\n");
	success = luaL_dostring(lua_state, str);
	if (success) {
		log_print("lua> error executing string\n");
	}
}
예제 #3
0
int CLuaSVM::ExecuteString( const char* Str )
{
	if( mbShutScript )
		return 0;
	int status	= luaL_dostring( mLS,Str );
	if(status != 0)
	{
		ErrorInfoOut( NULL,"ExecuteString():%s, error: %s\n", Str, lua_tostring(mLS, -1));
		return 1;
	}
	return 1;
}
예제 #4
0
/**
 * \brief Call a lua function to get a single value.
 */
gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
{
	gint result;
	gchar *data = NULL;
	gchar *dir;
	gchar *path;
	FileData **image_data;
	gchar *tmp;
	GError *error = NULL;

	/* Collection Table (Dummy at the moment) */
	lua_newtable(L);
	lua_setglobal(L, "Collection");

	/* Current Image */
	image_data = (FileData **)lua_newuserdata(L, sizeof(FileData *));
	luaL_getmetatable(L, "Image");
	lua_setmetatable(L, -2);
	lua_setglobal(L, "Image");

	*image_data = fd;
	if (file[0] == '\0')
		{
		result = luaL_dostring(L, function);
		}
	else
		{
		dir = g_build_filename(get_rc_dir(), "lua", NULL);
		path = g_build_filename(dir, file, NULL);
		result = luaL_dofile(L, path);
		g_free(path);
		g_free(dir);
		}

	if (result)
		{
		data = g_strdup_printf("Error running lua script: %s", lua_tostring(L, -1));
		return data;
		}
	data = g_strdup(lua_tostring(L, -1));
	tmp = g_locale_to_utf8(data, strlen(data), NULL, NULL, &error);
	if (error)
		{
		log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
		g_error_free(error);
		}
	else
		{
		g_free(data);
		data = g_strdup(tmp);
		} // if (error) { ... } else
	return data;
}
예제 #5
0
static int lua_reboot(lua_State *L)
{
	int argc = lua_gettop(L);
	#ifndef SKIP_ERROR_HANDLING
		if (argc != 0) return luaL_error(L, "wrong number of arguments");
	#endif
	drawCommand("System.reboot: ", "Closing interpreter.\n");
	char string[20];
	strcpy(string,"lpp_exit_0456432");
	luaL_dostring(L, "collectgarbage()");
	return luaL_error(L, string); // NOTE: This is a fake error
}
예제 #6
0
bool LuaState::LoadLuaCode(string const &code, string &error)
{
    if (luaL_dostring(this->L, code.c_str())) {
        error = lua_tostring(this->L, -1);
        lua_pop(this->L, 1);
        return false;
    }

    return this->DetermineCapabilities();

    return true;
}
예제 #7
0
파일: script.c 프로젝트: khwangster/wrk2
lua_State *script_create(char *file, char *url, char **headers) {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    (void) luaL_dostring(L, "wrk = require \"wrk\"");

    luaL_newmetatable(L, "wrk.addr");
    luaL_register(L, NULL, addrlib);
    luaL_newmetatable(L, "wrk.stats");
    luaL_register(L, NULL, statslib);
    luaL_newmetatable(L, "wrk.thread");
    luaL_register(L, NULL, threadlib);

    struct http_parser_url parts = {};
    script_parse_url(url, &parts);
    char *path = "/";
    size_t len = 0;

    if (parts.field_set & (1 << UF_PATH)) {
        path = &url[parts.field_data[UF_PATH].off];
    }

    const table_field fields[] = {
        { "lookup",  LUA_TFUNCTION, script_wrk_lookup  },
        { "connect", LUA_TFUNCTION, script_wrk_connect },
        { "path",    LUA_TSTRING,   path               },
        { NULL,      0,             NULL               },
    };

    lua_getglobal(L, "wrk");

    set_string(L, 4, "scheme", get_url_part(url, &parts, UF_SCHEMA, &len), len);
    set_string(L, 4, "host",   get_url_part(url, &parts, UF_HOST,   &len), len);
    set_string(L, 4, "port",   get_url_part(url, &parts, UF_PORT,   &len), len);
    set_fields(L, 4, fields);

    lua_getfield(L, 4, "headers");
    for (char **h = headers; *h; h++) {
        char *p = strchr(*h, ':');
        if (p && p[1] == ' ') {
            lua_pushlstring(L, *h, p - *h);
            lua_pushstring(L, p + 2);
            lua_settable(L, 5);
        }
    }
    lua_pop(L, 5);

    if (file && luaL_dofile(L, file)) {
        const char *cause = lua_tostring(L, -1);
        fprintf(stderr, "%s: %s\n", file, cause);
    }

    return L;
}
예제 #8
0
LUA_EXTERNC QTUILOADER_API int
luaopen_libqtuiloader(lua_State *L)
{
  // load module 'qt'
  if (luaL_dostring(L, "require 'qt'"))
    lua_error(L);
  // enrichs class QUiLoader.
  luaQ_pushmeta(L, &QUiLoader::staticMetaObject);
  luaQ_getfield(L, -1, "__metatable");
  luaQ_register(L, qtuiloader_lib, QCoreApplication::instance());
  return 0;
}
예제 #9
0
// Call the given string directly, used in the lua debug command.
int call_lua(std::string tocall) {
    lua_State* L = lua_state;

    update_globals(L);
    int err = luaL_dostring(L, tocall.c_str());
    if(err) {
        // Error handling.
        const char* error = lua_tostring(L, -1);
        debugmsg("Error in lua command: %s", error);
    }
    return err;
}
예제 #10
0
파일: lua.c 프로젝트: Andersbakken/mongoose
static void prepare_lua_environment(struct mg_connection *conn, lua_State *L) {
  const struct mg_request_info *ri = &conn->request_info;
  extern void luaL_openlibs(lua_State *);
  int i;

  luaL_openlibs(L);
#ifdef USE_LUA_SQLITE3
  { extern int luaopen_lsqlite3(lua_State *); luaopen_lsqlite3(L); }
#endif

  luaL_newmetatable(L, LUASOCKET);
  lua_pushliteral(L, "__index");
  luaL_newlib(L, luasocket_methods);
  lua_rawset(L, -3);
  lua_pop(L, 1);
  lua_register(L, "connect", lsp_connect);

  if (conn == NULL) return;

  // Register mg module
  lua_newtable(L);

  reg_function(L, "read", lsp_read, conn);
  reg_function(L, "write", lsp_write, conn);
  reg_function(L, "cry", lsp_cry, conn);
  reg_function(L, "include", lsp_include, conn);
  reg_function(L, "redirect", lsp_redirect, conn);
  reg_string(L, "version", MONGOOSE_VERSION);

  // Export request_info
  lua_pushstring(L, "request_info");
  lua_newtable(L);
  reg_string(L, "request_method", ri->request_method);
  reg_string(L, "uri", ri->uri);
  reg_string(L, "http_version", ri->http_version);
  reg_string(L, "query_string", ri->query_string);
  reg_int(L, "remote_ip", ri->remote_ip);
  reg_int(L, "remote_port", ri->remote_port);
  reg_int(L, "num_headers", ri->num_headers);
  lua_pushstring(L, "http_headers");
  lua_newtable(L);
  for (i = 0; i < ri->num_headers; i++) {
    reg_string(L, ri->http_headers[i].name, ri->http_headers[i].value);
  }
  lua_rawset(L, -3);
  lua_rawset(L, -3);

  lua_setglobal(L, "mg");

  // Register default mg.onerror function
  luaL_dostring(L, "mg.onerror = function(e) mg.write('\\nLua error:\\n', "
                "debug.traceback(e, 1)) end");
}
예제 #11
0
OpticLua::OpticLua(std::string lua) {
	luaState = luaL_newstate();
    luaL_openlibs(luaState);

	if(luaL_dostring(luaState, lua.c_str()) == 1) {
		std::string error = lua_tostring(luaState, -1);
		lua_pop(luaState, 1);
		throw OpticLuaException(error);
	}

	registerFunctions();
}
예제 #12
0
/**
 * Evaluate a Lua script.
 * @param script String with Lua code.
 * @return Non-zero if successful, zero on error.
 */
int LuaEngine::eval(const char* script)
{
	lua_State* L = (lua_State*) mLuaState;

	// Create temporary script string with an ending space.
	// There seems to be a bug in Lua when evaluating
	// statements like:
	//   "return 10"
	//   "x = 10"
	// But this works:
	//   "return 10 "
	//   "return (10)"
	//   "x = 10 "
	int length = strlen(script);
	char* s = (char*) malloc(length + 2);
	strcpy(s, script);
	s[length] = ' ';
	s[length + 1] = 0;

	// Evaluate Lua script.
	int result = luaL_dostring(L, s);

	// Free temporary script string.
	free(s);

	// Was there an error?
	if (0 != result)
	{
		MAUtil::String errorMessage;

    	if (lua_isstring(L, -1))
    	{
    		errorMessage = lua_tostring(L, -1);

            // Pop the error message.
        	lua_pop(L, 1);
    	}
    	else
    	{
    		errorMessage =
    			"There was a Lua error condition, but no error message.";
    	}

        lprintfln("Lua Error: %s\n", errorMessage.c_str());

    	// Print size of Lua stack (debug info).
    	lprintfln("Lua stack size: %i\n", lua_gettop(L));

    	reportLuaError(errorMessage.c_str());
	}

	return result == 0;
}
예제 #13
0
static void check_lua_expr(lua_State *L, const char *expr, const char *value) {
  const char *v, *var_name = "myVar";
  char buf[100];

  snprintf(buf, sizeof(buf), "%s = %s", var_name, expr);
  luaL_dostring(L, buf);
  lua_getglobal(L, var_name);
  v = lua_tostring(L, -1);
  printf("%s: %s: [%s] [%s]\n", __func__, expr, v == NULL ? "null" : v, value);
  ASSERT((value == NULL && v == NULL) ||
         (value != NULL && v != NULL && !strcmp(value, v)));
}
예제 #14
0
static void scriptClickCB(fltk::Widget* w, void *d)
{
    Script *s= (Script*)d;

    char buf[64];
    SNPRINTF(buf, sizeof(buf), "%s(%d)", s->funcName.c_str(), SCRIPT_EXEC);
    if (luaL_dostring(editorUI->luaState, buf) != 0)
    {
        const char *err = lua_tostring(editorUI->luaState, -1);
        fltk::message("Error while executing %s: %s",s->name.c_str(), err);
    }
}
예제 #15
0
void  luajit_demo()  
{  
    lua_State *lua = luaL_newstate();  
    assert(lua);  
    luaL_openlibs(lua);  
  
    const int status = luaL_dostring(lua, lua_code);  
    if(status)  
        printf("Couldn't execute LUA code: %s\n", lua_tostring(lua, -1));  
  
    lua_close(lua);  
}  
예제 #16
0
파일: ezshare.c 프로젝트: laheadle/ezshare
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
  lua_State* L = luaL_newstate();
  luaL_openlibs(L);
  luaL_dostring(L, "require \"ezshare\"");
  lua_getfield(L, LUA_GLOBALSINDEX, "run");
  lua_pushlightuserdata (L, hInstance);
  lua_pushlstring(L, lpCmdLine, strlen(lpCmdLine));
  lua_pushinteger(L, nCmdShow);
  lua_call(L, 3, 1); 
  return lua_tointeger(L, -1);
}
예제 #17
0
int CallHelloWorld()
{
	lua_State *L = lua_open();
	luaL_openlibs(L);

	const char *buf = "print('Hello World')";
	luaL_dostring(L,buf);

	lua_close(L);

	return 0;
}
예제 #18
0
void luaCommand(redisClient *c) {
    //printf("LUA: %s\n", c->argv[1]->ptr);
    LuaFlag   = PIPE_NONE_FLAG;
    LuaClient = c;             /* used in func redisLua */
    int s     = luaL_dostring(Lua, c->argv[1]->ptr);
    if (s) {
        const char *x = lua_tostring(Lua, -1);
        lua_pop(Lua, 1);
        addReplySds(c, sdscatprintf(sdsempty(), "-ERR Lua error: %s \r\n", x));
        return;
    }

    int lret = lua_gettop(Lua);
    //printf("LuaFlag: %d lret: %d\n", LuaFlag, lret);
    if (lua_istable(Lua, -1)) {
        const int len = lua_objlen(Lua, -1 );
        addReplySds(c, sdscatprintf(sdsempty(), "*%d\r\n", len));
        for (int i = 1; i <= len; ++i ) {
            lua_pushinteger(Lua, i);
            lua_gettable(Lua, -2);
            char *x = (char *)lua_tostring(Lua, -1);
            robj *r = _createStringObject(x);
            addReplyBulk(c, r);
            decrRefCount(r);
            lua_pop(Lua, 1);
        }
        lua_pop(Lua, 1);
    } else if (LuaFlag == PIPE_EMPTY_SET_FLAG) {
        addReply(c, shared.emptymultibulk);
        lua_pop(Lua, 1); /* pop because Pipe adds "-1" for Multi-NonRelIndxs */
    } else if (!lret) {
        addReply(c, shared.nullbulk);
    } else {
        char *x = (char *)lua_tostring(Lua, -1);
        if (!x) {
            addReply(c, shared.nullbulk);
        } else { 
            /* NOTE: if "client() is called in a lua func and the lua func
                     then returns "+OK" it will 'correctly' returned */
            if (LuaFlag == PIPE_ONE_LINER_FLAG &&
                (*x == '-' || *x == '+' || *x == ':')) {
                addReplySds(c, sdscatprintf(sdsempty(), "%s\r\n", x));
            } else {
                robj *r = _createStringObject(x);
                addReplyBulk(c, r);
                decrRefCount(r);
            }
        }
        lua_pop(Lua, 1);
    }
    lua_gc(Lua, LUA_GCCOLLECT, 0);
}
예제 #19
0
extern "C" int luaopen_audio_soundfile(lua_State * L) {

	// ensure array already is loaded:
	luaL_dostring(L, "require'Array'");
	lua_pop(L, 1);

	struct luaL_reg lib[] = {
		{ "read", lua_soundfile_read },
		{ NULL, NULL },
	};
	luaL_register(L, "audio.soundfile", lib);
	return 1;
}
예제 #20
0
int
execute_lua_string( lua_State * L, char * buffer )
{
    if ( (buffer == NULL) || (L == NULL) )
        return -1;

	if ( luaL_dostring(L, buffer) != 0 ) {
		fprintf(stderr,"%s\n", lua_tostring(L,-1));
		return -1;
	}

	return 0;
}
int main(int argc, char **argv ) { 
	_INIT_LIB_LUA((char * )0 ); 
	_INIT_LIB_LUALIB((char * )0 ); 
	_INIT_LIB_LAUXLIB((char * )0 ); 
	
	lua_State *L= lua_open(); int i= 0; luaL_openlibs(L ); 
	i= luaL_dostring(L, "io.stdout:setvbuf'no'" ); if(i ); 
	luaopen_shareMT(L ); 
	TEST_01(L ); 
	
	lua_close(L_S_ ); lua_close(L ); 
	return 0; 
} 
예제 #22
0
	inline void dostring(lua_State * L,const char * message)
	{
		error_info errorCode;

		if(luaL_dostring(L,message))
		{
			LEMON_USER_ERROR(errorCode,LEMONXX_LUA_DOFILE_ERROR);

			errorCode.error_msg(lemon::from_utf8(lua_tostring(L,-1)));

			errorCode.check_throw();
		}
	}
예제 #23
0
InclusionRule::InclusionRule( const std::string& script, const std::string& functionName )
    : L(lua_open()), m_functionName( functionName )
{
    luaL_openlibs(L);

    if ( luaL_dostring( L, script.c_str() ) )
    {
        std::string exceptionMsg = lua_tostring( L, -1 );
        lua_close(L);
        L = 0;
        throw Exception( UnicodeString( exceptionMsg.c_str() ) + UnicodeString(L"  Script: ") + UnicodeString(script.c_str()) );
    }
}
예제 #24
0
double LuaConfig::returnAsNumber(const std::string &cmd)
{
    if (luaL_dostring(L, cmd.c_str()))
    {
        std::cerr << "Error running Lua code '" << cmd << "'" << std::endl;
        if (lua_isstring(L, -1))
            std::cerr << lua_tostring(L, -1) << std::endl;
        exit(1);
    }
    if (lua_isnumber(L, -1))
        return lua_tonumber(L, -1);
    return 0.0;
}
예제 #25
0
void BaseParser::ReadUseRestrictionsSetting() {
    if( 0 != luaL_dostring( lua_state, "return use_turn_restrictions\n") ) {
        throw OSRMException("ERROR occured in scripting block");
    }
    if( lua_isboolean( lua_state, -1) ) {
        use_turn_restrictions = lua_toboolean(lua_state, -1);
    }
    if( use_turn_restrictions ) {
        SimpleLogger().Write() << "Using turn restrictions";
    } else {
        SimpleLogger().Write() << "Ignoring turn restrictions";
    }
}
예제 #26
0
int CCLuaEngine::executeString(const char *codes)
{
	int nRet =	luaL_dostring(m_state, codes);
	lua_gc(m_state, LUA_GCCOLLECT, 0);

    if (nRet != 0)
    {
        CCLOG("[LUA ERROR] %s", lua_tostring(m_state, -1));
        lua_pop(m_state, 1);
        return nRet;
    }
    return 0;
}
예제 #27
0
bool CLuaFn::GetLuaGlobal_ArrayCount(const char* pGlobalName, int& nCount)
{
	char szCommand[MAX_LUA_BUFF_200] = {'\0'};
#if WIN32	
	sprintf_s(szCommand, MAX_LUA_BUFF_200, "return #%s;", pGlobalName);
#else	
	sprintf(szCommand, "return #%s;", pGlobalName);
#endif	
	luaL_dostring(m_pState, szCommand);
	nCount = (int)lua_tonumber(m_pState, -1);
	lua_pop(m_pState, -1);
	return true;
}
예제 #28
0
bool CLuaFn::GetLuaGlobal_ArrayIndex(const char* pGlobalName, int nIndex, string& strGlobal)
{
	char szCommand[MAX_LUA_BUFF_200] = {'\0'};
#if WIN32		
	sprintf_s(szCommand, MAX_LUA_BUFF_200, "return %s[%d];", pGlobalName, nIndex);
#else	
	sprintf(szCommand, "return %s[%d];", pGlobalName, nIndex);
#endif	
	luaL_dostring(m_pState, szCommand);
	strGlobal = (string)lua_tostring(m_pState, -1);
	lua_pop(m_pState, -1);
	return true;
}
예제 #29
0
파일: interp.c 프로젝트: sebcat/birk
int interp_eval(interp_t *interp, const char *code) {
	assert(interp != NULL);
	assert(interp->L != NULL);
	assert(code != NULL);

	interp->err[0] = '\0';
	if (luaL_dostring(interp->L, code) != LUA_OK) {
		BIRK_LOADERR(interp);
		return BIRK_ERROR;
	}

	return BIRK_OK;
}
예제 #30
0
bool Lua::ExecuteCommandSilently(const char *command)
{
	if (!command || !command[0])
		return false;

	if ( luaL_dostring(mState, command) )
	{
		printf("-- Lua error %s\n", lua_tostring(mState, -1) );
		return false;
	}

	return true;
}