Skip to content

Commit

Permalink
Fill in ydoc-polyfill when running backend with the IDE (#11822)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach authored Dec 11, 2024
1 parent 9e00b9d commit 9cf7833
Show file tree
Hide file tree
Showing 26 changed files with 124 additions and 117 deletions.
6 changes: 3 additions & 3 deletions app/ydoc-server-polyglot/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { configureAllDebugLogs, docName, setupGatewayClient } from 'ydoc-server'

const host = YDOC_HOST ?? 'localhost'
const port = YDOC_PORT ?? 1234
const debug = YDOC_LS_DEBUG ?? false
const host = typeof YDOC_HOST == 'string' ? YDOC_HOST : 'localhost'
const port = typeof YDOC_PORT == 'number' ? YDOC_PORT : 1234
const debug = typeof YDOC_LS_DEBUG != 'undefined'

configureAllDebugLogs(debug)

Expand Down
3 changes: 3 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,9 @@ lazy val `runtime-language-epb` =
"org.graalvm.sdk" % "collections" % graalMavenPackagesVersion,
"org.graalvm.sdk" % "word" % graalMavenPackagesVersion,
"org.graalvm.sdk" % "nativeimage" % graalMavenPackagesVersion
),
Compile / internalModuleDependencies := Seq(
(`ydoc-polyfill` / Compile / exportedModule).value
)
)

Expand Down
1 change: 1 addition & 0 deletions engine/runtime-language-epb/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open module org.enso.runtime.language.epb {
requires java.logging;
requires org.graalvm.truffle;
requires org.enso.ydoc.polyfill;

provides com.oracle.truffle.api.provider.TruffleLanguageProvider with
org.enso.interpreter.epb.EpbLanguageProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.logging.Level;
import org.enso.ydoc.polyfill.web.WebEnvironment;
import org.graalvm.polyglot.Value;

/**
* A context for {@link EpbLanguage}. Provides access to both isolated Truffle contexts used in
Expand All @@ -21,6 +28,7 @@ final class EpbContext {
private final TruffleLanguage.Env env;
private @CompilationFinal TruffleContext innerContext;
private final TruffleLogger log;
private boolean polyfillInitialized;

/**
* Creates a new instance of this context.
Expand Down Expand Up @@ -75,4 +83,22 @@ public TruffleContext getInnerContext() {
public void log(Level level, String msg, Object... args) {
this.log.log(level, msg, args);
}

final void initializePolyfill(Node node, TruffleContext ctx) {
if (!polyfillInitialized) {
polyfillInitialized = true;
var exec = Executors.newSingleThreadScheduledExecutor();
Function<URL, Value> eval =
(url) -> {
try {
var src = Source.newBuilder("js", url).build();
var obj = ctx.evalPublic(node, src);
return Value.asValue(obj);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
};
WebEnvironment.initialize(eval, exec);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ private ForeignFunctionCallNode parseJs() {
var context = EpbContext.get(this);
var inner = context.getInnerContext();
if (inner != null) {
context.initializePolyfill(this, inner);

var code = foreignSource(langAndCode);
var args = Arrays.stream(argNames).collect(Collectors.joining(","));
var wrappedSrc = "var poly_enso_eval=function(" + args + "){" + code + "\n};poly_enso_eval";
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.enso.ydoc.polyfill;

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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ParserPolyfill implements ProxyExecutable, Polyfill {
public final class ParserPolyfill implements ProxyExecutable {

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

Expand All @@ -22,7 +21,6 @@ public final class ParserPolyfill implements ProxyExecutable, Polyfill {

public ParserPolyfill() {}

@Override
public 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
@@ -1,9 +1,7 @@
package org.enso.ydoc.polyfill.web;

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

private static final Logger log = LoggerFactory.getLogger(AbortController.class);
private static final String ABORT_CONTROLLER_JS = "abort-controller.js";

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

ctx.eval(jsSource).execute(this);
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(ABORT_CONTROLLER_JS));
fn.execute(this);
}

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

import java.util.UUID;
import org.enso.ydoc.Polyfill;
import java.util.function.Function;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
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 Polyfill, ProxyExecutable {
final class Crypto 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";

@Override
public void initialize(Context ctx) {
Source jsSource = Source.newBuilder("js", getClass().getResource(CRYPTO_JS)).buildLiteral();
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(CRYPTO_JS));

ctx.eval(jsSource).execute(this);
fn.execute(this);
}

@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 java.util.function.Function;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
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 Polyfill, ProxyExecutable {
final class EventEmitter implements ProxyExecutable {

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

Expand All @@ -29,12 +27,9 @@ final class EventEmitter implements Polyfill, ProxyExecutable {

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

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

ctx.eval(jsSource).execute(this);
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(EVENT_EMITTER_JS));
fn.execute(this);
}

@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 java.util.function.Function;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
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 Polyfill, ProxyExecutable {
final class EventTarget implements ProxyExecutable {

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

Expand All @@ -28,12 +26,9 @@ final class EventTarget implements Polyfill, ProxyExecutable {

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

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

ctx.eval(jsSource).execute(this);
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(EVENT_TARGET_JS));
fn.execute(this);
}

@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 java.util.function.Function;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.Logger;
Expand All @@ -13,20 +11,17 @@
* Implements the <a href="https://nodejs.org/api/perf_hooks.html">Performance measurement</a>
* Node.js API.
*/
final class Performance implements Polyfill, ProxyExecutable {
final class Performance 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";

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

ctx.eval(jsSource).execute(this);
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(PERFORMANCE_JS));
fn.execute(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.enso.ydoc.Polyfill;
import java.util.function.Function;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
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 Polyfill, ProxyExecutable {
final class Timers implements ProxyExecutable {

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

Expand All @@ -34,11 +32,9 @@ final class Timers implements Polyfill, ProxyExecutable {
this.executor = executor;
}

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

ctx.eval(jsSource).execute(this);
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(TIMERS_JS));
fn.execute(this);
}

private Future<?> setTimeout(Value func, long delay, Object[] args) {
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 java.util.function.Function;
import org.enso.ydoc.polyfill.Arguments;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
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 Polyfill, ProxyExecutable {
final class Util implements ProxyExecutable {

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

Expand All @@ -23,11 +21,9 @@ final class Util implements Polyfill, ProxyExecutable {

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

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

ctx.eval(jsSource).execute(this);
final void initialize(Function<java.net.URL, Value> eval) {
var fn = eval.apply(getClass().getResource(UTIL_JS));
fn.execute(this);
}

@Override
Expand Down
Loading

0 comments on commit 9cf7833

Please sign in to comment.