bool VRSDClientLuaImplementation::IsGlobalUserDataOfType(const char* Name, const char* Type)
{
  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);

  VMemoryTempBuffer<512> copyBuffer(Name); // operate on a copy string in the tokenizer
  
  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');

  lua_getfield(m_pLuaState, LUA_GLOBALSINDEX, Tokenizer.Next());
  if(LookupPath(Tokenizer) != HKV_SUCCESS)
    return false;

  // now the variable should be at the top of the stack
  return LUA_TestUserData(m_pLuaState, -1, Type) != NULL;
}
bool VRSDClientLuaImplementation::GetGlobalType(const char*pVariableName, char * pUserDataTypeName)
{
  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);

  VMemoryTempBuffer<512> copyBuffer(pVariableName); // operate on a copy string in the tokenizer

  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  lua_getfield(m_pLuaState, LUA_GLOBALSINDEX, Tokenizer.Next());

  if(LookupPath(Tokenizer) != HKV_SUCCESS)
    return false;

  const char * szName = VSWIG_Lua_typename(m_pLuaState, -1);
  sprintf(pUserDataTypeName, "%s", szName);
  
  return pUserDataTypeName[0] != 0;
}
Exemple #3
0
  ObjectInfo Resolver::FindPath(const string &path) const
  {
    ObjectInfo oi = ParsePath(path);

    if (oi.object.id != "")
    {
      if (oi.project.id == "")
        oi.project.id = FindProject(oi.project.name);

      DescribeObject(oi.project.id, oi.object.id, oi);

      oi.project.name = GetProjectName(oi.project.id);
    }
    else
    {
      if (oi.project.id == "")
        oi.project.id = FindProject(oi.project.name);
      else
        oi.project.name = GetProjectName(oi.project.id);

      if (oi.project.id != "")
        LookupPath(oi.project.id, oi.object.name, oi.object.folder, oi);
    }
    return oi;
  }
bool VRSDClientLuaImplementation::UpdateGlobalVariable(const char* szVarName, const char* szNewValue)
{
  if(!szVarName || !szNewValue || szVarName[0]==0)
    return false;

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

  VMemoryTempBuffer<512> copyBuffer(szVarName); // operate on a copy string in the tokenizer

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

  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  const char* pCurrent = Tokenizer.Next();
  unsigned int i = 0;

  const char* pLastField = NULL;

  lua_getfield(m_pLuaState, LUA_GLOBALSINDEX, pCurrent);
  if(lua_isnil(m_pLuaState, -1))
    return false;
  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);
  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, szNewValue))
    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_setglobal(m_pLuaState, szVarName);
  }

  return true;
}
  bool FullOrthancDataset::GetStringValue(std::string& result,
                                          const DicomPath& path) const
  {
    const Json::Value* value = LookupPath(path);

    if (value == NULL)
    {
      return false;
    }
    else
    {
      return GetStringInternal(result, *value);
    }
  }
  bool FullOrthancDataset::GetSequenceSize(size_t& size,
                                           const DicomPath& path) const
  {
    const Json::Value* sequence = LookupPath(path);

    if (sequence == NULL)
    {
      return false;
    }
    else
    {
      size = GetSequenceContent(*sequence).size();
      return true;
    }
  }
bool VRSDClientLuaImplementation::GetLocalType(const char* pVariableName, char * pUserDataTypeName)
{
  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(pVariableName); // operate on a copy string in the tokenizer

  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  char* pCurrent = Tokenizer.Next();

  pUserDataTypeName[0] = 0;

  while((pSymbolName = lua_getlocal(m_pLuaState, m_pActivationRecord, iLocalIndex)) != NULL)
  {                                                             //stack: ..., localX, TOP
    // check if this local variable is the one we want
    if(!strcmp(pSymbolName, pCurrent))
    {
      // there is already the local on the stack
      if(LookupPath(Tokenizer) != HKV_SUCCESS)
        return false;

      const char * szName = VSWIG_Lua_typename(m_pLuaState, -1);
      sprintf(pUserDataTypeName, "%s", szName);

      return pUserDataTypeName[0] != 0;
    }

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

  return false;
}
bool VRSDClientLuaImplementation::GetUserDataPointerFromLocal(const char* szVariable, void** ppUserData, void ** ppEnvironment)
{
  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;

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


  char* pSymbolName = NULL;
  int iLocalIndex = 1;

  VMemoryTempBuffer<512> copyBuffer(szVariable); // operate on a copy string in the tokenizer
  
  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  char* pCurrent = Tokenizer.Next();

  while((pSymbolName = (char*)lua_getlocal(m_pLuaState, m_pActivationRecord, iLocalIndex)) != NULL)
  {                                                       //Stack: ..., localX, TOP
    // check if this local variable is the one we want
    if(!strcmp(pSymbolName, pCurrent))
    {
      VLuaStackCleaner innerStackCleaner(m_pLuaState);
      //there is already the local on the stack...
      if(LookupPath(Tokenizer) != HKV_SUCCESS)
        return false;

      // now the variable is at the top of the stack
      *ppUserData = lua_touserdata(m_pLuaState, -1);    //Stack: ..., localX, {field}, TOP
      *ppEnvironment = m_pLuaState;
      return true;
    }

    // remove the value and update the index to get the next local variable
    lua_pop(m_pLuaState, 1);
    iLocalIndex++;
  }

  return false;
}
bool VRSDClientLuaImplementation::IsLocalUserDataOfType(const char* Name, const char* Type)
{
  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(Name); // operate on a copy string in the tokenizer
  
  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  char* pCurrent = Tokenizer.Next();

  while((pSymbolName = lua_getlocal(m_pLuaState, m_pActivationRecord, iLocalIndex)) != NULL)
  {                                                             //stack: ..., localX, TOP
    // check if this local variable is the one we want
    if(!strcmp(pSymbolName, pCurrent))
    {
      VLuaStackCleaner innerStackCleaner(m_pLuaState);
      // there is already the local on the stack

      if(LookupPath(Tokenizer) != HKV_SUCCESS)
        return false;

      // now the variable is at the top of the stack
      return LUA_TestUserData(m_pLuaState, -1, Type) != NULL;
    }

    // increment the index to get the next local variable
    iLocalIndex++;
    lua_pop(m_pLuaState, 1);
  }
 
  return false;
}
bool VRSDClientLuaImplementation::GetSubSymbolsForGlobal(char* GlobalName, DynArray_cl<VRSDScriptSymbol>& SubSymbols, unsigned int& SubSymbolCount)
{
  VASSERT(m_pLuaState);

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

  SubSymbolCount = 0;

  // 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;

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

  VMemoryTempBuffer<512> copyBuffer(GlobalName); // operate on a copy string in the tokenizer
  
  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  lua_getfield(m_pLuaState, LUA_GLOBALSINDEX, Tokenizer.Next());
  if(LookupPath(Tokenizer) != HKV_SUCCESS)
    return false;

  // now the variable should be at the top of the stack and we can get the subvariables of it
  
  // first key for the iteration
  lua_pushnil(m_pLuaState);
  
  while (lua_next(m_pLuaState, -2) != 0)
  {
    // after this the key is at -2 and the value at -1
    
    // we only want string fields and numeric fields
    // (lua_isstring returns also true for numbers, using
    // tostring later on will cast the number to a string)
    int iKeyType = lua_type(m_pLuaState, -2);
    if (iKeyType==LUA_TNUMBER || iKeyType==LUA_TSTRING)
    {  
      VString sKeyBuffer;

      //this if prevents a conversion of number on the Lua stack
      if(iKeyType==LUA_TNUMBER) sKeyBuffer.Format("%1.0f", lua_tonumber(m_pLuaState, -2));
      else                      sKeyBuffer = lua_tostring(m_pLuaState, -2);

      const char* pSymbolName = sKeyBuffer.AsChar();

      if(pSymbolName)
      {
        // table member variable
        if(lua_istable(m_pLuaState, -1))
        {
          // add a symbol for the table
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, "table", VRSDScriptSymbol::SYMBOL_TABLE);
        }
        // numeric member variable
        else if(lua_type(m_pLuaState, -1) == LUA_TNUMBER)
        {
          char buffer[32];
          sprintf(buffer, "%f", lua_tonumber(m_pLuaState, -1));
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, buffer, VRSDScriptSymbol::SYMBOL_NUMBER);
        }
        // string member variable
        else if(lua_type(m_pLuaState, -1) == LUA_TSTRING)
        {
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, lua_tostring(m_pLuaState, -1), VRSDScriptSymbol::SYMBOL_STRING);
        }
        // function member variable
        else if(lua_isfunction(m_pLuaState, -1))
        {
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, "function", VRSDScriptSymbol::SYMBOL_FUNCTION);
        }
        // userdata member variable
        else if(lua_isuserdata(m_pLuaState, -1))
        {
          char buffer[128];
          swig_type_info* type = (swig_type_info *)LUA_GetSwigType(m_pLuaState, -1);
          void * pUserData = lua_touserdata(m_pLuaState, -1);

          if(type)
          {
            vis_snprintf(buffer, 128, "userdata:0x%p [%s: 0x%p]", pUserData, type->str, ((swig_lua_userdata*)pUserData)->ptr);
          }
          else
          {
            vis_snprintf(buffer, 128, "userdata:0x%p", lua_touserdata(m_pLuaState, -1));
          }
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, buffer, VRSDScriptSymbol::SYMBOL_USERDATA);
        }
        else if(lua_isboolean(m_pLuaState, -1))
        {
          int iBoolVal = lua_toboolean(m_pLuaState, -1);
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, iBoolVal ? "true" : "false", VRSDScriptSymbol::SYMBOL_BOOLEAN);
        }
        else if(lua_isnil(m_pLuaState, -1))
        {
          AddSymbol(SubSymbols, SubSymbolCount, pSymbolName, "nil", VRSDScriptSymbol::SYMBOL_CLASS);
        }

      }
    }
    lua_pop(m_pLuaState, 1);  // remove the value, keep the key for the next iteration
  }

  return true;
}
bool VRSDClientLuaImplementation::GetSubSymbolsForLocal(char* LocalName, DynArray_cl<VRSDScriptSymbol>& SubSymbols, unsigned int& SubSymbolCount)
{
  VASSERT(m_pLuaState);

  if(!m_pLuaState || !m_pActivationRecord)
    return false;
  
  SubSymbolCount = 0;

  // 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 disableDebugCallback(m_bDebuggerRetrievingValues);

  char* pSymbolName = NULL;
  int iLocalIndex = 1;

  VMemoryTempBuffer<512> copyBuffer(LocalName); // operate on a copy string in the tokenizer
  
  VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.');
  char* pCurrent = Tokenizer.Next();

  while((pSymbolName = (char*)lua_getlocal(m_pLuaState, m_pActivationRecord, iLocalIndex)) != NULL)
  {                                                       //stack: .., localX, TOP
    // check if this local variable is the one we want
    if(!strcmp(pSymbolName, pCurrent))
    {
      //the local is already on the stack
      if(LookupPath(Tokenizer) != HKV_SUCCESS)
        return false;

      // now we can iterate over the contents of the table
      // first key for the iteration
      lua_pushnil(m_pLuaState);                           //stack: .., localX, {field}, nil, TOP

      //access the last field
      while (lua_next(m_pLuaState, -2) != 0)              //stack: .., localX, {field}, key, value TOP
      {
        // we only want string fields and numeric fields
        // (lua_isstring returns also true for numbers, using
        // tostring later on will cast the number to a string)
        int iKeyType = lua_type(m_pLuaState, -2);
        if (iKeyType==LUA_TNUMBER || iKeyType==LUA_TSTRING)
        {  
          VString sKeyBuffer;

          //this if prevents a conversion of number on the Lua stack
          if(iKeyType==LUA_TNUMBER) sKeyBuffer.Format("%1.0f", lua_tonumber(m_pLuaState, -2));
          else                      sKeyBuffer = lua_tostring(m_pLuaState, -2);

          if(!sKeyBuffer.IsEmpty())
          {
            int iValueType = lua_type(m_pLuaState, -1);
            VString sValueBuffer;

            // table member variable
            switch (iValueType) 
            {
              case LUA_TTABLE:
                // add a symbol for the table
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), "table", VRSDScriptSymbol::SYMBOL_TABLE);
                break;
            
              case LUA_TNUMBER:
                // numeric member variable
                sValueBuffer.Format("%f", lua_tonumber(m_pLuaState, -1));
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), sValueBuffer.AsChar(), VRSDScriptSymbol::SYMBOL_NUMBER);
                break;

              case LUA_TSTRING:
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), lua_tostring(m_pLuaState, -1), VRSDScriptSymbol::SYMBOL_STRING);
                break;

              case LUA_TFUNCTION:
                sValueBuffer.Format("function:0x%p", lua_tocfunction(m_pLuaState, -1));
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), sValueBuffer.AsChar(), VRSDScriptSymbol::SYMBOL_FUNCTION);
                break;
              
              case LUA_TBOOLEAN:
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), lua_toboolean(m_pLuaState, -1) ? "true" : "false", VRSDScriptSymbol::SYMBOL_BOOLEAN);
                break;

              case LUA_TNIL:
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), "nil", VRSDScriptSymbol::SYMBOL_CLASS);
                break;

              case LUA_TTHREAD:
                sValueBuffer.Format("thread:0x%p", lua_tothread(m_pLuaState, -1));
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), sValueBuffer.AsChar(), VRSDScriptSymbol::SYMBOL_CLASS);
                break;

              case LUA_TUSERDATA:
                sValueBuffer.Format("userdata:0x%p", lua_touserdata(m_pLuaState, -1));
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), sValueBuffer.AsChar(), VRSDScriptSymbol::SYMBOL_USERDATA);
                break;
            
              default:
                AddSymbol(SubSymbols, SubSymbolCount, sKeyBuffer.AsChar(), "unknown", VRSDScriptSymbol::SYMBOL_STRING);
                break;
            }
          }
        }

        // remove the value, keep the key for the next iteration
        lua_pop(m_pLuaState, 1);                        //stack: .., localX, {field}, key, TOP
      }
      return true;
    }

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

  return true;
}
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;
}