2010-10-13 12:51:10 -07:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Scripting.Hosting;
|
2010-10-01 16:33:16 -07:00
|
|
|
|
|
2010-11-19 17:09:17 -08:00
|
|
|
|
namespace Orchard.Scripting.Services {
|
2010-10-01 16:33:16 -07:00
|
|
|
|
public class ScriptingManager : IScriptingManager {
|
|
|
|
|
private readonly IScriptingRuntime _scriptingRuntime;
|
2010-10-13 12:51:10 -07:00
|
|
|
|
private Lazy<ScriptScope> _scope;
|
2010-11-04 12:16:44 -07:00
|
|
|
|
private Lazy<ObjectOperations> _operations;
|
2010-10-01 16:33:16 -07:00
|
|
|
|
|
|
|
|
|
public ScriptingManager(IScriptingRuntime scriptingRuntime) {
|
|
|
|
|
_scriptingRuntime = scriptingRuntime;
|
2010-10-13 12:51:10 -07:00
|
|
|
|
_scope = new Lazy<ScriptScope>(()=>_scriptingRuntime.CreateScope());
|
2010-11-04 12:16:44 -07:00
|
|
|
|
_operations = new Lazy<ObjectOperations>(()=>_scope.Value.Engine.CreateOperations());
|
2010-10-01 16:33:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public dynamic GetVariable(string name) {
|
2010-10-13 12:51:10 -07:00
|
|
|
|
return _scope.Value.GetVariable(name);
|
2010-10-01 16:33:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetVariable(string name, object value) {
|
2010-10-13 12:51:10 -07:00
|
|
|
|
_scope.Value.SetVariable(name, value);
|
2010-10-01 16:33:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-13 12:51:10 -07:00
|
|
|
|
public dynamic ExecuteExpression(string expression) {
|
|
|
|
|
return _scriptingRuntime.ExecuteExpression(expression, _scope.Value);
|
2010-10-01 16:33:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-18 23:50:28 -08:00
|
|
|
|
public dynamic ExecuteOperation(Func<ObjectOperations, object> invoke) {
|
2010-11-04 12:16:44 -07:00
|
|
|
|
return invoke(_operations.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-01 16:33:16 -07:00
|
|
|
|
public void ExecuteFile(string fileName) {
|
2010-10-13 12:51:10 -07:00
|
|
|
|
_scriptingRuntime.ExecuteFile(fileName, _scope.Value);
|
2010-10-01 16:33:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|