예제 #1
1
bool transferFunction(lua_State* toL, lua_State* fromL)
{
    bool result = false;
    if (!lua_iscfunction(fromL, -1) && lua_isfunction(fromL, -1))
    {
        try
        {
            TransferBuffer tb;
            if (lua_dump(fromL, functionWriter, &tb) == 0)
            {
                // sigh, was hoping to do this without any ifdefs... 
                // alas, i don't see how this one can be avoided
                #if LUA_VERSION_NUM > 501
                if (lua_load(toL, functionReader, &tb, "transferFunction", NULL) == 0)
                    result = true;
                #else
                if (lua_load(toL, functionReader, &tb, "transferFunction") == 0)
                    result = true;
                #endif
            }
        }
        catch (const std::exception& e)
        {
            (void) e;
            // catch a std::bad_alloc
            result = false;
        }
    }
    
    return result;
}
예제 #2
0
파일: lua.cpp 프로젝트: codyroux/lean
int load(lua_State * L, lua_Reader reader, void * data, char const * source) {
    #if LUA_VERSION_NUM < 502
    return lua_load(L, reader, data, source);
    #else
    return lua_load(L, reader, data, source, nullptr);
    #endif
}
예제 #3
0
static int luahtml_load(lua_State *L)
{
	struct luahtml_readdata rd;
	const char *outfunc = luaL_checkstring(L, 2);
	const char *chunkname = luaL_checkstring(L, 3);
	int res;

	if (lua_isuserdata(L, 1))
	{
		rd.utf8.file = luahtml_tofile(L);
		rd.utf8.readchar = luahtml_readfile;
	}
	else
	{
		rd.utf8.src = (unsigned char *) lua_tolstring(L, 1, &rd.utf8.srclen);
		rd.utf8.readchar = luahtml_readstring;
	}

	rd.utf8.accu = 0;
	rd.utf8.numa = 0;
	rd.utf8.bufc = -1;
	rd.code_present = 0;

	rd.state = PARSER_UNDEF;
	strcpy((char *) rd.buf0, " ");
	strcat((char *) rd.buf0, outfunc);
	strcat((char *) rd.buf0, "(");
	rd.buf = rd.buf0 + strlen((char *) rd.buf0);

#if LUA_VERSION_NUM < 502
	res = lua_load(L, luahtml_readparsed, &rd, chunkname);
#else
	res = lua_load(L, luahtml_readparsed, &rd, chunkname, NULL);
#endif
	if (res == 0)
	{
#if LUA_VERSION_NUM >= 502
		lua_pushvalue(L, 4);
		lua_setupvalue(L, -2, 1);
#endif
		lua_pushboolean(L, rd.code_present);
		return 2;
	}

	lua_pushnil(L);
	lua_insert(L, -2);
	/* nil, message on stack */
	return 2;
}
예제 #4
0
파일: lzip.c 프로젝트: skykapok/elpack
static int
lload(lua_State *L) {
	struct reader rd;
	rd.source = luaL_checklstring(L,1,&rd.source_sz);
	const char * filename = luaL_checkstring(L, 2);
	struct filter flt = { &rd.stream, inflateEnd, NULL, 0, 0 };
	struct rc4_sbox sbox;
	if (lua_isstring(L, 3)) {
		size_t keysz;
		const char * key = lua_tolstring(L, 3, &keysz);
		if (keysz != 8) {
			return luaL_error(L, "Only support 8 bytes key");
		}
		rc4_init(&sbox, (const uint8_t *)key);
		rd.crypt_sbox = &sbox;
	} else {
		rd.crypt_sbox = NULL;
	}
	rd.flags = Z_NO_FLUSH;
	z_stream *stream = &rd.stream;
	stream->zalloc = Z_NULL;
	stream->zfree = Z_NULL;
	stream->opaque = Z_NULL;
	stream->avail_in = 0;
	stream->avail_out = 0;
	int result = inflateInit(stream);
	lz_assert(L, result, &flt);
	result = lua_load (L, zip_reader, &rd, filename, "bt");
	inflateEnd(stream);
	if (result != LUA_OK) {
		return lua_error(L);
	}
	return 1;
}
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  LoadF lf;
  int status, readstatus;
  int c;
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
  if (filename == NULL) {
    lua_pushliteral(L, "=stdin");
    lf.f = stdin;
  }
  else {
    lua_pushfstring(L, "@%s", filename);
    lf.f = fopen(filename, "r");
  }
  if (lf.f == NULL) return errfile(L, fnameindex);  /* unable to open file */
  c = ungetc(getc(lf.f), lf.f);
  if (!(isspace(c) || isprint(c)) && lf.f != stdin) {  /* binary file? */
    fclose(lf.f);
    lf.f = fopen(filename, "rb");  /* reopen in binary mode */
    if (lf.f == NULL) return errfile(L, fnameindex); /* unable to reopen file */
  }
  status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  readstatus = ferror(lf.f);
  if (lf.f != stdin) fclose(lf.f);  /* close file (even in case of errors) */
  if (readstatus) {
    lua_settop(L, fnameindex);  /* ignore results from `lua_load' */
    return errfile(L, fnameindex);
  }
  lua_remove(L, fnameindex);
  return status;
}
예제 #6
0
파일: g_lua.c 프로젝트: Geptun/japp
int JPLua_LoadFile( lua_State *L, const char *file )
{// Loads a file using JA's FS functions, only use THIS to load files into lua!
	fileHandle_t	f		= 0;
	int				len		= trap->FS_Open( file, &f, FS_READ );
	gfd_t			gfd;
	int				status;
	
	if ( !f || len <= 0 )
	{// File doesn't exist
		Com_Printf( "JPLua_LoadFile: Failed to load %s, file doesn't exist\n", file );
		return 1;
	}
	gfd.f = f;
	gfd.dataRemaining = len;
	
	

	status = (lua_load(L, JPLua_LoadFile_Reader, &gfd, va("@%s", file)) || lua_pcall(L,0,0,0));
	if ( status )
	{// Error occoured
		Com_Printf( "JPLua_LoadFile: Failed to load %s: %s\n", file, lua_tostring( L, -1 ) );
		lua_pop( L, 1 );
	}
	
	trap->FS_Close( f );
	return status;
}
예제 #7
0
int GLua_LoadFile(lua_State *L, const char* file) {	// Loads a file using JA's FS functions, only use THIS to load files into lua!
	int len = 0;
	fileHandle_t f = 0;
	gfd_t gfd;
	int status;
	
	len = trap_FS_FOpenFile(file, &f, FS_READ);
	if (!f || len < 0) {
		// File doesn't exist
		G_Printf("GLua_LoadFile: Failed to load %s, file doesn't exist\n",file);
		return 1;
	} else if ( len == 0 ) {
		// File is empty
		G_Printf("GLua_LoadFile: Failed to load %s, file is empty\n",file);
		return 1;
	}
	gfd.f = f;
	gfd.dataRemaining = len;
	
	

	status = (lua_load(L, GLua_LoadFile_Reader, &gfd, va("@%s", file)) || lua_pcall(L,0,0,0));
	if (status) {
		// Error occoured
		G_Printf("GLua_LoadFile: Failed to load %s: %s\n",file, lua_tostring(L,-1));
		lua_pop(L,1);
	}
	
	trap_FS_FCloseFile(f);
	return status;
}
예제 #8
0
static int nobj_try_loading_ffi(lua_State *L, const char *ffi_mod_name,
		const char *ffi_init_code[], const ffi_export_symbol *ffi_exports, int priv_table)
{
	nobj_reader_state state = { ffi_init_code, 0 };
	int err;

	/* export symbols to priv_table. */
	while(ffi_exports->name != NULL) {
		lua_pushstring(L, ffi_exports->name);
		lua_pushlightuserdata(L, ffi_exports->sym.data);
		lua_settable(L, priv_table);
		ffi_exports++;
	}
	err = lua_load(L, nobj_lua_Reader, &state, ffi_mod_name);
	if(0 == err) {
		lua_pushvalue(L, -2); /* dup C module's table. */
		lua_pushvalue(L, priv_table); /* move priv_table to top of stack. */
		lua_remove(L, priv_table);
		lua_pushvalue(L, LUA_REGISTRYINDEX);
		err = lua_pcall(L, 3, 0, 0);
	}
	if(err) {
		const char *msg = "<err not a string>";
		if(lua_isstring(L, -1)) {
			msg = lua_tostring(L, -1);
		}
		printf("Failed to install FFI-based bindings: %s\n", msg);
		lua_pop(L, 1); /* pop error message. */
	}
	return err;
}
예제 #9
0
파일: ScriptLua.cpp 프로젝트: berak/e6
		// load & run
		virtual bool exec( const char *code, const char *marker ) 
		{
		    struct Handler 
		    {
		        const char *s;
		        size_t      size;
		  
		        Handler( const char *s, size_t z) : s(s),size(z) {}           
		      
		        static const char *get (lua_State *, void *ud, size_t *siz) 
				{
		            Handler *x = (Handler *)ud;
		            if (x->size == 0) return NULL;
		            *siz = x->size;
		            x->size = 0;
		            return x->s;
		        }
		    } xx( code, strlen(code) );

		    int res = lua_load(_l, Handler::get, &xx, marker);
			if ( res ) 
			{
				l_err->printLog( lua_tostring(_l, -1) );
				l_err->printLog( "\r\n" );
				lua_pop(_l, 1);
			    return 0;
			}
			return call(0);
		}
예제 #10
0
static void luaLoad ( lua_State* L, const std::string& path )
{
	std::string fullpath = "Scripts/" + path + ".lua";
#ifdef NDEBUG
	if (ResourceManager::FileExists("Scripts/" + path + ".lo"))
	{
		fullpath = "Scripts/" + path + ".lo";
	}
	else
	{
		CompileScript(path);
	}
#endif
	SDL_RWops* rwops = ResourceManager::OpenFile(fullpath);
    if (rwops)
    {
        int rc = lua_load(L, luaReader, (void*)rwops, path.c_str());
		SDL_RWclose(rwops);
		if (rc != 0)
		{
			luaHandleError(L);
			return;
		}
        rc = lua_pcall(L, 0, 0, 0);
        if (rc != 0)
        {
            luaHandleError(L);
			return;
        }
    }
    else
    {
        LOG("Scripting::Driver", LOG_WARNING, "Unable to load script: %s", path.c_str());
    }
}
예제 #11
0
static int run_file(lua_State* L, const char *filename)
{
    FILE* input = g_fopen(filename,"r");
    if (input==NULL)
    {
        log_printf(LOG_ERROR,"Could not load %s (%s)",filename,strerror(errno));
        return 0;
    }
    luax_card_reset_values(L);
    line_num = 0;
    lua_pushcfunction(L,print_debug_message);
    if (lua_load(L,read_chunk,input,filename,NULL)!=0)
    {
        log_printf(LOG_ERROR,"Syntax error on line %i in %s",line_num,filename);
        return 0;
    }
    fclose(input);
    if (lua_pcall(L, 0, LUA_MULTRET, -2)!=0)
    {
        lua_pop(L,1);
        return 0;
    }
    lua_pop(L,1);
    return 1;
}
예제 #12
0
int template_L_parse(lua_State *L)
{
	const char *file = luaL_checkstring(L, 1);
	struct template_parser parser;
	int lua_status;

	if( (parser.fd = open(file, O_RDONLY)) > 0 )
	{
		parser.flags   = 0;
		parser.bufsize = 0;
		parser.state   = T_STATE_TEXT_NEXT;

		lua_status = lua_load(L, template_reader, &parser, file);

		(void) close(parser.fd);


		if( lua_status == 0 )
		{
			return 1;
		}
		else
		{
			lua_pushnil(L);
			lua_pushinteger(L, lua_status);
			lua_pushlstring(L, parser.out, parser.outsize);
			return 3;
		}
	}

	lua_pushnil(L);
	lua_pushinteger(L, 255);
	lua_pushstring(L, "No such file or directory");
	return 3;
}
예제 #13
0
LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
								 const char *name, const char *mode) {
  LoadS ls;
  ls.s = buff;
  ls.size = size;
  return lua_load(L, getS, &ls, name, mode);
}
예제 #14
0
int luaL_load_lsp_file(lua_State* L,const char* filename)
{
    llsplib::lsp_State ctx;
    
    if(ctx.open(filename))
    {
	lua_pushfstring(L,"cannot open %s: %s",filename,strerror(errno));
	return LUA_ERRERR;
    }

    int status=lua_load(L,llsplib::lsp_reader,&ctx,filename);

    int line=ctx.line;
    
    ctx.close();

    if(status)
    {
	char tmp[512];

	int n=snprintf(tmp,sizeof(tmp),"%s:%i: %s",filename,line,lua_tostring(L,-1));

	if(n<0 || n>=sizeof(tmp))
	    n=sizeof(tmp)-1;

        lua_pop(L,1);
	
	lua_pushlstring(L,tmp,n);
    }    

    return status;
}
예제 #15
0
파일: main.c 프로젝트: joedrago/lbm
static int lbmLoadScript(lua_State * L, const char * name, const char * script, int len)
{
    int err;
    struct lbmScriptInfo info;
    info.script = script;
    info.len = len;
    err = lua_load(L, lbmLoadScriptReader, &info, name);
    if (err == 0)
    {
        err = lua_pcall(L, 0, LUA_MULTRET, 0);
        if (err == 0)
        {
            return 1;
        }
        else
        {
            // failed to run chunk
            printf("ERROR: %s\n", lua_tostring(L, -1));
            lua_pop(L, 1);
        }
    }
    else
    {
        // failed to load chunk
        printf("ERROR: %s\n", lua_tostring(L, -1));
        lua_pop(L, 1);
    }
    return 0;
}
예제 #16
0
파일: luac.cpp 프로젝트: aappleby/Lumina
// This compiles a chunk of code consisting of N copies of a dummy function, then
// hacks up the subprotos to point at protos on the stack... *headdesk*
static const LuaProto* combine(LuaThread* L, int n)
{
  if (n==1) {
    return L->stack_.top_[-1].getLClosure()->proto_;
  }
  else
  {
    std::string buffer;
    for(int i = 0; i < n; i++) {
      buffer += "(function()end)();";
    }

    Zio z;
    z.init(buffer.c_str(), buffer.size());
    if (lua_load(L, &z, "=(" PROGNAME ")", NULL) != LUA_OK) {
      fatal(lua_tostring(L,-1));
    }
    LuaProto* f = L->stack_.top_[-1].getLClosure()->proto_;
    for (int i=0; i<n; i++) {
      f->subprotos_[i] = L->stack_.top_[i-n-1].getLClosure()->proto_;
      if (f->subprotos_[i]->upvalues.size()>0) f->subprotos_[i]->upvalues[0].instack = 0;
    }
    f->lineinfo.clear();
    return f;
  }
}
예제 #17
0
파일: ltapi.c 프로젝트: acassis/lintouch
int ltapi_file_execute( lua_State * L, apr_file_t * file, const char * name )
{
    _ltapi_apr_file_chunkreader_data d;
    d.file = file;

    if( 0 != lua_load( L, _ltapi_apr_file_chunkreader,
                (void*) &d, name ) ) {
        /* an error occured while loading/parsing lua script */
        lt_server_logger_print( PRIO_DEFAULT,
                "ERROR while loading/parsing Lua script %s\n", name );
        lt_server_logger_print( PRIO_DEFAULT,
                "%s\n", lua_tostring( L, -1 ) );
        return 1;
    }

    if( 0 != lua_pcall( L, 0, 0, 0 ) ) {
        /* an error occured while executing lua script */
        lt_server_logger_print( PRIO_DEFAULT,
                "ERROR while executing Lua script %s\n", name );
        lt_server_logger_print( PRIO_DEFAULT,
                "%s\n", lua_tostring( L, -1 ) );
        return 1;
    }

    return 0;
}
예제 #18
0
LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
											 const char *mode) {
  LoadF lf;
  int status, readstatus;
  int c;
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
  if (filename == NULL) {
	lua_pushliteral(L, "=stdin");
	lf.f = stdin;
  }
  else {
	lua_pushfstring(L, "@%s", filename);
	lf.f = fopen(filename, "r");
	if (lf.f == NULL) return errfile(L, "open", fnameindex);
  }
  if (skipcomment(&lf, &c))  /* read initial portion */
	lf.buff[lf.n++] = '\n';  /* add line to correct line numbers */
  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
	lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
	if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
	skipcomment(&lf, &c);  /* re-read initial portion */
  }
  if (c != EOF)
	lf.buff[lf.n++] = c;  /* 'c' is the first character of the stream */
  status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
  readstatus = ferror(lf.f);
  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
  if (readstatus) {
	lua_settop(L, fnameindex);  /* ignore results from `lua_load' */
	return errfile(L, "read", fnameindex);
  }
  lua_remove(L, fnameindex);
  return status;
}
예제 #19
0
static int luaB_load (lua_State *L) {
  int status;
  size_t l;
  int top = lua_gettop(L);
  const char *s = lua_tolstring(L, 1, &l);
  const char *mode = luaL_optstring(L, 3, "bt");
  if (s != NULL) {  /* loading a string? */
    const char *chunkname = luaL_optstring(L, 2, s);
    status = (checkrights(L, mode, s) != NULL)
           || luaL_loadbuffer(L, s, l, chunkname);
  }
  else {  /* loading from a reader function */
    const char *chunkname = luaL_optstring(L, 2, "=(load)");
    loaddata ld;
    ld.mode = mode;
    luaL_checktype(L, 1, LUA_TFUNCTION);
    lua_settop(L, RESERVEDSLOT);  /* create reserved slot */
    status = lua_load(L, generic_reader, &ld, chunkname);
  }
  if (status == LUA_OK && top >= 4) {  /* is there an 'env' argument */
    lua_pushvalue(L, 4);  /* environment for loaded function */
    lua_setupvalue(L, -2, 1);  /* set it as 1st upvalue */
  }
  return load_aux(L, status);
}
예제 #20
0
파일: lauxlib.cpp 프로젝트: aappleby/Lumina
int luaL_loadfilex (LuaThread *L, const char *filename,
                                             const char *mode) {
  THREAD_CHECK(L);

  std::string filename2;
  if (filename == NULL) {
    filename = "stdin";
    filename2 = "=stdin";
  }
  else {
    filename2 = "@" + std::string(filename);
  }
  
  std::string buffer;
  if(readFileToString(filename,buffer)) {
    Zio z;
    z.init(buffer.c_str(), buffer.size());
    skipHeader(z);
    return lua_load(L, &z, filename2.c_str(), mode);
  }
  else {
    lua_pushstring(L, filename2.c_str());
    return errfile(L, "open", L->stack_.getTopIndex());
  }

}
예제 #21
0
파일: fx.c 프로젝트: siffiejoe/lua-fx
static void check_callable( lua_State* L, int i ) {
  int c = 0;
  switch( lua_type( L, i ) ) {
    case LUA_TFUNCTION:
      c = 1;
      break;
    case LUA_TUSERDATA: /* fall through */
    case LUA_TTABLE:
      if( luaL_getmetafield( L, i, "__call" ) ) {
        lua_pop( L, 1 );
        c = 1;
      }
      break;
    case LUA_TSTRING: {
      size_t code_size = 0;
      char const* code = lua_tolstring( L, i, &code_size );
      str_lambda_data d = { code, code_size, 0, 0 };
      char const* s = memchr( code, '=', code_size );
      if( !s || s[ 1 ] != '>' )
        luaL_argerror( L, i, "[lambda]:1: '=>' expected" );
      d.fa_pos = s - code;
      if( 0 != lua_load( L, str_lambda_reader, &d, "=[lambda]", NULL ) )
        luaL_argerror( L, i, lua_tostring( L, -1 ) );
      lua_call( L, 0, 1 );
      lua_replace( L, i );
      c = 1;
    }
  }
  luaL_argcheck( L, c, i, "callable value expected" );
}
예제 #22
0
파일: lauxlib.c 프로젝트: dodong471520/pap
LUALIB_API int luaL_loadwbuffer (lua_State *L, const lua_WChar *buff, size_t size,
                                const char *name) {
  LoadWS ls;
  ls.s = buff;
  ls.size = size;
  return lua_load(L, getWS, &ls, name);
}
예제 #23
0
파일: lauxlib.cpp 프로젝트: aappleby/Lumina
int luaL_loadbufferx (LuaThread *L, const char *buff, size_t size,
                                 const char *name, const char *mode) {
  THREAD_CHECK(L);
  
  Zio z;
  z.init(buff, size);
  return lua_load(L, &z, name, mode);
}
예제 #24
0
int main(void)
{
    lua_State *L=lua_open();
    lua_register(L,"print",print);
    if (lua_load(L,getF,stdin,"=stdin") || lua_pcall(L,0,0,0))
        fprintf(stderr,"%s\n",lua_tostring(L,-1));
    return 0;
}
예제 #25
0
static int luaB_load (lua_State *L) {
  int status;
  const char *cname = luaL_optstring(L, 2, "=(load)");
  luaL_checktype(L, 1, LUA_TFUNCTION);
  lua_settop(L, 3);  /* function, eventual name, plus one reserved slot */
  status = lua_load(L, generic_reader, NULL, cname);
  return load_aux(L, status);
}
int ngx_http_lua_clfactory_loadbuffer(lua_State *l, const char *buff, size_t size, const char *name)
{
    clfactory_buffer_ctx_t ls;
    ls.s = buff;
    ls.size = size;
    ls.sent_begin = ls.sent_end = 0;
    return lua_load(l, clfactory_getS, &ls, name);
}
예제 #27
0
	static int lua_loadfile(lua_State *L, const std::string& fname, const std::string& relativename)
	{
		lua_filestream lfs(fname);
		//lua uses '@' to know that this is a file (as opposed to something loaded via loadstring )
		std::string chunkname = '@' + relativename;
		LOG_LUA << "starting to read from " << fname << "\n";
		return  lua_load(L, &lua_filestream::lua_read_data, &lfs, chunkname.c_str(), "t");
	}
예제 #28
0
	bool compiled_chunk::run(lua_State* L) const 
	{
		chunks_it_ = chunks_.begin();
		if(lua_load(L, chunk_reader, reinterpret_cast<void*>(const_cast<compiled_chunk*>(this)), NULL, NULL) || lua_pcall(L, 0, 0, 0)) {
			const char* a = lua_tostring(L, -1);
			std::cerr << a << "\n";
			lua_pop(L, 1);
			return true;
		}
		return false;
	}
예제 #29
0
int luaL_loadstring_factory(lua_State *L, const char *s) {
    load_factory_t factory;

    factory.type = LOAD_TYPE_BUFFER;
    factory.data.string.str = s;
    factory.state = LOAD_STATE_PREFIX;
    factory.prefix = "return function()";
    factory.postfix = "end\n";

    return lua_load(L, loadstring_factory_reader, &factory, s);
}
예제 #30
0
int FileRead::readLuaScript(std::string filename, lua_State* mState)
{
	if( !boost::ends_with(filename, ".lua") )
	{
		filename += ".lua";
	}

	ReaderInfo info;
	info.file.open(filename);
	return lua_load(mState, chunkReader, &info, filename.c_str(), NULL);
}