我和我的朋友前段時間一起工作過。它意味着與js-ctypes一起使用。在Linux中有這些宏用於處理向字節數組添加文件描述符(uint32's)的列表:FD_SET
和FD_IS_SET
。該文檔在這裏 - http://linux.die.net/man/2/select用javascript編寫的FD_SET和FD_ISSET宏
我想知道是否有人能夠檢查,如果我這樣做的權利或有人知道任何人在JavaScript中做過?我需要完成對大小端的32位/ 64位支持,但如果它已經存在,我很想看到它,因爲當我們開展這項工作時,我們有很多不確定因素。
這裏是代碼,fd_set_get_idx
是這一切的輔助功能。
var MACROS = {
fd_set_set: function(fdset, fd) {
let { elem8, bitpos8 } = MACROS.fd_set_get_idx(fd);
console.info('elem8:', elem8.toString());
console.info('bitpos8:', bitpos8.toString());
fdset[elem8] = 1 << bitpos8;
},
fd_set_isset: function(fdset, fd) {
let { elem8, bitpos8 } = MACROS.fd_set_get_idx(fd);
console.info('elem8:', elem8.toString());
console.info('bitpos8:', bitpos8.toString());
return !!(fdset[elem8] & (1 << bitpos8));
},
fd_set_get_idx: function(fd) {
if (osname == 'darwin' /*is_mac*/) {
// We have an array of int32. This should hopefully work on Darwin
// 32 and 64 bit.
let elem32 = Math.floor(fd/32);
let bitpos32 = fd % 32;
let elem8 = elem32 * 8;
let bitpos8 = bitpos32;
if (bitpos8 >= 8) { // 8
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 16
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 24
bitpos8 -= 8;
elem8++;
}
return {'elem8': elem8, 'bitpos8': bitpos8};
} else { // else if (osname == 'linux' /*is_linux*/) { // removed the else if so this supports bsd and solaris now
// :todo: add 32bit support
// Unfortunately, we actually have an array of long ints, which is
// a) platform dependent and b) not handled by typed arrays. We manually
// figure out which byte we should be in. We assume a 64-bit platform
// that is little endian (aka x86_64 linux).
let elem64 = Math.floor(fd/64);
let bitpos64 = fd % 64;
let elem8 = elem64 * 8;
let bitpos8 = bitpos64;
if (bitpos8 >= 8) { // 8
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 16
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 24
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 32
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 40
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 48
bitpos8 -= 8;
elem8++;
}
if (bitpos8 >= 8) { // 56
bitpos8 -= 8;
elem8++;
}
return {'elem8': elem8, 'bitpos8': bitpos8};
}
}
};
好的舊'''','<<'和'&','|' –
有什麼問題另外,可以使用'for'循環... –
我不明白如果你只是想模擬'FD_XXX'函數或者如果你必須符合某些東西。JS,特別是ES6,有[DataView](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DataView)和[typed arrays](https://developer.mozilla.org/ en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)如果你想使用它們。無論如何,如果'FD_SET(i,set)'的語義只是'set [i] = i',我從頭開始看不出任何問題。你能澄清嗎?此外'fdset [elem8] = 1 << bitpos8'可能是錯誤的。 –