Beispiel #1
0
static int setVar(StackHandler & sa,VarRef * vr,void * data) {
  if (vr->access & (VAR_ACCESS_READ_ONLY|VAR_ACCESS_CONSTANT)) {
    ScriptStringVar256 msg;
    const SQChar * el = sa.GetString(2);
    SCSNPRINTF(msg.s,sizeof(msg),_T("setVar(): Cannot write to constant: %s"),el);
#if defined( MONASQ )
    return sa.ThrowError(msg.s);
#else
    throw SquirrelError(msg.s);
#endif
  } // if
  switch (vr->type) {
  case TypeInfo<INT>::TypeID: {
    INT * val = (INT *)data; // Address
    if (val) {
      *val = sa.GetInt(3);
      return sa.Return(*val);
    } // if
    break;
  } // case
  case TypeInfo<FLOAT>::TypeID: {
    FLOAT * val = (FLOAT *)data; // Address
    if (val) {
      *val = sa.GetFloat(3);
      return sa.Return(*val);
    } // if
    break;
  } // case
  case TypeInfo<bool>::TypeID: {
    bool * val = (bool *)data; // Address
    if (val) {
      *val = sa.GetBool(3) ? true : false;
      return sa.Return(*val);
    } // if
    break;
  } // case
  case VAR_TYPE_INSTANCE: {
    HSQUIRRELVM v = sa.GetVMPtr();
    // vr->copyFunc is the LHS variable type: the RHS var's type is ClassType<>::type() (both point to ClassType<>::copy()).
    // src will be null if the LHS and RHS types don't match.
    SQUserPointer src = sa.GetInstanceUp(3,(SQUserPointer)vr->copyFunc); // Effectively performs: ClassType<>::type() == ClassType<>getCopyFunc().
#if defined( MONASQ )
    if (!src) return sa.ThrowError(_T("INSTANCE type assignment mismatch"));
#else
    if (!src) throw SquirrelError(_T("INSTANCE type assignment mismatch"));
#endif
    vr->copyFunc(data,src);
#if 0 // Return an instance on the stack (allocates memory)
    if (!CreateNativeClassInstance(sa.GetVMPtr(),vr->typeName,data,0)) { // data = address
      ScriptStringVar256 msg;
      SCSNPRINTF(msg.s,sizeof(msg),_T("getVar(): Could not create instance: %s"),vr->typeName);
      throw SquirrelError(msg.s);
    } // if
    return 1;
#else // Don't return on stack.
    return 0;
#endif
  }
  case TypeInfo<SQUserPointer>::TypeID: {
    ScriptStringVar256 msg;
    const SQChar * el = sa.GetString(2);
    SCSNPRINTF(msg.s,sizeof(msg),_T("setVar(): Cannot write to an SQUserPointer: %s"),el);
#if defined( MONASQ )
    return sa.ThrowError(msg.s);
#else
    throw SquirrelError(msg.s);
#endif
  } // case
  case TypeInfo<ScriptStringVarBase>::TypeID: {
    ScriptStringVarBase * val = (ScriptStringVarBase *)data; // Address
    if (val) {
      const SQChar * strVal = sa.GetString(3);
      if (strVal) {
        *val = strVal;
        return sa.Return(val->s);
      } // if
    } // if
    break;
  } // case
  } // switch
  return SQ_ERROR;
} // setVar
Beispiel #2
0
static int getVar(StackHandler & sa,VarRef * vr,void * data) {
  switch (vr->m_type) {
  case TypeInfo<INT>::TypeID: {
    if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
      if (data) {
          INT v;
          // Support for different int sizes
          switch( vr->m_size ){
            case 1: v = *(char*)data; break;  
            case 2: v = *(short*)data; break;  
#ifdef _SQ64
            case 4: v = *(int*)data; break;
#endif            
            default: v = *(INT*)data;
          }
          return sa.Return(v);
      } // if
    } else {
      INT * val = (INT *)&data; // Constant value
      return sa.Return(*val);
    } // if
    break;
  } // case
  case TypeInfo<unsigned>::TypeID: {
	  if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
		  unsigned * val = (unsigned *)data; // Address
		  if (val){
            return sa.Return(static_cast<INT>(*val));
          }
	  } else {
		  unsigned * val = (unsigned *)&data; // Constant value
		  return sa.Return(static_cast<INT>(*val));
	  } // if
	  break;
  } // case
  case TypeInfo<FLOAT>::TypeID: {
    if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
      FLOAT * val = (FLOAT *)data; // Address
      if (val) {
        return sa.Return(*val);
      } // if
    } else {
      FLOAT * val = (FLOAT *)&data; // Constant value
      return sa.Return(*val);
    } // if
    break;
  } // case
  case TypeInfo<bool>::TypeID: {
    if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
      bool * val = (bool *)data; // Address
      if (val) {
        return sa.Return(*val);
      } // if
    } else {
      bool * val = (bool *)&data; // Constant value
      return sa.Return(*val);
    } // if
    break;
  } // case
  case VAR_TYPE_INSTANCE:
    if (!CreateNativeClassInstance(sa.GetVMPtr(),vr->varType->GetTypeName(),data,0)) { // data = address. Allocates memory.
      SCSNPRINTF(g_msg_throw.s,sizeof(g_msg_throw),_SC("getVar(): Could not create instance: %s"),vr->varType->GetTypeName());
      throw SquirrelError(g_msg_throw.s);
    } // if
    return 1;
  case TypeInfo<SQUserPointer>::TypeID: 
    return sa.Return(data); // The address of member variable, not the variable itself.
  case TypeInfo<ScriptStringVarBase>::TypeID: {
    if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
      ScriptStringVarBase * val = (ScriptStringVarBase *)data; // Address
      if (val) {
        return sa.Return(val->s);
      } // if
    } else {
      throw SquirrelError(_SC("getVar(): Invalid type+access: 'ScriptStringVarBase' with VAR_ACCESS_CONSTANT (use VAR_ACCESS_READ_ONLY instead)"));
    }
    break;
  } // case
  case TypeInfo<const SQChar *>::TypeID: {
    if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
      if( vr->m_access==VAR_ACCESS_READ_WRITE )
        throw SquirrelError(_SC("getVar(): Invalid type+access: 'const SQChar *' without VAR_ACCESS_CONSTANT"));
      // It is OK to read from a SQChar* if requested
      return sa.Return(*(const SQChar **)data); // Address
    } else {
      return sa.Return((const SQChar *)data); // Address
    }
    break;
  } // case
#ifdef SQPLUS_SUPPORT_STD_STRING
  case TypeInfo<std::string>::TypeID: {
    if (!(vr->m_access & VAR_ACCESS_CONSTANT)) {
      std::string *val = (std::string *)data; // Address
      if (val) {
        return sa.Return(val->c_str());
      }
    } else {
      throw SquirrelError(_SC("getVar(): Invalid type+access: 'std::string' with VAR_ACCESS_CONSTANT (use VAR_ACCESS_READ_ONLY instead)"));
    }
    break;
  } // case
#endif
  } // switch
  return SQ_ERROR;
} // getVar
Beispiel #3
0
static int getVar(StackHandler & sa,VarRef * vr,void * data) {
  switch (vr->type) {
  case TypeInfo<INT>::TypeID: {
    if (!(vr->access & VAR_ACCESS_CONSTANT)) {
      INT * val = (INT *)data; // Address
      if (val) {
        return sa.Return(*val);
      } // if
    } else {
      INT * val = (INT *)&data; // Constant value
      return sa.Return(*val);
    } // if
    break;
  } // case
  case TypeInfo<FLOAT>::TypeID: {
    if (!(vr->access & VAR_ACCESS_CONSTANT)) {
      FLOAT * val = (FLOAT *)data; // Address
      if (val) {
        return sa.Return(*val);
      } // if
    } else {
      FLOAT * val = (FLOAT *)&data; // Constant value
      return sa.Return(*val);
    } // if
    break;
  } // case
  case TypeInfo<bool>::TypeID: {
    if (!(vr->access & VAR_ACCESS_CONSTANT)) {
      bool * val = (bool *)data; // Address
      if (val) {
        return sa.Return(*val);
      } // if
    } else {
      bool * val = (bool *)&data; // Constant value
      return sa.Return(*val);
    } // if
    break;
  } // case
  case VAR_TYPE_INSTANCE:
    if (!CreateNativeClassInstance(sa.GetVMPtr(),vr->typeName,data,0)) { // data = address. Allocates memory.
      ScriptStringVar256 msg;
      SCSNPRINTF(msg.s,sizeof(msg),_T("getVar(): Could not create instance: %s"),vr->typeName);
#if defined( MONASQ )
      return sa.ThrowError(msg.s);
#else
      throw SquirrelError(msg.s);
#endif
    } // if
    return 1;
  case TypeInfo<SQUserPointer>::TypeID: {
    return sa.Return(data); // The address of member variable, not the variable itself.
  } // case
  case TypeInfo<ScriptStringVarBase>::TypeID: {
    if (!(vr->access & VAR_ACCESS_CONSTANT)) {
      ScriptStringVarBase * val = (ScriptStringVarBase *)data; // Address
      if (val) {
        return sa.Return(val->s);
      } // if
    } else {
#if defined( MONASQ )
      return sa.ThrowError(_T("getVar(): Invalid type+access: 'ScriptStringVarBase' with VAR_ACCESS_CONSTANT (use VAR_ACCESS_READ_ONLY instead)"));
#else
      throw SquirrelError(_T("getVar(): Invalid type+access: 'ScriptStringVarBase' with VAR_ACCESS_CONSTANT (use VAR_ACCESS_READ_ONLY instead)"));
#endif
    } // if
    break;
  } // case
  case TypeInfo<const SQChar *>::TypeID: {
    if (!(vr->access & VAR_ACCESS_CONSTANT)) {
#if defined( MONASQ )
      return sa.ThrowError(_T("getVar(): Invalid type+access: 'const SQChar *' without VAR_ACCESS_CONSTANT"));
#else
      throw SquirrelError(_T("getVar(): Invalid type+access: 'const SQChar *' without VAR_ACCESS_CONSTANT"));
#endif
    } else {
      return sa.Return((const SQChar *)data); // Address
    } // if
    break;
  } // case
  } // switch
  return SQ_ERROR;
} // getVar
static BOOL __CreateRefCountedInstance(HSQUIRRELVM v,const SQChar *classname,IUnknown *pRC,SQRELEASEHOOK hook)
{
    if(!CreateNativeClassInstance(v,classname,pRC,hook)) return FALSE;
    return TRUE;
}