Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polyglot JS Definitions #1451

Merged
merged 14 commits into from
Feb 8, 2021
Merged

Polyglot JS Definitions #1451

merged 14 commits into from
Feb 8, 2021

Conversation

kustosz
Copy link
Contributor

@kustosz kustosz commented Feb 4, 2021

Pull Request Description

Introduces a prototype for foreign JS definitions.
These are thread safe.
Performance is not great, currently a JS call seems ~500X slower than an in-enso call – probably because these cannot be easily inlined and require more memory-digging for synchronization. Further investigation will be required in the future.

Important Notes

Checklist

Please include the following checklist in your PR:

  • The documentation has been updated if necessary.
  • All code conforms to the Scala, Java, and Rust style guides.
  • All documentation and configuration conforms to the markdown and YAML style guides.
  • All code has been tested where possible.

@kustosz kustosz added Type: Enhancement p-highest Should be completed ASAP labels Feb 4, 2021
@kustosz kustosz self-assigned this Feb 4, 2021
Copy link
Contributor

@iamrecursion iamrecursion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff. A few important things are missing, though!

@@ -34,7 +34,8 @@ class ContextFactory {
strictErrors: Boolean = false
): PolyglotContext = {
val context = Context
.newBuilder(LanguageInfo.ID)
// TODO: Remove EPB from this list when https://github.com/oracle/graal/pull/3139 is merged
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: Remove EPB from this list when https://github.com/oracle/graal/pull/3139 is merged
// TODO: Remove EPB from this list when https://github.com/oracle/graal/pull/3139 is merged
// and available in our Graal release.

* {@link com.oracle.truffle.api.TruffleContext}s, one (often referred to as "outer") is allowed to
* run Enso, Host Java, and possibly other thread-ready languages. Languages that cannot safely run
* in a multithreaded environment are relegated to the other context (referred to as "inner"). The
* inner context provides a GIL capacity, ensuring that access to the single-threaded languages is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* inner context provides a GIL capacity, ensuring that access to the single-threaded languages is
* inner context provides a GIL capability, ensuring that access to the single-threaded languages is

* outer context values need to be specially wrapped before being passed (e.g. as arguments) to the
* inner context, and inner context values need rewrapping for use in the outer context. See {@link
* org.enso.interpreter.epb.runtime.PolyglotProxy} and {@link
* org.enso.interpreter.epb.node.ContextFlipNode} for details of how and when this wrapping is being
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* org.enso.interpreter.epb.node.ContextFlipNode} for details of how and when this wrapping is being
* org.enso.interpreter.epb.node.ContextFlipNode} for details of how and when this wrapping is

* done.
*
* <p>With the structure outlined above, EPB is the only language that is initialized in both inner
* and outer contexts and thus it is very minimal. It's only role is to manage both contexts and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* and outer contexts and thus it is very minimal. It's only role is to manage both contexts and
* and outer contexts and thus it is very minimal. Its only role is to manage both contexts and

*/
public static Result parse(Source source) {
String src = source.getCharacters().toString();
String[] langAndCode = src.split("#", 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you use separator here?

Comment on lines 71 to 74
if (tag.equals("js")) {
return ForeignLanguage.JS;
}
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this should delegate to a method on the ForeignLanguage enum for me.


@GenerateUncached
@ReportPolymorphism
public abstract class ContextFlipNode extends Node {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the name of this node really expresses what it does. ContextProxyNode?

@@ -26,9 +26,9 @@ private void createPolyglot(Language language, ModuleScope scope) {
scope.registerMethod(polyglot, "execute", ExecuteMethodGen.makeFunction(language));
scope.registerMethod(polyglot, "invoke", InvokeMethodGen.makeFunction(language));
scope.registerMethod(polyglot, "new", InstantiateMethodGen.makeFunction(language));
scope.registerMethod(polyglot, "eval", EvalMethodGen.makeFunction(language));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to remove this from Builtins.enso as well.

Comment on lines +13 to +14
type = "Polyglot",
name = "get_array_size",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add this to Builtins.enso.

@@ -5808,6 +5809,10 @@ object IR {
case object InvalidOperatorName extends Reason {
override def explanation: String = "Invalid operator name."
}

case object InvalidForeignDefinition extends Reason {
override def explanation: String = "Invalid foreign definition."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be more specific. "Invalid foreign definition foo is not a supported foreign language", or something like that.

@kustosz kustosz requested a review from iamrecursion February 8, 2021 15:41
Comment on lines +58 to +61
t = case str of
Text -> True
_ -> False
t.should_be_true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write this more nicely using fail in the False case.

@kustosz kustosz merged commit c4a0772 into main Feb 8, 2021
@kustosz kustosz deleted the wip/mk/poly_eval branch February 8, 2021 17:14
iamrecursion pushed a commit that referenced this pull request Feb 10, 2021
iamrecursion pushed a commit that referenced this pull request Feb 11, 2021
iamrecursion pushed a commit that referenced this pull request Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p-highest Should be completed ASAP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants