X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FInterpreter.cpp;h=fc428cd8365e998ea37128ef23adf2ede9dbb716;hb=8c8061a4f8b88410d6d93c039afe6affc4b69cf2;hp=8b54fc177913de18a1266f885cc8e959b6eb4eef;hpb=0f30d8254ff8b9e63795960ec031577cf68fbf95;p=l2e.git diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index 8b54fc1..fc428cd 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -68,20 +68,64 @@ const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &id } } +const Interpreter::ParsedDefinition &Interpreter::GetDefinition(const string &identifier) const { + std::map::const_iterator i(parsedDefinitions.find(identifier)); + if (i != parsedDefinitions.end()) { + return i->second; + } else { + throw Error("access to undefined object " + identifier); + } +} + -void *Interpreter::GetObject(int typeId, const std::string &name) { - std::map::const_iterator i(parsedDefinitions.find(name)); +void *Interpreter::GetObject( + int typeId, + const std::string &name) { + std::map::const_iterator + i(parsedDefinitions.find(name)); if (i != parsedDefinitions.end()) { - const TypeDescription &requested(TypeDescription::Get(typeId)); - const TypeDescription &actual(TypeDescription::Get(i->second.type)); + const TypeDescription &requested = + TypeDescription::Get(typeId); + const TypeDescription &actual = + TypeDescription::Get(i->second.type); if (requested.TypeId() == actual.TypeId()) { return values[actual.TypeId()][i->second.id]; } else if (actual.IsSubtypeOf(requested)) { - char *sub(reinterpret_cast(values[actual.TypeId()][i->second.id])); - std::ptrdiff_t offset(actual.SupertypeOffset(requested)); + char *sub = reinterpret_cast( + values[actual.TypeId()][i->second.id]); + std::ptrdiff_t offset = + actual.SupertypeOffset(requested); + return sub - offset; + } else { + throw Error("cannot cast " + actual.TypeName() + + " to " + requested.TypeName()); + } + } else { + throw Error("access to undefined object " + name); + } +} + +const void *Interpreter::GetObject( + int typeId, + const std::string &name) const { + std::map::const_iterator + i(parsedDefinitions.find(name)); + if (i != parsedDefinitions.end()) { + const TypeDescription &requested = + TypeDescription::Get(typeId); + const TypeDescription &actual = + TypeDescription::Get(i->second.type); + if (requested.TypeId() == actual.TypeId()) { + return values.at(actual.TypeId()).at(i->second.id); + } else if (actual.IsSubtypeOf(requested)) { + char *sub = reinterpret_cast( + values.at(actual.TypeId()).at(i->second.id)); + std::ptrdiff_t offset = + actual.SupertypeOffset(requested); return sub - offset; } else { - throw Error("cannot cast " + actual.TypeName() + " to " + requested.TypeName()); + throw Error("cannot cast " + actual.TypeName() + + " to " + requested.TypeName()); } } else { throw Error("access to undefined object " + name); @@ -287,11 +331,16 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis if (fd.IsAggregate()) { int arraySize(i->second->GetLiteral().ArraySize()); size_t memberSize = fd.IsReferenced() ? sizeof(char *) : fieldType.Size(); - char *aggregate = alloc.Alloc(arraySize * memberSize); - char *iter = aggregate; + Array array; + array.size = arraySize * memberSize; + array.data = alloc.Alloc(array.size); + array.ref = fd.IsReferenced(); + arrays.push_back(array); + char *iter = reinterpret_cast(array.data); if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) { const vector &list(i->second->GetLiteral().GetPropertyLists()); - for (vector::const_iterator j(list.begin()), end(list.end()); + for (vector::const_iterator + j(list.begin()), end(list.end()); j != end; ++j, iter += memberSize) { char *member; if (fd.IsReferenced()) { @@ -329,11 +378,12 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis *reinterpret_cast(iter) = GetObject(fd.TypeId(), *j); } else { - Postpone(iter, *j, fd.TypeId(), false); + Postpone(reinterpret_cast(array.data), + iter, *j, fd.TypeId(), false); } } } - std::memcpy(dest, &aggregate, sizeof(char *)); + std::memcpy(dest, &array.data, sizeof(char *)); dest += sizeof(char *); std::memcpy(dest, &arraySize, sizeof(int)); } else if (i->second->IsLiteral() && !fd.IsReferenced()) { @@ -355,7 +405,7 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis } } } else { - Postpone(object, i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate()); + Postpone(object, object + fd.Offset(), i->second->GetIdentifier(), fd.TypeId(), !fd.IsReferenced(), fd.IsAggregate()); } } td.Load(object); @@ -861,16 +911,14 @@ bool Interpreter::CanLink(const Value &v) const { } void Interpreter::Postpone( + char *object, char *dest, const std::string &identifier, int type, bool inlined, bool aggregate) { - char *str(alloc.Alloc(identifier.size() + 1)); - std::memcpy(str, identifier.c_str(), identifier.size()); - str[identifier.size()] = '\0'; postponedDefinitions.push_back( - PostponedDefinition(dest, str, type, inlined, aggregate)); + PostponedDefinition(object, dest, identifier, type, inlined, aggregate)); }