Example #1
0
static void PrintConstant(const LuaProto* f, int i)
{
  LuaValue v = f->constants[i];
  if(v.isNil()) {
    printf("nil");
    return;
  }

  if(v.isBool()) {
    printf(v.getBool() ? "true" : "false");
    return;
  }

  if(v.isNumber()) {
    printf(LUA_NUMBER_FMT,v.getNumber());
    return;
  }

  if(v.isString()) {
    PrintString(v.getString());
    return;
  }

  printf("? type=%d",v.type());
}
Example #2
0
void LuaTable::set(const LuaValue& k, const LuaValue& v, bool raw) {
    if (!raw && m_metaTable != NULL && !v.isTypeOf(LVT_Nil)) {
        if (!hasKey(k)) {
            LuaValue f = m_metaTable->get(LuaValue("__newindex"));
            if (f.isTypeOf(LVT_Nil)) {}
            else if (f.isTypeOf(LVT_Table)) {
                f.getTable()->set(k, v);
                return;
            } else if (f.isTypeOf(LVT_Function)) {
                vector<LuaValue> args, rets;
                addRef();
                args.push_back(LuaValue(this)); args.push_back(k); args.push_back(v);
                f.getFunction()->call(args, rets);
                return;
            } else ASSERT(0);
        }
    }

    if (k.isTypeOf(LVT_Number)) {
        int idx = (int)k.getNumber() - 1;
        if (idx >= 0 && idx < (int)m_vec.size()) {
            m_vec[idx] = v;
            return;
        }
        if (idx == (int)m_vec.size()) {
            m_vec.push_back(v);
            return;
        }
    }
    if (v.isTypeOf(LVT_Nil)) {
        m_hashTable.erase(k);
    } else m_hashTable[k] = v;
    return;
}
Example #3
0
bool LuaTable::hasKey(const LuaValue& k) const {
    if (k.isTypeOf(LVT_Number)) {
        int idx = (int)k.getNumber() - 1;
        if (idx >= 0 && idx < (int)m_vec.size()) return true;
    } 
    return m_hashTable.find(k) != m_hashTable.end();
}
Example #4
0
LuaValue LuaTable::get(const LuaValue& k, bool raw) const {
    if (k.isTypeOf(LVT_Number)) {
        int idx = (int)k.getNumber() - 1;
        if (idx >= 0 && idx < (int)m_vec.size()) return m_vec[idx];
    }
    auto iter = m_hashTable.find(k);
    if (iter != m_hashTable.end()) return iter->second;

    if (!raw && m_metaTable != NULL) {
        LuaValue f = m_metaTable->get(LuaValue("__index"));
        if (f.isTypeOf(LVT_Nil)) {}
        else if (f.isTypeOf(LVT_Table)) return f.getTable()->get(k);
        else if (f.isTypeOf(LVT_Function)) {
            vector<LuaValue> args, rets;
            {
                auto t = const_cast<LuaTable*>(this);
                t->addRef();
                args.push_back(LuaValue(t)); args.push_back(k);
            }
            f.getFunction()->call(args, rets);
            if (!rets.empty()) return rets[0];
        } else ASSERT(0);
    }

    return LuaValue::NIL;
}
Example #5
0
static void DumpConstants(const LuaProto* f, DumpState* D)
{
  int n = (int)f->constants.size();
  DumpInt(n,D);
  for(int i=0; i < n; i++)
  {
    LuaValue v = f->constants[i];
    DumpChar(v.type(),D);

    if(v.isBool()) {
      DumpChar(v.getBool() ? 1 : 0,D);
    } else if(v.isNumber()) {
      DumpNumber(v.getNumber(),D);
    } else if(v.isString()) {
      DumpString(v.getString(),D);
    }
  }
  n = (int)f->subprotos_.size();
  DumpInt(n,D);
  for (int i=0; i < n; i++) {
    DumpFunction(f->subprotos_[i],D);
  }
}