Represents a context of execution. A context of execution has a set of variables associated with it (its dictionary) and a parent context. When looking up a name from a context first the local context is searched. If the name is not found there the name lookup will be done against the parent context. Scopes, like IAttrbibuteCollections, support both being indexed by SymbolId for fast access as well as being indexed by object. The preferred access is via SymbolId and object access is provided for languages which require additional semantics. All features supported for feature IDs are also supported for objects (e.g. context-sentsitivity and attributes) but the object API does not contain all the same sets of overloads provided for convenience. TODO: Thread safety
 public TotemScopeExtension(TotemContext context, Scope scope)
     : base(scope)
 {
     _codeContext = new CodeContext(new TotemDictionary(
         new ScopeDictionaryStorage(context, scope)
     ), null, context);
 }
        public OptimizedScriptCode(Scope optimizedScope, DlrMainCallTarget optimizedTarget, SourceUnit sourceUnit)
            : base(sourceUnit) {
            ContractUtils.RequiresNotNull(optimizedScope, "optimizedScope");

            _optimizedScope = optimizedScope;
            _optimizedTarget = optimizedTarget;
        }
Exemple #3
0
 public RubyGlobalScope(RubyContext/*!*/ context, Scope/*!*/ scope, object/*!*/ mainObject, bool isHosted)
     : base(scope) {
     Assert.NotNull(context, scope, mainObject);
     _context = context;
     _mainObject = mainObject;
     _isHosted = isHosted;
 }
        public ScopeDictionaryStorage(TotemContext/*!*/ context, Scope/*!*/ scope)
        {
            Assert.NotNull(context, scope);

            _scope = scope;
            _context = context;
        }
 public GlobalScopeExtension(RubyContext/*!*/ context, Scope/*!*/ globalScope, object/*!*/ mainObject, bool isHosted)
     : base(globalScope) {
     Assert.NotNull(context, globalScope, mainObject);
     _context = context;
     _mainObject = mainObject;
     _isHosted = isHosted;
 }
Exemple #6
0
        public CodeContext(Scope scope, LanguageContext languageContext, CodeContext parent) {
            Assert.NotNull(languageContext);

            _languageContext = languageContext;
            _scope = scope;
            _parent = parent;
        }
Exemple #7
0
 protected override object InvokeTarget(LambdaExpression code, Scope scope) {
     //TODO code is used bizarrly here in this API
     if (_optimizedScope == null || scope != _optimizedScope) {
         EnsureCompiled(false);
         return _code.Run(scope, LanguageContext);
     } else {
         return _optimizedCode.Run();
     }
 }
Exemple #8
0
 public RubyGlobalScope(RubyContext/*!*/ context, Scope/*!*/ scope, RubyObject/*!*/ mainObject, bool isHosted)
     : base(scope) {
     Assert.NotNull(context, scope, mainObject);
     Debug.Assert(mainObject.ImmediateClass.IsSingletonClass);
         
     _context = context;
     _mainObject = mainObject;
     _isHosted = isHosted;
 }
Exemple #9
0
        /// <summary>
        /// Creates a new ModuleContext for the specified module.
        /// </summary>
        public ModuleContext(PythonModule/*!*/ module, PythonContext/*!*/ creatingContext) {
            ContractUtils.RequiresNotNull(module, "module");
            ContractUtils.RequiresNotNull(creatingContext, "creatingContext");

            _globals = module.__dict__;
            _pyContext = creatingContext;
            _globalScope = module.Scope;
            _globalContext = new CodeContext(_globals, this);
            _module = module;
        }
Exemple #10
0
        /// <summary>
        /// Creates a new ModuleContext which is backed by the specified dictionary.
        /// </summary>
        public ModuleContext(PythonDictionary/*!*/ globals, PythonContext/*!*/ creatingContext) {
            ContractUtils.RequiresNotNull(globals, "globals");
            ContractUtils.RequiresNotNull(creatingContext, "creatingContext");

            _globals = globals;
            _pyContext = creatingContext;
            _globalScope = new Scope(globals);
            _globalContext = new CodeContext(globals, this);
            _module = new PythonModule(globals, _globalScope);
            _globalScope.SetExtension(_pyContext.ContextId, new PythonScopeExtension(_pyContext, _module, this));
        }
        public ScriptDomainManager(DynamicRuntimeHostingProvider hostingProvider, DlrConfiguration configuration) {
            ContractUtils.RequiresNotNull(hostingProvider, "hostingProvider");
            ContractUtils.RequiresNotNull(configuration, "configuration");

            configuration.Freeze();

            _hostingProvider = hostingProvider;
            _configuration = configuration;

            _sharedIO = new SharedIO();

            // create the initial default scope
            _globals = new Scope();
        }
        protected object InvokeTarget(LambdaExpression code, Scope scope) {
            if (scope == _optimizedScope) {
                return _optimizedTarget(scope, LanguageContext);
            } 

            // new scope, compile unoptimized code and use that.
            if (_unoptimizedTarget == null) {
                // TODO: fix generated DLR ASTs - languages should remove their usage
                // of GlobalVariables and then this can go away.
                Expression<DlrMainCallTarget> lambda = new GlobalLookupRewriter().RewriteLambda(code);

                _unoptimizedTarget = lambda.Compile(SourceUnit.EmitDebugSymbols);
            }


            return _unoptimizedTarget(scope, LanguageContext);            
        }
Exemple #13
0
 private void EnsureCompiled(bool optimized) {
     //TODO too much duplicated code between these two blocks
     if (optimized) {
         if (_optimizedCode != null) return;
         var rewriter = new LightGlobalRewriter();
         var newLambda = rewriter.RewriteLambda(Code, Code.Name, LanguageContext, optimized);
         _optimizedScope = rewriter.Scope;
         var compiler = new LightCompiler();
         var interpreter = compiler.CompileTop(newLambda);
         _optimizedCode = new LightLambda(interpreter);
     } else {
         if (_code != null) return;
         var rewriter = new LightGlobalRewriter();
         var newLambda = rewriter.RewriteLambda(Code, Code.Name, LanguageContext, optimized);
         var compiler = new LightCompiler();
         var interpreter = compiler.CompileTop(newLambda);
         _code = new LightLambda(interpreter);
     }
 }
Exemple #14
0
 /// <summary>
 /// Creates a new Scope with the provided parent and dictionary.
 /// </summary>
 public Scope(Scope parent, IAttributesCollection dictionary)
     : this(parent, dictionary, true) {
 }
Exemple #15
0
 internal TotemModule(TotemContext context, Scope scope)
 {
     _dict = new TotemDictionary(new ScopeDictionaryStorage(context, scope));
     _scope = scope;
 }
 public PythonScopeExtension(PythonContext context, Scope scope) : base(scope) {
     _module = new PythonModule(context, scope);
     _modContext = new ModuleContext(_module, context);
 }
Exemple #17
0
 // emitted by GlobalRewriter
 // TODO: Python and JScript should do this
 public static CodeContext CreateTopLevelCodeContext(Scope scope, LanguageContext context) {
     context.EnsureScopeExtension(scope.ModuleScope);
     return new CodeContext(scope, context);
 }
Exemple #18
0
 private Scope CreateScope()
 {
     Scope ret = new Scope(new ObjectDictionaryExpando(_dict));
     ret.SetExtension(_context.ContextId, new TotemScopeExtension(_context, ret));
     return ret;
 }
Exemple #19
0
 private bool RequireFile(string/*!*/ path, Scope scope) {
     return _context.Loader.LoadFile(scope, null, _context.EncodePath(path), LoadFlags.Require);
 }
        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 }
                );
            }
        }
        public GlobalScopeDictionaryStorage(Scope scope)
            : base(CodeContext.GetModuleScope(scope)) {

        }
 public object Evaluate(Scope scope)
 {
     return this.Value;
 }
Exemple #23
0
 public CodeContext(Scope scope, LanguageContext languageContext)
     : this(scope, languageContext, null) {
 }
Exemple #24
0
        internal Scope GetLocalsScope() {
            ScopeData scopeData = CurrentScopeData;
            Scope scope = scopeData.Scope;
            if (scope == null) {
                Debug.Assert(_liftedLocals != null);

                List<SymbolId> visibleSymbols = new List<SymbolId>();
                List<VariableInfo> visibleLocals = new List<VariableInfo>();

                // Add parameters
                for (int i = 0; i < _funcInfo.Variables.Count; i++) {
                    if (_funcInfo.Variables[i].IsParameter && !_funcInfo.Variables[i].Hidden) {
                        visibleSymbols.Add(_funcInfo.Variables[i].Symbol);
                        visibleLocals.Add(_funcInfo.Variables[i]);
                    }
                }

                // Add locals
                foreach (VariableInfo varInfo in LocalsInCurrentScope) {
                    if (!varInfo.Hidden) {
                        visibleSymbols.Add(varInfo.Symbol);
                        visibleLocals.Add(varInfo);
                    }
                }

                IRuntimeVariables scopedLocals = new ScopedRuntimeVariables(visibleLocals, _liftedLocals);

                scope = new Scope(new LocalsDictionary(scopedLocals, visibleSymbols.ToArray()));

                scopeData.Scope = scope;
            }

            return scope;
        }
Exemple #25
0
 /// <summary>
 /// Creates a new Scope with the provided parent, dictionary and visibility.
 /// </summary>
 public Scope(Scope parent, IAttributesCollection dictionary, bool isVisible) {
     _parent = parent;
     _dict = dictionary ?? new SymbolDictionary();
     _isVisible = isVisible;
     _extensions = ScopeExtension.EmptyArray;
 }
        public GlobalScopeDictionaryStorage(Scope scope)
            : base(scope.ModuleScope) {

        }
Exemple #27
0
 public override object Run(Scope scope) {
     return EnsureTarget(_code)(scope, SourceUnit.LanguageContext);            
 }
 public override object Run(Scope scope) {
     return InvokeTarget(_code, scope);
 }
Exemple #29
0
 protected virtual object InvokeTarget(LambdaExpression code, Scope scope) {
     return EnsureTarget(code)(scope, LanguageContext);
 }
        /// <summary>
        /// Creates the methods and optimized Scope's which get associated with each ScriptCode.
        /// </summary>
        private Scope CompileOptimizedScope() {
            DlrMainCallTarget target;
            IAttributesCollection globals;
            if (UseLightweightScopes) {
                CompileWithArrayGlobals(out target, out globals);
            } else {
                CompileWithStaticGlobals(out target, out globals);
            }

            // Force creation of names used in other script codes into all optimized dictionaries
            Scope scope = new Scope(globals);
            ((IModuleDictionaryInitialization)globals).InitializeModuleDictionary(new CodeContext(scope, LanguageContext));

            // everything succeeded, commit the results
            _optimizedTarget = target;
            _optimizedScope = scope;

            return scope;
        }
Exemple #31
0
 public ScopeExtension(Scope scope)
 {
     ContractUtils.RequiresNotNull(scope, nameof(scope));
     Scope = scope;
 }