-
Notifications
You must be signed in to change notification settings - Fork 323
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
Polyglot JS Definitions #1451
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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); |
There was a problem hiding this comment.
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?
if (tag.equals("js")) { | ||
return ForeignLanguage.JS; | ||
} | ||
return null; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
type = "Polyglot", | ||
name = "get_array_size", |
There was a problem hiding this comment.
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." |
There was a problem hiding this comment.
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.
t = case str of | ||
Text -> True | ||
_ -> False | ||
t.should_be_true |
There was a problem hiding this comment.
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.
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: