xC-gen-proto: reduce enums to single bytes
That's already way more than we can possibly use.
This commit is contained in:
parent
8c8e06b015
commit
f3cc137342
|
@ -110,11 +110,11 @@ function codegen_enum(name, cg, ctype) {
|
||||||
|
|
||||||
# XXX: This should also check if it isn't out-of-range for any reason,
|
# XXX: This should also check if it isn't out-of-range for any reason,
|
||||||
# but our usage of sprintf() stands in the way a bit.
|
# but our usage of sprintf() stands in the way a bit.
|
||||||
CodegenSerialize[name] = "\tstr_pack_i32(w, %s);\n"
|
CodegenSerialize[name] = "\tstr_pack_i8(w, %s);\n"
|
||||||
CodegenDeserialize[name] = \
|
CodegenDeserialize[name] = \
|
||||||
"\t{\n" \
|
"\t{\n" \
|
||||||
"\t\tint32_t v = 0;\n" \
|
"\t\tint8_t v = 0;\n" \
|
||||||
"\t\tif (!msg_unpacker_i32(r, &v) || !v)\n" \
|
"\t\tif (!msg_unpacker_i8(r, &v) || !v)\n" \
|
||||||
"\t\t\treturn false;\n" \
|
"\t\t\treturn false;\n" \
|
||||||
"\t\t%s = v;\n" \
|
"\t\t%s = v;\n" \
|
||||||
"\t}\n"
|
"\t}\n"
|
||||||
|
|
|
@ -161,7 +161,7 @@ function codegen_begin() {
|
||||||
print "\tvar n int64"
|
print "\tvar n int64"
|
||||||
print "\tif err := json.Unmarshal(data, &n); err != nil {"
|
print "\tif err := json.Unmarshal(data, &n); err != nil {"
|
||||||
print "\t\treturn 0, err"
|
print "\t\treturn 0, err"
|
||||||
print "\t} else if n > math.MaxInt32 || n < math.MinInt32 {"
|
print "\t} else if n > math.MaxInt8 || n < math.MinInt8 {"
|
||||||
print "\t\treturn 0, errors.New(`integer out of range`)"
|
print "\t\treturn 0, errors.New(`integer out of range`)"
|
||||||
print "\t} else {"
|
print "\t} else {"
|
||||||
print "\t\treturn n, nil"
|
print "\t\treturn n, nil"
|
||||||
|
@ -191,7 +191,7 @@ function codegen_enum_value(name, subname, value, cg, goname) {
|
||||||
|
|
||||||
function codegen_enum(name, cg, gotype, fields) {
|
function codegen_enum(name, cg, gotype, fields) {
|
||||||
gotype = PrefixCamel name
|
gotype = PrefixCamel name
|
||||||
print "type " gotype " int"
|
print "type " gotype " int8"
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
print "const ("
|
print "const ("
|
||||||
|
@ -239,12 +239,10 @@ function codegen_enum(name, cg, gotype, fields) {
|
||||||
|
|
||||||
# XXX: This should also check if it isn't out-of-range for any reason,
|
# XXX: This should also check if it isn't out-of-range for any reason,
|
||||||
# but our usage of sprintf() stands in the way a bit.
|
# but our usage of sprintf() stands in the way a bit.
|
||||||
CodegenSerialize[name] = \
|
CodegenSerialize[name] = "\tdata = append(data, uint8(%s))\n"
|
||||||
"\tdata = binary.BigEndian.AppendUint32(data, uint32(%s))\n"
|
|
||||||
CodegenDeserialize[name] = \
|
CodegenDeserialize[name] = \
|
||||||
"\tif len(data) >= 4 {\n" \
|
"\tif len(data) >= 1 {\n" \
|
||||||
"\t\t%s = " gotype "(int32(binary.BigEndian.Uint32(data)))\n" \
|
"\t\t%s, data = " gotype "(data[0]), data[1:]\n" \
|
||||||
"\t\tdata = data[4:]\n" \
|
|
||||||
"\t} else {\n" \
|
"\t} else {\n" \
|
||||||
"\t\treturn nil, false\n" \
|
"\t\treturn nil, false\n" \
|
||||||
"\t}\n"
|
"\t}\n"
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
# Booleans are one byte each.
|
# Booleans are one byte each.
|
||||||
# Strings must be valid UTF-8, use u8<> to lift that restriction.
|
# Strings must be valid UTF-8, use u8<> to lift that restriction.
|
||||||
# String and array lengths are encoded as u32.
|
# String and array lengths are encoded as u32.
|
||||||
# Enumeration values automatically start at 1, and are encoded as i32.
|
# Enumeration values automatically start at 1, and are encoded as i8.
|
||||||
# Any struct or union field may be a variable-length array.
|
# Any struct or union field may be a variable-length array.
|
||||||
#
|
#
|
||||||
# Message framing is done externally, but also happens to prefix u32 lengths.
|
# Message framing is done externally, but also happens to prefix u32 lengths.
|
||||||
|
@ -189,6 +189,8 @@ function defenum( name, ident, value, cg) {
|
||||||
value = readnumber()
|
value = readnumber()
|
||||||
if (!value)
|
if (!value)
|
||||||
fatal("enumeration values cannot be zero")
|
fatal("enumeration values cannot be zero")
|
||||||
|
if (value < -128 || value > 127)
|
||||||
|
fatal("enumeration value out of range")
|
||||||
expect(accept(","))
|
expect(accept(","))
|
||||||
append(EnumValues, name, SUBSEP ident)
|
append(EnumValues, name, SUBSEP ident)
|
||||||
if (EnumValues[name, ident]++)
|
if (EnumValues[name, ident]++)
|
||||||
|
|
Loading…
Reference in New Issue