dom/apps/tests/asmjs/asmjs_app.sjs

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/tests/asmjs/asmjs_app.sjs	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,143 @@
     1.4 +var Cc = Components.classes;
     1.5 +var Ci = Components.interfaces;
     1.6 +var Cu = Components.utils;
     1.7 +
     1.8 +Cu.import("resource://gre/modules/Services.jsm");
     1.9 +Cu.import("resource://gre/modules/NetUtil.jsm");
    1.10 +
    1.11 +function handleRequest(aRequest, aResponse) {
    1.12 +  aResponse.setHeader("Access-Control-Allow-Origin", "*", false);
    1.13 +
    1.14 +  var query = getQuery(aRequest);
    1.15 +
    1.16 +  if ("setVersion" in query) {
    1.17 +    setState("version", query.setVersion);
    1.18 +    aResponse.write("OK");
    1.19 +    return;
    1.20 +  }
    1.21 +  var version = Number(getState("version"));
    1.22 +
    1.23 +  // setPrecompile sets the "precompile" field in the app manifest
    1.24 +  if ("setPrecompile" in query) {
    1.25 +    setState("precompile", query.setPrecompile);
    1.26 +    aResponse.write("OK");
    1.27 +    return;
    1.28 +  }
    1.29 +  var precompile = getState("precompile");
    1.30 +
    1.31 +  if ("getPackage" in query) {
    1.32 +    aResponse.setHeader("Content-Type", "application/zip", false);
    1.33 +    aResponse.write(buildAppPackage(version, precompile));
    1.34 +    return;
    1.35 +  }
    1.36 +
    1.37 +  if ("getManifest" in query) {
    1.38 +    aResponse.setHeader("Content-Type", "application/x-web-app-manifest+json", false);
    1.39 +    aResponse.write(getManifest(version, precompile));
    1.40 +    return;
    1.41 +  }
    1.42 +}
    1.43 +
    1.44 +function getQuery(aRequest) {
    1.45 +  var query = {};
    1.46 +  aRequest.queryString.split('&').forEach(function(val) {
    1.47 +    var [name, value] = val.split('=');
    1.48 +    query[decodeURIComponent(name)] = decodeURIComponent(value);
    1.49 +  });
    1.50 +  return query;
    1.51 +}
    1.52 +
    1.53 +function getTestFile(aName) {
    1.54 +  var file = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
    1.55 +
    1.56 +  var path = "chrome/dom/apps/tests/asmjs/" + aName;
    1.57 +
    1.58 +  path.split("/").forEach(function(component) {
    1.59 +    file.append(component);
    1.60 +  });
    1.61 +
    1.62 +  return file;
    1.63 +}
    1.64 +
    1.65 +function readFile(aFile) {
    1.66 +  var fstream = Cc["@mozilla.org/network/file-input-stream;1"].
    1.67 +                createInstance(Ci.nsIFileInputStream);
    1.68 +  fstream.init(aFile, -1, 0, 0);
    1.69 +  var data = NetUtil.readInputStreamToString(fstream, fstream.available());
    1.70 +  fstream.close();
    1.71 +  return data;
    1.72 +}
    1.73 +
    1.74 +function getManifest(aVersion, aPrecompile) {
    1.75 +  return readFile(getTestFile("manifest.webapp")).
    1.76 +           replace(/VERSIONTOKEN/g, aVersion).
    1.77 +           replace(/PRECOMPILETOKEN/g, aPrecompile);
    1.78 +}
    1.79 +
    1.80 +function buildAppPackage(aVersion, aPrecompile) {
    1.81 +  const PR_RDWR        = 0x04;
    1.82 +  const PR_CREATE_FILE = 0x08;
    1.83 +  const PR_TRUNCATE    = 0x20;
    1.84 +
    1.85 +  let zipFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
    1.86 +  zipFile.append("application.zip");
    1.87 +
    1.88 +  let zipWriter = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
    1.89 +  zipWriter.open(zipFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
    1.90 +
    1.91 +  // Add index.html file to the zip file
    1.92 +  zipWriter.addEntryFile("index.html",
    1.93 +                         Ci.nsIZipWriter.COMPRESSION_NONE,
    1.94 +                         getTestFile("index.html"),
    1.95 +                         false);
    1.96 +
    1.97 +  // Add manifest to the zip file
    1.98 +  var manStream = Cc["@mozilla.org/io/string-input-stream;1"].
    1.99 +                  createInstance(Ci.nsIStringInputStream);
   1.100 +  var manifest = getManifest(aVersion, aPrecompile);
   1.101 +  manStream.setData(manifest, manifest.length);
   1.102 +  zipWriter.addEntryStream("manifest.webapp", Date.now(),
   1.103 +                           Ci.nsIZipWriter.COMPRESSION_NONE,
   1.104 +                           manStream, false);
   1.105 +
   1.106 +  // Add an asm.js module to the zip file
   1.107 +  // The module also contains some code to perform tests
   1.108 +
   1.109 +  // Creates a module big enough to be cached
   1.110 +  // The module is different according to the app version, this way
   1.111 +  // we can test that on update the new module is compiled
   1.112 +  var code = "function f() { 'use asm';\n";
   1.113 +  for (var i = 0; i < 5000; i++) {
   1.114 +    code += "function g" + i + "() { return " + i + "}\n";
   1.115 +  }
   1.116 +  code += "return g" + aVersion + " }\n\
   1.117 +var jsFuns = SpecialPowers.Cu.getJSTestingFunctions();\n\
   1.118 +ok(jsFuns.isAsmJSCompilationAvailable(), 'asm.js compilation is available');\n\
   1.119 +ok(jsFuns.isAsmJSModule(f), 'f is an asm.js module');\n\
   1.120 +var g" + aVersion + " = f();\n\
   1.121 +ok(jsFuns.isAsmJSFunction(g" + aVersion + "), 'g" + aVersion + " is an asm.js function');\n\
   1.122 +ok(g" + aVersion + "() === " + aVersion + ", 'g" + aVersion + " returns the correct result');";
   1.123 +
   1.124 +  // If the "precompile" field contains this module, we test that it is loaded
   1.125 +  // from the cache.
   1.126 +  // If the "precompile" field doesn't contain the module or is incorrect, we
   1.127 +  // test that the module is not loaded from the cache.
   1.128 +  if (aPrecompile == '["asmjsmodule.js"]') {
   1.129 +    code += "ok(jsFuns.isAsmJSModuleLoadedFromCache(f), 'module loaded from cache');\n";
   1.130 +  } else {
   1.131 +    code += "ok(!jsFuns.isAsmJSModuleLoadedFromCache(f), 'module not loaded from cache');\n";
   1.132 +  }
   1.133 +
   1.134 +  code += "alert('DONE');";
   1.135 +
   1.136 +  var jsStream = Cc["@mozilla.org/io/string-input-stream;1"].
   1.137 +                 createInstance(Ci.nsIStringInputStream);
   1.138 +  jsStream.setData(code, code.length);
   1.139 +  zipWriter.addEntryStream("asmjsmodule.js", Date.now(),
   1.140 +                           Ci.nsIZipWriter.COMPRESSION_NONE,
   1.141 +                           jsStream, false);
   1.142 +
   1.143 +  zipWriter.close();
   1.144 +
   1.145 +  return readFile(zipFile);
   1.146 +}

mercurial