Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hand optimised methods #435

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 97 additions & 11 deletions src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ declare var RELEASE: boolean;
if (typeof RELEASE === 'undefined') global.RELEASE = false;

var trapped_methods: { [clsName: string]: { [methodName: string]: Function } } = {
'java/lang/ref/Reference': {
'Ljava/lang/ref/Reference;': {
// NOP, because we don't do our own GC and also this starts a thread?!?!?!
'<clinit>()V': function (thread: threading.JVMThread): void { }
},
'java/lang/System': {
'Ljava/lang/System;': {
'loadLibrary(Ljava/lang/String;)V': function (thread: threading.JVMThread, libName: JVMTypes.java_lang_String): void {
// Some libraries test if native libraries are available,
// and expect an exception if they are not.
Expand All @@ -43,35 +43,120 @@ var trapped_methods: { [clsName: string]: { [methodName: string]: Function } } =
}
}
},
'java/lang/Terminator': {
'Ljava/lang/Terminator;': {
'setup()V': function (thread: threading.JVMThread): void {
// XXX: We should probably fix this; we support threads now.
// Historically: NOP'd because we didn't support threads.
}
},
'java/nio/charset/Charset$3': {
'Ljava/nio/charset/Charset$3;': {
// this is trapped and NOP'ed for speed
'run()Ljava/lang/Object;': function (thread: threading.JVMThread, javaThis: JVMTypes.java_nio_charset_Charset$3): JVMTypes.java_lang_Object {
return null;
}
},
'sun/nio/fs/DefaultFileSystemProvider': {
'Lsun/nio/fs/DefaultFileSystemProvider;': {
// OpenJDK doesn't know what the "Doppio" platform is. Tell it to use the Linux file system.
'create()Ljava/nio/file/spi/FileSystemProvider;': function(thread: threading.JVMThread): void {
thread.setStatus(enums.ThreadStatus.ASYNC_WAITING);
var dfsp: ClassData.ReferenceClassData<JVMTypes.sun_nio_fs_DefaultFileSystemProvider> = <any> thread.getBsCl().getInitializedClass(thread, 'Lsun/nio/fs/DefaultFileSystemProvider;'),
dfspCls: typeof JVMTypes.sun_nio_fs_DefaultFileSystemProvider = <any> dfsp.getConstructor(thread);
dfspCls['createProvider(Ljava/lang/String;)Ljava/nio/file/spi/FileSystemProvider;'](thread, [thread.getJVM().internString('sun.nio.fs.LinuxFileSystemProvider')], util.forwardResult(thread));
}
},

'Ljava/lang/Math;': {
'min(II)I' : function(thread: threading.JVMThread, first: number, second: number): number {
return first < second ? first : second;
},
'abs(D)D' : function(thread: threading.JVMThread, value: number): number {
return value < 0 ? -value : value;
}
},

'Ljava/lang/Object;': {
'<init>()V' : function(thread: threading.JVMThread): void {
// NOP
}
},

'Ljava/lang/String;': {
'length()I' : function(thread: threading.JVMThread, obj: JVMTypes.java_lang_String): number {
const v1 = obj['java/lang/String/value'].array;
return v1.length;
},

'charAt(I)C' : function stringCharAt(thread: threading.JVMThread, obj: JVMTypes.java_lang_String, index: number): number {
if (index < 0) {
thread.throwNewException<JVMTypes.java_lang_Throwable>('Ljava/lang/StringIndexOutOfBoundsException;', ''+index);
} else {
const v1 = obj['java/lang/String/value'].array;
const len1 = v1.length;
if (index >= len1) {
thread.throwNewException<JVMTypes.java_lang_Throwable>('Ljava/lang/StringIndexOutOfBoundsException;', ''+index);
} else {
return v1[index];
}
}
},

'compareTo(Ljava/lang/String;)I' : function stringCompareTo(thread: threading.JVMThread, obj: JVMTypes.java_lang_String, otherString: JVMTypes.java_lang_String): number {
if (otherString == null) {
thread.throwNewException<JVMTypes.java_lang_Throwable>('Ljava/lang/NullPointerException;', '');
} else {
const v1 = obj['java/lang/String/value'].array;
const v2 = otherString['java/lang/String/value'].array;
const len1 = v1.length;
const len2 = v2.length;
const lim = Math.min(len1, len2);
let k = 0;
while (k < lim) {
const c1 = v1[k];
const c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
}
},

'Lcom/sun/tools/javac/file/ZipFileIndex;': {
'access$400([BI)I' : function(thread: threading.JVMThread, bArray: JVMTypes.JVMArray<number>, index: number): number {
const array: number[] = bArray.array;
return (
((array[index ] & 0xff) ) |
((array[index + 1] & 0xff) << 8 )
);
},

'access$500([BI)I' : function(thread: threading.JVMThread, bArray: JVMTypes.JVMArray<number>, index: number): number {
const array: number[] = bArray.array;
return (
((array[index ] & 0xff) ) |
((array[index + 1] & 0xff) << 8 ) |
((array[index + 2] & 0xff) << 16) |
((array[index + 3] & 0xff) << 24)
);
}
},

'Lcom/sun/tools/javac/util/List;': {
'nonEmpty()Z' : function(thread: threading.JVMThread, obj: JVMTypes.java_util_List): number {
return (<any>obj)['com/sun/tools/javac/util/List/tail'] === null ? 0 : 1;
}
}

};

function getTrappedMethod(clsName: string, methSig: string): Function {
clsName = util.descriptor2typestr(clsName);
if (trapped_methods.hasOwnProperty(clsName) && trapped_methods[clsName].hasOwnProperty(methSig)) {
return trapped_methods[clsName][methSig];
const trappedClass = trapped_methods[clsName];
if (trappedClass) {
return trappedClass[methSig];
}
return null;
return undefined;
}

/**
Expand Down Expand Up @@ -274,8 +359,9 @@ export class Method extends AbstractMethodField {

// Initialize 'code' property.
var clsName = this.cls.getInternalName();
if (getTrappedMethod(clsName, this.signature) !== null) {
this.code = getTrappedMethod(clsName, this.signature);
const trappedMethod = getTrappedMethod(clsName, this.signature);
if (trappedMethod !== undefined) {
this.code = trappedMethod;
this.accessFlags.setNative(true);
} else if (this.accessFlags.isNative()) {
if (this.signature.indexOf('registerNatives()V', 0) < 0 && this.signature.indexOf('initIDs()V', 0) < 0) {
Expand Down