Reputation: 83
I am using a DLL with js-ctypes that is made in C.
The method return a string, but when I try access the content of the pointer firefox crashes!
The following code works:
Function declaration:
var getStr = lib.declare("getString",
ctypes.default_abi,
ctypes.char.ptr,
ctypes.int32_t
);
Function call:
let number = new ctypes.int32_t(1);
var str = getStr(number);
console.log(str.toString());
str.readString();
The console.log
output's:
ctypes.char.ptr(ctypes.UInt64("0x64ff5b48"))
But this code don't work:
Function declaration:
var Core = {
init : function(){
this.lib = ctypes.open("library");
this.getStr = this.lib.declare("getString",
ctypes.default_abi,
ctypes.char.ptr,
ctypes.int32_t);
},
close : function(){
this.lib.close();
}
}
Function call
Core.init();
var number = new ctypes.int32_t(1);
var result = Core.getStr(number);
console.log(result.toString());
result.readString();
The console.log
output's:
ctypes.char.ptr(ctypes.UInt64("0x64ff5b48"))
The same thing !
With this way firefox crashes. Anyone know how to solve this? I was doing this way for modulation of the addon.
Upvotes: 1
Views: 252
Reputation: 83
I found the problem! Thank you Noitidart. In the second example I was closing the library before the str.readString()
. It makes firefox crash. I tried to reduce the code on the question post and forgot about this detail, I am sorry.
Upvotes: 2
Reputation: 37338
Try casting str
to known length like this: var strCasted = ctypes.cast(str, ctypes.char.array(100).ptr);
then try reading string like this: var jsStr = strCasted.contents.readString();
that should do the trick if not jump on #jsctypes moz channel we'll discuss it then update back here with the solution. Paste this to your url bar: irc://moznet/jsctypes
This tutorial on casting should help: https://gist.github.com/Noitidart/081ef49002a90fe43005#comment-1470308
Upvotes: 0