forked from mozilla/pluotsorbet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsshell.js
executable file
·252 lines (217 loc) · 5.65 KB
/
jsshell.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
// Define objects and functions that j2me.js expects
// but are unavailable in the shell environment.
if (typeof console === "undefined") {
var console = {
log: print,
}
}
console.info = function (c) {
putstr(String.fromCharCode(c));
};
console.error = function (c) {
putstr(String.fromCharCode(c));
};
var START_TIME = dateNow();
var performance = {
now: function () {
return dateNow();
}
};
function check() {
}
function parseArguments(options, tokens) {
var leftover = [];
for (var i = 0; i < tokens.length; i++) {
var tokenParts = tokens[i].split("=");
var name = null;
var value = tokenParts[1];
if (tokenParts[0].indexOf("--") === 0) {
name = tokenParts[0].substring(2);
} else if (tokenParts[0].indexOf("-") === 0) {
var shortName = tokenParts[0].substring(1);
name = shortName;
for (var longName in options) {
if (options[longName].short === shortName) {
name = longName;
break;
}
}
}
if (tokens[i][0] === "-") {
if (options[name]) {
switch (options[name].type) {
case "number":
options[name].value = Number(value);
break;
case "string":
options[name].value = value;
break;
}
} else {
print("Illegal option: " + name);
quit();
}
} else {
leftover.push(tokens[i]);
}
}
return leftover;
}
var options = {
"writers": {
short: "w",
value: "",
type: "string"
}
};
var files = parseArguments(options, scriptArgs);
if (files.length !== 1) {
print("error: One main class name must be specified.");
print("usage: jsshell <main class name>");
quit(1);
}
var callbacks = [];
var navigator = {
language: "en-US",
};
var document = {
documentElement: {
classList: {
add: function() {
},
},
},
querySelector: function() {
return {
addEventListener: function() {
},
};
},
getElementById: function() {
return {
addEventListener: function() {
},
getContext: function() {
return {
save: function() {
},
};
},
getBoundingClientRect: function() {
return { top: 0, left: 0, width: 0, height: 0 };
},
querySelector: function() {
return { style: "" };
},
dispatchEvent: function(event) {
},
style: "",
};
},
addEventListener: function() {
},
};
var microTaskQueue = null;
var window = {
addEventListener: function() {
},
crypto: {
getRandomValues: function() {
},
},
};
this.setZeroTimeout = window.setZeroTimeout = window.setTimeout = this.setTimeout = function (fn, interval) {
var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : [];
var task = microTaskQueue.scheduleInterval(fn, args, interval, false);
return task.id;
};
window.setInterval = function (fn, interval) {
var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : [];
var task = microTaskQueue.scheduleInterval(fn, args, interval, true);
return task.id;
};
window.clearTimeout = function (id) {
microTaskQueue.remove(id);
};
var Event = function() {
}
var config = {
logConsole: "native",
args: "",
};
var profileTimeline = false;
try {
if (profileTimeline) {
load("bld/shumway.js");
}
load("polyfill/promise.js", "libs/relooper.js", "libs/native.js", "bld/j2me.js",
"libs/zipfile.js",
"blackBox.js",
"libs/encoding.js",
"util.js",
"libs/jarstore.js",
"libs/long.js",
"native.js",
"midp/midp.js",
"midp/frameanimator.js",
"midp/fs.js",
"midp/crypto.js",
"midp/text_editor.js",
"midp/device_control.js",
"midp/crypto.js",
"libs/forge/util.js",
"libs/forge/md5.js",
"libs/jsbn/jsbn.js",
"libs/jsbn/jsbn2.js"
// "bld/classes.jar.js"
);
microTaskQueue = new J2ME.Shell.MicroTasksQueue();
// load("bld/classes.jar.js");
// load("bld/program.jar.js");
// load("bld/tests.jar.js");
// Define this down here so it overrides the version defined by native.js.
console.print = function (c) {
putstr(String.fromCharCode(c));
};
var dump = putstr;
CLASSES.addSourceDirectory("java/cldc1.1.1");
CLASSES.addSourceDirectory("java/midp");
// CLASSES.addSourceDirectory("bench/scimark2src");
JARStore.addBuiltIn("java/classes.jar", snarf("java/classes.jar", "binary").buffer);
JARStore.addBuiltIn("tests/tests.jar", snarf("tests/tests.jar", "binary").buffer);
JARStore.addBuiltIn("bench/benchmark.jar", snarf("bench/benchmark.jar", "binary").buffer);
//JARStore.addBuiltIn("program.jar", snarf("program.jar", "binary").buffer);
CLASSES.initializeBuiltinClasses();
var start = dateNow();
var jvm = new JVM();
var writers = J2ME.WriterFlags.None;
if (options.writers.value.indexOf("t") >= 0) {
writers |= J2ME.WriterFlags.Trace;
}
if (options.writers.value.indexOf("s") >= 0) {
writers |= J2ME.WriterFlags.TraceStack;
}
if (options.writers.value.indexOf("h") >= 0) {
writers |= J2ME.WriterFlags.Thread;
}
J2ME.writers = writers;
J2ME.enableRuntimeCompilation = false;
start = dateNow();
var runtime = jvm.startIsolate0(files[0], config.args);
// Pump Event Queue
microTaskQueue.run(100000, 10000, false, function () {
return true;
});
print("Time: " + (dateNow() - start).toFixed(4) + " ms");
print("Bytecodes: " + J2ME.bytecodeCount);
J2ME.interpreterCounter.traceSorted(new J2ME.IndentingWriter(false, function (x) {
print(x);
}));
if (profileTimeline) {
J2ME.timeline.createSnapshot().trace(new J2ME.IndentingWriter());
}
} catch (x) {
print(x);
print(x.stack);
}