Browse Source

Fix constant string as subscript in mapping

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 4 years ago
parent
commit
dbd5787620
2 changed files with 14 additions and 27 deletions
  1. 2 27
      src/emit/mod.rs
  2. 12 0
      tests/substrate_tests/mappings.rs

+ 2 - 27
src/emit/mod.rs

@@ -2431,23 +2431,7 @@ pub trait TargetRuntime<'a> {
                     let v = self.expression(bin, &e, vartab, function, ns);
 
                     let len = match e.ty() {
-                        ast::Type::DynamicBytes | ast::Type::String => {
-                            // field 0 is the length
-                            let array_len = unsafe {
-                                bin.builder.build_gep(
-                                    v.into_pointer_value(),
-                                    &[
-                                        bin.context.i32_type().const_zero(),
-                                        bin.context.i32_type().const_zero(),
-                                    ],
-                                    "array_len",
-                                )
-                            };
-
-                            bin.builder
-                                .build_load(array_len, "array_len")
-                                .into_int_value()
-                        }
+                        ast::Type::DynamicBytes | ast::Type::String => bin.vector_len(v),
                         _ => v
                             .get_type()
                             .size_of()
@@ -2475,16 +2459,7 @@ pub trait TargetRuntime<'a> {
 
                     match ty {
                         ast::Type::DynamicBytes | ast::Type::String => {
-                            let data = unsafe {
-                                bin.builder.build_gep(
-                                    v.into_pointer_value(),
-                                    &[
-                                        bin.context.i32_type().const_zero(),
-                                        bin.context.i32_type().const_int(2, false),
-                                    ],
-                                    "",
-                                )
-                            };
+                            let data = bin.vector_bytes(v);
 
                             bin.builder.build_call(
                                 bin.module.get_function("__memcpy").unwrap(),

+ 12 - 0
tests/substrate_tests/mappings.rs

@@ -389,6 +389,12 @@ fn test_user() {
             function rm(string name) public {
                 delete users[name];
             }
+
+            function get_foo() public view returns (bool, address) {
+                user storage s = users["foo"];
+
+                return (s.exists, s.addr);
+            }
         }"##,
     );
 
@@ -426,6 +432,12 @@ fn test_user() {
 
         assert_eq!(runtime.vm.output, GetRet(false, [0u8; 32]).encode());
     }
+
+    runtime.function("add", AddArg(b"foo".to_vec(), [1u8; 32]).encode());
+
+    runtime.function("get_foo", Vec::new());
+
+    assert_eq!(runtime.vm.output, GetRet(true, [1u8; 32]).encode());
 }
 
 #[test]