edit
[c11concurrency-benchmarks.git] / jsbench-2013.1 / browsercheck.js
1 /*
2  * Copyright (C) 2012 Purdue University
3  * Written by Gregor Richards
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  * 
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 (function() {
29     /* This kind of test is very difficult to generalize, so we just avoid
30      * browsers known not to work */
31     var browserMin = {
32         "mozilla": "2.0",
33         "webkit": "536.7",
34         "v8": "10.0",
35         "msie": "9.0",
36         "opera": "11.60"
37     };
38
39     /* Since Chrome doesn't use the same JS engine as WebKit, it actually has
40      * different dependencies */
41     var rchrome = /Chrome[\/]([\w.]+)/;
42     var match = rchrome.exec(navigator.userAgent);
43     if (match) {
44         delete $.browser.webkit;
45         $.browser.v8 = true;
46         $.browser.version = match[1];
47     }
48
49     var browserName = {
50         "mozilla": "Mozilla",
51         "webkit": "WebKit",
52         "v8": "Chrome",
53         "msie": "Microsoft Internet Explorer",
54         "opera": "Opera"
55     };
56
57     var versionName = {
58         "mozilla": "2.0 (e.g. Firefox 4.0)"
59     };
60
61     var real = "harness.html";
62
63     // version-number-based comparison
64     function versionCheck(min, real) {
65         var minComps = min.split(".");
66         var realComps = real.split(".");
67         while (realComps.length < minComps.length) realComps.push("0");
68
69         for (var i = 0; i < minComps.length; i++) {
70             var min = parseInt(minComps[i]);
71             var real = parseInt(realComps[i]);
72             if (real < min) return false;
73             else if (real > min) return true;
74         }
75
76         return true;
77     }
78
79     // test the browser version
80     window.onload = function() {
81         var badBrowser = false;
82         var goodVersion = 0;
83         var badVersion = 0;
84         for (var browser in browserMin) {
85             var version = browserMin[browser];
86             if ($.browser[browser]) {
87                 if (!versionCheck(version, $.browser.version)) {
88                     badBrowser = browser;
89                     goodVersion = version;
90                     if (badBrowser in versionName) goodVersion = versionName[badBrowser];
91                     badVersion = $.browser.version;
92                 }
93             }
94         }
95
96         var output = document.getElementById("output");
97         if (badBrowser) {
98             // we don't like your browser
99             output.innerHTML =
100                 "Sorry, but your browser is not supported. The minimum version of <b>" +
101                 browserName[badBrowser] +
102                 "</b> required to run these benchmarks is <b>" +
103                 goodVersion +
104                 "</b> (You are using <b>" +
105                 badVersion +
106                 "</b>). If you would like to attempt to run them anyway, <a href=\"" +
107                 real +
108                 "\">click here</a>, but it is highly likely that they will not " +
109                 "run, and if they do, the results may be unreliable.";
110
111         } else {
112             output.innerHTML =
113                 "Loading benchmarks ...";
114             window.location.href = real;
115
116         }
117     };
118 })();