Skip to content

Commit

Permalink
Minor follow up to #9528 (#9841)
Browse files Browse the repository at this point in the history
While playing with the implementation addressed some PR comments of mine and applied DRY.
  • Loading branch information
hubertp authored May 7, 2024
1 parent 459a264 commit 930f3c5
Show file tree
Hide file tree
Showing 24 changed files with 178 additions and 195 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ lazy val enso = (project in file("."))
`exploratory-benchmark-java-helpers`,
`benchmark-java-helpers`,
`benchmarks-common`,
`bench-processor`
`bench-processor`,
`ydoc-server`
)
.settings(Global / concurrentRestrictions += Tags.exclusive(Exclusive))
.settings(
Expand Down Expand Up @@ -1149,6 +1150,7 @@ lazy val `ydoc-server` = project
.enablePlugins(JPMSPlugin)
.configs(Test)
.settings(
frgaalJavaCompilerSetting,
crossPaths := false,
autoScalaLibrary := false,
Compile / run / fork := true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public Seq<IRPass> precursorPasses() {
}

@Override
@SuppressWarnings("unchecked")
public Seq<IRPass> invalidatedPasses() {
Object obj = scala.collection.immutable.Nil$.MODULE$;
return (scala.collection.immutable.List<IRPass>) obj;
Expand Down
43 changes: 27 additions & 16 deletions lib/java/ydoc-server/src/main/java/org/enso/ydoc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.enso.ydoc.polyfill.ParserPolyfill;
import org.enso.ydoc.polyfill.web.WebEnvironment;
import org.graalvm.polyglot.Source;
Expand All @@ -14,30 +15,40 @@ public class Main {
private Main() {}

public static void main(String[] args) throws Exception {
System.setProperty("helidon.serialFilter.pattern", "javax.management.**;java.lang.**;java.rmi.**;javax.security.auth.Subject;!*");
System.setProperty(
"helidon.serialFilter.pattern",
"javax.management.**;java.lang.**;java.rmi.**;javax.security.auth.Subject;!*");

var ydoc = Main.class.getResource(YDOC_SERVER_PATH);
var contextBuilder = WebEnvironment.createContext().allowIO(IOAccess.ALL);

Sampling.init();

try (var executor = Executors.newSingleThreadExecutor();
var parser = new ParserPolyfill()) {
var ydocJs = Source.newBuilder("js", ydoc).mimeType("application/javascript+module").build();

CompletableFuture.supplyAsync(contextBuilder::build, executor)
.thenAcceptAsync(
ctx -> {
WebEnvironment.initialize(ctx, executor);
parser.initialize(ctx);

ctx.eval(ydocJs);
},
executor)
.get();

// Can't use try-with-resource in ExecutorService because API was added in JDK19
var executor = Executors.newSingleThreadExecutor();
try {
try (var parser = new ParserPolyfill()) {
var ydocJs =
Source.newBuilder("js", ydoc).mimeType("application/javascript+module").build();

CompletableFuture.supplyAsync(contextBuilder::build, executor)
.thenAcceptAsync(
ctx -> {
WebEnvironment.initialize(ctx, executor);
parser.initialize(ctx);
ctx.eval(ydocJs);
},
executor)
.get();
}
System.out.println("Press enter to exit");
System.in.read();
} finally {
executor.shutdown();
var terminated = executor.awaitTermination(10, TimeUnit.SECONDS);
if (!terminated) {
executor.shutdownNow();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ParserPolyfill() {
}

@Override
public void initialize(Context ctx) {
public final void initialize(Context ctx) {
Source parserJs =
Source.newBuilder("js", ParserPolyfill.class.getResource(PARSER_JS)).buildLiteral();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.enso.ydoc.polyfill;

import org.enso.ydoc.Polyfill;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;

public abstract class PolyfillBase implements Polyfill {
private final String resourceName;

protected PolyfillBase(String resourceName) {
this.resourceName = resourceName;
}

@Override
public final void initialize(Context ctx) {
Source jsSource = Source.newBuilder("js", getClass().getResource(resourceName)).buildLiteral();

ctx.eval(jsSource).execute(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.enso.ydoc.polyfill.web;

import java.util.UUID;
import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
Expand All @@ -15,23 +12,13 @@
* href="https://nodejs.org/api/globals.html#class-abortcontroller">AbortController</a> Node.js
* interface.
*/
final class AbortController implements ProxyExecutable, Polyfill {
final class AbortController extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(AbortController.class);

private static final String RANDOM_UUID = "random-uuid";

private static final String ABORT_CONTROLLER_JS = "abort-controller.js";

AbortController() {}

@Override
public void initialize(Context ctx) {
Source abortControllerJs =
Source.newBuilder("js", AbortController.class.getResource(ABORT_CONTROLLER_JS))
.buildLiteral();

ctx.eval(abortControllerJs).execute(this);
AbortController() {
super(ABORT_CONTROLLER_JS);
}

@Override
Expand All @@ -40,10 +27,6 @@ public Object execute(Value... arguments) {

log.debug(Arguments.toString(arguments));

return switch (command) {
case RANDOM_UUID -> UUID.randomUUID().toString();

default -> throw new IllegalStateException(command);
};
throw new IllegalStateException(command);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
package org.enso.ydoc.polyfill.web;

import java.util.UUID;
import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Implements the <a href="https://nodejs.org/api/crypto.html">Crypto</a> Node.js interface. */
final class Crypto implements ProxyExecutable, Polyfill {
final class Crypto extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(Crypto.class);

private static final String RANDOM_UUID = "random-uuid";

private static final String CRYPTO_JS = "crypto.js";

Crypto() {}

@Override
public void initialize(Context ctx) {
Source cryptoJs = Source.newBuilder("js", Crypto.class.getResource(CRYPTO_JS)).buildLiteral();

ctx.eval(cryptoJs).execute(this);
Crypto() {
super(CRYPTO_JS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
Expand All @@ -17,7 +15,7 @@
* Implements the <a href="https://nodejs.org/api/events.html#class-eventemitter">EventEmitter</a>
* Node.js interface.
*/
final class EventEmitter implements ProxyExecutable, Polyfill {
final class EventEmitter extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(EventEmitter.class);

Expand All @@ -29,14 +27,8 @@ final class EventEmitter implements ProxyExecutable, Polyfill {

private static final String EVENT_EMITTER_JS = "event-emitter.js";

EventEmitter() {}

@Override
public void initialize(Context ctx) {
Source eventEmitterJs =
Source.newBuilder("js", EventEmitter.class.getResource(EVENT_EMITTER_JS)).buildLiteral();

ctx.eval(eventEmitterJs).execute(this);
EventEmitter() {
super(EVENT_EMITTER_JS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
Expand All @@ -16,7 +14,7 @@
* Implements the <a href="https://nodejs.org/api/events.html#class-eventtarget">EventTarget</a>
* Node.js interface.
*/
final class EventTarget implements ProxyExecutable, Polyfill {
final class EventTarget extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(EventTarget.class);

Expand All @@ -28,14 +26,8 @@ final class EventTarget implements ProxyExecutable, Polyfill {

private static final String EVENT_TARGET_JS = "event-target.js";

EventTarget() {}

@Override
public void initialize(Context ctx) {
Source eventTargetJs =
Source.newBuilder("js", EventTarget.class.getResource(EVENT_TARGET_JS)).buildLiteral();

ctx.eval(eventTargetJs).execute(this);
EventTarget() {
super(EVENT_TARGET_JS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.enso.ydoc.polyfill.web;

import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
Expand All @@ -13,22 +11,16 @@
* Implements the <a href="https://nodejs.org/api/perf_hooks.html">Performance measurement</a>
* Node.js API.
*/
final class Performance implements ProxyExecutable, Polyfill {
final class Performance extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(Performance.class);

private static final String NOW = "now";

private static final String PERFORMANCE_JS = "performance.js";

Performance() {}

@Override
public void initialize(Context ctx) {
Source performanceJs =
Source.newBuilder("js", Performance.class.getResource(PERFORMANCE_JS)).buildLiteral();

ctx.eval(performanceJs).execute(this);
Performance() {
super(PERFORMANCE_JS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Implements the <a href="https://nodejs.org/api/timers.html">Timers</a> Node.js API. */
final class Timers implements ProxyExecutable, Polyfill {
final class Timers extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(Timers.class);

Expand All @@ -39,16 +37,10 @@ final class Timers implements ProxyExecutable, Polyfill {
private final ExecutorService executor;

Timers(ExecutorService executor) {
super(TIMERS_JS);
this.executor = executor;
}

@Override
public void initialize(Context ctx) {
Source timersJs = Source.newBuilder("js", Timers.class.getResource(TIMERS_JS)).buildLiteral();

ctx.eval(timersJs).execute(this);
}

public Object setTimeout(Value func, long delay, Value[] args) {
return scheduledExecutor.schedule(execute(func, args), delay, TIME_UNIT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.enso.ydoc.Polyfill;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.enso.ydoc.polyfill.PolyfillBase;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.io.ByteSequence;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Implements the <a href="https://nodejs.org/api/util.html">Util</a> Node.js API. */
final class Util implements ProxyExecutable, Polyfill {
final class Util extends PolyfillBase implements ProxyExecutable {

private static final Logger log = LoggerFactory.getLogger(Util.class);

Expand All @@ -23,13 +21,8 @@ final class Util implements ProxyExecutable, Polyfill {

private static final String UTIL_JS = "util.js";

Util() {}

@Override
public void initialize(Context ctx) {
Source utilJs = Source.newBuilder("js", Util.class.getResource(UTIL_JS)).buildLiteral();

ctx.eval(utilJs).execute(this);
Util() {
super(UTIL_JS);
}

@Override
Expand Down
Loading

0 comments on commit 930f3c5

Please sign in to comment.