AssemblyScript / assemblyscript

A TypeScript-like language for WebAssembly.
https://www.assemblyscript.org
Apache License 2.0
16.6k stars 650 forks source link

The ascii generated when the function returns a string type is incorrect #2847

Closed 996ccs closed 2 weeks ago

996ccs commented 1 month ago

Question

I wrote a function getA using AssemblyScript to return an A string. After compilation, I got a wasm file. Then I used other wasm runtime to parse the wasm file and called the getA function hoping to get the ascii value 65 corresponding to A. But what I got was [2, 0, 0, 0, 2, 0, 0, 0, 65] The wat file compiled by AssemblyScript is as follows


(module
 (type $0 (func (result i32)))
 (memory $0 1)
 (data $0 (i32.const 1036) "\1c")
 (data $0.1 (i32.const 1048) "\02\00\00\00\02\00\00\00A")
 (export "getA" (func $assembly/index/getA))
 (export "memory" (memory $0))
 (func $assembly/index/getA (result i32)
  i32.const 1056
 )
)

Judging from the compiled product above, the prefix '\02\00\00\00\02\00\00\00' is obviously redundant.

dcodeIO commented 1 month ago

What you are seeing there is a string header, type 2, length 2, data in 16-bit Unicode.

See also: https://www.assemblyscript.org/runtime.html#header-layout

CountBleck commented 1 month ago

Also note that the pointer returned by getA is 1056, which is in the middle of the data segment (at 1048) you're looking at. The location of the A is indeed at 1056, and the managed object header is placed eight bytes before the location referred to by the pointer.

github-actions[bot] commented 2 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!