pluotsorbet/context.js

284 строки
7.2 KiB
JavaScript
Исходник Обычный вид История

2014-07-24 01:00:36 +04:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
2014-08-07 04:04:44 +04:00
function Context(runtime) {
2014-07-24 11:48:21 +04:00
this.frames = [];
this.runtime = runtime;
this.runtime.addContext(this);
}
Context.prototype.kill = function() {
this.runtime.removeContext(this);
}
Context.prototype.current = function() {
var frames = this.frames;
return frames[frames.length - 1];
}
Context.prototype.pushFrame = function(methodInfo, consumes) {
var caller = this.current();
var callee = new Frame(methodInfo, caller.stack, caller.stack.length - consumes);
this.frames.push(callee);
Instrument.callEnterHooks(methodInfo, caller, callee);
return callee;
}
Context.prototype.popFrame = function() {
var callee = this.frames.pop();
var caller = this.current();
Instrument.callExitHooks(callee.methodInfo, caller, callee);
if (callee.localsBase)
caller.stack.length = callee.localsBase;
return caller;
}
2014-07-26 00:09:49 +04:00
Context.prototype.pushClassInitFrame = function(classInfo) {
2014-08-07 04:11:27 +04:00
if (this.runtime.initialized[classInfo.className])
2014-07-26 00:09:49 +04:00
return;
2014-07-26 22:42:02 +04:00
classInfo.thread = this.thread;
var syntheticMethod = {
2014-09-22 12:46:49 +04:00
syntheticKey: "ClassInitSynthetic:" + classInfo.className,
2014-07-26 22:42:02 +04:00
classInfo: {
2014-09-26 06:20:41 +04:00
vmc: {},
vfc: {},
2014-07-26 22:42:02 +04:00
constant_pool: [
null,
2014-07-29 15:26:03 +04:00
{ tag: TAGS.CONSTANT_Methodref, class_index: 2, name_and_type_index: 4 },
{ tag: TAGS.CONSTANT_Class, name_index: 3 },
2014-07-26 22:42:02 +04:00
{ bytes: "java/lang/Class" },
{ name_index: 5, signature_index: 6 },
{ bytes: "invoke_clinit" },
{ bytes: "()V" },
2014-07-29 15:26:03 +04:00
{ tag: TAGS.CONSTANT_Methodref, class_index: 2, name_and_type_index: 8 },
2014-07-26 22:42:02 +04:00
{ name_index: 9, signature_index: 10 },
{ bytes: "init9" },
{ bytes: "()V" },
],
},
code: [
0x2a, // aload_0
0x59, // dup
0x59, // dup
0x59, // dup
0xc2, // monitorenter
2014-07-26 22:42:02 +04:00
0xb7, 0x00, 0x01, // invokespecial <idx=1>
0xb7, 0x00, 0x07, // invokespecial <idx=7>
0xc3, // monitorexit
2014-07-26 22:42:02 +04:00
0xb1, // return
],
exception_table: [],
};
this.current().stack.push(classInfo.getClassObject(this));
2014-07-26 22:42:02 +04:00
this.pushFrame(syntheticMethod, 1);
}
2014-07-26 00:09:49 +04:00
Context.prototype.raiseException = function(className, message) {
if (!message)
message = "";
message = "" + message;
var syntheticMethod = {
2014-09-22 12:35:12 +04:00
syntheticKey: "RaiseExceptionSynthetic",
classInfo: {
2014-09-26 06:20:41 +04:00
vmc: {},
vfc: {},
constant_pool: [
2014-07-26 00:23:10 +04:00
null,
2014-07-29 15:26:03 +04:00
{ tag: TAGS.CONSTANT_Class, name_index: 2 },
{ bytes: className },
2014-07-26 00:23:10 +04:00
{ tag: TAGS.CONSTANT_String, string_index: 4 },
{ bytes: message },
2014-07-29 15:26:03 +04:00
{ tag: TAGS.CONSTANT_Methodref, class_index: 1, name_and_type_index: 6 },
2014-07-26 00:23:10 +04:00
{ name_index: 7, signature_index: 8 },
{ bytes: "<init>" },
{ bytes: "(Ljava/lang/String;)V" },
],
},
code: [
2014-07-26 00:24:30 +04:00
0xbb, 0x00, 0x01, // new <idx=1>
0x59, // dup
0x12, 0x03, // ldc <idx=2>
0xb7, 0x00, 0x05, // invokespecial <idx=5>
0xbf // athrow
],
exception_table: [],
};
this.pushFrame(syntheticMethod, 0);
}
Context.prototype.raiseExceptionAndYield = function(className, message) {
this.raiseException(className, message);
throw VM.Yield;
}
2014-07-26 02:24:14 +04:00
Context.prototype.execute = function(stopFrame) {
Instrument.callResumeHooks(this.current());
do {
2014-07-26 00:09:49 +04:00
try {
VM.execute(this);
} catch (e) {
2014-07-26 04:54:34 +04:00
switch (e) {
case VM.Yield:
// Ignore the yield and continue executing instructions on this thread.
2014-07-26 04:54:34 +04:00
break;
case VM.Pause:
2014-09-22 10:59:15 +04:00
Instrument.callPauseHooks(this.current());
2014-07-26 04:54:34 +04:00
return;
default:
2014-07-26 00:09:49 +04:00
throw e;
2014-07-26 04:54:34 +04:00
}
2014-07-26 00:09:49 +04:00
}
} while (this.current() !== stopFrame);
2014-07-26 00:09:49 +04:00
}
2014-07-26 02:24:14 +04:00
Context.prototype.start = function(stopFrame) {
var ctx = this;
2014-07-26 04:54:34 +04:00
ctx.stopFrame = stopFrame;
2014-07-26 02:24:14 +04:00
window.setZeroTimeout(function() {
Instrument.callResumeHooks(ctx.current());
2014-07-26 02:24:14 +04:00
try {
VM.execute(ctx);
} catch (e) {
2014-07-26 04:54:34 +04:00
switch (e) {
case VM.Yield:
break;
case VM.Pause:
2014-09-22 10:59:15 +04:00
Instrument.callPauseHooks(ctx.current());
2014-07-26 04:54:34 +04:00
return;
default:
console.info(e);
2014-07-26 02:24:14 +04:00
throw e;
2014-07-26 04:54:34 +04:00
}
2014-07-26 02:24:14 +04:00
}
Instrument.callPauseHooks(ctx.current());
if (ctx.current() === stopFrame) {
ctx.kill();
return;
}
2014-07-26 02:24:14 +04:00
ctx.start(stopFrame);
});
}
2014-07-26 04:54:34 +04:00
Context.prototype.resume = function() {
this.start(this.stopFrame);
}
2014-07-26 22:42:02 +04:00
2014-07-29 12:52:56 +04:00
Context.prototype.block = function(obj, queue, lockLevel) {
2014-07-29 01:25:23 +04:00
if (!obj[queue])
obj[queue] = [];
obj[queue].push(this);
this.lockLevel = lockLevel;
2014-07-29 01:25:23 +04:00
throw VM.Pause;
}
2014-07-29 12:07:15 +04:00
Context.prototype.unblock = function(obj, queue, notifyAll, callback) {
while (obj[queue] && obj[queue].length) {
var ctx = obj[queue].pop();
if (!ctx)
continue;
callback(ctx);
if (!notifyAll)
break;
}
}
Context.prototype.wakeup = function(obj) {
2014-07-29 13:24:21 +04:00
if (this.lockTimeout !== null) {
window.clearTimeout(this.lockTimeout);
this.lockTimeout = null;
}
if (obj.lock) {
2014-07-29 13:24:21 +04:00
if (!obj.ready)
obj.ready = [];
obj.ready.push(this);
} else {
while (this.lockLevel-- > 0)
this.monitorEnter(obj);
this.resume();
2014-07-29 12:52:56 +04:00
}
}
2014-07-26 22:42:02 +04:00
Context.prototype.monitorEnter = function(obj) {
var lock = obj.lock;
if (!lock) {
obj.lock = { thread: this.thread, level: 1 };
2014-07-26 22:42:02 +04:00
return;
}
if (lock.thread === this.thread) {
++lock.level;
2014-07-26 22:42:02 +04:00
return;
}
this.block(obj, "ready", 1);
2014-07-26 22:42:02 +04:00
}
Context.prototype.monitorExit = function(obj) {
var lock = obj.lock;
if (lock.thread !== this.thread)
this.raiseExceptionAndYield("java/lang/IllegalMonitorStateException");
if (--lock.level > 0) {
2014-07-26 22:42:02 +04:00
return;
}
obj.lock = null;
2014-07-29 12:07:15 +04:00
this.unblock(obj, "ready", false, function(ctx) {
ctx.wakeup(obj);
2014-07-29 12:07:15 +04:00
});
}
Context.prototype.wait = function(obj, timeout) {
var lock = obj.lock;
if (!lock || lock.thread !== this.thread)
this.raiseExceptionAndYield("java/lang/IllegalMonitorStateException");
2014-07-29 12:07:15 +04:00
if (timeout < 0)
this.raiseExceptionAndYield("java/lang/IllegalArgumentException");
var lockLevel = lock.level;
while (lock.level > 0)
this.monitorExit(obj);
2014-07-29 12:52:56 +04:00
if (timeout) {
var self = this;
this.lockTimeout = window.setTimeout(function() {
obj.waiting.forEach(function(ctx, n) {
if (ctx === self) {
obj.waiting[n] = null;
ctx.wakeup(obj);
2014-07-29 12:52:56 +04:00
}
});
}, timeout);
} else {
this.lockTimeout = null;
}
this.block(obj, "waiting", lockLevel);
}
Context.prototype.notify = function(obj, notifyAll) {
if (!obj.lock || obj.lock.thread !== this.thread)
this.raiseExceptionAndYield("java/lang/IllegalMonitorStateException");
2014-07-29 12:07:15 +04:00
this.unblock(obj, "waiting", notifyAll, function(ctx) {
2014-07-29 13:24:21 +04:00
ctx.wakeup(obj);
2014-07-29 12:07:15 +04:00
});
2014-07-26 22:42:02 +04:00
}
2014-08-07 00:49:46 +04:00
Context.prototype.newPrimitiveArray = function(type, size) {
2014-08-07 04:04:44 +04:00
return this.runtime.newPrimitiveArray(type, size);
}
2014-08-07 00:52:18 +04:00
Context.prototype.newArray = function(typeName, size) {
2014-08-07 04:04:44 +04:00
return this.runtime.newArray(typeName, size);
2014-08-07 00:52:18 +04:00
}
2014-08-07 00:49:46 +04:00
Context.prototype.newMultiArray = function(typeName, lengths) {
2014-08-07 04:04:44 +04:00
return this.runtime.newMultiArray(typeName, lengths);
2014-08-07 00:49:46 +04:00
}
Context.prototype.newObject = function(classInfo) {
2014-08-07 04:04:44 +04:00
return this.runtime.newObject(classInfo);
}
Context.prototype.newString = function(s) {
2014-08-07 04:04:44 +04:00
return this.runtime.newString(s);
}