-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/java/polyglot-ydoc-server/src/main/java/org/enso/polyfill/encoding/EncodingPolyfill.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.enso.polyfill.encoding; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
|
||
import org.enso.polyfill.Polyfill; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Source; | ||
import org.graalvm.polyglot.Value; | ||
import org.graalvm.polyglot.proxy.ProxyExecutable; | ||
|
||
public final class EncodingPolyfill implements ProxyExecutable, Polyfill { | ||
|
||
private static final String TEXT_DECODER_DECODE = "text-decoder-decode"; | ||
|
||
private static final String ENCODING_POLYFILL_JS = "encoding-polyfill.js"; | ||
|
||
public EncodingPolyfill() { | ||
} | ||
|
||
@Override | ||
public void initialize(Context ctx) { | ||
Source encodingPolyfillJs = Source | ||
.newBuilder("js", EncodingPolyfill.class.getResource(ENCODING_POLYFILL_JS)) | ||
.buildLiteral(); | ||
|
||
ctx.eval(encodingPolyfillJs).execute(this); | ||
} | ||
|
||
@Override | ||
public Object execute(Value... arguments) { | ||
var command = arguments[0].asString(); | ||
System.err.println(command + " " + Arrays.toString(arguments)); | ||
|
||
return switch (command) { | ||
case TEXT_DECODER_DECODE -> { | ||
var encoding = arguments[1].asString(); | ||
var data = arguments[2].as(int[].class); | ||
|
||
var charset = encoding == null ? StandardCharsets.UTF_8 : Charset.forName(encoding); | ||
// Convert unsigned Uint8Array to byte[] | ||
var bytes = new byte[data.length]; | ||
for (int i = 0; i < data.length; i++) { | ||
bytes[i] = (byte) data[i]; | ||
} | ||
|
||
yield charset.decode(ByteBuffer.wrap(bytes)).toString(); | ||
} | ||
|
||
default -> | ||
throw new IllegalStateException(command); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...a/polyglot-ydoc-server/src/main/resources/org/enso/polyfill/encoding/encoding-polyfill.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(function (jvm) { | ||
|
||
class TextDecoder { | ||
|
||
constructor(encoding) { | ||
if (typeof encoding === 'string') { | ||
this._encoding = encoding; | ||
} | ||
} | ||
|
||
decode(arr) { | ||
return jvm('text-decoder-decode', this._encoding, arr); | ||
} | ||
} | ||
|
||
globalThis.TextDecoder = TextDecoder; | ||
|
||
}) |
78 changes: 78 additions & 0 deletions
78
lib/java/polyglot-ydoc-server/src/test/java/org/enso/polyfill/EncodingPolyfillTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.enso.polyfill; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import org.enso.polyfill.encoding.EncodingPolyfill; | ||
import org.graalvm.polyglot.Context; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class EncodingPolyfillTest { | ||
|
||
private Context context; | ||
private ExecutorService executor; | ||
|
||
public EncodingPolyfillTest() { | ||
} | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
executor = Executors.newSingleThreadExecutor(); | ||
var encoding = new EncodingPolyfill(); | ||
var b = Context.newBuilder("js"); | ||
|
||
var chromePort = Integer.getInteger("inspectPort", -1); | ||
if (chromePort > 0) { | ||
b.option("inspect", ":" + chromePort); | ||
} | ||
|
||
context = CompletableFuture | ||
.supplyAsync(() -> { | ||
var ctx = b.build(); | ||
encoding.initialize(ctx); | ||
return ctx; | ||
}, executor) | ||
.get(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
executor.close(); | ||
context.close(); | ||
} | ||
|
||
@Test | ||
public void textDecoderDecodeUtf8() throws Exception { | ||
var code = """ | ||
let decoder = new TextDecoder(); | ||
var arr = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]); | ||
decoder.decode(arr); | ||
"""; | ||
|
||
var result = CompletableFuture | ||
.supplyAsync(() -> context.eval("js", code), executor) | ||
.get(); | ||
|
||
Assert.assertEquals("Hello World!", result.as(String.class)); | ||
} | ||
|
||
@Test | ||
public void textDecoderDecodeWindows1251() throws Exception { | ||
var code = """ | ||
let decoder = new TextDecoder('windows-1251'); | ||
var arr = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]); | ||
decoder.decode(arr); | ||
"""; | ||
|
||
var result = CompletableFuture | ||
.supplyAsync(() -> context.eval("js", code), executor) | ||
.get(); | ||
|
||
Assert.assertEquals("Привет, мир!", result.as(String.class)); | ||
} | ||
|
||
} |