String

Kotlin Web Assembly direct buffer access with UTF-16 string

Submitted by Dickens A S on Fri, 05/01/2020 - 06:07

Kotlin WebAssembly runtime provides a global JavaScript array called heap

which can be accessed if the pointer of a variable is known

The pointer and size of the variable can be determined by library methods stringPointer and stringLengthBytes

The from JavaScript the value can be updated with below logic

Kotlin Main Code

Kotlin passing string to Web Assembly

Submitted by Dickens A S on Thu, 04/30/2020 - 14:09

From JavaScript as of now we cannot pass string to WebAssembly, therefore the string will be converted to an array of bytes and kept in browser's memory object of the kotlin web assembly framework object kotlinObject

From WebAssembly by calling the JavaScript method for each character, transfer the numeric value

Then convert the numeric value to character and combine to a string in kotlin

This is achieved using below code

JavaScript: string to UTF array utlity