Skip to content

Commit 226b6e5

Browse files
committed
Use a Partial Class for the DOMString factory.
1 parent b3ff47a commit 226b6e5

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed
Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Volker Berlin (i-net software)
2+
* Copyright 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.
@@ -16,74 +16,50 @@
1616
package de.inetsoftware.jwebassembly.api.java.lang;
1717

1818
import de.inetsoftware.jwebassembly.api.annotation.Import;
19+
import de.inetsoftware.jwebassembly.api.annotation.Partial;
1920
import de.inetsoftware.jwebassembly.api.annotation.Replace;
2021

2122
/**
22-
* Replacement methods for the class java.lang.String.
23+
* Additional methods for the class java.lang.String.
2324
*
2425
* @author Volker Berlin
2526
*/
27+
@Partial( "java/lang/String" )
2628
class ReplacementForString {
2729

2830
/**
29-
* Replacement for new String(byte[])
31+
* hold the DOMString if there is already any
3032
*/
31-
@Replace( "java/lang/String.<init>([B)V" )
32-
private static String newFromBytes(byte[] bytes) {
33-
return newFromSubBytes( bytes, 0, bytes.length );
34-
}
33+
private Object domStr;
3534

3635
/**
37-
* Replacement for new String(byte[],int,int). Decode the bytes with the platform default encoding UTF-8.
36+
* Create a DOMString via JavaScript from char array.
3837
*/
39-
@Replace( "java/lang/String.<init>([BII)V" )
40-
private static String newFromSubBytes( byte[] bytes, int offset, int length ) {
41-
int count = 0;
42-
char[] buffer = new char[length - offset];
43-
while( offset < length ) {
44-
int ch = bytes[offset++] & 0xFF;
45-
46-
if( ch <= 0x7F ) {
47-
//ch = ch;
48-
} else if( ch <= 0xDF ) {
49-
ch = ((ch & 0x1F) << 6) | (bytes[offset++] & 0x3F);
50-
} else if( ch <= 0xEF ) {
51-
ch = ((ch & 0x0F) << 12) | ((bytes[offset++] & 0x3F) << 6) | (bytes[offset++] & 0x3F);
52-
} else {
53-
ch = ((ch & 0x07) << 18) | ((bytes[offset++] & 0x3F) << 12) | ((bytes[offset++] & 0x3F) << 6) | (bytes[offset++] & 0x3F);
54-
// high surrogate
55-
buffer[count++] = (char)(0xD7C0 + ((ch >> 10) & 0x3ff));
56-
// low surrogate
57-
ch = 0xDC00 + (ch & 0x3ff);
58-
}
59-
60-
buffer[count++] = (char)ch;
61-
}
62-
63-
return new String( buffer, 0, count );
64-
}
38+
@Import( module = "Web", name = "fromChars", js = "(value)=>{" + //
39+
"var s='';" + //
40+
"for(var i=0;i<value.length;i++){" + //
41+
"s+=String.fromCharCode(value[i]);" + //
42+
"}" + //
43+
"return s}" )
44+
private static native Object fromChars( char[] value );
6545

6646
/**
67-
* Replacement for new String(char[])
47+
* Getter and factory for DOMStrings.
48+
* @return the string
6849
*/
69-
@Replace( "java/lang/String.<init>([C)V" )
70-
static String newFromChars(char[] value) {
71-
// does not call the replacement method directly. Else the code will compiled two times.
72-
return new String( value, 0, value.length );
50+
@Replace( "de/inetsoftware/jwebassembly/web/JSObject.domString(Ljava/lang/String;)Ljava/lang/String;" )
51+
private Object domString() {
52+
Object domStr = this.domStr;
53+
if( domStr == null ) {
54+
//TODO toCharArray() create a copy which we not need, but it work with Java 8 and 11. A better solution can be a multi release jar file.
55+
domStr = fromChars( toCharArray() );
56+
this.domStr = domStr;
57+
}
58+
return domStr;
7359
}
7460

7561
/**
76-
* Replacement for new String(char[],int,int)
77-
* If module and name is not set then the original from the replaced method is used.
62+
* Placeholder for existing public method.
7863
*/
79-
@Import( module = "StringHelper", name = "newFromSubChars", js = "(value,off,count)=>{" + //
80-
"var s='';" + //
81-
"for(var i=off;i<off+count;i++){" + //
82-
"s+=String.fromCharCode(value[i]);" + //
83-
"}" + //
84-
"return s}" )
85-
@Replace( "java/lang/String.<init>([CII)V" )
86-
static String newFromSubChars(char[] value, int offset, int count) {
87-
return null; // for compiler
88-
}
64+
native public char[] toCharArray();
8965
}

0 commit comments

Comments
 (0)