|
1 var Cc = Components.classes; |
|
2 var Ci = Components.interfaces; |
|
3 var Cu = Components.utils; |
|
4 |
|
5 Cu.import("resource://gre/modules/Services.jsm"); |
|
6 Cu.import("resource://gre/modules/NetUtil.jsm"); |
|
7 |
|
8 function handleRequest(aRequest, aResponse) { |
|
9 aResponse.setHeader("Access-Control-Allow-Origin", "*", false); |
|
10 |
|
11 var query = getQuery(aRequest); |
|
12 |
|
13 if ("setVersion" in query) { |
|
14 setState("version", query.setVersion); |
|
15 aResponse.write("OK"); |
|
16 return; |
|
17 } |
|
18 var version = Number(getState("version")); |
|
19 |
|
20 // setPrecompile sets the "precompile" field in the app manifest |
|
21 if ("setPrecompile" in query) { |
|
22 setState("precompile", query.setPrecompile); |
|
23 aResponse.write("OK"); |
|
24 return; |
|
25 } |
|
26 var precompile = getState("precompile"); |
|
27 |
|
28 if ("getPackage" in query) { |
|
29 aResponse.setHeader("Content-Type", "application/zip", false); |
|
30 aResponse.write(buildAppPackage(version, precompile)); |
|
31 return; |
|
32 } |
|
33 |
|
34 if ("getManifest" in query) { |
|
35 aResponse.setHeader("Content-Type", "application/x-web-app-manifest+json", false); |
|
36 aResponse.write(getManifest(version, precompile)); |
|
37 return; |
|
38 } |
|
39 } |
|
40 |
|
41 function getQuery(aRequest) { |
|
42 var query = {}; |
|
43 aRequest.queryString.split('&').forEach(function(val) { |
|
44 var [name, value] = val.split('='); |
|
45 query[decodeURIComponent(name)] = decodeURIComponent(value); |
|
46 }); |
|
47 return query; |
|
48 } |
|
49 |
|
50 function getTestFile(aName) { |
|
51 var file = Services.dirsvc.get("CurWorkD", Ci.nsIFile); |
|
52 |
|
53 var path = "chrome/dom/apps/tests/asmjs/" + aName; |
|
54 |
|
55 path.split("/").forEach(function(component) { |
|
56 file.append(component); |
|
57 }); |
|
58 |
|
59 return file; |
|
60 } |
|
61 |
|
62 function readFile(aFile) { |
|
63 var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. |
|
64 createInstance(Ci.nsIFileInputStream); |
|
65 fstream.init(aFile, -1, 0, 0); |
|
66 var data = NetUtil.readInputStreamToString(fstream, fstream.available()); |
|
67 fstream.close(); |
|
68 return data; |
|
69 } |
|
70 |
|
71 function getManifest(aVersion, aPrecompile) { |
|
72 return readFile(getTestFile("manifest.webapp")). |
|
73 replace(/VERSIONTOKEN/g, aVersion). |
|
74 replace(/PRECOMPILETOKEN/g, aPrecompile); |
|
75 } |
|
76 |
|
77 function buildAppPackage(aVersion, aPrecompile) { |
|
78 const PR_RDWR = 0x04; |
|
79 const PR_CREATE_FILE = 0x08; |
|
80 const PR_TRUNCATE = 0x20; |
|
81 |
|
82 let zipFile = Services.dirsvc.get("TmpD", Ci.nsIFile); |
|
83 zipFile.append("application.zip"); |
|
84 |
|
85 let zipWriter = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter); |
|
86 zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); |
|
87 |
|
88 // Add index.html file to the zip file |
|
89 zipWriter.addEntryFile("index.html", |
|
90 Ci.nsIZipWriter.COMPRESSION_NONE, |
|
91 getTestFile("index.html"), |
|
92 false); |
|
93 |
|
94 // Add manifest to the zip file |
|
95 var manStream = Cc["@mozilla.org/io/string-input-stream;1"]. |
|
96 createInstance(Ci.nsIStringInputStream); |
|
97 var manifest = getManifest(aVersion, aPrecompile); |
|
98 manStream.setData(manifest, manifest.length); |
|
99 zipWriter.addEntryStream("manifest.webapp", Date.now(), |
|
100 Ci.nsIZipWriter.COMPRESSION_NONE, |
|
101 manStream, false); |
|
102 |
|
103 // Add an asm.js module to the zip file |
|
104 // The module also contains some code to perform tests |
|
105 |
|
106 // Creates a module big enough to be cached |
|
107 // The module is different according to the app version, this way |
|
108 // we can test that on update the new module is compiled |
|
109 var code = "function f() { 'use asm';\n"; |
|
110 for (var i = 0; i < 5000; i++) { |
|
111 code += "function g" + i + "() { return " + i + "}\n"; |
|
112 } |
|
113 code += "return g" + aVersion + " }\n\ |
|
114 var jsFuns = SpecialPowers.Cu.getJSTestingFunctions();\n\ |
|
115 ok(jsFuns.isAsmJSCompilationAvailable(), 'asm.js compilation is available');\n\ |
|
116 ok(jsFuns.isAsmJSModule(f), 'f is an asm.js module');\n\ |
|
117 var g" + aVersion + " = f();\n\ |
|
118 ok(jsFuns.isAsmJSFunction(g" + aVersion + "), 'g" + aVersion + " is an asm.js function');\n\ |
|
119 ok(g" + aVersion + "() === " + aVersion + ", 'g" + aVersion + " returns the correct result');"; |
|
120 |
|
121 // If the "precompile" field contains this module, we test that it is loaded |
|
122 // from the cache. |
|
123 // If the "precompile" field doesn't contain the module or is incorrect, we |
|
124 // test that the module is not loaded from the cache. |
|
125 if (aPrecompile == '["asmjsmodule.js"]') { |
|
126 code += "ok(jsFuns.isAsmJSModuleLoadedFromCache(f), 'module loaded from cache');\n"; |
|
127 } else { |
|
128 code += "ok(!jsFuns.isAsmJSModuleLoadedFromCache(f), 'module not loaded from cache');\n"; |
|
129 } |
|
130 |
|
131 code += "alert('DONE');"; |
|
132 |
|
133 var jsStream = Cc["@mozilla.org/io/string-input-stream;1"]. |
|
134 createInstance(Ci.nsIStringInputStream); |
|
135 jsStream.setData(code, code.length); |
|
136 zipWriter.addEntryStream("asmjsmodule.js", Date.now(), |
|
137 Ci.nsIZipWriter.COMPRESSION_NONE, |
|
138 jsStream, false); |
|
139 |
|
140 zipWriter.close(); |
|
141 |
|
142 return readFile(zipFile); |
|
143 } |