using MSAst = System.Linq.Expressions;
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Scripting;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Compiler.Ast;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
namespace IronPython.Compiler {
internal class RuntimeScriptCode : RunnableScriptCode {
private readonly CodeContext _optimizedContext;
private Func<FunctionCode, object> _optimizedTarget;
private ScriptCode _unoptimizedCode;
public RuntimeScriptCode(PythonAst ast, CodeContext codeContext)
: base(ast) {
Debug.Assert(codeContext.GlobalScope.GetExtension(codeContext.LanguageContext.ContextId) != null);
Debug.Assert(ast.Type == typeof(MSAst.Expression<Func<FunctionCode, object>>));
_optimizedContext = codeContext;
}
public override object Run() {
return InvokeTarget(CreateScope());
}
public override object Run(Scope scope) {
return InvokeTarget(scope);
}
public override FunctionCode GetFunctionCode(bool register) {
EnsureCompiled();
return EnsureFunctionCode(_optimizedTarget, false, register);
}
private object InvokeTarget(Scope scope) {
if (scope == _optimizedContext.GlobalScope && !_optimizedContext.LanguageContext.EnableTracing) {
EnsureCompiled();
Exception e = PythonOps.SaveCurrentException();
var funcCode = EnsureFunctionCode(_optimizedTarget, false, true);
PushFrame(_optimizedContext, funcCode);
try {
if (Ast.CompilerContext.SourceUnit.Kind == SourceCodeKind.Expression) {
return OptimizedEvalWrapper(funcCode);
}
return _optimizedTarget(funcCode);
} finally {
PythonOps.RestoreCurrentException(e);
PopFrame();
}
}
if (_unoptimizedCode == null) {
((PythonCompilerOptions)Ast.CompilerContext.Options).Optimized = false;
Interlocked.CompareExchange(
ref _unoptimizedCode,
Ast.MakeLookupCode().ToScriptCode(),
null
);
}
return _unoptimizedCode.Run(scope);
}
private object OptimizedEvalWrapper(FunctionCode funcCode) {
try {
return _optimizedTarget(funcCode);
} catch (Exception e) {
PythonOps.UpdateStackTrace(e, _optimizedContext, Code, 0);
throw;
}
}
public override Scope CreateScope() {
return _optimizedContext.GlobalScope;
}
private void EnsureCompiled() {
if (_optimizedTarget == null) {
Interlocked.CompareExchange(ref _optimizedTarget, Compile(), null);
}
}
private Func<FunctionCode, object> Compile() {
var pco = (PythonCompilerOptions)Ast.CompilerContext.Options;
var pc = (PythonContext)SourceUnit.LanguageContext;
if (pc.ShouldInterpret(pco, SourceUnit)) {
return ((Microsoft.Scripting.Ast.LightExpression<Func<FunctionCode, object>>)Ast.GetLambda()).Compile(pc.Options.CompilationThreshold);
} else {
return ((Microsoft.Scripting.Ast.LightExpression<Func<FunctionCode, object>>)Ast.GetLambda()).ReduceToLambda().Compile(pc.EmitDebugSymbols(SourceUnit));
}
}
}
}