Exemple #1
0
        public static void Init(Context cx, IScriptable scope, bool zealed)
        {
            BuiltinGlobal obj = new BuiltinGlobal();

            for (int id = 1; id <= LAST_SCOPE_FUNCTION_ID; ++id)
            {
                string name;
                int    arity = 1;
                switch (id)
                {
                case Id_decodeURI:
                    name = "decodeURI";
                    break;

                case Id_decodeURIComponent:
                    name = "decodeURIComponent";
                    break;

                case Id_encodeURI:
                    name = "encodeURI";
                    break;

                case Id_encodeURIComponent:
                    name = "encodeURIComponent";
                    break;

                case Id_escape:
                    name = "escape";
                    break;

                case Id_eval:
                    name = "eval";
                    break;

                case Id_isFinite:
                    name = "isFinite";
                    break;

                case Id_isNaN:
                    name = "isNaN";
                    break;

                case Id_isXMLName:
                    name = "isXMLName";
                    break;

                case Id_parseFloat:
                    name = "parseFloat";
                    break;

                case Id_parseInt:
                    name  = "parseInt";
                    arity = 2;
                    break;

                case Id_unescape:
                    name = "unescape";
                    break;

                case Id_uneval:
                    name = "uneval";
                    break;

                default:
                    throw Context.CodeBug();
                }
                IdFunctionObject f = new IdFunctionObject(obj, FTAG, id, name, arity, scope);
                if (zealed)
                {
                    f.SealObject();
                }
                f.ExportAsScopeProperty();
            }

            ScriptableObject.DefineProperty(scope, "NaN", (object)double.NaN, ScriptableObject.DONTENUM);
            ScriptableObject.DefineProperty(scope, "Infinity", (System.Double.PositiveInfinity), ScriptableObject.DONTENUM);
            ScriptableObject.DefineProperty(scope, "undefined", Undefined.Value, ScriptableObject.DONTENUM);

            string [] errorMethods = new string [] {
                "ConversionError",
                "EvalError",
                "RangeError",
                "ReferenceError",
                "SyntaxError",
                "TypeError",
                "URIError",
                "InternalError",
                "JavaException"
            };

            /*
             * Each error constructor gets its own Error object as a prototype,
             * with the 'name' property set to the name of the error.
             */
            for (int i = 0; i < errorMethods.Length; i++)
            {
                string      name       = errorMethods [i];
                IScriptable errorProto = ScriptRuntime.NewObject(cx, scope, "Error", ScriptRuntime.EmptyArgs);
                errorProto.Put("name", errorProto, name);
                if (zealed)
                {
                    if (errorProto is ScriptableObject)
                    {
                        ((ScriptableObject)errorProto).SealObject();
                    }
                }
                IdFunctionObject ctor = new IdFunctionObject(obj, FTAG, Id_new_CommonError, name, 1, scope);
                ctor.MarkAsConstructor(errorProto);
                if (zealed)
                {
                    ctor.SealObject();
                }
                ctor.ExportAsScopeProperty();
            }
        }
        public static void Init (Context cx, IScriptable scope, bool zealed)
        {
            BuiltinGlobal obj = new BuiltinGlobal ();

            for (int id = 1; id <= LAST_SCOPE_FUNCTION_ID; ++id) {
                string name;
                int arity = 1;
                switch (id) {

                    case Id_decodeURI:
                        name = "decodeURI";
                        break;

                    case Id_decodeURIComponent:
                        name = "decodeURIComponent";
                        break;

                    case Id_encodeURI:
                        name = "encodeURI";
                        break;

                    case Id_encodeURIComponent:
                        name = "encodeURIComponent";
                        break;

                    case Id_escape:
                        name = "escape";
                        break;

                    case Id_eval:
                        name = "eval";
                        break;

                    case Id_isFinite:
                        name = "isFinite";
                        break;

                    case Id_isNaN:
                        name = "isNaN";
                        break;

                    case Id_isXMLName:
                        name = "isXMLName";
                        break;

                    case Id_parseFloat:
                        name = "parseFloat";
                        break;

                    case Id_parseInt:
                        name = "parseInt";
                        arity = 2;
                        break;

                    case Id_unescape:
                        name = "unescape";
                        break;

                    case Id_uneval:
                        name = "uneval";
                        break;

                    default:
                        throw Context.CodeBug ();

                }
                IdFunctionObject f = new IdFunctionObject (obj, FTAG, id, name, arity, scope);
                if (zealed) {
                    f.SealObject ();
                }
                f.ExportAsScopeProperty ();
            }

            ScriptableObject.DefineProperty (scope, "NaN", (object)double.NaN, ScriptableObject.DONTENUM);
            ScriptableObject.DefineProperty (scope, "Infinity", (System.Double.PositiveInfinity), ScriptableObject.DONTENUM);
            ScriptableObject.DefineProperty (scope, "undefined", Undefined.Value, ScriptableObject.DONTENUM);

            string [] errorMethods = new string [] {
                "ConversionError",
                "EvalError",
                "RangeError",
                "ReferenceError",
                "SyntaxError",
                "TypeError",
                "URIError",
                "InternalError",
                "JavaException"
            };

            /*
            Each error constructor gets its own Error object as a prototype,
            with the 'name' property set to the name of the error.
            */
            for (int i = 0; i < errorMethods.Length; i++) {
                string name = errorMethods [i];
                IScriptable errorProto = ScriptRuntime.NewObject (cx, scope, "Error", ScriptRuntime.EmptyArgs);
                errorProto.Put ("name", errorProto, name);
                if (zealed) {
                    if (errorProto is ScriptableObject) {
                        ((ScriptableObject)errorProto).SealObject ();
                    }
                }
                IdFunctionObject ctor = new IdFunctionObject (obj, FTAG, Id_new_CommonError, name, 1, scope);
                ctor.MarkAsConstructor (errorProto);
                if (zealed) {
                    ctor.SealObject ();
                }
                ctor.ExportAsScopeProperty ();
            }
        }