Perlito5

 view release on metacpan or  search on metacpan

lib/Perlito5/JavaScript3/Runtime.pm  view on Meta::CPAN

    var res = [];
    for (i = 0; i < arguments.length; i++) {
        if (arguments[i] == null) {
            res.push(null)
        }
        else if (arguments[i].hasOwnProperty("get_lvalues")) {
            // container
            arguments[i].get_lvalues(res);
        }
        else if (arguments[i] instanceof Array) {
            // js Array -- possibly generated by p5context()
            // maybe too late to get lvalues -- needs more testing
            // this doesn't handle nested Array
            o = arguments[i];
            for (j = 0; j < o.length; j++) {
                res.push(o[j]);
            }
        }
        else {
            // non-container
            res.push(arguments[i]);
        }
    }
    return res;
};

p5list_to_a = function() {
    var res = [];
    for (i = 0; i < arguments.length; i++) {
        if (arguments[i] == null) {
            res.push(null)
        }
        else if (arguments[i].hasOwnProperty("get_values")) {
            // container
            arguments[i].get_values(res);
        }
        else if (arguments[i] instanceof Array) {
            // js Array -- possibly generated by p5context()
            o = arguments[i];
            for (j = 0; j < o.length; j++) {
                res.push(o[j]);
            }
        }
        else {
            // non-container
            res.push(arguments[i]);
        }
    }
    return res;
};

p5a_to_h = function(a) {
    var res = {};
    for (i = 0; i < a.length; i+=2) {
        res[p5str(a[i])] = a[i+1];
    }
    return res;
};

if (isNode) {
    var fs = require("fs");
}

p5context = function(List__, p5want) {
    if (p5want) {
        return p5list_to_a.apply(null, List__);
    }
    // scalar: return the last value
    var o = List__;
    while (o instanceof Array) {
        o =   o.length
            ? o[o.length-1]
            : null;
    }
    return o;
}

p5code = function(o) {
    if (typeof o === "function") {
        return o;
    }
    return o.p5code();
};

p5str = function(o) {
    if (o == null) {
        return "";
    }
    if (typeof o === "object" && (o instanceof Array)) {
        return CORE.join(["", o]);
    }
    if (typeof o.p5string === "function") {
        return o.p5string();
    }
    if (typeof o == "number" && Math.abs(o) < 0.0001 && o != 0) {
        return o.toExponential().replace(/e-(\d)$/,"e-0$1");
    }
    if (typeof o === "boolean") {
        return o ? "1" : "";
    }
    if (typeof o !== "string") {
        return "" + o;
    }
    return o;
};

p5num = function(o) {
    if (o == null) {
        return 0;
    }
    if (typeof o === "object" && (o instanceof Array)) {
        return o.length;
    }
    if (typeof o.p5num === "function") {
        return o.p5num();
    }
    if (typeof o !== "number") {
        return parseFloat(p5str(o));
    }
    return o;
};



( run in 0.636 second using v1.01-cache-2.11-cpan-39bf76dae61 )