pluotsorbet/native.js

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

2014-07-13 01:07:11 +04:00
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
2014-07-21 08:32:47 +04:00
'Use strict';
2014-07-13 01:07:11 +04:00
2014-07-21 08:32:47 +04:00
var Native = {};
2014-07-15 10:47:20 +04:00
Native.invoke = function(ctx, methodInfo) {
2014-07-21 09:08:08 +04:00
if (!methodInfo.native) {
var key = methodInfo.classInfo.className + "." + methodInfo.name + "." + methodInfo.signature;
2014-07-24 11:21:59 +04:00
methodInfo.native = Native[key];
2014-07-21 09:08:08 +04:00
}
methodInfo.native.call(null, ctx, ctx.current().stack);
2014-07-13 01:07:11 +04:00
}
Native["java/lang/System.arraycopy.(Ljava/lang/Object;ILjava/lang/Object;II)V"] = function(ctx, stack) {
var length = stack.pop(), dstOffset = stack.pop(), dst = stack.pop(), srcOffset = stack.pop(), src = stack.pop();
if (!src || !dst)
ctx.raiseException("java/lang/NullPointerException", "Cannot copy to/from a null array.");
var srcClass = src.class;
var dstClass = dst.class;
if (!srcClass.isArrayClass || !dstClass.isArrayClass)
ctx.raiseException("java/lang/ArrayStoreException", "Can only copy to/from array types.");
if (srcClass != dstClass && (!srcClass.elementClass || !dstClass.elementClass || !srcClass.elementClass.isAssignableTo(dstClass.elementClass)))
ctx.raiseException("java/lang/ArrayStoreException", "Incompatible component types.");
if (srcOffset < 0 || (srcOffset+length) > src.length || dstOffset < 0 || (dstOffset+length) > dst.length || length < 0)
ctx.raiseException("java/lang/ArrayIndexOutOfBoundsException", "Invalid index.");
2014-07-13 20:03:11 +04:00
if (dst !== src || dstOffset < srcOffset) {
for (var n = 0; n < length; ++n)
dst[dstOffset++] = src[srcOffset++];
} else {
dstOffset += length;
srcOffset += length;
for (var n = 0; n < length; ++n)
dst[--dstOffset] = src[--srcOffset];
2014-07-13 10:13:53 +04:00
}
}
2014-07-15 09:13:01 +04:00
Native["java/lang/System.getProperty0.(Ljava/lang/String;)Ljava/lang/String;"] = function(ctx, stack) {
var key = stack.pop();
var value;
2014-07-18 09:00:32 +04:00
switch (util.fromJavaString(key)) {
2014-07-15 09:13:01 +04:00
case "microedition.encoding":
value = ctx.newString("UTF-8");
break;
default:
console.log("UNKNOWN PROPERTY: " + util.fromJavaString(key));
value = null;
break;
2014-07-15 09:13:01 +04:00
}
stack.push(value);
2014-07-15 09:13:01 +04:00
}
2014-07-15 10:19:26 +04:00
Native["java/lang/System.currentTimeMillis.()J"] = function(ctx, stack) {
stack.push2(Long.fromNumber(Date.now()));
}
Native["com/sun/cldchi/jvm/JVM.unchecked_char_arraycopy.([CI[CII)V"] = function(ctx, stack) {
var length = stack.pop(), dstOffset = stack.pop(), dst = stack.pop(), srcOffset = stack.pop(), src = stack.pop();
2014-07-15 10:19:26 +04:00
if (dst !== src || dstOffset < srcOffset) {
for (var n = 0; n < length; ++n)
dst[dstOffset++] = src[srcOffset++];
} else {
dstOffset += length;
srcOffset += length;
for (var n = 0; n < length; ++n)
dst[--dstOffset] = src[--srcOffset];
}
}
2014-07-15 10:37:33 +04:00
Native["java/lang/Object.getClass.()Ljava/lang/Class;"] = function(ctx, stack) {
stack.push(stack.pop().class.getClassObject());
2014-07-18 10:02:28 +04:00
}
Native["java/lang/Class.getName.()Ljava/lang/String;"] = function(ctx, stack) {
var obj = stack.pop();
stack.push(util.cache(obj, "getName", function () {
return ctx.newString(obj.vmClass.className.replace("/", ".", "g"));
}));
2014-07-18 10:02:28 +04:00
}
Native["java/lang/Class.forName.(Ljava/lang/String;)Ljava/lang/Class;"] = function(ctx, stack) {
var name = stack.pop();
2014-07-18 09:00:32 +04:00
var className = util.fromJavaString(name).replace(".", "/", "g");
2014-07-18 12:12:38 +04:00
var classInfo = (className[0] === "[") ? null : CLASSES.getClass(className);
2014-07-15 10:47:20 +04:00
if (!classInfo) {
ctx.raiseException("java/lang/ClassNotFoundException", "'" + className + "' not found.");
2014-07-15 10:47:20 +04:00
}
stack.push(classInfo.getClassObject());
2014-07-15 10:47:20 +04:00
}
Native["java/lang/Class.newInstance.()Ljava/lang/Object;"] = function(ctx, stack) {
var classObject = stack.pop();
var className = classObject.vmClass.className;
var syntheticMethod = {
classInfo: {
constant_pool: [
{ name_index: 1 },
{ bytes: className },
{ class_index: 0, name_and_type_index: 3 },
{ name_index: 4, signature_index: 5 },
{ bytes: "<init>" },
{ bytes: "()V" },
]
},
code: [
0xbb, 0x00, 0x00, // new <idx=0>
0x59, // dup
0xb7, 0x00, 0x02, // invokespecial <idx=2>
0xb0 // areturn
],
};
ctx.pushFrame(syntheticMethod, 0);
throw VM.Yield;
};
Native["java/lang/Class.isInterface.()Z"] = function(ctx, stack) {
var classObject = stack.pop();
2014-07-19 04:55:09 +04:00
var classInfo = classObject.vmClass;
stack.push(ACCESS_FLAGS.isInterface(classInfo.access_flags) ? 1 : 0);
2014-07-19 04:55:09 +04:00
}
Native["java/lang/Class.isArray.()Z"] = function(ctx, stack) {
var classObject = stack.pop();
2014-07-19 04:55:09 +04:00
var classInfo = classObject.vmClass;
stack.push(classInfo.isArrayClass ? 1 : 0);
2014-07-19 04:55:09 +04:00
}
Native["java/lang/Class.isAssignableFrom.(Ljava/lang/Class;)Z"] = function(ctx, stack) {
var fromClass = stack.pop(), classObject = stack.pop();
if (!fromClass)
ctx.raiseException("java/lang/NullPointerException");
stack.push(fromClass.vmClass.isAssignableTo(classObject.vmClass) ? 1 : 0);
2014-07-19 04:55:09 +04:00
}
Native["java/lang/Class.isInstance.(Ljava/lang/Object;)Z"] = function(ctx, stack) {
var obj = stack.pop(), classObject = stack.pop();
stack.push((obj && obj.class.isAssignableTo(classObject.vmClass)) ? 1 : 0);
2014-07-19 04:55:09 +04:00
}
Native["java/lang/Float.floatToIntBits.(F)I"] = (function(ctx, stack) {
2014-07-19 01:44:50 +04:00
var fa = Float32Array(1);
var ia = Int32Array(fa.buffer);
return function(ctx, stack) {
fa[0] = stack.pop();
stack.push(ia[0]);
2014-07-19 01:44:50 +04:00
}
})();
Native["java/lang/Double.doubleToLongBits.(D)J"] = (function(ctx, stack) {
2014-07-19 01:44:50 +04:00
var da = Float64Array(1);
var ia = Int32Array(da.buffer);
return function(d) {
da[0] = stack.pop2();
stack.push2(Long.fromBits(ia[0], ia[1]));
2014-07-19 01:44:50 +04:00
}
})();
Native["java/lang/Float.intBitsToFloat.(I)F"] = (function(ctx, stack) {
var fa = Float32Array(1);
var ia = Int32Array(fa.buffer);
return function(i) {
ia[0] = stack.pop();
stack.push(fa[0]);
}
})();
Native["java/lang/Double.longBitsToDouble.(J)D"] = (function(ctx, stack) {
var da = Float64Array(1);
var ia = Int32Array(da.buffer);
return function() {
var l = stack.pop2();
2014-07-19 05:33:12 +04:00
ia[0] = l.low_;
ia[1] = l.high_;
stack.push2(da[0]);
}
})();
Native["java/lang/Throwable.fillInStackTrace.()V"] = (function(ctx, stack) {
2014-07-18 10:02:28 +04:00
});
Native["java/lang/Throwable.obtainBackTrace.()Ljava/lang/Object;"] = (function(ctx, stack) {
stack.push(null);
2014-07-19 04:55:09 +04:00
});
Native["java/lang/Runtime.freeMemory.()J"] = function(ctx, stack) {
stack.push2(Long.fromInt(0x800000));
2014-07-19 20:23:14 +04:00
}
Native["java/lang/Runtime.totalMemory.()J"] = function(ctx, stack) {
stack.push2(Long.fromInt(0x1000000));
2014-07-19 20:23:14 +04:00
}
Native["java/lang/Runtime.gc.()V"] = function(ctx, stack) {
2014-07-19 20:23:14 +04:00
}
Native["java/lang/Math.floor.(D)D"] = function(ctx, stack) {
stack.push2(Math.floor(stack.pop2()));
}
Native["java/lang/Thread.currentThread.()Ljava/lang/Thread;"] = function(ctx, stack) {
stack.push(ctx.thread);
2014-07-20 11:35:00 +04:00
}
Native["java/lang/Thread.setPriority0.(II)V"] = function(ctx, stack) {
var newPriority = stack.pop(), oldPriority = stack.pop(), thread = stack.pop();
2014-07-20 11:35:00 +04:00
}
Native["java/lang/Thread.start0.()V"] = function(ctx, stack) {
var thread = stack.pop();
// The main thread starts during bootstrap and don't allow calling start()
// on already running threads.
2014-07-21 03:27:24 +04:00
if (thread === CLASSES.mainThread || thread.running)
ctx.raiseException("java/lang/IllegalThreadStateException");
thread.running = true;
2014-07-21 03:27:24 +04:00
var run = CLASSES.getMethod(thread.class, "run", "()V", false, true);
window.setZeroTimeout(function () {
var ctx = new Context();
ctx.thread = thread;
var caller = new Frame();
caller.stack.push(thread);
ctx.frames.push(caller);
ctx.pushFrame(run, 1);
ctx.run(caller);
});
2014-07-20 11:35:00 +04:00
}
2014-07-24 11:21:59 +04:00
Native["java/lang/Thread.sleep.(J)V"] = function(thread, delay) {
var delay = stack.pop2(), thread = stack.pop();
2014-07-24 11:21:59 +04:00
// FIXME
2014-07-21 09:24:07 +04:00
}
2014-07-24 11:21:59 +04:00
Native["com/sun/cldchi/io/ConsoleOutputStream.write.(I)V"] = (function() {
var s = "";
return function(ctx, stack) {
var ch = stack.pop(), obj = stack.pop();
if (ch === 10) {
2014-07-17 12:41:50 +04:00
document.getElementById("output").textContent += s + "\n";
s = "";
return;
}
s += String.fromCharCode(ch);
}
})();
2014-07-19 04:55:09 +04:00
Native["com/sun/cldc/io/ResourceInputStream.open.(Ljava/lang/String;)Ljava/lang/Object;"] = function(ctx, stack) {
var name = stack.pop();
2014-07-19 04:55:09 +04:00
var fileName = util.fromJavaString(name);
var data = CLASSES.loadFile(fileName);
var obj = null;
if (data) {
obj = CLASSES.newObject(CLASSES.java_lang_Object);
obj.data = Uint8Array(data);
obj.pos = 0;
}
stack.push(obj);
2014-07-19 04:55:09 +04:00
};
Native["com/sun/cldc/io/ResourceInputStream.bytesRemain.(Ljava/lang/Object;)I"] = function(ctx, stack) {
var handle = stack.pop();
stack.push(handle.data.length - handle.pos);
2014-07-19 04:55:09 +04:00
}
Native["com/sun/cldc/io/ResourceInputStream.readByte.(Ljava/lang/Object;)I"] = function(ctx, stack) {
var handle = stack.pop();
stack.push(handle.data[handle.pos++]);
2014-07-19 04:55:09 +04:00
}
Native["com/sun/cldc/io/ResourceInputStream.readBytes.(Ljava/lang/Object;[BII)I"] = function(ctx, stack) {
var len = stack.pop(), off = stack.pop(), b = stack.pop(), handle = stack.pop();
2014-07-19 04:55:09 +04:00
var data = handle.data;
var remaining = data.length - handle.pos;
if (remaining > len)
len = remaining;
for (var n = 0; n < len; ++n)
b[off+n] = data[n];
handle.pos += len;
stack.push(len);
2014-07-19 04:55:09 +04:00
}
2014-07-19 05:23:49 +04:00
Native["com/sun/cldc/i18n/uclc/DefaultCaseConverter.toLowerCase.(C)C"] = function(ctx, stack) {
stack.push(String.fromCharCode(stack.pop()).toLowerCase().charCodeAt(0));
}
Native["com/sun/cldc/i18n/uclc/DefaultCaseConverter.toUpperCase.(C)C"] = function(ctx, stack) {
stack.push(String.fromCharCode(stack.pop()).toUpperCase().charCodeAt(0));
2014-07-19 05:23:49 +04:00
}