forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
145 lines (129 loc) · 3.9 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- 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 load(file, responseType) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", file, true);
xhr.responseType = responseType;
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function() {
reject();
};
xhr.send(null);
});
}
function loadScript(path) {
return new Promise(function(resolve, reject) {
var element = document.createElement('script');
element.setAttribute("type", "text/javascript");
element.setAttribute("src", path);
document.getElementsByTagName("head")[0].appendChild(element);
element.onload = resolve;
});
}
// 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
var jvm = new JVM();
var main = urlParams.main || "com/sun/midp/main/MIDletSuiteLoader";
MIDP.midletClassName = urlParams.midletClassName ? urlParams.midletClassName.replace(/\//g, '.') : "RunTests";
var jars = ["java/classes.jar", "tests/tests.jar"];
if (urlParams.jars) {
jars = jars.concat(urlParams.jars.split(":"));
}
if (urlParams.pushConn && urlParams.pushMidlet) {
MIDP.ConnectionRegistry.addConnection({
connection: urlParams.pushConn,
midlet: urlParams.pushMidlet,
filter: "*",
suiteId: "1"
});
}
var initFS = new Promise(function(resolve, reject) {
fs.init(resolve);
}).then(function() {
return Promise.all([
new Promise(function(resolve, reject) {
fs.mkdir("/Persistent", resolve);
}),
new Promise(function(resolve, reject) {
fs.exists("/_main.ks", function(exists) {
if (exists) {
resolve();
} else {
load("certs/_main.ks", "blob").then(function(data) {
fs.create("/_main.ks", data, function() {
resolve();
});
});
}
});
})
]);
});
var loadingPromises = [initFS];
jars.forEach(function(jar) {
loadingPromises.push(load(jar, "arraybuffer").then(function(data) {
jvm.addPath(jar, data);
}));
});
if (urlParams.jad) {
loadingPromises.push(load(urlParams.jad, "text").then(function(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();
}
});
}));
}
if (MIDP.midletClassName == "RunTests") {
loadingPromises.push(loadScript("tests/native.js"),
loadScript("tests/contacts.js"),
loadScript("tests/override.js"));
}
Promise.all(loadingPromises).then(function() {
jvm.initializeBuiltinClasses();
jvm.startIsolate0(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();
}