pluotsorbet/override.js

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

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';
var Override = {};
2014-09-24 10:56:54 +04:00
Override.getKey = function(methodInfo) {
return methodInfo.classInfo.className + "." + methodInfo.name + "." + methodInfo.signature;
}
Override.hasMethod = function(methodInfo) {
return ("override" in methodInfo || Override.getKey(methodInfo) in Override);
2014-09-24 10:56:54 +04:00
}
Override.invoke = function(ctx, methodInfo) {
2014-09-24 11:32:43 +04:00
if (!methodInfo.override) {
var key = Override.getKey(methodInfo);
methodInfo.override = Override[key];
if (!methodInfo.override) {
2014-09-24 11:32:43 +04:00
console.error("Missing override: " + key);
ctx.raiseExceptionAndYield("java/lang/RuntimeException", key + " not found");
}
2014-09-24 11:32:43 +04:00
}
methodInfo.override.call(null, ctx, ctx.current().stack);
}
Override["com/ibm/oti/connection/file/Connection.decode.(Ljava/lang/String;)Ljava/lang/String;"] = function(ctx, stack) {
2014-09-24 23:55:17 +04:00
var string = util.fromJavaString(stack.pop());
stack.push(ctx.newString(decodeURIComponent(string)));
}
Override["com/ibm/oti/connection/file/Connection.encode.(Ljava/lang/String;)Ljava/lang/String;"] = function(ctx, stack) {
var string = util.fromJavaString(stack.pop());
2014-09-25 04:44:06 +04:00
stack.push(ctx.newString(string.replace(/[^a-zA-Z0-9-_\.!~\*\\'()/:]/g, encodeURIComponent)));
}
2014-09-25 23:14:32 +04:00
Override["java/lang/Math.min.(II)I"] = function(ctx, stack) {
var b = stack.pop(), a = stack.pop();
stack.push(a <= b ? a : b);
}
Override["java/io/ByteArrayInputStream.read.()I"] = function(ctx, stack) {
var _this = stack.pop();
var pos = _this.class.getField("pos", "I").get(_this);
var count = _this.class.getField("count", "I").get(_this);
var buf = _this.class.getField("buf", "[B").get(_this);
if (pos < count) {
var value = buf[pos++] & 0xFF;
_this.class.getField("pos", "I").set(_this, pos);
stack.push(value);
} else {
stack.push(-1);
}
}