mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Implement scripting manager end-2-end
--HG-- branch : dev
This commit is contained in:
@@ -7,10 +7,25 @@ using Orchard.Widgets.SimpleScripting;
|
|||||||
namespace Orchard.Tests.Modules.SimpleScripting {
|
namespace Orchard.Tests.Modules.SimpleScripting {
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class SimpleScriptingTests {
|
public class SimpleScriptingTests {
|
||||||
|
[Test]
|
||||||
|
public void EngineThrowsSyntaxErrors() {
|
||||||
|
var engine = new ScriptingEngine(Enumerable.Empty<IRuleProvider>(), new StubCacheManager());
|
||||||
|
Assert.That(() => engine.Matches("true+"), Throws.Exception);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void EngineThrowsEvalErrors() {
|
||||||
|
var engine = new ScriptingEngine(Enumerable.Empty<IRuleProvider>(), new StubCacheManager());
|
||||||
|
Assert.That(() => engine.Matches("1 + 1"), Throws.Exception);
|
||||||
|
}
|
||||||
[Test]
|
[Test]
|
||||||
public void EngineUnderstandsPrimitiveValues() {
|
public void EngineUnderstandsPrimitiveValues() {
|
||||||
//var engine = new ScriptingEngine(Enumerable.Empty<IRuleProvider>(), new StubCacheManager());
|
var engine = new ScriptingEngine(Enumerable.Empty<IRuleProvider>(), new StubCacheManager());
|
||||||
//Assert.That(engine.Matches("true"), Is.True);
|
Assert.That(engine.Matches("true"), Is.True);
|
||||||
|
}
|
||||||
|
[Test]
|
||||||
|
public void EngineUnderstandsPrimitiveValues2() {
|
||||||
|
var engine = new ScriptingEngine(Enumerable.Empty<IRuleProvider>(), new StubCacheManager());
|
||||||
|
Assert.That(engine.Matches("true and true"), Is.True);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Orchard.Caching;
|
using Orchard.Caching;
|
||||||
|
using Orchard.Localization;
|
||||||
using Orchard.Widgets.Services;
|
using Orchard.Widgets.Services;
|
||||||
using Orchard.Widgets.SimpleScripting.Ast;
|
using Orchard.Widgets.SimpleScripting.Ast;
|
||||||
using Orchard.Widgets.SimpleScripting.Compiler;
|
using Orchard.Widgets.SimpleScripting.Compiler;
|
||||||
@@ -18,24 +19,44 @@ namespace Orchard.Widgets.SimpleScripting {
|
|||||||
public ScriptingEngine(IEnumerable<IRuleProvider> ruleProviders, ICacheManager cacheManager) {
|
public ScriptingEngine(IEnumerable<IRuleProvider> ruleProviders, ICacheManager cacheManager) {
|
||||||
_ruleProviders = ruleProviders;
|
_ruleProviders = ruleProviders;
|
||||||
_cacheManager = cacheManager;
|
_cacheManager = cacheManager;
|
||||||
|
T = NullLocalizer.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Localizer T { get; set; }
|
||||||
|
|
||||||
public bool Matches(string expression) {
|
public bool Matches(string expression) {
|
||||||
|
var expr = _cacheManager.Get(expression, ctx => {
|
||||||
|
var ast = ParseExpression(expression);
|
||||||
|
return new { Tree = ast, Errors = ast.GetErrors().ToList() };
|
||||||
|
});
|
||||||
|
|
||||||
var expressionTree = _cacheManager.Get(expression, ctx =>
|
if (expr.Errors.Any()) {
|
||||||
ParseExpression(expression));
|
//TODO: Collect all errors
|
||||||
|
throw new OrchardException(T("Syntax error: {0}", expr.Errors.First().Message));
|
||||||
|
}
|
||||||
|
|
||||||
object result = EvaluateExpression(expressionTree.Root);
|
var result = EvaluateExpression(expr.Tree);
|
||||||
|
if (result.IsError) {
|
||||||
|
throw new ApplicationException(result.Error.Message);
|
||||||
|
}
|
||||||
|
|
||||||
return (bool)Convert.ChangeType(result, typeof (bool));
|
if (!result.IsBool) {
|
||||||
|
throw new OrchardException(T("Expression is not a boolean value"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.BoolValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
private AbstractSyntaxTree ParseExpression(string expression) {
|
private AbstractSyntaxTree ParseExpression(string expression) {
|
||||||
return new Parser(expression).Parse();
|
return new Parser(expression).Parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private object EvaluateExpression(AstNode root) {
|
private EvaluationResult EvaluateExpression(AbstractSyntaxTree tree) {
|
||||||
throw new NotImplementedException();
|
var context = new EvaluationContext {
|
||||||
|
Tree = tree,
|
||||||
|
MethodInvocationCallback = (m, args) => Evaluate(m, args)
|
||||||
|
};
|
||||||
|
return new Interpreter().Evalutate(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object Evaluate(string name, IEnumerable<object> args) {
|
private object Evaluate(string name, IEnumerable<object> args) {
|
||||||
|
Reference in New Issue
Block a user