pluotsorbet/main.js

144 строки
3.9 KiB
JavaScript

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
function parseManifest(data) {
data
.replace(/\r\n|\r/g, "\n")
.replace(/\n /g, "")
.split("\n")
.forEach(function(entry) {
if (entry) {
var keyval = entry.split(':');
MIDP.manifest[keyval[0]] = keyval[1].trim();
}
});
}
function load(file, responseType, cb) {
var xhr = new XMLHttpRequest();
xhr.open("GET", file, true);
xhr.responseType = responseType;
xhr.onload = function () {
cb(xhr.response);
}
xhr.send(null);
}
function run(className, args) {
if (urlParams.pushConn && urlParams.pushMidlet) {
MIDP.pushRegistrations.push({
connection: urlParams.pushConn,
midlet: urlParams.pushMidlet,
filter: "*",
suiteId: "1",
id: ++MIDP.lastRegistrationId,
});
}
function startJVM() {
var jvm = new JVM();
var jars = ["java/classes.jar", "tests/tests.jar"];
if (urlParams.jars)
jars = jars.concat(urlParams.jars.split(":"));
(function loadNextJar() {
if (jars.length) {
var jar = jars.shift();
load(jar, "arraybuffer", function (data) {
jvm.addPath(jar, data);
loadNextJar();
});
} else {
jvm.initializeBuiltinClasses();
if (urlParams.jad) {
load(urlParams.jad, "text", function(data) {
parseManifest(data);
jvm.startIsolate0(className, args);
});
} else {
jvm.startIsolate0(className, args);
}
}
})();
}
fs.exists("/_main.ks", function(exists) {
if (exists) {
startJVM();
} else {
load("certs/_main.ks", "blob", function(data) {
fs.create("/_main.ks", data, function() {
startJVM();
});
});
}
});
}
// To launch the unit tests: ?main=RunTests
// To launch the MIDP demo: ?main=com/sun/midp/main/MIDletSuiteLoader&midletClassName=HelloCommandMIDlet
// To launch a JAR file: ?main=com/sun/midp/main/MIDletSuiteLoader&args=app.jar
fs.init(function() {
fs.mkdir("/Persistent", function() {
var main = urlParams.main || "com/sun/midp/main/MIDletSuiteLoader";
MIDP.midletClassName = urlParams.midletClassName ? urlParams.midletClassName.replace(/\//g, '.') : "RunTests";
if (MIDP.midletClassName == "RunTests") {
var element = document.createElement('script');
element.setAttribute("type", "text/javascript");
element.setAttribute("src", "tests/native.js");
document.getElementsByTagName("head")[0].appendChild(element);
var testContactsScript = document.createElement('script');
testContactsScript.setAttribute("type", "text/javascript");
testContactsScript.setAttribute("src", "tests/contacts.js");
document.getElementsByTagName("head")[0].appendChild(testContactsScript);
testContactsScript.onload = run.bind(null, main, urlParams.args);
} else {
run(main, urlParams.args);
}
});
});
function getIsOff(button) {
return button.textContent.contains("OFF");
}
function toggle(button) {
var isOff = getIsOff(button);
button.textContent = button.textContent.replace(isOff ? "OFF" : "ON", isOff ? "ON" : "OFF");
}
window.onload = function() {
document.getElementById("clearstorage").onclick = function() {
asyncStorage.clear();
};
document.getElementById("trace").onclick = function() {
VM.DEBUG = !VM.DEBUG;
toggle(this);
};
document.getElementById("printAllExceptions").onclick = function() {
VM.DEBUG_PRINT_ALL_EXCEPTIONS = !VM.DEBUG_PRINT_ALL_EXCEPTIONS;
toggle(this);
};
document.getElementById("profile").onclick = function() {
if (getIsOff(this)) {
Instrument.startProfile();
} else {
Instrument.stopProfile();
}
toggle(this);
};
if (Instrument.profiling) {
toggle(document.getElementById("profile"));
}
};
if (urlParams.profile && !/no|0/.test(urlParams.profile)) {
Instrument.startProfile();
}