aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compile/expressions.c6
-rw-r--r--src/compile/promotions.c15
-rw-r--r--src/stdlib/integers.c2
-rw-r--r--src/types.c3
-rw-r--r--test/serialization.tm60
5 files changed, 58 insertions, 28 deletions
diff --git a/src/compile/expressions.c b/src/compile/expressions.c
index 098ed4ca..4e6c720f 100644
--- a/src/compile/expressions.c
+++ b/src/compile/expressions.c
@@ -12,8 +12,10 @@
public
Text_t compile_maybe_incref(env_t *env, ast_t *ast, type_t *t) {
if (is_idempotent(ast) && can_be_mutated(env, ast)) {
- if (t->tag == ListType) return Texts("LIST_COPY(", compile_to_type(env, ast, t), ")");
- else if (t->tag == TableType) return Texts("TABLE_COPY(", compile_to_type(env, ast, t), ")");
+ type_t *actual = get_type(env, ast);
+ if (t->tag == ListType && type_eq(t, actual)) return Texts("LIST_COPY(", compile_to_type(env, ast, t), ")");
+ else if (t->tag == TableType && type_eq(t, actual))
+ return Texts("TABLE_COPY(", compile_to_type(env, ast, t), ")");
}
return compile_to_type(env, ast, t);
}
diff --git a/src/compile/promotions.c b/src/compile/promotions.c
index d453b764..68b4f64c 100644
--- a/src/compile/promotions.c
+++ b/src/compile/promotions.c
@@ -25,6 +25,21 @@ bool promote(env_t *env, ast_t *ast, Text_t *code, type_t *actual, type_t *neede
type_t *more_complete = most_complete_type(actual, needed);
if (more_complete) return true;
+ // Serialization/deserialization:
+ if (type_eq(needed, Type(ListType, Type(ByteType)))) {
+ *code = Texts("generic_serialize((", compile_declaration(actual, Text("[1]")), "){", *code, "}, ",
+ compile_type_info(actual), ")");
+ return true;
+ } else if (type_eq(actual, Type(ListType, Type(ByteType)))) {
+ *code = Texts("({ ", compile_declaration(needed, Text("deserialized")),
+ ";\n"
+ "generic_deserialize(",
+ *code, ", &deserialized, ", compile_type_info(needed),
+ ");\n"
+ "deserialized; })");
+ return true;
+ }
+
// Optional promotion:
if (needed->tag == OptionalType && type_eq(actual, Match(needed, OptionalType)->type)) {
*code = promote_to_optional(actual, *code);
diff --git a/src/stdlib/integers.c b/src/stdlib/integers.c
index e7a58ef3..962c97cf 100644
--- a/src/stdlib/integers.c
+++ b/src/stdlib/integers.c
@@ -27,7 +27,7 @@ int Int$print(FILE *f, Int_t i) {
}
}
-static inline Text_t _int64_to_text(int64_t n) {
+static Text_t _int64_to_text(int64_t n) {
if (n == INT64_MIN) return Text("-9223372036854775808");
char buf[21] = {[20] = 0}; // Big enough for INT64_MIN + '\0'
diff --git a/src/types.c b/src/types.c
index ff86ab8f..900588f5 100644
--- a/src/types.c
+++ b/src/types.c
@@ -274,6 +274,9 @@ PUREFUNC bool can_promote(type_t *actual, type_t *needed) {
// No promotion necessary:
if (type_eq(actual, needed)) return true;
+ // Serialization/deserialization
+ if (type_eq(actual, Type(ListType, Type(ByteType))) || type_eq(needed, Type(ListType, Type(ByteType)))) return true;
+
if (actual->tag == NumType && needed->tag == IntType) return false;
if (actual->tag == IntType && (needed->tag == NumType || needed->tag == BigIntType)) return true;
diff --git a/test/serialization.tm b/test/serialization.tm
index b97b122f..18858d0e 100644
--- a/test/serialization.tm
+++ b/test/serialization.tm
@@ -6,72 +6,82 @@ enum MyEnum(Zero, One(x:Int), Two(x:Num, y:Text))
func main()
do
>> obj := Int64(123)
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Int64) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Int64 = bytes
+ assert roundtrip == obj
do
>> obj := 5
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Int) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Int = bytes
+ assert roundtrip == obj
do
>> obj := 9999999999999999999999999999999999999999999999999999
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Int) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Int = bytes
+ assert roundtrip == obj
do
>> obj := "Héllo"
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Text) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Text = bytes
+ assert roundtrip == obj
do
>> obj := [Int64(10), Int64(20), Int64(30)].reversed()
- >> bytes := obj.serialized()
- assert deserialize(bytes -> [Int64]) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : [Int64] = bytes
+ assert roundtrip == obj
do
>> obj := yes
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Bool) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Bool = bytes
+ assert roundtrip == obj
do
>> obj := @[10, 20]
- >> bytes := obj.serialized()
- >> roundtrip := deserialize(bytes -> @[Int])
+ >> bytes : [Byte] = obj
+ >> roundtrip : @[Int] = bytes
assert roundtrip != obj
assert roundtrip[] == obj[]
do
>> obj := {"A":10, "B":20; fallback={"C":30}}
- >> bytes := obj.serialized()
- >> roundtrip := deserialize(bytes -> {Text:Int})
+ >> bytes : [Byte] = obj
+ >> roundtrip : {Text:Int} = bytes
assert roundtrip == obj
assert roundtrip.fallback == obj.fallback
do
>> obj := @Foo("root")
>> obj.next = @Foo("abcdef", next=obj)
- >> bytes := obj.serialized()
- >> roundtrip := deserialize(bytes -> @Foo)
+ >> bytes : [Byte] = obj
+ >> roundtrip : @Foo = bytes
assert "$roundtrip" == "$obj"
do
>> obj := MyEnum.Two(123, "OKAY")
- >> bytes := obj.serialized()
+ >> bytes : [Byte] = obj
+ >> roundtrip : MyEnum = bytes
assert deserialize(bytes -> MyEnum) == obj
do
>> obj : Text? = "Hello"
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Text?) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Text? = bytes
+ assert roundtrip == obj
do
>> obj : Num? = none
- >> bytes := obj.serialized()
- assert deserialize(bytes -> Num?) == obj
+ >> bytes : [Byte] = obj
+ >> roundtrip : Num? = bytes
+ assert roundtrip == obj
do
cases := [0, -1, 1, 10, 100000, 999999999999999999999999999]
for i in cases
- >> bytes := i.serialized()
- assert deserialize(bytes -> Int) == i
+ >> bytes : [Byte] = i
+ >> roundtrip : Int = bytes
+ assert roundtrip == i