]> gitweb.factorcode.org Git - factor.git/commitdiff
bare: adding checks for valid union values.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 16 Jun 2022 16:42:22 +0000 (09:42 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 16 Jun 2022 16:42:22 +0000 (09:42 -0700)
extra/bare/bare-tests.factor
extra/bare/bare.factor

index 8ae502f4c49901bc81809235535e22570896b3f3..da8f0382a142ab946956fd38ff1b500cfe0c73fd 100644 (file)
@@ -242,6 +242,10 @@ IN: bare.tests
     ] sequences:map
 ] unit-test
 
+[
+    B{ 0x03 0x03 } T{ bare:union f { { int 0 } { uint 1 } { str 2 } } } bare>
+] [ invalid-union? ] must-fail-with
+
 ! struct
 
 {
index c4eb438552b575bc7e0df64f0ef8201e673a0f9d..e22a6e5e94cbb6a092b422bba2d9e228a42cd33e 100644 (file)
@@ -9,8 +9,6 @@ strings words words.constant ;
 
 IN: bare
 
-ERROR: invalid-enum value ;
-
 SINGLETONS: uint ;
 SINGLETONS: int ;
 SINGLETONS: u8 u16 u32 u64 ;
@@ -29,6 +27,9 @@ TUPLE: struct fields ;
 TUPLE: user name type ;
 TUPLE: schema types ;
 
+ERROR: invalid-enum value ;
+ERROR: invalid-union value ;
+
 GENERIC: write-bare ( obj schema -- )
 
 M: uint write-bare
@@ -139,7 +140,8 @@ M: map read-bare
     '[ _ _ [ read-bare ] bi@ 2array ] replicate ;
 
 M: union read-bare
-    [ uint read-bare ] dip members>> value-at read-bare ;
+    [ uint read-bare ] dip members>> ?value-at
+    [ read-bare ] [ invalid-union ] if ;
 
 M: user read-bare type>> read-bare ;