javascript - 用 javascript 编写的 FD_SET 和 FD_ISSET 宏

标签 javascript c linux macros jsctypes

我和我的 friend 不久前就研究过这个。它适用于 js-ctypes。在 Linux 中,有这些宏用于处理将文件描述符列表 (uint32) 添加到字节数组:FD_SETFD_IS_SET。文档在这里 - http://linux.die.net/man/2/select

我想知道是否有人能够检查我这样做是否正确,或者有人知道有人用 javascript 做过这件事吗?我需要完成对 big 和 little endian 的 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};
            }
        }
};

最佳答案

我把识别字节序和字长的任务留给你了。
下面的代码模拟了 FD_XXX 函数,并允许您指定字节顺序和大小。

<!doctype>
<html>
    <head>
        <script>
            var SIZE_32 = 4
            var SIZE_64 = 8

            var LITTLE_ENDIAN = [0, 1, 2, 3, 4, 5, 6, 7];
            var BIG_ENDIAN = [7, 6, 5, 4, 3, 2, 1, 0];

            function fdset(setSize, endianness, size)
            {
                var buffer = new Uint8Array(div(setSize + 7, 8));



                function div(a, b)
                {
                    return Math.floor(a / b);
                }

                function make_index(index)
                {
                    return div(index, 8 * size) * size + endianness[div(index % (8 * size), 8)] % size;
                }

                buffer.set_bit = function(index)
                {
                    buffer[make_index(index)] |= 1 << (index % 8);
                };

                buffer.clear_bit = function(index)
                {
                    buffer[make_index(index)] &= ~(index % 8);
                };

                buffer.get_bit = function(index)
                {
                    return buffer[make_index(index)] & 1 << (index % 8);
                };

                buffer.zero = function()
                {
                    buffer.fill(0);
                }


                return buffer;
            }

            function FD_SET(fd, fdset)
            {
                fdset.set_bit(fd);
            }

            function FD_ISSET(fd, fdset)
            {
                return !!fdset.get_bit(fd);
            }

            function FD_CLR(fd, fdset)
            {
                return fdset.clear_bit(fd);
            }

            function FD_ZERO(fdset)
            {
                return fdset.zero();
            }


        </script>
    </head>
    <body>
        <script>
            var s = fdset(128, LITTLE_ENDIAN, SIZE_64);

            //s in an Uint8Array

            console.log(s);

            FD_SET(0, s);    //Byte 0 = 1
            FD_SET(9, s);    //Byte 1 = 2
            FD_SET(18, s);   //Byte 2 = 4
            FD_SET(27, s);   //Byte 3 = 8
            FD_SET(36, s);   //Byte 4 = 16
            FD_SET(45, s);   //Byte 5 = 32
            FD_SET(54, s);   //Byte 6 = 64
            FD_SET(63, s);   //Byte 7 = 128

            FD_SET(120, s);  //Byte 15 = 1
            FD_SET(113, s);  //Byte 14 = 2
            FD_SET(106, s);  //Byte 13 = 4
            FD_SET(99, s);   //Byte 12 = 8
            FD_SET(92, s);   //Byte 11 = 16
            FD_SET(85, s);   //Byte 10 = 32
            FD_SET(78, s);   //Byte 9 = 64
            FD_SET(71, s);   //Byte 8 = 128

            console.log(s);

            //64 bits, BE: [128, 64, 32, 16, 8, 4, 2, 1, 1, 2, 4, 8, 16, 32, 64, 128]
            //64 bits, LE: [1, 2, 4, 8, 16, 32, 64, 128, 128, 64, 32, 16, 8, 4, 2, 1]
            //32 bits, BE: [8, 4, 2, 1, 128, 64, 32, 16, 16, 32, 64, 128, 1, 2, 4, 8]
            //32 bits, LE: [1, 2, 4, 8, 16, 32, 64, 128, 128, 64, 32, 16, 8, 4, 2, 1]
        </script>
    </body>
</html>

fdset 函数返回一个 Uint8Array,您可以将其传递给 native 函数或进一步阐述。
setSize 设置支持的最大文件描述符。


请注意,js ctypes 已经有一个数组类型和通常的 [U]intXX_t 类型在 native 字节序中,可惜没有映射到 32/的类型基于平台的 64 位整数并且没有 sizeof 运算符2,因此您仍然需要执行外部检查来检测字长。

使用 ctypes 会更自然。
供引用hereFD_XXX 函数的官方实现。

你可以定义一个 struct具有类型为 array 的单个字段的 uint32/64_t .
然后模仿 C 源代码的行为,小心使用 UInt64需要时1 并避免 shifts .


1 JS只有双数,这些数有53位尾数,10位指数和1位符号。当使用位运算符时, double 被转换为整数,因为它是指定精度的尾数(指数只是一个比例,符号只是一个倒数),这个数字最多可以携带与 53 位一样多的信息数字。

2据我所知,我根本不是js ctypes方面的专家。

关于javascript - 用 javascript 编写的 FD_SET 和 FD_ISSET 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38455486/

相关文章:

javascript - 范围对象

c - 我是否可以通过将变量设为静态来提高性能?

ruby - 需要 bigdecimal loaderror ruby

javascript - 将远程文件读取到浏览器中的 Node 缓冲区中

javascript - JSPDF addHTML 不显示背景图片

javascript - FullCalendar ListView - 如何隐藏 "all-day"

C - 向用户输入添加引号

c - C 中的错误处理,void 返回函数

c - 关于 creat() 系统调用模式参数的快速问题

linux - 使 bash printf 将数组视为一个参数