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

Meta.get_annotation and extension methods #7000

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.enso.interpreter.node.expression.builtin.meta;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.library.CachedLibrary;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.BaseNode;
import org.enso.interpreter.node.callable.thunk.ThunkExecutorNode;
Expand All @@ -13,12 +18,6 @@
import org.enso.interpreter.runtime.scope.ModuleScope;
import org.enso.interpreter.runtime.state.State;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.library.CachedLibrary;

@BuiltinMethod(
type = "Meta",
name = "get_annotation",
Expand All @@ -29,6 +28,27 @@ public abstract class GetAnnotationNode extends BaseNode {
abstract Object execute(
VirtualFrame frame, State state, Object target, Object method, Object parameter);

@Specialization
Object doExecute(
VirtualFrame frame,
State state,
Function methodFunction,
Object method,
Object parameter,
@CachedLibrary(limit = "3") TypesLibrary types,
@Cached ThunkExecutorNode thunkExecutorNode,
@Cached ExpectStringNode expectStringNode
) {
String parameterName = expectStringNode.execute(parameter);
Annotation annotation = methodFunction.getSchema().getAnnotation(parameterName);
if (annotation != null) {
Function thunk =
Function.thunk(annotation.getExpression().getCallTarget(), frame.materialize());
return thunkExecutorNode.executeThunk(frame, thunk, state, getTailStatus());
}
return EnsoContext.get(this).getNothing();
}

@Specialization
Object doExecute(
VirtualFrame frame,
Expand Down
7 changes: 7 additions & 0 deletions test/Tests/src/Semantic/Meta_Define_Extensions.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import project.Semantic.Meta_Define_Type.Meta_Spec_Type


@which (self -> self.b + self.a)
Meta_Spec_Type.describe_better self which = case which of
"mul" -> self.a * self.b
_ -> self.a / self.b
8 changes: 8 additions & 0 deletions test/Tests/src/Semantic/Meta_Define_Type.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Meta_Spec_Type
Value a b

@which (self -> self.a+self.b)
describe self which="first" = case which of
"first" -> self.a
_ -> self.b

11 changes: 11 additions & 0 deletions test/Tests/src/Semantic/Meta_Define_Usage.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from Standard.Base import Meta, Nothing
import project.Semantic.Meta_Define_Type.Meta_Spec_Type
import project.Semantic.Meta_Define_Extensions

call_describe_better it = it.describe_better
annotate_describe_better it = Meta.get_annotation it "describe_better" "which"
obtain_describe_better it =
fn = it.describe_better...
anno = Meta.get_annotation fn... Nothing "which"
anno.is_nothing
fn...
27 changes: 26 additions & 1 deletion test/Tests/src/Semantic/Meta_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ from Standard.Test import Test, Test_Suite
import Standard.Test.Extensions
import Standard.Test.Test_Result.Test_Result

import project.Semantic.Meta_Define_Type.Meta_Spec_Type
import project.Semantic.Meta_Define_Usage

type My_Type
@foo (test_method)
@baz (My_Type.Value 1 2 3)
Expand Down Expand Up @@ -390,11 +393,33 @@ spec =
case atom.bar of
n : Number -> Test.fail "Not changed to number: "+n
_ -> Nothing
Test.group "Unresolved symbol"

Test.group "Unresolved symbol" <|
Test.specify "should be treated as a Function" <|
(_.is_nothing) . is_a Function . should_equal True
(.is_nothing) . is_a Function . should_equal True
Meta.type_of (_.is_nothing) . should_equal Function
Meta.type_of (.is_nothing) . should_equal Function

Test.group "Extension methods and get_annotation" <|
it = Meta_Spec_Type.Value 6 7

Test.specify "verify calling everything works" <|
it.describe . should_equal 6
it.describe "second" . should_equal 7
Panic.catch Any (it.describe_better) (_->"Not executed") . should_equal "Not executed"
Meta_Define_Usage.call_describe_better it "mul" . should_equal 42

Test.specify "verify calling everything works" <|
describe_anno = Meta.get_annotation it.describe... Nothing "which"
describe_anno it . should_equal 13
Copy link
Member Author

Choose a reason for hiding this comment

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

This is very "verbose", @jdunkerley! One has to:

  • a lambda function @which (self -> self.a) as annotation
  • gets a lambda function via Meta.get_annotation
  • needs to invoke it

I was hoping Meta.get_annotation actually invokes the code in @which self.a by itself internally! E.g. one could reference self in the code after @which and it evaluation would happen by itself too.

With the current state of affairs, there is no reason why Meta.get_annotation shall take self argument! It rather needs a Function!

Copy link
Member Author

Choose a reason for hiding this comment

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

67fb583 let's anyone "resolve" a function and then pass such function as a handle around. Then everyone can query for annotations associated with that function.

Copy link
Member

Choose a reason for hiding this comment

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

Agree on the get_annotation having to unwind to get the value is annoying.

I wrapped it in the visualization layer and haven't thought of it since!



# Meta_Define_Usage can resolve the describe_better method
fn = (Meta_Define_Usage.obtain_describe_better it)...
fn.is_nothing . should_be_false
# we can query annotations for the resolved function
describe_better_anno = Meta.get_annotation fn... Nothing "which"
describe_better_anno it . should_equal 13

main = Test_Suite.run_main spec