Exemplo n.º 1
0
void set_lua_variable(lua_State *l, char *name, int value)
{
  /* See if there's a local variable that matches the given name.
   * If we find it, then set the value. If not, we'll keep walking back
   * up the stack levels until we've exhausted all of the closures.
   * At that point, set a global instead. */

  lua_Debug ar;
  int stacksize = get_call_stack_size(l);
  int stacklevel,i;

  /* This C call is stacklevel 0; the function that called is, 1; and so on. */
  for (stacklevel=0; stacklevel<stacksize; stacklevel++) {
    const char *local_name;
    lua_getstack(l, stacklevel, &ar);
    lua_getinfo(l, "nSluf", &ar); /* get all the info there is. Could probably be whittled down. */
    i=1;
    while ( (local_name=lua_getlocal(l, &ar, i++)) ) {
      if (!strcmp(name, local_name)) {
	/* Found the local! Set it, and exit. */
	lua_pop(l, 1);              // pop the local's old value
	lua_pushinteger(l, value);  // push the new value
	lua_setlocal(l, &ar, i-1); // set the value (note: i was incremented)
	lua_pop(l, 2);
	return;
      }
      lua_pop(l, 1);
    }
  }  

  /* Didn't find a local with that name anywhere. Set it as a global. */
  lua_pushinteger(l, value);
  lua_setglobal(l, name);
  lua_pop(l, 3);
}
Exemplo n.º 2
0
static int setlocal (lua_State *L) {
  lua_Debug ar;
  if (!lua_getstack(L, luaL_checkint(L, 1), &ar))  /* level out of range? */
    return luaL_argerror(L, 1, "level out of range");
  luaL_checkany(L, 3);
  lua_pushstring(L, lua_setlocal(L, &ar, luaL_checkint(L, 2)));
  return 1;
}
Exemplo n.º 3
0
static void setlocal (void) {
  lua_Object func = lua_stackedfunction(luaL_check_int(1));
  int numvar;
  luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
  numvar = findlocal(func, 2);
  lua_pushobject(luaL_nonnullarg(3));
  if (!lua_setlocal(func, numvar))
    lua_error("no such local variable");
}
Exemplo n.º 4
0
static int db_setlocal (lua_State *L) {
  int arg;
  lua_State *L1 = getthread(L, &arg);
  lua_Debug ar;
  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */
    return luaL_argerror(L, arg+1, "level out of range");
  luaL_checkany(L, arg+3);
  lua_settop(L, arg+3);
  lua_xmove(L, L1, 1);
  lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));
  return 1;
}
Exemplo n.º 5
0
// -----------------------------------------------------------------------
// Write the current stack value into global or local variable
void Write_Ref_Variable(lua_State *L, int idx, lua_Debug *local)
{
    if(idx >= 0)
    {
        // Global variable
        lua_setfield(L, LUA_GLOBALSINDEX, local->name);
    }
    else
    {
        // Local one
        lua_setlocal(L, local, -idx);
    }
}
Exemplo n.º 6
0
static int db_setlocal (lua_State *L) {
  int arg;
  const char *name;
  lua_State *L1 = getthread(L, &arg);
  lua_Debug ar;
  int level = (int)luaL_checkinteger(L, arg + 1);
  int nvar = (int)luaL_checkinteger(L, arg + 2);
  if (!lua_getstack(L1, level, &ar))  /* out of range? */
    return luaL_argerror(L, arg+1, "level out of range");
  luaL_checkany(L, arg+3);
  lua_settop(L, arg+3);
  lua_xmove(L, L1, 1);
  name = lua_setlocal(L1, &ar, nvar);
  if (name == NULL)
    lua_pop(L1, 1);  /* pop value (if not popped by 'lua_setlocal') */
  lua_pushstring(L, name);
  return 1;
}
bool VRSDClientLuaImplementation::UpdateLocalVariable(const char* Variable, const char* NewValue)
{
  if(!Variable || !NewValue)
    return false;

  VASSERT(m_pLuaState);

  if(!m_pLuaState || !m_pActivationRecord)
    return false;

  // we can only get local symbols without a crash if we are really in a Lua code execution path
  if(strcmp(m_pActivationRecord->what, "Lua"))
    return true;

  VLuaStackCleaner stackCleaner(m_pLuaState);
  ScopedBooleanToTrue disableDebugHook(m_bDebuggerRetrievingValues);

  const char* pSymbolName = NULL;
  int iLocalIndex = 1;

  VMemoryTempBuffer<512> copyBuffer(Variable); // operate on a copy string in the tokenizer
  
  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  char* pCurrent = Tokenizer.Next();
  int i = 0;
  const char* pLastField = NULL;

  // go through all local variables
  while((pSymbolName = lua_getlocal(m_pLuaState, m_pActivationRecord, iLocalIndex)) != NULL)
  {
    // check if this local variable is the one we want
    if(!strcmp(pSymbolName, pCurrent))
    {
      VLuaStackCleaner innerStackCleaner(m_pLuaState);
      if(LookupPath(Tokenizer, &pLastField) != HKV_SUCCESS)
        return false;

      // now the variable is at the top of the stack, update its value
      int iLuaType = lua_type(m_pLuaState, -1);

      // pop off the field again
      lua_pop(m_pLuaState, 1);

      bool bIsIntegerKey = false;
      if(pLastField && VStringUtil::IsIntegerString(pLastField))
      {
        bIsIntegerKey = true;
        lua_pushnumber(m_pLuaState, (LUA_NUMBER)atoi(pLastField));
      }

      if (!PushValue(iLuaType, NewValue))
      {
        return false;
      }

      if(Tokenizer.GetTokenCount() > 1)
      {
        VASSERT(pLastField != NULL);
        if(bIsIntegerKey)
        {
          lua_settable(m_pLuaState, -3);
        }
        else
        {
          lua_setfield(m_pLuaState, -2, pLastField);
        }
      }
      else
      {
        lua_setlocal(m_pLuaState, m_pActivationRecord, iLocalIndex);
      }

      break;
    }

    // clean up the stack and increment the index to get the next local variable
    lua_pop(m_pLuaState, 1);
    iLocalIndex++;
  }

  return true;
}
Exemplo n.º 8
0
const char * LuaDebug::SetLocal(const lua_Debug * ar, int n) const
{
	return lua_setlocal(L(), ar, n);
}