public override Scope CreateScope() { if (_optimizedScope == null) { CachedOptimizedCodeAttribute[] attrs = (CachedOptimizedCodeAttribute[])_code.Method.GetCustomAttributes(typeof(CachedOptimizedCodeAttribute), false); // create the CompilerContext for the ScriptCode CachedOptimizedCodeAttribute optimizedCode = attrs[0]; // create the storage for the global scope GlobalsDictionary dict = new GlobalsDictionary(SymbolTable.StringsToIds(optimizedCode.Names)); // create the CodeContext for the code from the storage Scope scope = new Scope(dict); CodeContext context = new CodeContext(scope, SourceUnit.LanguageContext); // initialize the tuple IModuleDictionaryInitialization ici = dict as IModuleDictionaryInitialization; if (ici != null) { ici.InitializeModuleDictionary(context); } _optimizedScope = scope; } return _optimizedScope; }
public LambdaExpression RewriteLambda(LambdaExpression lambda, string name, LanguageContext languageContext, bool optimized) { Debug.Assert(_context == null); Debug.Assert(lambda.Parameters.Count == 0); if (optimized) { _wrappers = new Dictionary<GlobalVariableExpression, ModuleGlobalWrapper>(); var customDictionary = new GlobalsDictionary(); this.Scope = new Scope(customDictionary); //context.EnsureScopeExtension(scope.ModuleScope); //return new CodeContext(scope, context); _codeContext = new CodeContext(this.Scope, languageContext); _context = Expression.Constant(_codeContext); var ret = (LambdaExpression)Visit(lambda); //??? customDictionary.SetData(new List<ModuleGlobalWrapper>(_wrappers.Values).ToArray()); return ret; } else { // Fix up the top-level lambda to have a scope and language parameters var scopeParameter = Expression.Parameter(typeof(Scope), "$scope"); var languageParameter = Expression.Parameter(typeof(LanguageContext), "$language"); var contextVariable = Expression.Variable(typeof(CodeContext), "$globalContext"); _context = contextVariable; lambda = (LambdaExpression)Visit(lambda); return Expression.Lambda<DlrMainCallTarget>( AstUtils.AddScopedVariable( lambda.Body, contextVariable, Expression.Call(typeof(ScriptingRuntimeHelpers).GetMethod("CreateTopLevelCodeContext"), scopeParameter, languageParameter) ), name, new[] { scopeParameter, languageParameter } ); } }