예제 #1
0
        private static object JsConstructor(Context cx, IScriptable scope, object [] args)
        {
            int arglen = args.Length;

            System.Text.StringBuilder sourceBuf = new System.Text.StringBuilder();

            sourceBuf.Append("function ");

            /* version != 1.2 Function constructor behavior -
             * print 'anonymous' as the function name if the
             * version (under which the function was compiled) is
             * less than 1.2... or if it's greater than 1.2, because
             * we need to be closer to ECMA.
             */
            if (cx.Version != Context.Versions.JS1_2)
            {
                sourceBuf.Append("anonymous");
            }
            sourceBuf.Append('(');

            // Append arguments as coma separated strings
            for (int i = 0; i < arglen - 1; i++)
            {
                if (i > 0)
                {
                    sourceBuf.Append(',');
                }
                sourceBuf.Append(ScriptConvert.ToString(args [i]));
            }
            sourceBuf.Append(") {");
            if (arglen != 0)
            {
                // append function body
                string funBody = ScriptConvert.ToString(args [arglen - 1]);
                sourceBuf.Append(funBody);
            }
            sourceBuf.Append('}');
            string source = sourceBuf.ToString();

            int [] linep    = new int [1];
            string filename = Context.GetSourcePositionFromStack(linep);

            if (filename == null)
            {
                filename  = "<eval'ed string>";
                linep [0] = 1;
            }

            string sourceURI = ScriptRuntime.makeUrlForGeneratedScript(false, filename, linep [0]);

            IScriptable global = ScriptableObject.GetTopLevelScope(scope);

            ErrorReporter reporter;

            reporter = DefaultErrorReporter.ForEval(cx.ErrorReporter);

            // Compile with explicit interpreter instance to force interpreter
            // mode.
            return(cx.CompileFunction(global, source, new Interpreter(), reporter, sourceURI, 1, (object)null));
        }
예제 #2
0
        internal static IRef createSpecial(Context cx, object obj, string name)
        {
            IScriptable target = ScriptConvert.ToObjectOrNull(cx, obj);

            if (target == null)
            {
                throw ScriptRuntime.UndefReadError(obj, name);
            }

            Types type;

            if (name.Equals("__proto__"))
            {
                type = Types.Proto;
            }
            else if (name.Equals("__parent__"))
            {
                type = Types.Parent;
            }
            else
            {
                throw new ArgumentException(name);
            }

            if (!cx.HasFeature(Context.Features.ParentProtoProperties))
            {
                // Clear special after checking for valid name!
                type = Types.None;
            }

            return(new SpecialRef(target, type, name));
        }
예제 #3
0
        public IdEnumeration(object value, Context cx, bool enumValues)
        {
            obj = ScriptConvert.ToObjectOrNull(cx, value);
            if (obj != null)
            {
                // null or undefined do not cause errors but rather lead to empty
                // "for in" loop
                this.enumValues = enumValues;

                // enumInit should read all initial ids before returning
                // or "for (a.i in a)" would wrongly enumerate i in a as well
                ChangeObject();
            }
        }
예제 #4
0
        public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args)
        {
            if (!f.HasTag(FUNCTION_TAG))
            {
                return(base.ExecIdCall(f, cx, scope, thisObj, args));
            }
            int id = f.MethodId;

            switch (id)
            {
            case Id_constructor:
                return(JsConstructor(cx, scope, args));


            case Id_toString: {
                BaseFunction realf  = RealFunction(thisObj, f);
                int          indent = ScriptConvert.ToInt32(args, 0);
                return(realf.Decompile(indent, Decompiler.TO_STRING_FLAG));
            }


            case Id_toSource: {
                BaseFunction realf  = RealFunction(thisObj, f);
                int          indent = 0;
                int          flags  = Decompiler.TO_SOURCE_FLAG;
                if (args.Length != 0)
                {
                    indent = ScriptConvert.ToInt32(args [0]);
                    if (indent >= 0)
                    {
                        flags = 0;
                    }
                    else
                    {
                        indent = 0;
                    }
                }
                return(realf.Decompile(indent, flags));
            }


            case Id_apply:
            case Id_call:
                return(ScriptRuntime.applyOrCall(id == Id_apply, cx, scope, thisObj, args));
            }
            throw new ArgumentException(Convert.ToString(id));
        }
예제 #5
0
        private static int PrintSourceNumber(string source, int offset, System.Text.StringBuilder sb)
        {
            double number = 0.0;
            char   type   = source [offset];

            ++offset;
            if (type == 'S')
            {
                if (sb != null)
                {
                    int ival = source [offset];
                    number = ival;
                }
                ++offset;
            }
            else if (type == 'J' || type == 'D')
            {
                if (sb != null)
                {
                    long lbits;
                    lbits  = (long)source [offset] << 48;
                    lbits |= (long)source [offset + 1] << 32;
                    lbits |= (long)source [offset + 2] << 16;
                    lbits |= (long)source [offset + 3];
                    if (type == 'J')
                    {
                        number = lbits;
                    }
                    else
                    {
                        number = BitConverter.Int64BitsToDouble(lbits);
                    }
                }
                offset += 4;
            }
            else
            {
                // Bad source
                throw new ApplicationException();
            }
            if (sb != null)
            {
                sb.Append(ScriptConvert.ToString(number, 10));
            }
            return(offset);
        }
예제 #6
0
        public object Set(Context cx, object value)
        {
            switch (type)
            {
            case Types.None:
                return(ScriptRuntime.setObjectProp(target, name, value, cx));

            case Types.Proto:
            case Types.Parent: {
                IScriptable obj = ScriptConvert.ToObjectOrNull(cx, value);
                if (obj != null)
                {
                    // Check that obj does not contain on its prototype/scope
                    // chain to prevent cycles
                    IScriptable search = obj;
                    do
                    {
                        if (search == target)
                        {
                            throw Context.ReportRuntimeErrorById("msg.cyclic.value", name);
                        }
                        if (type == Types.Proto)
                        {
                            search = search.GetPrototype();
                        }
                        else
                        {
                            search = search.ParentScope;
                        }
                    }while (search != null);
                }
                if (type == Types.Proto)
                {
                    target.SetPrototype(obj);
                }
                else
                {
                    target.ParentScope = obj;
                }
                return(obj);
            }

            default:
                throw Context.CodeBug();
            }
        }
예제 #7
0
        /// <summary> Note that if the <code>delegee</code> is <code>null</code>,
        /// this method creates a new instance of the Delegator itself
        /// rathert than forwarding the call to the
        /// <code>delegee</code>. This permits the use of Delegator
        /// prototypes.
        ///
        /// </summary>
        /// <param name="cx">the current Context for this thread
        /// </param>
        /// <param name="scope">an enclosing scope of the caller except
        /// when the function is called from a closure.
        /// </param>
        /// <param name="args">the array of arguments
        /// </param>
        /// <returns> the allocated object
        ///
        /// </returns>

        public virtual IScriptable Construct(Context cx, IScriptable scope, object [] args)
        {
            if (obj == null)
            {
                //this little trick allows us to declare prototype objects for
                //Delegators
                Delegator   n = NewInstance();
                IScriptable delegee;
                if (args.Length == 0)
                {
                    delegee = new BuiltinObject();
                }
                else
                {
                    delegee = ScriptConvert.ToObject(cx, scope, args [0]);
                }
                n.Delegee = delegee;
                return(n);
            }
            else
            {
                return(((IFunction)obj).Construct(cx, scope, args));
            }
        }