Replace dlr scripting implementation with the new lightweight engine

DLR scripting engine still available in new Orchard.Scripting.Dlr module

--HG--
branch : dev
rename : src/Orchard.Web/Modules/Orchard.Scripting/ScriptingManager.cs => src/Orchard.Web/Modules/Orchard.Scripting/ScriptExpressionEvaluator.cs
This commit is contained in:
Renaud Paquay
2010-11-28 12:03:11 -08:00
parent 14dd1c754f
commit f96abb981a
11 changed files with 159 additions and 107 deletions

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace Orchard.Scripting {
public interface IGlobalMethodProvider {
void Process(GlobalMethodContext context);
}
public class GlobalMethodContext {
public string FunctionName { get; set; }
public IList<object> Arguments { get; set; }
public object Result { get; set; }
}
}

View File

@@ -1,5 +1,7 @@

using System.Collections.Generic;
namespace Orchard.Scripting {
public interface IScriptExpressionEvaluator : ISingletonDependency {
object Evaluate(string expression, IEnumerable<IGlobalMethodProvider> providers);
}
}

View File

@@ -6,6 +6,11 @@ Version: 0.8.0
OrchardVersion: 0.8.0
Description: The scripting module enables the possibility to execute scripts using a simple custom scripting language.
Features:
Orchard.Scripting.:
Description: Simple scripting support.
Orchard.Scripting:
Description: Scripting support.
Category: Scripting
Orchard.Scripting.Lightweight:
Name: Lightweight scripting
Description: Custom lightweight and simple scripting language.
Dependencies: Orchard.Scripting
Category: Scripting

View File

@@ -54,6 +54,7 @@
<Compile Include="Ast\IAstNodeWithToken.cs" />
<Compile Include="Ast\MethodCallAstNode.cs" />
<Compile Include="Ast\UnaryAstNode.cs" />
<Compile Include="IGlobalMethodProvider.cs" />
<Compile Include="IScriptExpressionEvaluator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Compiler\Interpreter.cs" />
@@ -63,7 +64,7 @@
<Compile Include="Compiler\Token.cs" />
<Compile Include="Compiler\Tokenizer.cs" />
<Compile Include="Compiler\TokenKind.cs" />
<Compile Include="ScriptingManager.cs" />
<Compile Include="ScriptExpressionEvaluator.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Orchard\Orchard.Framework.csproj">

View File

@@ -2,38 +2,24 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.Caching;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Scripting.Ast;
using Orchard.Scripting.Compiler;
namespace Orchard.Scripting {
public class GlobalMethodContext {
public string FunctionName { get; set; }
public IList<object> Arguments { get; set; }
public object Result { get; set; }
}
public interface IGlobalMethodProvider {
object Process(GlobalMethodContext context);
}
public interface IScriptingEngine : IDependency {
bool Matches(string expression);
}
public class ScriptingEngine : IScriptingEngine {
private readonly IEnumerable<IGlobalMethodProvider> _ruleProviders;
[OrchardFeature("Orchard.Scripting.Lightweight")]
public class ScriptExpressionEvaluator : IScriptExpressionEvaluator {
private readonly ICacheManager _cacheManager;
public ScriptingEngine(IEnumerable<IGlobalMethodProvider> ruleProviders, ICacheManager cacheManager) {
_ruleProviders = ruleProviders;
public ScriptExpressionEvaluator(ICacheManager cacheManager) {
_cacheManager = cacheManager;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public bool Matches(string expression) {
public object Evaluate(string expression, IEnumerable<IGlobalMethodProvider> providers) {
var expr = _cacheManager.Get(expression, ctx => {
var ast = ParseExpression(expression);
return new { Tree = ast, Errors = ast.GetErrors().ToList() };
@@ -44,38 +30,38 @@ namespace Orchard.Scripting {
throw new OrchardException(T("Syntax error: {0}", expr.Errors.First().Message));
}
var result = EvaluateExpression(expr.Tree);
var result = EvaluateExpression(expr.Tree, providers);
if (result.IsError) {
throw new ApplicationException(result.Error.Message);
}
if (!result.IsBool) {
throw new OrchardException(T("Expression is not a boolean value"));
}
return result.BoolValue;
return result.Value;
}
private AbstractSyntaxTree ParseExpression(string expression) {
return new Parser(expression).Parse();
}
private EvaluationResult EvaluateExpression(AbstractSyntaxTree tree) {
private EvaluationResult EvaluateExpression(AbstractSyntaxTree tree, IEnumerable<IGlobalMethodProvider> providers) {
var context = new EvaluationContext {
Tree = tree,
MethodInvocationCallback = (m, args) => Evaluate(m, args)
MethodInvocationCallback = (m, args) => Evaluate(providers, m, args)
};
return new Interpreter().Evalutate(context);
}
private object Evaluate(string name, IEnumerable<object> args) {
var ruleContext = new GlobalMethodContext() { FunctionName = name, Arguments = args.ToArray() };
private object Evaluate(IEnumerable<IGlobalMethodProvider> globalMethodProviders, string name, IEnumerable<object> args) {
var globalMethodContext = new GlobalMethodContext {
FunctionName = name,
Arguments = args.ToArray(),
Result = null
};
foreach (var ruleProvider in _ruleProviders) {
ruleProvider.Process(ruleContext);
foreach (var globalMethodProvider in globalMethodProviders) {
globalMethodProvider.Process(globalMethodContext);
}
return ruleContext.Result;
return globalMethodContext.Result;
}
}
}