Commit 9046dc1a authored by Kenton Varda's avatar Kenton Varda

Support AnyPointer constant values.

The trick here is that you must specify the value as a separate constant with a defined type. Then, you can reference that constant where an AnyPointer is expected.

Eventually we should maybe support some sort of inline syntax that specifies a type explicitly...
parent 08295ff2
...@@ -2726,6 +2726,15 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader ...@@ -2726,6 +2726,15 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader
if (result.getReader().as<DynamicList>().getSchema() == type.asList()) { if (result.getReader().as<DynamicList>().getSchema() == type.asList()) {
return kj::mv(result); return kj::mv(result);
} }
} else if (type.isAnyPointer()) {
switch (type.whichAnyPointerKind()) {
case schema::Type::AnyPointer::Unconstrained::ANY_KIND:
case schema::Type::AnyPointer::Unconstrained::LIST:
return kj::mv(result);
case schema::Type::AnyPointer::Unconstrained::STRUCT:
case schema::Type::AnyPointer::Unconstrained::CAPABILITY:
break;
}
} }
break; break;
...@@ -2742,6 +2751,15 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader ...@@ -2742,6 +2751,15 @@ kj::Maybe<Orphan<DynamicValue>> ValueTranslator::compileValue(Expression::Reader
if (result.getReader().as<DynamicStruct>().getSchema() == type.asStruct()) { if (result.getReader().as<DynamicStruct>().getSchema() == type.asStruct()) {
return kj::mv(result); return kj::mv(result);
} }
} else if (type.isAnyPointer()) {
switch (type.whichAnyPointerKind()) {
case schema::Type::AnyPointer::Unconstrained::ANY_KIND:
case schema::Type::AnyPointer::Unconstrained::STRUCT:
return kj::mv(result);
case schema::Type::AnyPointer::Unconstrained::LIST:
case schema::Type::AnyPointer::Unconstrained::CAPABILITY:
break;
}
} }
break; break;
......
...@@ -1640,6 +1640,16 @@ TEST(Encoding, Constants) { ...@@ -1640,6 +1640,16 @@ TEST(Encoding, Constants) {
checkList(*test::TestConstants::ENUM_LIST_CONST, {TestEnum::FOO, TestEnum::GARPLY}); checkList(*test::TestConstants::ENUM_LIST_CONST, {TestEnum::FOO, TestEnum::GARPLY});
} }
TEST(Encoding, AnyPointerConstants) {
auto reader = test::ANY_POINTER_CONSTANTS.get();
EXPECT_EQ("baz", reader.getAnyKindAsStruct().getAs<TestAllTypes>().getTextField());
EXPECT_EQ("baz", reader.getAnyStructAsStruct().as<TestAllTypes>().getTextField());
EXPECT_EQ(111111111, reader.getAnyKindAsList().getAs<List<int32_t>>()[0]);
EXPECT_EQ(111111111, reader.getAnyListAsList().as<List<int32_t>>()[0]);
}
TEST(Encoding, GlobalConstants) { TEST(Encoding, GlobalConstants) {
EXPECT_EQ(12345u, test::GLOBAL_INT); EXPECT_EQ(12345u, test::GLOBAL_INT);
EXPECT_EQ("foobar", test::GLOBAL_TEXT.get()); EXPECT_EQ("foobar", test::GLOBAL_TEXT.get());
......
...@@ -743,6 +743,21 @@ const embeddedData :Data = embed "testdata/packed"; ...@@ -743,6 +743,21 @@ const embeddedData :Data = embed "testdata/packed";
const embeddedText :Text = embed "testdata/short.txt"; const embeddedText :Text = embed "testdata/short.txt";
const embeddedStruct :TestAllTypes = embed "testdata/binary"; const embeddedStruct :TestAllTypes = embed "testdata/binary";
struct TestAnyPointerConstants {
anyKindAsStruct @0 :AnyPointer;
anyStructAsStruct @1 :AnyStruct;
anyKindAsList @2 :AnyPointer;
anyListAsList @3 :AnyList;
}
const anyPointerConstants :TestAnyPointerConstants = (
anyKindAsStruct = TestConstants.structConst,
anyStructAsStruct = TestConstants.structConst,
anyKindAsList = TestConstants.int32ListConst,
anyListAsList = TestConstants.int32ListConst,
);
interface TestInterface { interface TestInterface {
foo @0 (i :UInt32, j :Bool) -> (x :Text); foo @0 (i :UInt32, j :Bool) -> (x :Text);
bar @1 () -> (); bar @1 () -> ();
......
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