Exemplo n.º 1
0
static int type_const_index(lua_State* L)
{
  lua_pushvalue(L,lua_upvalueindex(1));
  return 1;
}
Exemplo n.º 2
0
	int32 LuaUpValueIndex(int32 index)
	{
		return lua_upvalueindex(index);
	}
Exemplo n.º 3
0
int CLuaDatabaseDefs::OOP_DbQuery ( lua_State* luaVM )
{
    //  handle dbQuery ( [ function callbackFunction, [ table callbackArguments, ] ] element connection, string query, ... )
    CLuaFunctionRef iLuaFunction; CLuaArguments callbackArgs; CDatabaseConnectionElement* pElement; SString strQuery; CLuaArguments Args;

    CScriptArgReader argStream ( luaVM );

    argStream.ReadUserData ( pElement );
    if ( argStream.NextIsFunction () )
    {
        argStream.ReadFunction ( iLuaFunction );
        if ( argStream.NextIsTable () )
        {
            argStream.ReadLuaArgumentsTable( callbackArgs );
        }
    }
    argStream.ReadString ( strQuery );
    argStream.ReadLuaArguments ( Args );
    argStream.ReadFunctionComplete ();

    if ( !argStream.HasErrors () )
    {
        // Start async query
        CDbJobData* pJobData = g_pGame->GetDatabaseManager ()->QueryStart ( pElement->GetConnectionHandle (), strQuery, &Args );
        if ( !pJobData )
        {
            if ( !g_pGame->GetDatabaseManager ()->IsLastErrorSuppressed () )
                m_pScriptDebugging->LogWarning ( luaVM, "%s failed; %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), *g_pGame->GetDatabaseManager ()->GetLastErrorMessage () );
            lua_pushboolean ( luaVM, false );
            return 1;
        }
        // Make callback function if required
        if ( VERIFY_FUNCTION ( iLuaFunction ) )
        {
            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CLuaArguments Arguments;
                Arguments.PushDbQuery ( pJobData );
                Arguments.PushArguments ( callbackArgs );
                pJobData->SetCallback ( CLuaDatabaseDefs::DbQueryCallback, g_pGame->GetLuaCallbackManager ()->CreateCallback ( pLuaMain, iLuaFunction, Arguments ) );
            }
        }
        // Add debug info incase query result does not get collected
        pJobData->SetLuaDebugInfo ( g_pGame->GetScriptDebugging ()->GetLuaDebugInfo ( luaVM ) );
        lua_pushquery ( luaVM, pJobData );
        return 1;
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Exemplo n.º 4
0
/*
	unsigned address
	 string address
	integer type
	integer session
	string message
	 lightuserdata message_ptr
	 integer len
 */
static int
_send(lua_State *L) {
    struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
    int addr_type = lua_type(L,1);
    uint32_t dest = 0;
    switch(addr_type) {
    case LUA_TNUMBER:
        dest = lua_tounsigned(L,1);
        break;
    case LUA_TSTRING: {
        const char * addrname = lua_tostring(L,1);
        if (addrname[0] == '.' || addrname[0] == ':') {
            dest = skynet_queryname(context, addrname);
            if (dest == 0) {
                luaL_error(L, "Invalid name %s", addrname);
            }
        } else if ('0' <= addrname[0] && addrname[0] <= '9') {
            luaL_error(L, "Invalid name %s: must not start with a digit", addrname);
        } else {
            return _sendname(L, context, addrname);
        }
        break;
    }
    default:
        return luaL_error(L, "address must be number or string, got %s",lua_typename(L,addr_type));
    }

    int type = luaL_checkinteger(L, 2);
    int session = 0;
    if (lua_isnil(L,3)) {
        type |= PTYPE_TAG_ALLOCSESSION;
    } else {
        session = luaL_checkinteger(L,3);
    }

    int mtype = lua_type(L,4);
    switch (mtype) {
    case LUA_TSTRING: {
        size_t len = 0;
        void * msg = (void *)lua_tolstring(L,4,&len);
        if (len == 0) {
            msg = NULL;
        }
        session = skynet_send(context, 0, dest, type, session , msg, len);
        save_session(L, type, session);
        break;
    }
    case LUA_TLIGHTUSERDATA: {
        void * msg = lua_touserdata(L,4);
        int size = luaL_checkinteger(L,5);
        session = skynet_send(context, 0, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size);
        save_session(L, type, session);
        break;
    }
    default:
        luaL_error(L, "skynet.send invalid param %s", lua_typename(L, lua_type(L,4)));
    }
    if (session < 0) {
        // send to invalid address
        // todo: maybe throw error is better
        return 0;
    }
    lua_pushinteger(L,session);
    return 1;
}
Exemplo n.º 5
0
/// Wrapper around lua_upvalueindex.
///
/// \param index The first parameter to lua_upvalueindex.
///
/// \return The return value of lua_upvalueindex.
int
lutok::state::upvalue_index(const int index)
{
    return lua_upvalueindex(index);
}
Exemplo n.º 6
0
static int stdcall_closure (lua_State *L1) 
{
	lua_stdcallCFunction function = (lua_stdcallCFunction) lua_touserdata(L1, lua_upvalueindex(1));
	return function (L1);
}
Exemplo n.º 7
0
/**
 * get the next row from the resultset
 *
 * returns a lua-table with the fields (starting at 1)
 *
 * @return 0 on error, 1 on success
 *
 */
static int proxy_resultset_rows_iter(lua_State *L) {
	GRef *ref = *(GRef **)lua_touserdata(L, lua_upvalueindex(1));
	proxy_resultset_t *res = ref->udata;
	network_packet packet;
	GPtrArray *fields = res->fields;
	gsize i;
	int err = 0;
	network_mysqld_lenenc_type lenenc_type;
    
	GList *chunk = res->row;
    
	g_return_val_if_fail(chunk != NULL, 0);

	packet.data = chunk->data;
	packet.offset = 0;

	err = err || network_mysqld_proto_skip_network_header(&packet);
	err = err || network_mysqld_proto_peek_lenenc_type(&packet, &lenenc_type);
	g_return_val_if_fail(err == 0, 0); /* protocol error */
    
	switch (lenenc_type) {
	case NETWORK_MYSQLD_LENENC_TYPE_ERR:
		/* a ERR packet instead of real rows
		 *
		 * like "explain select fld3 from t2 ignore index (fld3,not_existing)"
		 *
		 * see mysql-test/t/select.test
		 */
	case NETWORK_MYSQLD_LENENC_TYPE_EOF:
		/* if we find the 2nd EOF packet we are done */
		return 0;
	case NETWORK_MYSQLD_LENENC_TYPE_INT:
	case NETWORK_MYSQLD_LENENC_TYPE_NULL:
		break;
	}
    
	lua_newtable(L);
    
	for (i = 0; i < fields->len; i++) {
		guint64 field_len;
        
		err = err || network_mysqld_proto_peek_lenenc_type(&packet, &lenenc_type);
		g_return_val_if_fail(err == 0, 0); /* protocol error */

		switch (lenenc_type) {
		case NETWORK_MYSQLD_LENENC_TYPE_NULL:
			network_mysqld_proto_skip(&packet, 1);
			lua_pushnil(L);
			break;
		case NETWORK_MYSQLD_LENENC_TYPE_INT:
			err = err || network_mysqld_proto_get_lenenc_int(&packet, &field_len);
			err = err || !(field_len <= packet.data->len); /* just to check that we don't overrun by the addition */
			err = err || !(packet.offset + field_len <= packet.data->len); /* check that we have enough string-bytes for the length-encoded string */
			if (err) return luaL_error(L, "%s: row-data is invalid", G_STRLOC);
            
			lua_pushlstring(L, packet.data->str + packet.offset, field_len);

			err = err || network_mysqld_proto_skip(&packet, field_len);
			break;
		default:
			/* EOF and ERR should come up here */
			err = 1;
			break;
		}

		/* lua starts its tables at 1 */
		lua_rawseti(L, -2, i + 1);
		g_return_val_if_fail(err == 0, 0); /* protocol error */
	}
    
	res->row = res->row->next;
    
	return 1;
}
Exemplo n.º 8
0
luaV_pushtype(dict_T, dict, luaV_Dict)
luaV_type_tostring(dict, LUAVIM_DICT)

    static int
luaV_dict_len (lua_State *L)
{
    dict_T *d = luaV_unbox(L, luaV_Dict, 1);
    lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
    return 1;
}

    static int
luaV_dict_iter (lua_State *L UNUSED)
{
#ifdef FEAT_EVAL
    hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
    int n = lua_tointeger(L, lua_upvalueindex(3));
    dictitem_T *di;
    if (n <= 0) return 0;
    while (HASHITEM_EMPTY(hi)) hi++;
    di = dict_lookup(hi);
    lua_pushstring(L, (char *) hi->hi_key);
    luaV_pushtypval(L, &di->di_tv);
    lua_pushlightuserdata(L, (void *) (hi + 1));
    lua_replace(L, lua_upvalueindex(2));
    lua_pushinteger(L, n - 1);
    lua_replace(L, lua_upvalueindex(3));
    return 2;
#else
    return 0;
#endif
Exemplo n.º 9
0
    static LUA_DECLARE( destroy )
    {
        delete (dxElements*)lua_touserdata( L, lua_upvalueindex( 1 ) );

        return 0;
    }
Exemplo n.º 10
0
static int lutok::LObject<T>::thunk(state& s) {
	T* obj = check(s, 1);
	s.remove(1);
	lutok::cxx_function * m = s.to_userdata<lutok::cxx_function>(lua_upvalueindex(1));
	return (obj->*(m))(l);
}
Exemplo n.º 11
0
static int __copy ( lua_State *_l ) {
    // if isbuiltin( _obj ) then
    if ( ex_lua_isbuiltin(_l, OBJ_IDX) ) {
        lua_pop(_l,1); // pops mt

        // assert(_obj.copy, "please provide copy function for builtin type: " .. typename(_obj) )
        lua_getfield(_l,OBJ_IDX,"copy");
        if ( lua_isnil(_l,-1) ) {
            ex_lua_typename(_l,OBJ_IDX);
            return luaL_error ( _l, "please provide copy function for builtin type: %s", lua_tostring(_l,-1) );
            lua_pop(_l,2); // pops typename, nil 
            lua_pushnil(_l);
            return 1;
        }
        // return _obj:copy()
        lua_pushvalue(_l,OBJ_IDX);
        lua_call(_l,1,1);
        return 1;
    }

    // elseif type(_obj) ~= "table" then
    if ( lua_istable(_l,OBJ_IDX) == 0 ) {
        // return _obj
        lua_pushvalue(_l,OBJ_IDX);
        return 1;
    }

    // elseif lookup_table[_obj] then
    lua_pushvalue(_l,OBJ_IDX);
    lua_gettable(_l,lua_upvalueindex(1));
    if ( lua_isnil(_l,-1) == 0 ) {
        // return lookup_table[_obj]
        return 1;
    }
    lua_pop(_l,1); // pops lookup_table[_obj] 

    // local new_table = {}
    lua_newtable(_l);
    // lookup_table[_obj] = new_table
    lua_pushvalue(_l,OBJ_IDX); // k
    lua_pushvalue(_l,-2); // v
    lua_settable(_l,lua_upvalueindex(1));
    // for key, value in pairs(_obj) do
    //     new_table[_copy(key)] = _copy(value)
    // end
    lua_pushnil(_l); /* first key */
    while ( lua_next(_l,OBJ_IDX) != 0 ) {
        /* uses 'key' (at index -2) and 'value' (at index -1) */

        // top
        // v
        // k
        // DEBUG { 
        // ex_log ( "should be:\nv\nk" );
        // ex_lua_dump_stack(_l);
        // ex_log ( "" );
        // } DEBUG end 

        // ======================================================== 
        // _copy(key)
        // ======================================================== 

        lua_pushvalue(_l,lua_upvalueindex(1));
        lua_pushcclosure(_l,__copy,1);
        // top
        // f
        // v
        // k
        // DEBUG { 
        // ex_log ( "should be:\nf\nv\nk" );
        // ex_lua_dump_stack(_l);
        // ex_log ( "" );
        // } DEBUG end 

        lua_pushvalue(_l,-3); // key
        lua_call(_l,1,1);

        // ======================================================== 
        // _copy(value)
        // ======================================================== 

        lua_pushvalue(_l,lua_upvalueindex(1));
        lua_pushcclosure(_l,__copy,1);
        // top
        // f
        // r (result)
        // v
        // k
        // DEBUG { 
        // ex_log ( "should be:\nf\nr\nv\nk" );
        // ex_lua_dump_stack(_l);
        // ex_log ( "" );
        // } DEBUG end 

        lua_pushvalue(_l,-3); // value
        lua_call(_l,1,1);

        // ======================================================== 
        // new_table[_copy(key)] = _copy(value)
        // ======================================================== 

        // top
        // r (result)
        // r (result)
        // v
        // k
        // DEBUG { 
        // ex_log ( "should be:\nr\nr\nv\nk" );
        // ex_lua_dump_stack(_l);
        // ex_log ( "" );
        // } DEBUG end 
        lua_settable(_l,-5);

        // DEBUG { 
        // ex_log ( "done!" );
        // ex_lua_dump_stack(_l);
        // ex_log ( "" );
        // } DEBUG end 
        /* removes 'value'; keeps 'key' for next iteration */
        lua_pop(_l, 1);
    }

    // return setmetatable(new_table, getmetatable(_obj))
    if ( lua_getmetatable(_l,OBJ_IDX) ) {
        lua_setmetatable(_l,-1);
    }

    // return new_table
    return 1;
}
Exemplo n.º 12
0
void CScriptDebugging::LogBadLevel ( lua_State* luaVM, unsigned int uiRequiredLevel )
{
    // Populate a message to print/send
    LogWarning ( luaVM, "Requires level '%d' @ '%s", uiRequiredLevel, lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
}
Exemplo n.º 13
0
void CScriptDebugging::LogBadType ( lua_State* luaVM )
{
    // Populate a message to print/send
    LogWarning ( luaVM, "Bad argument @ '%s'", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
}
Exemplo n.º 14
0
void CScriptDebugging::LogBadPointer ( lua_State* luaVM, const char* szArgumentType, unsigned int uiArgument )
{
    assert ( szArgumentType );

    // Populate a message to print/send
    LogWarning ( luaVM, "Bad '%s' pointer @ '%s'(%u)", szArgumentType, lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), uiArgument );
}
Exemplo n.º 15
0
Arquivo: gua.c Projeto: lengbing/gua
int callGoFunc (lua_State *L) {
    return callFunc(L, lua_tointeger(L, lua_upvalueindex(1)));
}
Exemplo n.º 16
0
 int LuaCTable::staticMetaNewIndex(lua_State *l)
 {
     LuaCTable* thethis = reinterpret_cast<LuaCTable*>(lua_touserdata(l, lua_upvalueindex(1)));
     thethis->metaNewIndex(l);
     return 0;
 }
Exemplo n.º 17
0
int read_message(lua_State* lua)
{
    void* luserdata = lua_touserdata(lua, lua_upvalueindex(1));
    if (NULL == luserdata) {
        lua_pushstring(lua, "read_message() invalid lightuserdata");
        lua_error(lua);
    }
    lua_sandbox* lsb = (lua_sandbox*)luserdata;

    const char* field;
    int fi = 0, ai = 0;
    switch (lua_gettop(lua)) {
    case 3:
        ai = luaL_checkint(lua, 3);
        if (ai < 0) {
            lua_pushstring(lua, "read_message() array index must be >= 0");
            lua_error(lua);
        }
        // fall-thru
    case 2:
        fi = luaL_checkint(lua, 2);
        if (fi < 0) {
            lua_pushstring(lua, "read_message() field index must be >= 0");
            lua_error(lua);
        }
        // fall-thru
    case 1:
        field = luaL_checkstring(lua, 1);
        break;
    default:
        lua_pushstring(lua, "read_message() incorrect number of arguments");
        lua_error(lua);
        break;
    }

    struct go_lua_read_message_return gr;
    // Cast away constness of the Lua string, the value is not modified
    // and it will save a copy.
    gr = go_lua_read_message(lsb->m_go, (char*)field, fi, ai);
    if (gr.r1 == NULL) {
        lua_pushnil(lua);
    } else {
        switch (gr.r0) {
        case 0:
            lua_pushlstring(lua, gr.r1, gr.r2);
            free(gr.r1);
            break;
        case 1:
            lua_pushlstring(lua, gr.r1, gr.r2);
            break;
        case 2:
            if (strncmp("Pid", field, 3) == 0
                || strncmp("Severity", field, 8) == 0) {
                lua_pushinteger(lua, *((GoInt32*)gr.r1));
            } else {
                lua_pushnumber(lua, *((GoInt*)gr.r1));
            }
            break;
        case 3:
            lua_pushnumber(lua, *((GoFloat64*)gr.r1));
            break;
        case 4:
            lua_pushboolean(lua, *((GoInt8*)gr.r1));
            break;
        }
    }
    return 1;
}
Exemplo n.º 18
0
static int root_destroy( lua_State *L )
{
    delete (LuaRoot*)lua_touserdata( L, lua_upvalueindex( 1 ) );

    return 0;
}
Exemplo n.º 19
0
			static int entry_point(lua_State* L)
			{
				function_object_impl const* impl_const = *(function_object_impl const**) lua_touserdata(L, lua_upvalueindex(1));

				// TODO: Can this be done differently?
				function_object_impl* impl = const_cast<function_object_impl*>(impl_const);
				invoke_context ctx;
				int results = 0;

# ifndef LUABIND_NO_EXCEPTIONS
				bool exception_caught = invoke_defer(L, impl, ctx, impl->f, results);
				if(exception_caught) lua_error(L);
# else
				results = invoke(L, *impl, ctx, impl->f, Signature(), impl->policies);
# endif
				if(!ctx) {
					ctx.format_error(L, impl);
					lua_error(L);
				}

				return results;
			}
Exemplo n.º 20
0
/**
 * @upvalue z_stream - Memory for the z_stream.
 * @upvalue remainder - Any remainder from the last deflate call.
 *
 * @param string - "print" to deflate stream.
 * @param int - flush output buffer? Z_SYNC_FLUSH, Z_FULL_FLUSH, or Z_FINISH.
 *
 * if no params, terminates the stream (as if we got empty string and Z_FINISH).
 */
static int lz_filter_impl(lua_State *L, int (*filter)(z_streamp, int), int (*end)(z_streamp), char* name) {
    int flush = Z_NO_FLUSH, result;
    z_stream* stream;
    luaL_Buffer buff;
    size_t avail_in;

    if ( filter == deflate ) {
        const char *const opts[] = { "none", "sync", "full", "finish", NULL };
        flush = luaL_checkoption(L, 2, opts[0], opts);
        if ( flush ) flush++;
        /* Z_NO_FLUSH(0) Z_SYNC_FLUSH(2), Z_FULL_FLUSH(3), Z_FINISH (4) */

        /* No arguments or nil, we are terminating the stream: */
        if ( lua_gettop(L) == 0 || lua_isnil(L, 1) ) {
            flush = Z_FINISH;
        }
    }

    stream = (z_stream*)lua_touserdata(L, lua_upvalueindex(1));
    if ( stream == NULL ) {
        if ( lua_gettop(L) >= 1 && lua_isstring(L, 1) ) {
            lua_pushfstring(L, "IllegalState: calling %s function when stream was previously closed", name);
            lua_error(L);
        }
        lua_pushstring(L, "");
        lua_pushboolean(L, 1);
        return 2; /* Ignore duplicate calls to "close". */
    }

    luaL_buffinit(L, &buff);

    if ( lua_gettop(L) > 1 ) lua_pushvalue(L, 1);

    if ( lua_isstring(L, lua_upvalueindex(2)) ) {
        lua_pushvalue(L, lua_upvalueindex(2));
        if ( lua_gettop(L) > 1 && lua_isstring(L, -2) ) {
            lua_concat(L, 2);
        }
    }

    /*  Do the actual deflate'ing: */
    if (lua_gettop(L) > 0) {
        stream->next_in = (unsigned char*)lua_tolstring(L, -1, &avail_in);
    } else {
        stream->next_in = NULL;
        avail_in = 0;
    }
    stream->avail_in = avail_in;

    if ( ! stream->avail_in && ! flush ) {
        /*  Passed empty string, make it a noop instead of erroring out. */
        lua_pushstring(L, "");
        lua_pushboolean(L, 0);
        lua_pushinteger(L, stream->total_in);
        lua_pushinteger(L, stream->total_out);
        return 4;
    }

    do {
        stream->next_out  = (unsigned char*)luaL_prepbuffer(&buff);
        stream->avail_out = LUAL_BUFFERSIZE;
        result = filter(stream, flush);
        if ( Z_BUF_ERROR != result ) {
            /* Ignore Z_BUF_ERROR since that just indicates that we
             * need a larger buffer in order to proceed.  Thanks to
             * Tobias Markmann for finding this bug!
             */
            lz_assert(L, result, stream, __FILE__, __LINE__);
        }
        luaL_addsize(&buff, LUAL_BUFFERSIZE - stream->avail_out);
    } while ( stream->avail_out == 0 );

    /*  Need to do this before we alter the stack: */
    luaL_pushresult(&buff);

    /*  Save remainder in lua_upvalueindex(2): */
    if ( NULL != stream->next_in ) {
        lua_pushlstring(L, (char*)stream->next_in, stream->avail_in);
        lua_replace(L, lua_upvalueindex(2));
    }

    /*  "close" the stream/remove finalizer: */
    if ( result == Z_STREAM_END ) {
        /*  Clear-out the metatable so end is not called twice: */
        lua_pushnil(L);
        lua_setmetatable(L, lua_upvalueindex(1));

        /*  nil the upvalue: */
        lua_pushnil(L);
        lua_replace(L, lua_upvalueindex(1));

        /*  Close the stream: */
        lz_assert(L, end(stream), stream, __FILE__, __LINE__);

        lua_pushboolean(L, 1);
    } else {
        lua_pushboolean(L, 0);
    }
    lua_pushinteger(L, stream->total_in);
    lua_pushinteger(L, stream->total_out);
    return 4;
}
Exemplo n.º 21
0
int lua_uv_timer_cb_closure(lua_State * L) {
	lua_pushvalue(L, lua_upvalueindex(1)); // handler
	lua_pushvalue(L, lua_upvalueindex(2)); // udata
	lua_call(L, 1, 0);	// pcall?
	return 0;
}
Exemplo n.º 22
0
static int lz_checksum(lua_State *L) {
    if ( lua_gettop(L) <= 0 ) {
        lua_pushvalue(L, lua_upvalueindex(3));
        lua_pushvalue(L, lua_upvalueindex(4));
    } else if ( lua_isfunction(L, 1) ) {
        checksum_combine_t combine = (checksum_combine_t)
                                     lua_touserdata(L, lua_upvalueindex(2));

        lua_pushvalue(L, 1);
        lua_call(L, 0, 2);
        if ( ! lua_isnumber(L, -2) || ! lua_isnumber(L, -1) ) {
            luaL_argerror(L, 1, "expected function to return two numbers");
        }

        /* Calculate and replace the checksum */
        lua_pushnumber(L,
                       combine((uLong)lua_tonumber(L, lua_upvalueindex(3)),
                               (uLong)lua_tonumber(L, -2),
                               (z_off_t)lua_tonumber(L, -1)));
        lua_pushvalue(L, -1);
        lua_replace(L, lua_upvalueindex(3));

        /* Calculate and replace the length */
        lua_pushnumber(L,
                       lua_tonumber(L, lua_upvalueindex(4)) + lua_tonumber(L, -2));
        lua_pushvalue(L, -1);
        lua_replace(L, lua_upvalueindex(4));
    } else {
        const Bytef* str;
        size_t       len;

        checksum_t checksum = (checksum_t)
                              lua_touserdata(L, lua_upvalueindex(1));
        str = (const Bytef*)luaL_checklstring(L, 1, &len);

        /* Calculate and replace the checksum */
        lua_pushnumber(L,
                       checksum((uLong)lua_tonumber(L, lua_upvalueindex(3)),
                                str,
                                len));
        lua_pushvalue(L, -1);
        lua_replace(L, lua_upvalueindex(3));

        /* Calculate and replace the length */
        lua_pushnumber(L,
                       lua_tonumber(L, lua_upvalueindex(4)) + len);
        lua_pushvalue(L, -1);
        lua_replace(L, lua_upvalueindex(4));
    }
    return 2;
}
Exemplo n.º 23
0
//打印信息
static int
_error(lua_State *L) {
	struct server_context * context = lua_touserdata(L, lua_upvalueindex(1));
	server_error(context, "%s", luaL_checkstring(L,1));
	return 0;
}
Exemplo n.º 24
0
static int onEquipChangeListener(lua_State *l) {
	Player *p = LuaObject<Player>::GetFromLua(lua_upvalueindex(1));
	p->onChangeEquipment.emit();
	return 0;
}
Exemplo n.º 25
0
static int l_gettrans(lua_State *L)
{
  lua_pushvalue(L, lua_upvalueindex(1));
  lua_getfield(L, 1, TABLE_INDEX);
  return 1;
}
Exemplo n.º 26
0
int CLuaFunctionDefs::ReloadBanList ( lua_State* luaVM )
{
    bool bSuccess = CStaticFunctionDefinitions::ReloadBanList ();
    if ( !bSuccess )
        m_pScriptDebugging->LogError ( luaVM, "%s: Ban List failed to reload, fix any errors and run again", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
    lua_pushboolean ( luaVM, bSuccess );
    return 1;
}
Exemplo n.º 27
0
int luaCompat_upvalueIndex(lua_State* L, int which, int num_upvalues)
{ /* lua5 */
  UNUSED(num_upvalues);

  return lua_upvalueindex(which);
}
Exemplo n.º 28
0
/// 从 buffer 读取数据, 返回 lua 函数返回值的个数, 函数的第一个返回值是 queue.
static int
filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
	struct queue *q = lua_touserdata(L, 1);
	struct uncomplete * uc = find_uncomplete(q, fd);	// 注意, 这里将 uncomplete 从 queue.hash 中移除了
	if (uc) {
		// fill uncomplete
		// 填充 uncomplete
		if (uc->read < 0) {		// 当包头还未完整读取的情况
			// read size
			assert(uc->read == -1);

			// 获得数据内容大小
			int pack_size = *buffer;
			pack_size |= uc->header << 8 ;

			// 偏移到实际数据内容指针开始位置
			++buffer;

			// 实际的数据内容大小
			--size;

			uc->pack.size = pack_size;	// 记录实际需要读取的内容大小
			uc->pack.buffer = skynet_malloc(pack_size);	// 分配内存空间
			uc->read = 0;	// 标记还未开始读取数据内容
		}

		// 计算需要读取的数据
		int need = uc->pack.size - uc->read;

		if (size < need) {	// 如果 buffer 待读取的数据还不足, 尽可能读取能够读取的数据
			// 读取可读的数据
			memcpy(uc->pack.buffer + uc->read, buffer, size);
			uc->read += size;

			// 再次压入到 queue.hash 中
			int h = hash_fd(fd);
			uc->next = q->hash[h];
			q->hash[h] = uc;
			return 1;
		}

		// 读取完整的数据内容
		memcpy(uc->pack.buffer + uc->read, buffer, need);

		// 跳过已经读取的内容
		buffer += need;

		// 计算剩余的可读取数据大小
		size -= need;

		// buffer 中的数据恰好足够读取
		if (size == 0) {
			lua_pushvalue(L, lua_upvalueindex(TYPE_DATA));	// macro TYPE_DATA
			lua_pushinteger(L, fd);	// socket id
			lua_pushlightuserdata(L, uc->pack.buffer);	// buffer
			lua_pushinteger(L, uc->pack.size);	// buffer size
			skynet_free(uc);
			return 5;
		}

		// more data
		// buffer 有更多的数据可读, 将数据压入 queue.queue 中
		push_data(L, fd, uc->pack.buffer, uc->pack.size, 0);
		skynet_free(uc);
		push_more(L, fd, buffer, size);	// 继续读取剩下的数据
		lua_pushvalue(L, lua_upvalueindex(TYPE_MORE));	// macro TYPE_MORE
		return 2;
	} else {
		if (size == 1) {	// 仅读取包头的 1 个数据
			struct uncomplete * uc = save_uncomplete(L, fd);
			uc->read = -1;
			uc->header = *buffer;
			return 1;
		}

		// 读取包头的数据
		int pack_size = read_size(buffer);
		buffer+=2;
		size-=2;

		// 如果 buffer 的数据不够读, 将 buffer 数据全部读取
		if (size < pack_size) {
			struct uncomplete * uc = save_uncomplete(L, fd);
			uc->read = size;
			uc->pack.size = pack_size;
			uc->pack.buffer = skynet_malloc(pack_size);
			memcpy(uc->pack.buffer, buffer, size);
			return 1;
		}

		// 如果 buffer 的数据恰好是 1 个包的数据大小, 将 buffer 数据全部读取
		if (size == pack_size) {
			// just one package
			lua_pushvalue(L, lua_upvalueindex(TYPE_DATA));	// macro TYPE_DATA
			lua_pushinteger(L, fd);				// socket id
			void * result = skynet_malloc(pack_size);
			memcpy(result, buffer, size);
			lua_pushlightuserdata(L, result);	// buffer
			lua_pushinteger(L, size);			// buffer size
			return 5;
		}

		// more data
		// 如果 buffer 的数据大于 1 个包的数据大小, 那么继续读取 buffer 里面的数据
		push_data(L, fd, buffer, pack_size, 1);
		buffer += pack_size;
		size -= pack_size;
		push_more(L, fd, buffer, size);
		lua_pushvalue(L, lua_upvalueindex(TYPE_MORE));	// macro TYPE_MORE

		return 2;
	}
}
Exemplo n.º 29
0
int CLuaDatabaseDefs::DbExec ( lua_State* luaVM )
{
    //  bool dbExec ( element connection, string query, ... )
    CDatabaseConnectionElement* pElement; SString strQuery; CLuaArguments Args;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pElement );
    argStream.ReadString ( strQuery );
    argStream.ReadLuaArguments ( Args );

    if ( !argStream.HasErrors () )
    {
        // Start async query
        CDbJobData* pJobData = g_pGame->GetDatabaseManager ()->Exec ( pElement->GetConnectionHandle (), strQuery, &Args );
        if ( !pJobData )
        {
            if ( !g_pGame->GetDatabaseManager ()->IsLastErrorSuppressed () )
                m_pScriptDebugging->LogError ( luaVM, "%s failed: %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), *g_pGame->GetDatabaseManager ()->GetLastErrorMessage () );
            lua_pushboolean ( luaVM, false );
            return 1;
        }
        // Add callback for tracking errors
        pJobData->SetCallback ( DbExecCallback, NULL );
        pJobData->SetLuaDebugInfo ( g_pGame->GetScriptDebugging ()->GetLuaDebugInfo ( luaVM ) );

        lua_pushboolean ( luaVM, true );
        return 1;
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Exemplo n.º 30
0
static int lautoc_struct_newindex(lua_State *L)
{
  luaA_struct_to_member_name_typeid(L, lua_tonumber(L,lua_upvalueindex(1)), lua_touserdata(L,-3), lua_tostring(L,-2),-1);
  return 0;
}