All files / src/lua/bit lua-bit.ts

97.82% Statements 45/46
60% Branches 6/10
100% Functions 9/9
97.36% Lines 37/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 1061x   1x 1x   1x   20x 20x 20x 20x 20x 20x 20x 20x 20x                   20x         20x 140x       140x       140x     8x 8x   8x   15x               15x   8x         8x         8x       8x   8x   8x 8x       8x             8x 8x     140x         20x 20x           1x    
import { Logger } from "../../logger";
 
const fengari = require("fengari");
const lua = fengari.lua;
 
export default class LuaBitLib {
    public static LoadLibrary(L: any): void {
        this.logger.debug("Assembling lua bit library");
        const band = (x1: any, x2: any) => x1 & x2;
        const bor = (x1: any, x2: any) => x1 | x2;
        const bxor = (x1: any, x2: any) => x1 ^ x2;
        const bnot = (x1: any) => ~x1;
        const lshift = (x1: any, x2: any) => x1 << x2;
        const rshift = (x1: any, x2: any) => x1 >> x2;
        const arshift = (x1: any, x2: any) => x1 >>> x2;
        const bit: any = {
            arshift,
            band,
            bnot,
            bor,
            bxor,
            lshift,
            rshift
        };
 
        lua.lua_createtable(
            L,
            0,
            Object.keys(bit).length
        );
        for (const key of Object.keys(bit)) {
            this.logger.debug(
                `Registering bit lib function ${key}: %j`,
                bit[key]
            );
            lua.lua_pushstring(
                L,
                key
            );
            lua.lua_pushjsfunction(
                L,
                (LIB: any) => {
                    const n = lua.lua_gettop(LIB);
                    const args = new Array(n);
 
                    for (let i = 0; i < n; i++) {
                        let value: any;
                        value = lua.lua_tonumber(
                            LIB,
                            i + 1
                        );
                        /*
                         * Numbers are always integer to redis
                         * value = parseInt(value, 10);
                         */
                        args[i] = value;
                    }
                    this.logger.debug(
                        `BIT calling ${key} with %j`,
                        ...args
                    );
 
                    const returned: any = bit[key].call(
                        null,
                        ...args
                    );
 
                    this.logger.debug(
                        `BIT ${key} returned ${returned}: %j`,
                        returned
                    );
                    this.logger.debug(`return value is ${returned ? returned.constructor.name : "not defined"}`);
 
                    switch (true) {
                    case (returned && returned.constructor.name === "Number"):
                        this.logger.debug("Push number");
                        lua.lua_pushnumber(
                            LIB,
                            returned
                        );
                        break;
                    default:
                        this.logger.warn(
                            "Not prepared to push type %j",
                            returned
                        );
                    }
                    this.logger.debug(`returned.length is ${Array.isArray(returned) ? returned.length : 1}`);
                    return (Array.isArray(returned) ? returned.length : 1);
                }
            );
            lua.lua_rawset(
                L,
                -3
            );
        }
        this.logger.debug("Setting \"bit\" global");
        lua.lua_setglobal(
            L,
            "bit"
        );
    }
 
    private static logger: Logger = new Logger(module.id);
}