text constants

This commit is contained in:
David Renshaw 2014-10-02 09:42:13 -04:00
parent 2ed3f5f76c
commit fc717eca19
3 changed files with 10 additions and 15 deletions

View file

@ -1363,7 +1363,6 @@ private:
struct ConstText { struct ConstText {
bool needsSchema; bool needsSchema;
kj::StringTree decl; kj::StringTree decl;
kj::StringTree def;
}; };
ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema) { ConstText makeConstText(kj::StringPtr scope, kj::StringPtr name, ConstSchema schema) {
@ -1393,19 +1392,16 @@ private:
return ConstText { return ConstText {
false, false,
kj::strTree("public static final ", typeName_, ' ', upperCase, " = ", kj::strTree("public static final ", typeName_, ' ', upperCase, " = ",
literalValue(constProto.getType(), constProto.getValue()), ";\n"), literalValue(constProto.getType(), constProto.getValue()), ";\n")
scope.size() == 0 ? kj::strTree() : kj::strTree(
"final ", typeName_, ' ', scope, upperCase, ";\n")
}; };
case schema::Value::TEXT: { case schema::Value::TEXT: {
kj::String constType = kj::strTree(
"::capnp::_::ConstText<", schema.as<Text>().size(), ">").flatten();
return ConstText { return ConstText {
true, true,
kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"), kj::strTree("public static final org.capnproto.Text.Reader ", upperCase,
kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", " = new org.capnproto.Text.Reader(Schemas.b_",
kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(),
", ", constProto.getValue().getText().size(), ");\n")
}; };
} }
@ -1414,7 +1410,6 @@ private:
"::capnp::_::ConstData<", schema.as<Data>().size(), ">").flatten(); "::capnp::_::ConstData<", schema.as<Data>().size(), ">").flatten();
return ConstText { return ConstText {
true, true,
kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"),
kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_",
kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n")
}; };
@ -1425,7 +1420,6 @@ private:
"::capnp::_::ConstStruct<", typeName_, ">").flatten(); "::capnp::_::ConstStruct<", typeName_, ">").flatten();
return ConstText { return ConstText {
true, true,
kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"),
kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_",
kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n")
}; };
@ -1436,7 +1430,6 @@ private:
"::capnp::_::ConstList<", typeName(type.getList().getElementType()), ">").flatten(); "::capnp::_::ConstList<", typeName(type.getList().getElementType()), ">").flatten();
return ConstText { return ConstText {
true, true,
kj::strTree(linkage, "const ", constType, ' ', upperCase, ";\n"),
kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_",
kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n")
}; };
@ -1444,7 +1437,7 @@ private:
case schema::Value::ANY_POINTER: case schema::Value::ANY_POINTER:
case schema::Value::INTERFACE: case schema::Value::INTERFACE:
return ConstText { false, kj::strTree(), kj::strTree() }; return ConstText { false, kj::strTree() };
} }
KJ_UNREACHABLE; KJ_UNREACHABLE;
@ -1683,8 +1676,7 @@ private:
kj::strTree(), kj::strTree(),
kj::strTree(), kj::strTree(),
kj::strTree(),
kj::mv(constText.def),
}; };
} }

View file

@ -84,6 +84,8 @@ class EncodingSuite extends FunSuite {
assert(1234.5f == TestConstants.FLOAT32_CONST); assert(1234.5f == TestConstants.FLOAT32_CONST);
(-123e45) should equal (TestConstants.FLOAT64_CONST); (-123e45) should equal (TestConstants.FLOAT64_CONST);
(TestConstants.TEXT_CONST.toString()) should equal ("foo");
(TestConstants.ENUM_CONST) should equal (TestEnum.CORGE); (TestConstants.ENUM_CONST) should equal (TestEnum.CORGE);
} }

View file

@ -196,6 +196,7 @@ struct TestConstants {
const uint64Const :UInt64 = 12345678901234567890; const uint64Const :UInt64 = 12345678901234567890;
const float32Const :Float32 = 1234.5; const float32Const :Float32 = 1234.5;
const float64Const :Float64 = -123e45; const float64Const :Float64 = -123e45;
const textConst :Text = "foo";
# ... # ...
const enumConst :TestEnum = corge; const enumConst :TestEnum = corge;
} }