var itest; function InitWrappers() { // itest = Module.cwrap('itest', 'string'); // itest = (str) => { // const buffer = Module._malloc(str.length + 1) // console.log({buffer}) // Module.stringToUTF8(str, buffer, str.length + 1) // return Module.ccall('itest', 'string', ['number'], [buffer]) // } window.read_str_pass = Module.cwrap('read_str_pass', 'void', ['number']) } window.cjs = new (class { str_pass_size() { return Module.ccall('str_pass_size', 'number') } write_string(string) { const buffer = Module._malloc(string.length + 1) Module.stringToUTF8(string, buffer, string.length + 1) return buffer } set_string_pass(str_address, size, other = 0) { const offset = Module._malloc(this.str_pass_size()) Module.setValue(offset, str_address, 'i32') Module.setValue(offset + 4, size, 'i32') Module.setValue(offset + 8, other, 'i32') return offset } // [START, SIZE, 0] string_pass(string) { const str_address = this.write_string(string) return this.set_string_pass(str_address, string.length) } receive(offset) { const { str_address, str_length, other } = this.get_string_pass(offset) const string = Module.UTF8ToString(str_address, str_length) Module._free(offset, this.str_pass_size()) return string } get_string_pass(offset) { const str_address = Module.getValue(offset, 'i32') const str_length = Module.getValue(offset + 4, 'i32') const other = Module.getValue(offset + 8, 'i32') return { str_address, str_length, other } } fire_event_bus(key) { const key_pass = this.string_pass(key) Module.ccall('fire_event_bus', null, ['number'], [key_pass]) } })