Skip to content

Commit

Permalink
initialize main thread during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgal committed Jul 20, 2014
1 parent 22f90f5 commit 18724d8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ Classes.prototype.newObject = function(className) {
return new (this.getClass(className, true).constructor)();
}

Classes.prototype.invokeConstructor = function(obj) {
var ctor = CLASSES.getMethod(obj.class, "<init>", "()V", false, false);
VM.invoke(ctor, [obj]);
}

Classes.prototype.newPrimitiveArray = function(type, size) {
var constructor = ARRAYS[type];
if (!constructor.prototype.class)
Expand Down
9 changes: 9 additions & 0 deletions frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,12 @@ Frame.prototype.backTrace = function() {
}
return stack.join("\n");
}

Frame.prototype.getThread = function() {
for (var frame = this; frame.caller; frame = frame.caller) {
var thread = frame.thread;
if (thread)
return thread;
}
return null;
}
3 changes: 2 additions & 1 deletion java/cldc1.1.1/java/lang/Thread.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ private void init(Runnable target, String name) {
Thread parent = currentThread();
this.target = target;
this.name = name.toCharArray();
this.priority = parent.getPriority();
if (parent != null)
this.priority = parent.getPriority();
setPriority(priority);
}

Expand Down
4 changes: 3 additions & 1 deletion jvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ JVM.prototype.run = function(className) {
ensureInitialized("java/lang/Object");
ensureInitialized("java/lang/Class");
ensureInitialized("java/lang/String");
ensureInitialized("java/lang/Thread");
ensureInitialized("java/lang/Throwable");
ensureInitialized("java/lang/Error");

var mainThread = CLASSES.newObject("java/lang/Thread");
CLASSES.invokeConstructor(mainThread);

VM.invoke(entryPoint, [null]);
}
15 changes: 12 additions & 3 deletions native.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Native.prototype.invokeNative = function(caller, methodInfo) {
methodInfo.name + "." +
methodInfo.signature];
}
var result = methodInfo.native.apply(null, args);
var result = methodInfo.native.apply(caller, args);
if (signature.OUT.length)
pushType(signature.OUT[0].type, result);
}
Expand Down Expand Up @@ -121,8 +121,7 @@ Native.prototype["java/lang/Class.newInstance.()Ljava/lang/Object;"] = function(
var classInfo = classObject.vmClass;
CLASSES.initClass(classInfo);
var obj = new (classInfo.constructor)();
var ctor = CLASSES.getMethod(classInfo, "<init>", "()V", false, false);
VM.invoke(ctor, [obj]);
CLASSES.invokeConstructor(obj);
return obj;
};

Expand Down Expand Up @@ -207,6 +206,16 @@ Native.prototype["java/lang/Math.floor.(D)D"] = function(d) {
return Math.floor(d);
}

Native.prototype["java/lang/Thread.currentThread.()Ljava/lang/Thread;"] = function() {
return this.getThread();
}

Native.prototype["java/lang/Thread.setPriority0.(II)V"] = function(thread, oldPriority, newPriority) {
}

Native.prototype["java/lang/Thread.start0.()V"] = function() {
}

Native.prototype["com/sun/cldchi/io/ConsoleOutputStream.write.(I)V"] = (function() {
var s = "";
return function(obj, ch) {
Expand Down

0 comments on commit 18724d8

Please sign in to comment.