Skip to content

Commit 0cc67a5

Browse files
committed
implementation of System.identityHashCode()
1 parent a69922f commit 0cc67a5

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.api.java.lang;
17+
18+
import de.inetsoftware.jwebassembly.api.annotation.Replace;
19+
20+
/**
21+
* Replacement methods for the class java.lang.Object.
22+
*
23+
* @author Volker Berlin
24+
*/
25+
class ReplacementForObject {
26+
27+
/**
28+
* Replacement for {@link Object#hashCode()}
29+
*/
30+
@Replace( "java/lang/Object.hashCode()I" )
31+
static int hashCode(Object x) {
32+
return System.identityHashCode( x );
33+
}
34+
}

src/de/inetsoftware/jwebassembly/api/java/lang/ReplacementForSystem.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Volker Berlin (i-net software)
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,9 +30,7 @@ class ReplacementForSystem {
3030
*/
3131
@Replace( "java/lang/System.currentTimeMillis()J" )
3232
@Import( module = "System", name = "currentTimeMillis", js = "() => BigInt(Date.now())")
33-
static long currentTimeMillis() {
34-
return 0; // for Java compiler
35-
}
33+
static native long currentTimeMillis();
3634

3735
/**
3836
* Replacement for {@link System#arraycopy(Object, int, Object, int, int)}
@@ -45,7 +43,17 @@ static long currentTimeMillis() {
4543
"}" + //
4644
"}" )
4745
@Replace( "java/lang/System.arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V" )
48-
static void arraycopy() {
49-
// nothing
50-
}
46+
static native void arraycopy();
47+
48+
/**
49+
* Replacement for {@link System#identityHashCode(Object)}
50+
*/
51+
@Import( module = "NonGC", name = "identityHashCode", js = "(o) => {" //
52+
+ "var h=o[1];" //
53+
+ "while(h==0){" //
54+
+ "o[1]=h=Math.round((Math.random()-0.5)*0xffff);" //
55+
+ "}" //
56+
+ "return h}" )
57+
@Replace( "java/lang/System.identityHashCode(Ljava/lang/Object;)I" )
58+
static native int identityHashCode(Object x);
5159
}

0 commit comments

Comments
 (0)