-
Notifications
You must be signed in to change notification settings - Fork 326
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
Builtins expose Enso methods #11687
Builtins expose Enso methods #11687
Conversation
No longer true what this test tested
@@ -543,6 +543,12 @@ boolean isObjectWithMembers(Object object, InteropLibrary interop) { | |||
if (interop.isTime(object)) { | |||
return false; | |||
} | |||
if (interop.isDuration(object)) { |
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.
Both TimeZone and Duration now have members, so we need to add these guards here.
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.
The whole EqualsXyzNode
set of @Specialize
annotation is fragile, unreadable, not fully testable and deserves a rewrite. At least that's the feeling I get every time I am changing the code.
Such a gentle change is OK, unless total rewrite is attempted.
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.
See my answer in #11687 (comment) - yes it is a mess.
@JaroslavTulach |
|
@Akirathan can you merge in the most recent |
# Conflicts: # engine/runtime-integration-tests/src/test/java/org/enso/example/PolyglotTestClass.java # engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/builtins/BuiltinsJavaInteropTest.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/builtin/BuiltinObject.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoDate.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoDateTime.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoDuration.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoFile.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoTimeOfDay.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoTimeZone.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/ManagedResource.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/Ref.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/hash/EnsoHashMap.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/vector/Array.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/vector/ArrayLikeHelpers.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/data/vector/Vector.java # engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/Warning.java
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.
The idea of using static methods on Type
seems to be a great solution to all the problems we had so far. The PR is simple and unlocks a functionality that was clearly missing.
- cannot call
Ref.get
: https://github.com/enso-org/enso/pull/11577/files#r1848224116 - cannot call
File.path
on EnsoFile - cannot call
to_text
onVector
- discovered while working on Warnings displayed aftermain
exit do not use definedto_text
#11569
Can you simplify these three known workarounds in the production code as well? Then this PR will immediately be useful for Enso users!
ctx, | ||
() -> { | ||
var interop = InteropLibrary.getUncached(); | ||
var refUnwrapped = ContextUtils.unwrapValue(ctx, ref); |
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.
OK, so this is result of Ref.new 42
.
assertThat( | ||
"Ref builtin object should not have any members", | ||
interop.hasMembers(refUnwrapped), | ||
is(false)); |
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.
The Ref
object has no members.
"Ref should have a meta-object (Ref type)", | ||
interop.hasMetaObject(refUnwrapped), | ||
is(true)); | ||
var refMeta = interop.getMetaObject(refUnwrapped); |
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.
One has to ask for Ref
object's meta-object - e.g. Type
assertThat( | ||
"Ref meta-object should have a 'get' method", | ||
interop.isMemberInvocable(refMeta, "get"), | ||
is(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.
and there is invocable method get
on the Ref
type.
"Ref meta-object should have a 'get' method", | ||
interop.isMemberInvocable(refMeta, "get"), | ||
is(true)); | ||
var res = interop.invokeMember(refMeta, "get", new Object[] {refUnwrapped}); |
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.
The method can be invoked as static method on the Ref
type with first argument being the Ref
object.
The approach to export methods as internal members of types and not on atoms/builtin objects was chosen because of #12043 - at the moment, it is not possible to implement |
@JaroslavTulach Usages of I tried to simplify diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java
index 7b8dce1d6e..8acf02eedd 100644
--- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java
+++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java
@@ -8,7 +8,9 @@ import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
+import com.oracle.truffle.api.interop.UnknownIdentifierException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
+import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
@@ -201,6 +203,7 @@ public final class WithWarnings extends EnsoObject {
for (var w : warns) {
try {
var wText = node.execute(toText, state, new Object[] {w.getValue()});
+ var myText = invokeToText(w, where);
if (wText instanceof Text t) {
if (prefix != null) {
text = text.add(prefix);
@@ -214,6 +217,19 @@ public final class WithWarnings extends EnsoObject {
return text;
}
+ private static Object invokeToText(Warning warn, Node where) {
+ System.out.println("[mylog] invokeToText");
+ var interop = InteropLibrary.getUncached();
+ try {
+ var meta = interop.getMetaObject(warn);
+ var toTextRes = interop.invokeMember(meta, "to_text", warn);
+ return toTextRes;
+ } catch (UnsupportedMessageException | ArityException | UnknownIdentifierException |
+ UnsupportedTypeException e) {
+ throw EnsoContext.get(where).raiseAssertionPanic(where, e.getMessage(), e);
+ }
+ }
+
@ExportMessage
Object send(Message message, Object[] args, @CachedLibrary(limit = "3") ReflectionLibrary lib)
throws Exception { But that does not work, because Let's merge this PR with the current changes. |
# Conflicts: # distribution/lib/Standard/Base/0.0.0-dev/src/Meta/Enso_Project.enso
Closes #11589
Pull Request Description
Builtin types expose methods via the interop protocol. In fact, all types expose methods via interop protocol.
To get and invoke a method on a builtin object via interop protocol:
Example in InvokeBuiltinMethodViaInteropTest
To get and invoke a method on a builtin object via
org.graalvm.polyglot.Value
(this can be done, e.g., in std libs in Java code):Example in BuiltinTypesExposeMethodsTest.
Important Notes
Method members are reported in Type.getMembers as internal members.
This has a side effect that in the debugger, types have method properties (but not atoms):
The first attempt to implement
hasMember
andgetMembers
onBuiltinObject
abandoned - #11687 (comment)Checklist
Please ensure that the following checklist has been satisfied before submitting the PR:
Scala,
Java,
TypeScript,
and
Rust
style guides. In case you are using a language not listed above, follow the Rust style guide.
or the Snowflake database integration, a run of the Extra Tests has been scheduled.