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

Return function schema for atom constructors #8743

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -767,6 +767,9 @@ public static FunctionPointer fromFunction(Function function) {
public static int[] collectNotAppliedArguments(Function function) {
FunctionSchema functionSchema = function.getSchema();
Object[] preAppliedArguments = function.getPreAppliedArguments();
if (preAppliedArguments == null) {
preAppliedArguments = new Object[functionSchema.getArgumentsCount()];
}
boolean isStatic = preAppliedArguments[0] instanceof Type;
int selfArgumentPosition = isStatic ? -1 : 0;
int[] notAppliedArguments = new int[functionSchema.getArgumentsCount()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.enso.interpreter.instrument.profiling.ExecutionTime
import org.enso.interpreter.node.callable.FunctionCallInstrumentationNode.FunctionCall
import org.enso.interpreter.node.expression.builtin.meta.TypeOfNode
import org.enso.interpreter.runtime.`type`.{Types, TypesGen}
import org.enso.interpreter.runtime.callable.atom.AtomConstructor
import org.enso.interpreter.runtime.callable.function.Function
import org.enso.interpreter.runtime.control.ThreadInterruptedException
import org.enso.interpreter.runtime.error.{
Expand Down Expand Up @@ -344,12 +345,12 @@ object ProgramExecutionSupport {
syncState: UpdatesSynchronizationState,
value: ExpressionValue
)(implicit ctx: RuntimeContext): Unit = {
val expressionId = value.getExpressionId
val methodPointer = toMethodCall(value)
val expressionId = value.getExpressionId
val methodCall = toMethodCall(value)
if (
!syncState.isExpressionSync(expressionId) ||
(
methodPointer.isDefined && !syncState.isMethodPointerSync(
methodCall.isDefined && !syncState.isMethodPointerSync(
expressionId
)
) ||
Expand Down Expand Up @@ -409,13 +410,26 @@ object ProgramExecutionSupport {
val schema = value.getValue match {
case function: Function =>
val functionInfo = FunctionPointer.fromFunction(function)
toMethodPointer(functionInfo).map { methodPointer =>
Api.FunctionSchema(
methodPointer,
FunctionPointer.collectNotAppliedArguments(function).toVector
val notAppliedArguments = FunctionPointer
.collectNotAppliedArguments(function)
.toVector
toMethodPointer(functionInfo).map(methodPointer =>
Api.FunctionSchema(methodPointer, notAppliedArguments)
)
case atomConstructor: AtomConstructor =>
val functionInfo = new FunctionPointer(
Copy link
Member

Choose a reason for hiding this comment

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

Create FunctionPointer.fromConstructor(...) method and test it with #8663 (comment) test.

atomConstructor.getDefinitionScope.getModule.getName,
atomConstructor.getType.getQualifiedName,
atomConstructor.getName
)
val notAppliedArguments = FunctionPointer
.collectNotAppliedArguments(
atomConstructor.getConstructorFunction
)
}

.toVector
toMethodPointer(functionInfo).map(methodPointer =>
Api.FunctionSchema(methodPointer, notAppliedArguments)
)
case _ =>
None
}
Expand All @@ -430,7 +444,7 @@ object ProgramExecutionSupport {
Api.ExpressionUpdate(
value.getExpressionId,
Option(value.getType),
methodPointer,
methodCall,
value.getProfilingInfo.map { case e: ExecutionTime =>
Api.ProfilingInfo.ExecutionTime(e.getNanoTimeElapsed)
}.toVector,
Expand All @@ -444,7 +458,7 @@ object ProgramExecutionSupport {
)

syncState.setExpressionSync(expressionId)
if (methodPointer.isDefined) {
if (methodCall.isDefined) {
syncState.setMethodPointerSync(expressionId)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.concurrent.locks.ReentrantLock;
import org.enso.compiler.context.LocalScope;
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.node.ClosureRootNode;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.node.MethodRootNode;
import org.enso.interpreter.node.callable.argument.ReadArgumentNode;
import org.enso.interpreter.node.callable.function.BlockNode;
import org.enso.interpreter.node.expression.atom.InstantiateNode;
Expand Down Expand Up @@ -161,15 +161,8 @@ private Function buildConstructorFunction(
}
BlockNode instantiateBlock = BlockNode.buildSilent(assignments, instantiateNode);
RootNode rootNode =
ClosureRootNode.build(
language,
localScope,
definitionScope,
instantiateBlock,
section,
type.getName() + "." + name,
null,
false);
MethodRootNode.build(
language, localScope, definitionScope, instantiateBlock, section, type, name);
RootCallTarget callTarget = rootNode.getCallTarget();
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved
return new Function(callTarget, null, new FunctionSchema(annotations, args));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,23 @@ class BuiltinTypesTest
3
) should contain theSameElementsAs Seq(
Api.Response(requestId, Api.PushContextResponse(contextId)),
TestMessages.update(contextId, idMain, ConstantsGen.FUNCTION),
TestMessages.update(
contextId,
idMain,
ConstantsGen.FUNCTION,
payload = Api.ExpressionUpdate.Payload.Value(
functionSchema = Some(
Api.FunctionSchema(
Api.MethodPointer(
"Enso_Test.Test.Main",
"Enso_Test.Test.Main.Foo",
"Bar"
),
Vector(0)
)
)
)
),
context.executionComplete(contextId)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,20 +1004,42 @@ class RuntimeServerTest
contextId,
id_x_0,
ConstantsGen.FUNCTION_BUILTIN,
Api.MethodCall(
Api
.MethodPointer("Enso_Test.Test.Main", "Enso_Test.Test.Main.T", "A")
methodCall = Some(
Api.MethodCall(Api.MethodPointer(moduleName, s"$moduleName.T", "A"))
),
payload = Api.ExpressionUpdate.Payload.Value(
functionSchema = Some(
Api.FunctionSchema(
Api.MethodPointer(moduleName, s"$moduleName.T", "A"),
Vector(0, 1)
)
)
)
),
TestMessages.update(
contextId,
id_x_1,
ConstantsGen.FUNCTION_BUILTIN
ConstantsGen.FUNCTION_BUILTIN,
methodCall = Some(
Api.MethodCall(
Api.MethodPointer(moduleName, s"$moduleName.T", "A"),
Vector(1)
)
),
payload = Api.ExpressionUpdate.Payload.Value(
functionSchema = Some(
Api.FunctionSchema(
Api.MethodPointer(moduleName, s"$moduleName.T", "A"),
Vector(1)
)
)
)
),
TestMessages.update(
contextId,
id_x_2,
"Enso_Test.Test.Main.T"
s"$moduleName.T",
Api.MethodCall(Api.MethodPointer(moduleName, s"$moduleName.T", "A"))
),
context.executionComplete(contextId)
)
Expand Down
Loading