Commit 07b979a3 authored by Kenton Varda's avatar Kenton Varda

Test Dynamic API.

parent 08d73bef
...@@ -49,6 +49,8 @@ includecapnp_HEADERS = \ ...@@ -49,6 +49,8 @@ includecapnp_HEADERS = \
src/capnproto/layout.h \ src/capnproto/layout.h \
src/capnproto/list.h \ src/capnproto/list.h \
src/capnproto/message.h \ src/capnproto/message.h \
src/capnproto/schema.h \
src/capnproto/dynamic.h \
src/capnproto/io.h \ src/capnproto/io.h \
src/capnproto/serialize.h \ src/capnproto/serialize.h \
src/capnproto/serialize-packed.h \ src/capnproto/serialize-packed.h \
...@@ -73,6 +75,8 @@ libcapnproto_a_SOURCES= \ ...@@ -73,6 +75,8 @@ libcapnproto_a_SOURCES= \
src/capnproto/layout.c++ \ src/capnproto/layout.c++ \
src/capnproto/list.c++ \ src/capnproto/list.c++ \
src/capnproto/message.c++ \ src/capnproto/message.c++ \
src/capnproto/schema.c++ \
src/capnproto/dynamic.c++ \
src/capnproto/io.c++ \ src/capnproto/io.c++ \
src/capnproto/serialize.c++ \ src/capnproto/serialize.c++ \
src/capnproto/serialize-packed.c++ src/capnproto/serialize-packed.c++
...@@ -117,6 +121,8 @@ capnproto_test_SOURCES = \ ...@@ -117,6 +121,8 @@ capnproto_test_SOURCES = \
src/capnproto/logging-test.c++ \ src/capnproto/logging-test.c++ \
src/capnproto/layout-test.c++ \ src/capnproto/layout-test.c++ \
src/capnproto/message-test.c++ \ src/capnproto/message-test.c++ \
src/capnproto/schema-test.c++ \
src/capnproto/dynamic-test.c++ \
src/capnproto/encoding-test.c++ \ src/capnproto/encoding-test.c++ \
src/capnproto/serialize-test.c++ \ src/capnproto/serialize-test.c++ \
src/capnproto/serialize-packed-test.c++ \ src/capnproto/serialize-packed-test.c++ \
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -427,7 +427,8 @@ struct WireHelpers { ...@@ -427,7 +427,8 @@ struct WireHelpers {
word* ptr; word* ptr;
if (ref->isNull()) { if (ref->isNull()) {
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
ptr = allocate(ref, segment, size.total(), WireReference::STRUCT); ptr = allocate(ref, segment, size.total(), WireReference::STRUCT);
ref->structRef.set(size); ref->structRef.set(size);
} else { } else {
...@@ -508,7 +509,8 @@ struct WireHelpers { ...@@ -508,7 +509,8 @@ struct WireHelpers {
word* ptr; word* ptr;
if (ref->isNull()) { if (ref->isNull()) {
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
return ListBuilder(); return ListBuilder();
} }
ptr = copyMessage(segment, ref, defaultRef); ptr = copyMessage(segment, ref, defaultRef);
...@@ -619,7 +621,8 @@ struct WireHelpers { ...@@ -619,7 +621,8 @@ struct WireHelpers {
word* ptr; word* ptr;
if (ref->isNull()) { if (ref->isNull()) {
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
return ObjectBuilder(); return ObjectBuilder();
} else { } else {
ptr = copyMessage(segment, ref, reinterpret_cast<const WireReference*>(defaultValue)); ptr = copyMessage(segment, ref, reinterpret_cast<const WireReference*>(defaultValue));
...@@ -668,7 +671,8 @@ struct WireHelpers { ...@@ -668,7 +671,8 @@ struct WireHelpers {
if (ref == nullptr || ref->isNull()) { if (ref == nullptr || ref->isNull()) {
useDefault: useDefault:
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
return StructReader(nullptr, nullptr, nullptr, 0 * BITS, 0 * REFERENCES, 0 * BITS, return StructReader(nullptr, nullptr, nullptr, 0 * BITS, 0 * REFERENCES, 0 * BITS,
std::numeric_limits<int>::max()); std::numeric_limits<int>::max());
} }
...@@ -714,7 +718,8 @@ struct WireHelpers { ...@@ -714,7 +718,8 @@ struct WireHelpers {
const word* ptr; const word* ptr;
if (ref == nullptr || ref->isNull()) { if (ref == nullptr || ref->isNull()) {
useDefault: useDefault:
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
return ListReader(); return ListReader();
} }
segment = nullptr; segment = nullptr;
...@@ -874,7 +879,8 @@ struct WireHelpers { ...@@ -874,7 +879,8 @@ struct WireHelpers {
const void* defaultValue, ByteCount defaultSize)) { const void* defaultValue, ByteCount defaultSize)) {
if (ref == nullptr || ref->isNull()) { if (ref == nullptr || ref->isNull()) {
useDefault: useDefault:
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
defaultValue = ""; defaultValue = "";
} }
return Text::Reader(reinterpret_cast<const char*>(defaultValue), defaultSize / BYTES); return Text::Reader(reinterpret_cast<const char*>(defaultValue), defaultSize / BYTES);
...@@ -973,7 +979,8 @@ struct WireHelpers { ...@@ -973,7 +979,8 @@ struct WireHelpers {
const word* ptr; const word* ptr;
if (ref == nullptr || ref->isNull()) { if (ref == nullptr || ref->isNull()) {
useDefault: useDefault:
if (defaultValue == nullptr) { if (defaultValue == nullptr ||
reinterpret_cast<const WireReference*>(defaultValue)->isNull()) {
return ObjectReader(); return ObjectReader();
} }
segment = nullptr; segment = nullptr;
......
...@@ -647,7 +647,7 @@ struct ObjectBuilder { ...@@ -647,7 +647,7 @@ struct ObjectBuilder {
ObjectBuilder(): kind(ObjectKind::NULL_POINTER), structBuilder() {} ObjectBuilder(): kind(ObjectKind::NULL_POINTER), structBuilder() {}
ObjectBuilder(StructBuilder structBuilder) ObjectBuilder(StructBuilder structBuilder)
: kind(ObjectKind::STRUCT), structBuilder(structBuilder) {} : kind(ObjectKind::STRUCT), structBuilder(structBuilder) {}
ObjectBuilder(ListBuilder listBuilderBuilder) ObjectBuilder(ListBuilder listBuilder)
: kind(ObjectKind::LIST), listBuilder(listBuilder) {} : kind(ObjectKind::LIST), listBuilder(listBuilder) {}
}; };
......
...@@ -122,6 +122,12 @@ Maybe<StructSchema::Member> StructSchema::findMemberByName(Text::Reader name) co ...@@ -122,6 +122,12 @@ Maybe<StructSchema::Member> StructSchema::findMemberByName(Text::Reader name) co
return findSchemaMemberByName(raw, name, 0, getMembers()); return findSchemaMemberByName(raw, name, 0, getMembers());
} }
StructSchema::Member StructSchema::getMemberByName(Text::Reader name) const {
Maybe<StructSchema::Member> member = findMemberByName(name);
PRECOND(member != nullptr, "struct has no such member", name);
return *member;
}
Maybe<StructSchema::Union> StructSchema::Member::getContainingUnion() const { Maybe<StructSchema::Union> StructSchema::Member::getContainingUnion() const {
if (unionIndex == 0) return nullptr; if (unionIndex == 0) return nullptr;
return parent.getMembers()[unionIndex - 1].asUnion(); return parent.getMembers()[unionIndex - 1].asUnion();
...@@ -142,6 +148,12 @@ Maybe<StructSchema::Member> StructSchema::Union::findMemberByName(Text::Reader n ...@@ -142,6 +148,12 @@ Maybe<StructSchema::Member> StructSchema::Union::findMemberByName(Text::Reader n
return findSchemaMemberByName(parent.raw, name, index + 1, getMembers()); return findSchemaMemberByName(parent.raw, name, index + 1, getMembers());
} }
StructSchema::Member StructSchema::Union::getMemberByName(Text::Reader name) const {
Maybe<StructSchema::Member> member = findMemberByName(name);
PRECOND(member != nullptr, "struct has no such member", name);
return *member;
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
EnumSchema::EnumerantList EnumSchema::getEnumerants() const { EnumSchema::EnumerantList EnumSchema::getEnumerants() const {
...@@ -152,6 +164,12 @@ Maybe<EnumSchema::Enumerant> EnumSchema::findEnumerantByName(Text::Reader name) ...@@ -152,6 +164,12 @@ Maybe<EnumSchema::Enumerant> EnumSchema::findEnumerantByName(Text::Reader name)
return findSchemaMemberByName(raw, name, 0, getEnumerants()); return findSchemaMemberByName(raw, name, 0, getEnumerants());
} }
EnumSchema::Enumerant EnumSchema::getEnumerantByName(Text::Reader name) const {
Maybe<EnumSchema::Enumerant> enumerant = findEnumerantByName(name);
PRECOND(enumerant != nullptr, "enum has no such enumerant", name);
return *enumerant;
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
InterfaceSchema::MethodList InterfaceSchema::getMethods() const { InterfaceSchema::MethodList InterfaceSchema::getMethods() const {
...@@ -162,6 +180,12 @@ Maybe<InterfaceSchema::Method> InterfaceSchema::findMethodByName(Text::Reader na ...@@ -162,6 +180,12 @@ Maybe<InterfaceSchema::Method> InterfaceSchema::findMethodByName(Text::Reader na
return findSchemaMemberByName(raw, name, 0, getMethods()); return findSchemaMemberByName(raw, name, 0, getMethods());
} }
InterfaceSchema::Method InterfaceSchema::getMethodByName(Text::Reader name) const {
Maybe<InterfaceSchema::Method> method = findMethodByName(name);
PRECOND(method != nullptr, "interface has no such method", name);
return *method;
}
// ======================================================================================= // =======================================================================================
ListSchema ListSchema::of(schema::Type::Body::Which primitiveType) { ListSchema ListSchema::of(schema::Type::Body::Which primitiveType) {
......
...@@ -100,8 +100,12 @@ public: ...@@ -100,8 +100,12 @@ public:
class MemberList; class MemberList;
MemberList getMembers() const; MemberList getMembers() const;
Maybe<Member> findMemberByName(Text::Reader name) const; Maybe<Member> findMemberByName(Text::Reader name) const;
Member getMemberByName(Text::Reader name) const;
// Like findMemberByName() but throws an exception on failure.
private: private:
StructSchema(const internal::RawSchema* raw): Schema(raw) {} StructSchema(const internal::RawSchema* raw): Schema(raw) {}
template <typename T> static inline StructSchema fromImpl() { template <typename T> static inline StructSchema fromImpl() {
...@@ -148,8 +152,12 @@ public: ...@@ -148,8 +152,12 @@ public:
Union() = default; Union() = default;
MemberList getMembers() const; MemberList getMembers() const;
Maybe<Member> findMemberByName(Text::Reader name) const; Maybe<Member> findMemberByName(Text::Reader name) const;
Member getMemberByName(Text::Reader name) const;
// Like findMemberByName() but throws an exception on failure.
private: private:
inline Union(const Member& base): Member(base) {} inline Union(const Member& base): Member(base) {}
...@@ -187,8 +195,12 @@ public: ...@@ -187,8 +195,12 @@ public:
class EnumerantList; class EnumerantList;
EnumerantList getEnumerants() const; EnumerantList getEnumerants() const;
Maybe<Enumerant> findEnumerantByName(Text::Reader name) const; Maybe<Enumerant> findEnumerantByName(Text::Reader name) const;
Enumerant getEnumerantByName(Text::Reader name) const;
// Like findEnumerantByName() but throws an exception on failure.
private: private:
EnumSchema(const internal::RawSchema* raw): Schema(raw) {} EnumSchema(const internal::RawSchema* raw): Schema(raw) {}
template <typename T> static inline EnumSchema fromImpl() { template <typename T> static inline EnumSchema fromImpl() {
...@@ -250,8 +262,12 @@ public: ...@@ -250,8 +262,12 @@ public:
class MethodList; class MethodList;
MethodList getMethods() const; MethodList getMethods() const;
Maybe<Method> findMethodByName(Text::Reader name) const; Maybe<Method> findMethodByName(Text::Reader name) const;
Method getMethodByName(Text::Reader name) const;
// Like findMethodByName() but throws an exception on failure.
private: private:
InterfaceSchema(const internal::RawSchema* raw): Schema(raw) {} InterfaceSchema(const internal::RawSchema* raw): Schema(raw) {}
template <typename T> static inline InterfaceSchema fromImpl() { template <typename T> static inline InterfaceSchema fromImpl() {
......
...@@ -498,7 +498,7 @@ outerFileContext schemaNodes = fileContext where ...@@ -498,7 +498,7 @@ outerFileContext schemaNodes = fileContext where
DescEnum d -> muJust $ enumContext context d DescEnum d -> muJust $ enumContext context d
_ -> muNull _ -> muNull
context "typeSchema" = case desc of context "typeSchema" = case desc of
DescUnion u -> muNull DescUnion _ -> muNull
_ -> muJust $ schemaContext context desc _ -> muJust $ schemaContext context desc
context s = parent s context s = parent s
......
...@@ -523,7 +523,7 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where ...@@ -523,7 +523,7 @@ encodeSchema requestedFiles allFiles = (encRoot, nodesForEmbedding) where
BuiltinType BuiltinFloat64 -> 11 BuiltinType BuiltinFloat64 -> 11
BuiltinType BuiltinText -> 12 BuiltinType BuiltinText -> 12
BuiltinType BuiltinData -> 13 BuiltinType BuiltinData -> 13
BuiltinType BuiltinObject -> 19 BuiltinType BuiltinObject -> 18
ListType _ -> 14 ListType _ -> 14
EnumType _ -> 15 EnumType _ -> 15
StructType _ -> 16 StructType _ -> 16
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment