kitty
Deno.UnsafeCallback Error (bypass string from c++ to deno)
------------------------------
This is output of deno code
------------------------------
[JS] #100
[C++][print_two] START
[C++][print_two] buff : 'Hello World'
[JS] #300
[JS] age: 88
[Object: null prototype] {}
[JS] Error in callback: TypeError: Failed to execute 'decode' on 'TextDecoder': Argument 1 is not an ArrayBuffer, SharedArrayBuffer, or a view on one
[C++][print_two] END
[JS] #200
9 replies
Deno.UnsafeCallback Error (bypass string from c++ to deno)
-------------------------------------
typescript
-------------------------------------
const ffi = Deno.dlopen("./fib.so", {
set_status_callback: { parameters: ["function"], result: "void", nonblocking: true },
print_two: { parameters: ["buffer"], result: "void", nonblocking: true },
} as const
);
const callback = new Deno.UnsafeCallback(
{ parameters: ["i32", "buffer"], result: "void" } as const, (age: number, msg: any) => {
try {
console.log(
[JS] #300
);
console.log([JS] age: ${age}
);
console.log(msg);
const decoder = new TextDecoder();
const str = decoder.decode(msg);
console.log(str);
console.log([JS] #400
);
}
catch (e) {
console.error([JS] Error in callback: ${e}
);
}
},
);
// Pass the callback pointer to dynamic library
ffi.symbols.set_status_callback(callback.pointer);
console.log('[JS] #100');
const buffer = new TextEncoder().encode("Hello World" + String.fromCharCode(0));
await ffi.symbols.print_two(buffer);
console.log('[JS] #200');
ffi.close();
export {};9 replies
Deno.UnsafeCallback Error (bypass string from c++ to deno)
-------------------------------------
cpp
-------------------------------------
// callback function type
typedef void (callback_type)(int, const char);
callback_type callback = nullptr;
// set callback function
extern "C" void set_status_callback(callback_type cb) {
callback = cb;
}
extern "C" void print_two(const char* buff)
{
printf("[C++][%s] START\n", FUNCTION);
printf("[C++][%s] buff : '%s'\n", FUNCTION, buff);
callback(88, buff);
printf("[C++][%s] END\n", FUNCTION);
}
9 replies