-
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
10 changed files
with
266 additions
and
25 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
73 changes: 73 additions & 0 deletions
73
lib/java/ydoc-server/src/main/java/org/enso/ydoc/polyfill/ParserPolyfill.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,73 @@ | ||
package org.enso.ydoc.polyfill; | ||
|
||
import java.util.Arrays; | ||
import org.enso.syntax2.Parser; | ||
import org.enso.ydoc.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 ParserPolyfill implements AutoCloseable, ProxyExecutable, Polyfill { | ||
|
||
private static final String PARSE_TREE = "parse-tree"; | ||
private static final String XX_HASH_128 = "xx-hash-128"; | ||
private static final String IS_IDENT_OR_OPERATOR = "is-ident-or-operator"; | ||
|
||
private static final String PARSER_JS = "parser.js"; | ||
|
||
private final Parser parser; | ||
|
||
ParserPolyfill() { | ||
Parser p; | ||
try { | ||
p = Parser.create(); | ||
} catch (LinkageError err) { | ||
err.printStackTrace(); | ||
throw err; | ||
} | ||
this.parser = p; | ||
|
||
} | ||
|
||
@Override | ||
public void initialize(Context ctx) { | ||
Source parserJs = | ||
Source.newBuilder("js", ParserPolyfill.class.getResource(PARSER_JS)).buildLiteral(); | ||
|
||
ctx.eval(parserJs).execute(this); | ||
} | ||
|
||
@Override | ||
public Object execute(Value... arguments) { | ||
var command = arguments[0].asString(); | ||
System.err.println(command + " " + Arrays.toString(arguments)); | ||
|
||
return switch (command) { | ||
case PARSE_TREE -> { | ||
var input = arguments[1].asString(); | ||
|
||
yield parser.parseInput(input); | ||
} | ||
|
||
case XX_HASH_128 -> { | ||
var input = arguments[1].asString(); | ||
|
||
yield Integer.toString(input.hashCode()); | ||
} | ||
|
||
case IS_IDENT_OR_OPERATOR -> { | ||
var input = arguments[1].asString(); | ||
|
||
yield parser.isIdentOrOperator(input); | ||
} | ||
|
||
default -> throw new IllegalStateException(command); | ||
}; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
parser.close(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
lib/java/ydoc-server/src/main/resources/org/enso/ydoc/polyfill/parser.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,15 @@ | ||
(function (jvm) { | ||
|
||
globalThis.parse_tree = function(code) { | ||
const byteBuffer = jvm('parse-tree', code); | ||
return new Uint8Array(new ArrayBuffer(byteBuffer)); | ||
}; | ||
|
||
globalThis.xxHash128 = function(input) { | ||
return jvm('xx-hash-128', input); | ||
}; | ||
|
||
globalThis.is_ident_or_operator = function(code) { | ||
return jvm('is-ident-or-operator', code); | ||
}; | ||
}) |
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
93 changes: 93 additions & 0 deletions
93
lib/java/ydoc-server/src/test/java/org/enso/ydoc/polyfill/ParserPolyfillTest.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,93 @@ | ||
package org.enso.ydoc.polyfill; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.HostAccess; | ||
import org.graalvm.polyglot.io.ByteSequence; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ParserPolyfillTest { | ||
|
||
private Context context; | ||
private ExecutorService executor; | ||
private ParserPolyfill parser; | ||
|
||
public ParserPolyfillTest() {} | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
executor = Executors.newSingleThreadExecutor(); | ||
parser = new ParserPolyfill(); | ||
|
||
var hostAccess = | ||
HostAccess.newBuilder(HostAccess.EXPLICIT) | ||
.allowArrayAccess(true) | ||
.allowBufferAccess(true) | ||
.build(); | ||
var b = Context.newBuilder("js").allowHostAccess(hostAccess).allowExperimentalOptions(true); | ||
|
||
var chromePort = Integer.getInteger("inspectPort", -1); | ||
if (chromePort > 0) { | ||
b.option("inspect", ":" + chromePort); | ||
} | ||
|
||
context = | ||
CompletableFuture.supplyAsync( | ||
() -> { | ||
var ctx = b.build(); | ||
parser.initialize(ctx); | ||
return ctx; | ||
}, | ||
executor) | ||
.get(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
executor.close(); | ||
context.close(); | ||
parser.close(); | ||
} | ||
|
||
@Test | ||
public void parseTree() throws Exception { | ||
var code = | ||
""" | ||
const arr = parse_tree(`main = 1 + 2`) | ||
arr.buffer | ||
"""; | ||
|
||
var result = CompletableFuture.supplyAsync(() -> context.eval("js", code), executor).get(); | ||
|
||
Assert.assertTrue(result.as(ByteSequence.class).length() > 0); | ||
} | ||
|
||
@Test | ||
public void xxHash128() throws Exception { | ||
var code = | ||
""" | ||
xxHash128(`main = 1 + 2`) | ||
"""; | ||
|
||
var result = CompletableFuture.supplyAsync(() -> context.eval("js", code), executor).get(); | ||
|
||
Assert.assertEquals("1764801540", result.asString()); | ||
} | ||
|
||
@Test | ||
public void isIdentOrOperator() throws Exception { | ||
var code = | ||
""" | ||
is_ident_or_operator(`ident`) | ||
"""; | ||
|
||
var result = CompletableFuture.supplyAsync(() -> context.eval("js", code), executor).get(); | ||
|
||
Assert.assertEquals(1, result.asLong()); | ||
} | ||
} |
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,3 @@ | ||
module org.enso.syntax { | ||
exports org.enso.syntax2; | ||
} |
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
Oops, something went wrong.