Closed spino17 closed 1 month ago
It looks like your writing directly to linear memory, right? I'm wondering if you are calling __new
from the runtime to initialize a new string (or utf-8 encoded buffer in your case)
If your not, any other allocation is going to overwrite that data and it can't be parsed correctly.
I optimized your code:
function readStringFromMemory(ptr: usize): string {
const len = load<i32>(ptr);
return String.UTF8.decodeUnsafe(ptr + 4, len);
}
function getLengthPrefixedString(s: string): ArrayBuffer {
const newLen = String.UTF8.byteLength(s);
const buffer = new ArrayBuffer(4 + newLen);
store<i32>(changetype<usize>(buffer), newLen, 0);
String.UTF8.encodeUnsafe(changetype<usize>(s), s.length, changetype<usize>(buffer) + 4);
return buffer;
}
Thanks @JairusSW for the response. No I am not adding any _new in the host runtime side while setting the memory! I saw the glue code generated in js and they don't use _new, they just set the memory appropriately and assemblyscript can interpret that as string or any other type. Can you please let me know where is the disconnect? I can be completely wrong with this thinking. And much thanks for the above optimized code 😄
What language is your runtime/app loader written in? Perhaps I can spin up an example
Also do you have WhatsApp or Discord? It might be easier to figure this out there
Thanks @JairusSW for the help here, it's much needed and I highly appreciate. My runtime is in rust wasmer. My discord is spino17. Would love to connect with you over there and carry forward the discussion.
I am trying out below code which has some imports which gets length prefixed strings from memory. These host native imports implementation is closed. Below is the code
I am not understanding where the issue is, when I am using hardcoded strings (commented in above code), it is working fine, but when I am reading it from the memory and then parsing it, it is only able to initialise just the counter field rest the maps seem to be uninitialised. Is this issue related to
json-as
orassemblyscript
garbage collection? I even tried compiler option--runtime stub
but same error is occurring.