js/src/tests/js1_8_5/extensions/shell.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_8_5/extensions/shell.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/*
     1.6 + * Any copyright is dedicated to the Public Domain.
     1.7 + * http://creativecommons.org/licenses/publicdomain/
     1.8 + */
     1.9 +
    1.10 +
    1.11 +// The Worker constructor can take a relative URL, but different test runners
    1.12 +// run in different enough environments that it doesn't all just automatically
    1.13 +// work. For the shell, we use just a filename; for the browser, see browser.js.
    1.14 +var workerDir = '';
    1.15 +
    1.16 +// explicitly turn on js185
    1.17 +// XXX: The browser currently only supports up to version 1.8
    1.18 +if (typeof version != 'undefined')
    1.19 +{
    1.20 +  version(185);
    1.21 +}
    1.22 +
    1.23 +// A little pattern-matching library.
    1.24 +var Match =
    1.25 +
    1.26 +(function() {
    1.27 +
    1.28 +    function Pattern(template) {
    1.29 +        // act like a constructor even as a function
    1.30 +        if (!(this instanceof Pattern))
    1.31 +            return new Pattern(template);
    1.32 +
    1.33 +        this.template = template;
    1.34 +    }
    1.35 +
    1.36 +    Pattern.prototype = {
    1.37 +        match: function(act) {
    1.38 +            return match(act, this.template);
    1.39 +        },
    1.40 +
    1.41 +        matches: function(act) {
    1.42 +            try {
    1.43 +                return this.match(act);
    1.44 +            }
    1.45 +            catch (e if e instanceof MatchError) {
    1.46 +                return false;
    1.47 +            }
    1.48 +        },
    1.49 +
    1.50 +        assert: function(act, message) {
    1.51 +            try {
    1.52 +                return this.match(act);
    1.53 +            }
    1.54 +            catch (e if e instanceof MatchError) {
    1.55 +                throw new Error((message || "failed match") + ": " + e.message);
    1.56 +            }
    1.57 +        },
    1.58 +
    1.59 +        toString: function() "[object Pattern]"
    1.60 +    };
    1.61 +
    1.62 +    Pattern.ANY = new Pattern;
    1.63 +    Pattern.ANY.template = Pattern.ANY;
    1.64 +
    1.65 +    var quote = uneval;
    1.66 +
    1.67 +    function MatchError(msg) {
    1.68 +        this.message = msg;
    1.69 +    }
    1.70 +
    1.71 +    MatchError.prototype = {
    1.72 +        toString: function() {
    1.73 +            return "match error: " + this.message;
    1.74 +        }
    1.75 +    };
    1.76 +
    1.77 +    function isAtom(x) {
    1.78 +        return (typeof x === "number") ||
    1.79 +            (typeof x === "string") ||
    1.80 +            (typeof x === "boolean") ||
    1.81 +            (x === null) ||
    1.82 +            (typeof x === "object" && x instanceof RegExp);
    1.83 +    }
    1.84 +
    1.85 +    function isObject(x) {
    1.86 +        return (x !== null) && (typeof x === "object");
    1.87 +    }
    1.88 +
    1.89 +    function isArrayLike(x) {
    1.90 +        return isObject(x) && ("length" in x);
    1.91 +    }
    1.92 +
    1.93 +    function matchAtom(act, exp) {
    1.94 +        if ((typeof exp) === "number" && isNaN(exp)) {
    1.95 +            if ((typeof act) !== "number" || !isNaN(act))
    1.96 +                throw new MatchError("expected NaN, got: " + quote(act));
    1.97 +            return true;
    1.98 +        }
    1.99 +
   1.100 +        if (exp === null) {
   1.101 +            if (act !== null)
   1.102 +                throw new MatchError("expected null, got: " + quote(act));
   1.103 +            return true;
   1.104 +        }
   1.105 +
   1.106 +        if (exp instanceof RegExp) {
   1.107 +            if (!(act instanceof RegExp) || exp.source !== act.source)
   1.108 +                throw new MatchError("expected " + quote(exp) + ", got: " + quote(act));
   1.109 +            return true;
   1.110 +        }
   1.111 +
   1.112 +        switch (typeof exp) {
   1.113 +        case "string":
   1.114 +            if (act !== exp)
   1.115 +                throw new MatchError("expected " + exp.quote() + ", got " + quote(act));
   1.116 +            return true;
   1.117 +        case "boolean":
   1.118 +        case "number":
   1.119 +            if (exp !== act)
   1.120 +                throw new MatchError("expected " + exp + ", got " + quote(act));
   1.121 +            return true;
   1.122 +        }
   1.123 +
   1.124 +        throw new Error("bad pattern: " + exp.toSource());
   1.125 +    }
   1.126 +
   1.127 +    function matchObject(act, exp) {
   1.128 +        if (!isObject(act))
   1.129 +            throw new MatchError("expected object, got " + quote(act));
   1.130 +
   1.131 +        for (var key in exp) {
   1.132 +            if (!(key in act))
   1.133 +                throw new MatchError("expected property " + key.quote() + " not found in " + quote(act));
   1.134 +            match(act[key], exp[key]);
   1.135 +        }
   1.136 +
   1.137 +        return true;
   1.138 +    }
   1.139 +
   1.140 +    function matchArray(act, exp) {
   1.141 +        if (!isObject(act) || !("length" in act))
   1.142 +            throw new MatchError("expected array-like object, got " + quote(act));
   1.143 +
   1.144 +        var length = exp.length;
   1.145 +        if (act.length !== exp.length)
   1.146 +            throw new MatchError("expected array-like object of length " + length + ", got " + quote(act));
   1.147 +
   1.148 +        for (var i = 0; i < length; i++) {
   1.149 +            if (i in exp) {
   1.150 +                if (!(i in act))
   1.151 +                    throw new MatchError("expected array property " + i + " not found in " + quote(act));
   1.152 +                match(act[i], exp[i]);
   1.153 +            }
   1.154 +        }
   1.155 +
   1.156 +        return true;
   1.157 +    }
   1.158 +
   1.159 +    function match(act, exp) {
   1.160 +        if (exp === Pattern.ANY)
   1.161 +            return true;
   1.162 +
   1.163 +        if (exp instanceof Pattern)
   1.164 +            return exp.match(act);
   1.165 +
   1.166 +        if (isAtom(exp))
   1.167 +            return matchAtom(act, exp);
   1.168 +
   1.169 +        if (isArrayLike(exp))
   1.170 +            return matchArray(act, exp);
   1.171 +
   1.172 +        return matchObject(act, exp);
   1.173 +    }
   1.174 +
   1.175 +    return { Pattern: Pattern,
   1.176 +             MatchError: MatchError };
   1.177 +
   1.178 +})();
   1.179 +
   1.180 +function referencesVia(from, edge, to) {
   1.181 +    edge = "edge: " + edge;
   1.182 +    var edges = findReferences(to);
   1.183 +    if (edge in edges && edges[edge].indexOf(from) != -1)
   1.184 +        return true;
   1.185 +
   1.186 +    // Be nice: make it easy to fix if the edge name has just changed.
   1.187 +    var alternatives = [];
   1.188 +    for (var e in edges) {
   1.189 +        if (edges[e].indexOf(from) != -1)
   1.190 +            alternatives.push(e);
   1.191 +    }
   1.192 +    if (alternatives.length == 0) {
   1.193 +        print("referent not referred to by referrer after all");
   1.194 +    } else {
   1.195 +        print("referent is not referenced via: " + uneval(edge));
   1.196 +        print("but it is referenced via:       " + uneval(alternatives));
   1.197 +    }
   1.198 +    print("all incoming edges, from any object:");
   1.199 +    for (var e in edges)
   1.200 +        print(e);
   1.201 +    return false;
   1.202 +}
   1.203 +
   1.204 +// Note that AsmJS ArrayBuffers have a minimum size, currently 4096 bytes. If a
   1.205 +// smaller size is given, a regular ArrayBuffer will be returned instead.
   1.206 +function AsmJSArrayBuffer(size) {
   1.207 +    var ab = new ArrayBuffer(size);
   1.208 +    (new Function('global', 'foreign', 'buffer', '' +
   1.209 +'        "use asm";' +
   1.210 +'        var i32 = new global.Int32Array(buffer);' +
   1.211 +'        function g() {};' +
   1.212 +'        return g;' +
   1.213 +''))(Function("return this")(),null,ab);
   1.214 +    return ab;
   1.215 +}

mercurial