Esempio n. 1
0
        public virtual Binding BindSymbolForWrite(BindingRequest request)
        {
            var scope        = request.Thread.CurrentScope;
            var existingSlot = scope.Info.GetSlot(request.Symbol);

            //1. If new only, check it does not exist yet, create and return it
            if (request.Flags.IsSet(BindingRequestFlags.NewOnly))
            {
                if (existingSlot != null)
                {
                    request.Thread.ThrowScriptError("Variable {0} already exists.", request.Symbol);
                }

                var newSlot = scope.AddSlot(request.Symbol);
                return(new SlotBinding(newSlot, request.FromNode, request.FromScopeInfo));
            }

            //2. If exists, then return it
            if (existingSlot != null && request.Flags.IsSet(BindingRequestFlags.ExistingOrNew))
            {
                //TODO: For external client, check that slot is actually public or exported
                return(new SlotBinding(existingSlot, request.FromNode, request.FromScopeInfo));
            }

            //3. Check external module imports
            foreach (var imp in request.FromModule.Imports)
            {
                var result = imp.Bind(request);
                if (result != null)
                {
                    return(result);
                }
            }

            //4. If nothing found, create new slot in current scope
            if (request.Flags.IsSet(BindingRequestFlags.ExistingOrNew))
            {
                var newSlot = scope.AddSlot(request.Symbol);
                return(new SlotBinding(newSlot, request.FromNode, request.FromScopeInfo));
            }

            //5. Check built-in methods
            var builtIn = BuiltIns.Bind(request);

            if (builtIn != null)
            {
                return(builtIn);
            }

            //6. If still not found, return null.
            return(null);
        }
Esempio n. 2
0
        public virtual Binding BindSymbolForRead(BindingRequest request)
        {
            var symbol = request.Symbol;
            // First check current and enclosing scopes
            var currScope = request.Thread.CurrentScope;

            do
            {
                var existingSlot = currScope.Info.GetSlot(symbol);
                if (existingSlot != null)
                {
                    return(new SlotBinding(existingSlot, request.FromNode, request.FromScopeInfo));
                }
                currScope = currScope.Parent;
            } while (currScope != null);

            // If not found, check imports
            foreach (var imp in request.FromModule.Imports)
            {
                var result = imp.Bind(request);
                if (result != null)
                {
                    return(result);
                }
            }

            // Check built-in modules
            var builtIn = BuiltIns.Bind(request);

            if (builtIn != null)
            {
                return(builtIn);
            }

            // if not found, return null
            return(null);
        }