-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from active-logic/Fast-Bindings
Cache function bindings
- Loading branch information
Showing
15 changed files
with
188 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Elk/Runtime/Basic/Runner-Details/InternalFunctionBinding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Elk.Basic.Graph; | ||
|
||
namespace Elk.Basic.Runtime{ | ||
public class InternalFunctionBinding : InvocationBinding{ | ||
|
||
protected object target; // NOTE unused; for instance func | ||
protected FuncDef function; | ||
|
||
public InternalFunctionBinding(object target, FuncDef function){ | ||
this.target = target; | ||
this.function = function; | ||
} | ||
|
||
override protected object Invoke( | ||
object[] values, Runner<Context> ρ, Context cx | ||
){ | ||
cx.Push(function.parameters, values); | ||
var @out = ρ.Eval(function.body, cx); | ||
cx.Pop(); | ||
return @out; | ||
} | ||
|
||
}} |
2 changes: 1 addition & 1 deletion
2
Elk/Runtime/Extern/IEnumerableExt.cs.meta → ...r-Details/InternalFunctionBinding.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Reflection; | ||
using Elk.Basic.Graph; | ||
using Elk.Util; | ||
|
||
namespace Elk.Basic.Runtime{ | ||
public abstract class InvocationBinding{ | ||
|
||
public object Eval(Invocation ι, Runner ρ, Context cx) | ||
=> Invoke(ι.values, ρ, cx); | ||
|
||
protected abstract object Invoke( | ||
object[] values, Runner<Context> ρ, Context cx | ||
); | ||
|
||
}} |
11 changes: 11 additions & 0 deletions
11
Elk/Runtime/Basic/Runner-Details/InvocationBinding.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,27 @@ | ||
using Array = System.Array; | ||
using Ex = System.Exception; | ||
using UnityEngine; | ||
using Elk.Util; | ||
using Elk.Bindings.CSharp; | ||
using Elk.Basic.Graph; | ||
using Elk.Basic.Runtime; | ||
|
||
namespace Elk.Basic{ | ||
public class InvocationEval{ | ||
|
||
public object Eval(Invocation ι, Runner ρ, Context cx){ | ||
var values = EvalArgs(ι.arguments, ρ, cx); | ||
cx.graph.Push(ι.name + values.NeatFormat()); | ||
var @out = DoEval(ι, values, ρ, cx); | ||
ρ.EvalArgs(ι.arguments, @out: ι.values, cx); | ||
cx.graph.Push(ι.name + ι.values.NeatFormat()); | ||
var @out = DoEval(ι, ρ, cx); | ||
cx.graph.Pop(@out); | ||
return @out; | ||
} | ||
|
||
public object DoEval(Invocation inv, object[] values, | ||
Runner ρ, Context cx){ | ||
object @out; | ||
if(Invoke(inv.name, inv.arguments, values, ρ, cx, out @out)) | ||
return @out; | ||
return cx.externals.Invoke(inv.name, values); | ||
} | ||
|
||
public bool Invoke(string name, object[] args, | ||
object[] values, Runner ρ, | ||
Context cx, out object @out){ | ||
foreach(var module in cx.modules){ | ||
var fdef = Array.Find( | ||
module, | ||
x => FuncMatches(x, name, args?.Length ?? 0) | ||
); | ||
if(fdef != null){ | ||
@out = InvokeInternalFunction(fdef, values, ρ, cx); | ||
return true; | ||
} | ||
public object DoEval(Invocation ι, Runner ρ, Context cx){ | ||
if(ι.binding == null){ | ||
ι.binding = cx.modules .Bind(ι, ρ, cx) | ||
?? cx.externals.Bind(ι.name, ι.values); | ||
} | ||
@out = null; | ||
return false; | ||
} | ||
|
||
bool FuncMatches(FuncDef fdef, string name, int argLength){ | ||
if(fdef == null){ | ||
Debug.LogError("fdef is null"); | ||
return false; | ||
} | ||
if(fdef.name == null){ | ||
Debug.LogError("fdef name is null"); | ||
return false; | ||
} | ||
return fdef.name == name && fdef.paramCount == argLength; | ||
} | ||
|
||
object InvokeInternalFunction(FuncDef func, | ||
object[] values, | ||
Runner ρ, | ||
Context cx){ | ||
cx.Push(func.parameters, values); | ||
var @out = ρ.Eval(func.body, cx); | ||
cx.Pop(); | ||
return @out; | ||
} | ||
|
||
object[] EvalArgs(object[] args, Runner ρ, Context cx){ | ||
var len = args?.Length ?? 0; | ||
var @out = new object[len]; | ||
for(int i = 0; i < len; i++){ | ||
@out[i] = ρ.Eval(args[i], cx); | ||
} | ||
return @out; | ||
return ((InvocationBinding)ι.binding).Eval(ι, ρ, cx); | ||
} | ||
|
||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
using Array = System.Array; | ||
using Elk.Basic.Graph; | ||
|
||
namespace Elk.Basic.Runtime{ | ||
public static class Modules{ | ||
|
||
public static InvocationBinding Bind( | ||
this IEnumerable<FuncDef[]> self, Invocation ι, | ||
Runner ρ, Context cx | ||
){ | ||
foreach(var module in self){ | ||
var fdef = Array.Find( | ||
module, x => x.Matches(ι.name, ι.argumentCount) | ||
); | ||
if(fdef != null){ | ||
return new InternalFunctionBinding(null, fdef); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
}} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Reflection; | ||
using Elk.Util; | ||
using Elk.Basic; | ||
using Elk.Basic.Graph; | ||
using Elk.Basic.Runtime; | ||
|
||
namespace Elk.Bindings.CSharp{ | ||
public class ExternalFunctionBinding : InvocationBinding{ | ||
|
||
readonly object target; | ||
readonly MethodInfo method; | ||
|
||
public ExternalFunctionBinding(object target, MethodInfo method){ | ||
this.target = target; | ||
this.method = method; | ||
} | ||
|
||
override protected object Invoke( | ||
object[] values, Runner<Context> ρ, Context cx | ||
) => method.Invoke(target, values); | ||
|
||
}} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 11 additions & 6 deletions
17
Elk/Runtime/Extern/IEnumerableExt.cs → Elk/Runtime/Extern/Externals.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters