Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8045 in orxonox.OLD for branches/script_engine


Ignore:
Timestamp:
May 31, 2006, 10:20:16 PM (18 years ago)
Author:
snellen
Message:

added some security checks

Location:
branches/script_engine/src/lib/script_engine
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/script_engine/src/lib/script_engine/Script.cc

    r8044 r8045  
    44Script::Script()
    55{
    6 
    76  returnCount = argumentCount = 0;
    87
     
    1514  luaopen_math(luaState);
    1615  luaopen_debug(luaState);
    17 
    1816
    1917}
     
    3028 {
    3129
     30   if(currentFile.length() != 0)
     31     printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str());
     32
    3233   int error = luaL_loadfile (luaState, filename.c_str());
    3334
     
    3738
    3839     if(error == 0)
     40     {
     41      currentFile = filename;
    3942      return true;
     43     }
    4044     else
    4145     {
    4246      reportError(error);
    43       return false;
    4447     }
     48
    4549   }
    4650   else
     
    5458 bool Script::executeFile()
    5559 {
    56    int error = lua_pcall(luaState, 0, LUA_MULTRET, 0);
    57    if( error== 0)
    58      return true;
    59    else
     60   if(currentFile.length() != 0)
    6061   {
    61      reportError(error);
    62      return false;
     62    int error = lua_pcall(luaState, 0, 0, 0);
     63    if( error == 0)
     64      return true;
     65     else
     66      {
     67       reportError(error);
     68       return false;
     69      }
    6370   }
    64 
     71   return false;
    6572 }
    6673
    67  void Script::selectFunction(std::string& functionName, int retCount)
     74 bool Script::selectFunction(std::string& functionName, int retCount)
    6875 {
     76
    6977   returnCount = retCount;
    7078   argumentCount = 0;
     
    7280   lua_pushlstring(luaState, currentFunction.c_str(), currentFunction.size() );
    7381   lua_gettable(luaState, LUA_GLOBALSINDEX);
     82
     83  /* if(lua_isfunction( luaState , -1))
     84   {
     85     return true;
     86   }
     87   else
     88   return false;*/
     89   return true;
    7490 }
    7591
    7692 //return number of returned values
    77  void Script::executeFunction()
     93 bool Script::executeFunction()
    7894 {
    79     lua_pcall(luaState, argumentCount, returnCount,0);
     95   if(currentFunction.length() != 0 )
     96   {
     97    int error = lua_pcall(luaState, argumentCount, returnCount,0);
     98    if(error != 0)
     99    {
     100     reportError(error);
     101     return false;
     102    }
     103    else
     104    {
     105      currentFunction.assign("");//a function gets unusable after beeing called for the first time
     106      returnCount = argumentCount = 0;
     107      return true;
     108    }
     109   }
     110   else
     111     printf("Error: no function selected.\n");
    80112 }
    81113
     
    115147 }
    116148
     149 bool Script::pushParam(double param, std::string& toFunction)
     150 {
     151   if(currentFunction.compare(toFunction) == 0)
     152   {
     153     lua_pushnumber(luaState,(lua_Number) param);
     154     argumentCount++;
     155     return true;
     156   }
     157   else
     158   {
     159     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
     160     return false;
     161   }
     162
     163 }
     164
    117165 int Script::reportError(int error)
    118166 {
  • branches/script_engine/src/lib/script_engine/Script.h

    r8044 r8045  
    2020    lua_State* getLuaState() const{return luaState;}
    2121
    22     void selectFunction(std::string& functionName, int retCount);
    23     void  executeFunction(); //return number of returned values
     22    bool selectFunction(std::string& functionName, int retCount);
     23    bool executeFunction();
    2424    bool pushParam(int param, std::string& toFunction);//overload this function to add different types
    2525    bool pushParam(float param, std::string& toFunction);
     26    bool pushParam(double param, std::string& toFunction);
    2627
    2728
     
    3334
    3435    lua_State*         luaState;         //!< The lua_State that the Script works on
     36    std::string        currentFile;      //!< The file that is loaded in the script
    3537    std::string        currentFunction;  //!< The name of the current active function (the one that gets the pushed parameter)
    3638    int                argumentCount;    //!< Number of arguments for the current function
  • branches/script_engine/src/lib/script_engine/account.cc

    r8044 r8045  
    6666    {
    6767      Script script;
     68
     69      // Register classes with the script
    6870      Lunar<Account>::Register(&script);
    6971      Lunar<Object>::Register(&script);
     
    7375      Account *b = new Account(30);
    7476
     77      // Add instances to the script
    7578      Lunar<Object>::insertObject(&script,obj,"Obj",true);
    7679      Lunar<Account>::insertObject(&script,&a,"a",false);
     
    8184
    8285      if(script.loadFile(file))
    83         printf("file %s succefully loaded\n", file.c_str());
     86        printf("File %s succefully loaded\n", file.c_str());
    8487
    8588      //script.executeFile();
    8689
    8790      std::string main("main");
    88       script.selectFunction(main,1);
    89       script.pushParam(3,main);
     91      script.selectFunction(main,0);
     92      script.pushParam(3.14159,main);
    9093      script.executeFunction();
    9194
Note: See TracChangeset for help on using the changeset viewer.