public dynamic ExecuteExpression(string engine, string expression, ScriptScope scope)
        {
            if (!_runtimes.ContainsKey(engine)) throw new InvalidOperationException("No \"" + engine + "\" engine registered.");

            _eventHandler.BeforeExecution(new BeforeExecutionContext(engine, expression, scope));
            var output = _runtimes[engine].ExecuteExpression(expression, scope);
            _eventHandler.AfterExecution(new AfterExecutionContext(engine, expression, scope, output));
            return output;
        }
 public dynamic ExecuteFile(string path, ScriptScope scope)
 {
     return
         RunPhpExecutor(
             requestContext =>
             {
                 requestContext.ScriptContext.Include(path, true);
             },
             scope);
 }
        public dynamic ExecuteExpression(string expression, ScriptScope scope)
        {
            try
            {
                using (var context = new JavascriptContext())
                {
                    foreach (var variable in scope.Variables)
                    {
                        context.SetParameter(variable.Key, variable.Value);
                    }

                    context.SetParameter("Factory", new TypeFactory(scope.Assemblies));

                    var workContext = _wcaWork.Value.GetContext();
                    var orchardGlobal = new Dictionary<string, dynamic>();
                    orchardGlobal["WorkContext"] = workContext;
                    orchardGlobal["OrchardServices"] = workContext.Resolve<IOrchardServices>();
                    orchardGlobal["Layout"] = new StaticShape(workContext.Layout);

                    var existing = scope.GetVariable("Orchard");
                    if (existing != null)
                    {
                        if (!(existing is IDictionary<string, object>)) throw new ArgumentException("The Orchard global variable should be an IDictionary<string, dynamic>.");

                        var existingDictionary = existing as IDictionary<string, dynamic>;
                        foreach (var existingItem in existingDictionary)
                        {
                            orchardGlobal[existingItem.Key] = existingItem.Value;
                        }
                    }

                    context.SetParameter("Orchard", orchardGlobal);

                    _eventHandler.BeforeExecution(new BeforeJavaScriptExecutionContext(scope, context));
                    var output = context.Run(expression);
                    _eventHandler.AfterExecution(new AfterJavaScriptExecutionContext(scope, context));

                    foreach (var variableName in scope.Variables.Select(kvp => kvp.Key))
                    {
                        scope.SetVariable(variableName, context.GetParameter(variableName));
                    }

                    return output;
                }
            }
            catch (Exception ex)
            {
                if (ex.IsFatal()) throw;

                throw new ScriptRuntimeException("The JavaScript script could not be executed.", ex);
            }
        }
 public dynamic ExecuteExpression(string expression, ScriptScope scope)
 {
     return
         RunPhpExecutor(
             requestContext =>
             {
                 DynamicCode.Eval(
                     expression,
                     false,      // Phalanger internal stuff
                     requestContext.ScriptContext,
                     null,       // local variables
                     null,       // reference to "$this"
                     null,       // current class context
                     scope.Name, // file name, used for debug and cache key
                     1, 1,       // position in the file used for debug and cache key
                     -1,         // something internal
                     null        // current namespace, used in CLR mode
                 );
             },
             scope);
 }
        public dynamic CompileAndRun(string engine, string expression, ScriptScope scope)
        {
            _eventHandler.BeforeCompilation(new BeforeDotNetCompilationContext(scope));

            var parameters = new CompilerParameters { GenerateInMemory = true, TreatWarningsAsErrors = false };
            foreach (var item in scope.Assemblies) parameters.ReferencedAssemblies.Add(item.Location);

            CompilerResults result;
            var providerOptions = new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } };
            switch (engine)
            {
                case "C#":
                    result = new CSharpCodeProvider(providerOptions).CompileAssemblyFromSource(parameters, expression);
                    break;
                case "VB":
                    result = new VBCodeProvider(providerOptions).CompileAssemblyFromSource(parameters, expression);
                    break;
                default:
                    throw new ArgumentException(T("Undefined .NET scripting engine.").ToString());
            }

            _eventHandler.AfterCompilation(new AfterDotNetCompilationContext(scope, result));

            if (result.Errors.HasErrors)
            {
                var builder = new StringBuilder();
                foreach (var item in result.Errors) builder.Append(Environment.NewLine + item);
                throw new ScriptRuntimeException(Environment.NewLine + T("The following compile error(s) need to be fixed:") + builder.ToString());
            }

            var entryClass = result.CompiledAssembly.CreateInstance("DotNetScripting");

            _eventHandler.BeforeExecution(new BeforeDotNetExecutionContext(scope));

            var scriptResult = entryClass.GetType().GetMethod("Main").Invoke(entryClass, new object[] { });

            _eventHandler.AfterExecution(new AfterDotNetExecutionContext(scope));

            return scriptResult;
        }
 public BeforePhpExecutionContext(ScriptScope scope, ScriptContext context)
     : base(scope, context)
 {
 }
 protected PhpScriptingEventContext(ScriptScope scope, ScriptContext context)
 {
     Scope = scope;
     Context = context;
 }
 public BeforeDotNetCompilationContext(ScriptScope scope)
     : base(scope)
 {
 }
 public AfterPhpExecutionContext(ScriptScope scope, ScriptContext context)
     : base(scope, context)
 {
 }
        private dynamic RunPhpExecutor(Action<RequestContext> executor, ScriptScope scope)
        {
            try
            {
                var workContext = _wcaWork.Value.GetContext();

                using (var requestContext = RequestContext.Initialize(ApplicationContext.Default, workContext.HttpContext.ApplicationInstance.Context))
                {
                    var scriptContext = requestContext.ScriptContext;
                    using (scriptContext.OutputStream = new MemoryStream())
                    {
                        using (scriptContext.Output = new StreamWriter(scriptContext.OutputStream))
                        {
                            var assemblyLoader = scriptContext.ApplicationContext.AssemblyLoader;
                            assemblyLoader.Load(typeof(PhpHash).Assembly, null); // PhpNetClassLibrary.dll

                            foreach (var assembly in scope.Assemblies)
                            {
                                assemblyLoader.Load(assembly, null);
                            }

                            var orchardGlobal = new Dictionary<string, dynamic>();
                            orchardGlobal["WORK_CONTEXT"] = workContext;
                            orchardGlobal["ORCHARD_SERVICES"] = workContext.Resolve<IOrchardServices>();
                            orchardGlobal["LAYOUT"] = new StaticShape(workContext.Layout);

                            var existing = scope.GetVariable("_ORCHARD");
                            if (existing != null)
                            {
                                if (!(existing is IDictionary<string, object>)) throw new ArgumentException("The $_ORCHARD superglobal variable should be an IDictionary<string, dynamic>.");

                                var existingDictionary = existing as IDictionary<string, dynamic>;
                                foreach (var existingItem in existingDictionary)
                                {
                                    orchardGlobal[existingItem.Key] = existingItem.Value;
                                }
                            }

                            scope.SetVariable("_ORCHARD", orchardGlobal);

                            foreach (var item in scope.Variables)
                            {
                                scriptContext.Globals.TrySetMember(item.Key, item.Value);
                            }

                            // Setting globals like this only works for primitive types.
                            // Operators.SetVariable(scriptContext, null, "test", "");

                            _eventHandler.BeforeExecution(new BeforePhpExecutionContext(scope, scriptContext));
                            executor(requestContext);
                            _eventHandler.AfterExecution(new AfterPhpExecutionContext(scope, scriptContext));

                            foreach (var variable in scriptContext.GlobalVariables)
                            {
                                scope.SetVariable(variable.Key.ToString(), variable.Value);
                            }

                            scriptContext.Output.Flush();
                            scriptContext.OutputStream.Position = 0;
                            using (var streamReader = new StreamReader(scriptContext.OutputStream))
                            {
                                return streamReader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (PhpException ex)
            {
                throw new ScriptRuntimeException("The PHP script could not be executed.", ex);
            }
        }
 public BeforeDotNetExecutionContext(ScriptScope scope)
     : base(scope)
 {
 }
 public AfterDotNetExecutionContext(ScriptScope scope)
     : base(scope)
 {
 }
 protected DotNetScriptingEventContext(ScriptScope scope)
 {
     Scope = scope;
 }
 public BeforeJavaScriptExecutionContext(ScriptScope scope, JavascriptContext context)
     : base(scope, context)
 {
 }
 protected ScriptingEventContext(string engine, string expression, ScriptScope scope)
 {
     Engine = engine;
     Expression = expression;
     Scope = scope;
 }
 public BeforeExecutionContext(string engine, string expression, ScriptScope scope)
     : base(engine, expression, scope)
 {
 }
 public AfterExecutionContext(string engine, string expression, ScriptScope scope, dynamic output)
     : base(engine, expression, scope)
 {
     Output = output;
 }
 public dynamic ExecuteExpression(string expression, ScriptScope scope)
 {
     return _runtime.CompileAndRun(Descriptor.Name, expression, scope);
 }
 protected JavaScriptScriptingEventContext(ScriptScope scope, JavascriptContext context)
 {
     Scope = scope;
     Context = context;
 }
 public AfterDotNetCompilationContext(ScriptScope scope, CompilerResults result)
     : base(scope)
 {
     Result = result;
 }