Sunday, 15 January 2012

javascript - How to get value of a UInt64, toString only way? -



javascript - How to get value of a UInt64, toString only way? -

i have this: "ctypes.uint64("7")"

it returned this:

var chars = sendmessage(htoolbar, tb_getbuttontextw, local_tbb.idcommand, ctypes.voidptr_t(0));

so

console.log('chars=', chars, chars.tostring(), uneval(chars));

gives

'chars=' 'uint64 { }' "7" 'ctypes.uint64("7")'

so can value going chars.tostring(), have run parseint on that, there anyway read property? chars.uint64?

the problem 64-bit integers in js-ctypes javascript lacks compatible type. javascript numbers ieee double precision floating point numbers (double), , can represent 53-bit integers @ most. shouldn't trying parse int yourself, unless know fact result fit double. e.g. cannot know pointers.

e.g. consider following:

// 6 * 8-bit = 48 bit; 48 < 53, ok ((parseint("0xffffffffffff", 16) + 2) == parseint("0xffffffffffff", 16)) == false // however, 7 * 8-bit = 56 bit; 56 < 53, not ok ((parseint("0xffffffffffffff", 16) + 2) == parseint("0xffffffffffffff", 16)) == true // oops, compared equal, because double precision floating point // cannot actual hold parseint result, still below 64-bit!

lets deal 64-bit integers in js properly...

if want comparisons, utilize uint64.compare()/int64.compare(), e.g.

// number == number ctypes.uint64.compare(ctypes.uint64("7"), ctypes.uint64("7")) == 0 // number != number ctypes.uint64.compare(ctypes.uint64("7"), ctypes.uint64("6")) != 0 // number > number ctypes.uint64.compare(ctypes.uint64("7"), ctypes.uint64("6")) > 0 // number < number ctypes.uint64.compare(ctypes.uint64("7"), ctypes.uint64("8")) < 0

if need result, not sure 32-bit unsigned integer, can observe if you're dealing 32 bit unsigned integers packed uint64:

ctypes.uint64.compare(ctypes.uint64("7"), ctypes.uint64("0xffffffff")) < 0

and analog 32-bit signed integers in int64, need compare minimum , maximum:

ctypes.int64.compare(ctypes.int64("7"), ctypes.int64("2147483647")) < 0 && ctypes.int64.compare(ctypes.int64("7"), ctypes.int64("-2147483648")) > 0

so, 1 time know or detected fit js double, safe phone call parseint on it.

var number = ...; if (ctypes.uint64.compare(number, ctypes.uint64("0xffffffff")) > 0) { throw error("whoops, unexpectedly big value our code not handle correctly"); } chars = parseint(chars.tostring(), 10);

(for sake of completeness, there uint64.hi()/int64.hi() , uint64.lo()/int64.lo() high , low 32-bits real 64-bit integers , 64-bit integer math (e.g.), beware of endianess).

ps: homecoming value of sendmessage intptr_t not uintptr_t, of import here because sendmessage(hwnd, tb_getbuttontext, ...) may homecoming -1 on failure!

so putting (untested):

var sendmessage = user32.declare( 'sendmessagew', ctypes.winapi_abi, ctypes.intptr_t, ctypes.voidptr_t, // hwnd ctypes.uint32_t, // msg ctypes.uintptr_t, // wparam ctypes.intptr_t // lparam ); // ... var chars = sendmessage(htoolbar, tb_getbuttontextw, local_tbb.idcommand, ctypes.voidptr_t(0)); if (ctypes.int64.compare(chars, ctypes.int64("0")) < 0) { throw new error("tb_getbuttontext returned failure (negative value)"); } if (ctypes.int64.comare(chars, ctypes.int64("32768")) > 0) { throw new error("tb_getbuttontext returned unreasonably big number > 32kib"); } chars = parseint(chars.tostring());

javascript firefox-addon jsctypes

No comments:

Post a Comment