MCPcopy Create free account
hub / github.com/bytecodealliance/WASI-Virt / string

Method string

src/data.rs:102–114  ·  view source on GitHub ↗

Allocate a static string and return its pointer If the string already exists, return the existing pointer

(&mut self, str: &str)

Source from the content-addressed store, hash-verified

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()?;

Callers 1

create_io_virtFunction · 0.80

Calls 2

stack_allocMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected