Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 2)
            {
                result = "wrong # args: should be \"global varName ?varName ...?\"";
                return(ReturnCode.Error);
            }

            ICallFrame localFrame = null;

            if (interpreter.GetVariableFrameViaResolvers(
                    LookupFlags.Default, ref localFrame,
                    ref result) == ReturnCode.Ok)
            {
                if ((localFrame != null) &&
                    !interpreter.IsGlobalCallFrame(localFrame))
                {
                    bool useNamespaces = interpreter.AreNamespacesEnabled();

                    for (int argumentIndex = 1;
                         argumentIndex < arguments.Count;
                         argumentIndex++)
                    {
                        string     varName    = arguments[argumentIndex];
                        ICallFrame otherFrame = interpreter.CurrentGlobalFrame;

                        if (useNamespaces)
                        {
                            string         qualifiers     = null;
                            string         tail           = null;
                            NamespaceFlags namespaceFlags = NamespaceFlags.None;

                            if (NamespaceOps.SplitName(
                                    varName, ref qualifiers, ref tail,
                                    ref namespaceFlags,
                                    ref result) == ReturnCode.Ok)
                            {
                                //
                                // NOTE: For linking between call frames, use
                                //       the simple variable name only.
                                //
                                varName = tail;
                            }
                            else
                            {
                                return(ReturnCode.Error);
                            }

                            if (FlagOps.HasFlags(namespaceFlags,
                                                 NamespaceFlags.Qualified, true))
                            {
                                INamespace @namespace = NamespaceOps.Lookup(
                                    interpreter, qualifiers, false, false,
                                    ref result);

                                if (@namespace != null)
                                {
                                    otherFrame = @namespace.VariableFrame;
                                }
                                else
                                {
                                    return(ReturnCode.Error);
                                }
                            }
                        }

                        if (ScriptOps.LinkVariable(
                                interpreter, localFrame, varName, otherFrame,
                                varName, ref result) != ReturnCode.Ok)
                        {
                            return(ReturnCode.Error);
                        }
                    }

                    result = String.Empty;
                }
                else
                {
                    // already in global scope... this is a NOP.
                    result = String.Empty;
                }

                return(ReturnCode.Ok);
            }
            else
            {
                return(ReturnCode.Error);
            }
        }
Ejemplo n.º 2
0
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            ReturnCode code = ReturnCode.Ok;

            if (interpreter != null)
            {
                if (arguments != null)
                {
                    if (arguments.Count >= 3)
                    {
                        ICallFrame otherFrame = null;

                        FrameResult frameResult = interpreter.GetCallFrame(
                            arguments[1], ref otherFrame, ref result);

                        if (frameResult != FrameResult.Invalid)
                        {
                            int count = arguments.Count - ((int)frameResult + 1);

                            if ((count & 1) == 0)
                            {
                                lock (interpreter.SyncRoot) /* TRANSACTIONAL */
                                {
                                    ICallFrame localFrame = null;

                                    code = interpreter.GetVariableFrameViaResolvers(
                                        LookupFlags.Default, ref localFrame, ref result);

                                    if (code == ReturnCode.Ok)
                                    {
                                        int argumentIndex = ((int)frameResult + 1); // skip "upvar ?level?"

                                        for (; count > 0; count -= 2, argumentIndex += 2)
                                        {
                                            string otherName = arguments[argumentIndex];
                                            string localName = arguments[argumentIndex + 1];

                                            code = ScriptOps.LinkVariable(
                                                interpreter, localFrame, localName,
                                                otherFrame, otherName, ref result);

                                            if (code != ReturnCode.Ok)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                result = "wrong # args: should be \"upvar ?level? otherVar localVar ?otherVar localVar ...?\"";
                                code   = ReturnCode.Error;
                            }
                        }
                        else
                        {
                            code = ReturnCode.Error;
                        }
                    }
                    else
                    {
                        result = "wrong # args: should be \"upvar ?level? otherVar localVar ?otherVar localVar ...?\"";
                        code   = ReturnCode.Error;
                    }
                }
                else
                {
                    result = "invalid argument list";
                    code   = ReturnCode.Error;
                }
            }
            else
            {
                result = "invalid interpreter";
                code   = ReturnCode.Error;
            }

            return(code);
        }
Ejemplo n.º 3
0
        ///////////////////////////////////////////////////////////////////////

        #region IExecute Members
        public override ReturnCode Execute(
            Interpreter interpreter,
            IClientData clientData,
            ArgumentList arguments,
            ref Result result
            )
        {
            if (interpreter == null)
            {
                result = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (arguments == null)
            {
                result = "invalid argument list";
                return(ReturnCode.Error);
            }

            if (arguments.Count < 2)
            {
                result = "wrong # args: should be \"variable ?name value...? name ?value?\"";
                return(ReturnCode.Error);
            }

            ICallFrame localFrame = null;

            if (interpreter.GetVariableFrameViaResolvers(
                    LookupFlags.Default, ref localFrame,
                    ref result) != ReturnCode.Ok)
            {
                return(ReturnCode.Error);
            }

            if (localFrame == null)
            {
                result = "local call frame is invalid";
                return(ReturnCode.Error);
            }

            if (!localFrame.IsVariable)
            {
                result = "local call frame does not support variables";
                return(ReturnCode.Error);
            }

            bool       useNamespaces    = interpreter.AreNamespacesEnabled();
            INamespace currentNamespace = null;

            if (useNamespaces &&
                interpreter.GetCurrentNamespaceViaResolvers(
                    null, LookupFlags.Default, ref currentNamespace,
                    ref result) != ReturnCode.Ok)
            {
                return(ReturnCode.Error);
            }

            ICallFrame otherFrame = null;

            if ((currentNamespace != null) &&
                !interpreter.IsGlobalNamespace(currentNamespace))
            {
                otherFrame = currentNamespace.VariableFrame;
            }
            else
            {
                otherFrame = interpreter.CurrentGlobalFrame;
            }

            for (int argumentIndex = 1;
                 argumentIndex < arguments.Count;
                 argumentIndex += 2)
            {
                string varName = arguments[argumentIndex];

                lock (interpreter.SyncRoot) /* TRANSACTIONAL */
                {
                    VariableFlags flags = VariableFlags.NoElement;

                    if (!useNamespaces)
                    {
                        flags |= VariableFlags.GlobalOnly;
                    }

                    IVariable otherVariable = null;
                    Result    error         = null;

                    if (interpreter.GetVariableViaResolversWithSplit(
                            varName, ref flags, ref otherVariable,
                            ref error) != ReturnCode.Ok)
                    {
                        if (FlagOps.HasFlags(
                                flags, VariableFlags.NotFound, true))
                        {
                            error = null;

                            if (interpreter.AddVariable2(
                                    VariableFlags.Undefined | flags,
                                    varName, null, true, ref otherVariable,
                                    ref error) != ReturnCode.Ok)
                            {
                                result = error;
                                return(ReturnCode.Error);
                            }
                        }
                        else
                        {
                            //
                            // NOTE: We did not search for the variable, let
                            //       the caller know why.
                            //
                            result = error;
                            return(ReturnCode.Error);
                        }
                    }

                    //
                    // NOTE: Create the variable link between the local frame
                    //       (i.e. a procedure, etc) and the other frame (i.e.
                    //       namespace or global).
                    //
                    if (CallFrameOps.IsLocal(localFrame))
                    {
                        error = null;

                        if (ScriptOps.LinkVariable(
                                interpreter, localFrame, varName, otherFrame,
                                varName, ref error) != ReturnCode.Ok)
                        {
                            result = error;
                            return(ReturnCode.Error);
                        }
                    }

                    //
                    // NOTE: If they provided a value, set it now.
                    //
                    // BUGFIX: This must be done after setting up the link
                    //         and not before; otherwise, the LinkVariable
                    //         method will detect a defined variable with
                    //         the same name in the local call frame and
                    //         refuse to overwrite it (by design).
                    //
                    if ((argumentIndex + 1) < arguments.Count)
                    {
                        error = null;

                        if (interpreter.SetVariableValue2(
                                VariableFlags.None, otherFrame, varName,
                                null, arguments[argumentIndex + 1].Value, null,
                                ref otherVariable, ref error) != ReturnCode.Ok)
                        {
                            result = error;
                            return(ReturnCode.Error);
                        }
                    }
                }
            }

            result = String.Empty;
            return(ReturnCode.Ok);
        }