Skip to content

Commit

Permalink
Minor follow up to #9528
Browse files Browse the repository at this point in the history
Addressing some PR comments, DRY.
  • Loading branch information
hubertp committed May 2, 2024
1 parent 5995a00 commit 2fbfecb
Show file tree
Hide file tree
Showing 21 changed files with 80 additions and 117 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
17 changes: 14 additions & 3 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,15 +15,19 @@ 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()) {
// Can't use try-with-resource in ExecutorService because API was added in JDK19
var executor = Executors.newSingleThreadExecutor();
try {
var parser = new ParserPolyfill();
var ydocJs = Source.newBuilder("js", ydoc).mimeType("application/javascript+module").build();

CompletableFuture.supplyAsync(contextBuilder::build, executor)
Expand All @@ -38,6 +43,12 @@ public static void main(String[] args) throws Exception {

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
@@ -0,0 +1,21 @@
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 void initialize(Context ctx) {
Source abortControllerJs =
Source.newBuilder("js", getClass().getResource(resourceName)).buildLiteral();

ctx.eval(abortControllerJs).execute(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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 +13,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 +28,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
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;
Expand All @@ -29,7 +27,7 @@
* Implements the WebSocket and WebSocketServer interfaces of the <a
* href="https://www.npmjs.com/package/ws">ws</a> NPM package.
*/
final class WebSocket implements ProxyExecutable, Polyfill {
final class WebSocket extends PolyfillBase implements ProxyExecutable {

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

Expand All @@ -49,17 +47,10 @@ final class WebSocket implements ProxyExecutable, Polyfill {
private final ExecutorService executor;

WebSocket(ExecutorService executor) {
super(WEBSOCKET_JS);
this.executor = executor;
}

@Override
public void initialize(Context ctx) {
Source webSocketJs =
Source.newBuilder("js", WebSocket.class.getResource(WEBSOCKET_JS)).buildLiteral();

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

@Override
public Object execute(Value... arguments) {
var command = arguments[0].asString();
Expand Down
Loading

0 comments on commit 2fbfecb

Please sign in to comment.