forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jvm.js
95 lines (74 loc) · 3.23 KB
/
jvm.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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
'use strict';
var CLASSES;
var JVM = function() {
if (this instanceof JVM) {
CLASSES = new Classes();
} else {
return new JVM();
}
}
JVM.prototype.addPath = function(path, data) {
return CLASSES.addPath(path, data);
}
JVM.prototype.initializeBuiltinClasses = function() {
// These classes are guaranteed to not have a static initializer.
CLASSES.java_lang_Object = CLASSES.loadClass("java/lang/Object");
CLASSES.java_lang_Class = CLASSES.loadClass("java/lang/Class");
CLASSES.java_lang_String = CLASSES.loadClass("java/lang/String");
CLASSES.java_lang_Thread = CLASSES.loadClass("java/lang/Thread");
}
JVM.prototype.startIsolate0 = function(className, args) {
var runtime = new Runtime(this);
var ctx = new Context(runtime);
var com_sun_cldc_isolate_Isolate = CLASSES.getClass("com/sun/cldc/isolate/Isolate");
var isolate = ctx.newObject(com_sun_cldc_isolate_Isolate);
isolate.id = util.id();
var caller = new Frame();
ctx.frames.push(caller);
var array = ctx.newArray("[Ljava/lang/String;", args.length);
for (var n = 0; n < args.length; ++n)
array[n] = args[n] ? ctx.newString(args[n]) : null;
caller.stack.push(isolate);
caller.stack.push(ctx.newString(className.replace(/\./g, "/")));
caller.stack.push(array);
ctx.pushFrame(CLASSES.getMethod(com_sun_cldc_isolate_Isolate, "I.<init>.(Ljava/lang/String;[Ljava/lang/String;)V"), 3);
ctx.execute(caller);
caller.stack.push(isolate);
ctx.pushFrame(CLASSES.getMethod(com_sun_cldc_isolate_Isolate, "I.start.()V"), 1);
ctx.start(caller);
}
JVM.prototype.startIsolate = function(isolate) {
var mainClass = util.fromJavaString(isolate.class.getField("I._mainClass.Ljava/lang/String;").get(isolate)).replace(/\./g, "/");
var mainArgs = isolate.class.getField("I._mainArgs.[Ljava/lang/String;").get(isolate);
mainArgs.forEach(function(str, n) {
mainArgs[n] = util.fromJavaString(str);
});
var runtime = new Runtime(this);
var ctx = new Context(runtime);
isolate.runtime = runtime;
runtime.isolate = isolate;
runtime.updateStatus(2); // STARTED
var classInfo = CLASSES.getClass(mainClass);
if (!classInfo)
throw new Error("Could not find or load main class " + mainName);
var entryPoint = CLASSES.getEntryPoint(classInfo);
if (!entryPoint)
throw new Error("Could not find main method in class " + mainName);
var caller = new Frame();
ctx.frames.push(caller);
ctx.thread = runtime.mainThread = ctx.newObject(CLASSES.java_lang_Thread);
ctx.thread.pid = util.id();
ctx.thread.alive = true;
caller.stack.push(runtime.mainThread);
caller.stack.push(ctx.newString("main"));
ctx.pushFrame(CLASSES.getMethod(CLASSES.java_lang_Thread, "I.<init>.(Ljava/lang/String;)V"), 2);
ctx.execute(caller);
var args = ctx.newArray("[Ljava/lang/String;", mainArgs.length);
for (var n = 0; n < mainArgs.length; ++n)
args[n] = mainArgs[n] ? ctx.newString(mainArgs[n]) : null;
caller.stack.push(args);
ctx.pushFrame(entryPoint, 1);
ctx.start(caller);
}