Allocate a static string and return its pointer If the string already exists, return the existing pointer
(&mut self, str: &str)
| 100 | /// Allocate a static string and return its pointer |
| 101 | /// If the string already exists, return the existing pointer |
| 102 | pub fn string(&mut self, str: &str) -> Result<u32> { |
| 103 | if let Some(&ptr) = self.strings.get(str) { |
| 104 | return Ok(ptr); |
| 105 | } |
| 106 | // 1 for null termination |
| 107 | // because of zero fill we are already null-terminated |
| 108 | let len = str.as_bytes().len(); |
| 109 | let bytes = self.stack_alloc(len + 1, 1)?; |
| 110 | bytes[0..len].copy_from_slice(str.as_bytes()); |
| 111 | bytes[len] = 0; |
| 112 | self.strings.insert(str.to_string(), self.stack_ptr as u32); |
| 113 | Ok(self.stack_ptr as u32) |
| 114 | } |
| 115 | pub fn finish(mut self, module: &mut Module) -> Result<()> { |
| 116 | // stack embedding |
| 117 | let memory = module.get_memory_id()?; |
no test coverage detected