public static void init()

in rhino/src/main/java/org/mozilla/javascript/NativeGlobal.java [26:182]


    public static void init(Context cx, Scriptable scope, boolean sealed) {
        defineGlobalFunction(
                scope,
                sealed,
                "decodeURI",
                1,
                (callCx, callScope, thisObj, args) -> js_decodeURI(args));
        defineGlobalFunction(
                scope,
                sealed,
                "decodeURIComponent",
                1,
                (callCx, callScope, thisObj, args) -> js_decodeURIComponent(args));
        defineGlobalFunction(
                scope,
                sealed,
                "encodeURI",
                1,
                (callCx, callScope, thisObj, args) -> js_encodeURI(args));
        defineGlobalFunction(
                scope,
                sealed,
                "encodeURIComponent",
                1,
                (callCx, callScope, thisObj, args) -> js_encodeURIComponent(args));
        defineGlobalFunction(
                scope, sealed, "escape", 1, (callCx, callScope, thisObj, args) -> js_escape(args));
        defineGlobalFunction(
                scope,
                sealed,
                "isFinite",
                1,
                (callCx, callScope, thisObj, args) -> js_isFinite(args));
        defineGlobalFunction(
                scope, sealed, "isNaN", 1, (callCx, callScope, thisObj, args) -> js_isNaN(args));
        defineGlobalFunction(
                scope,
                sealed,
                "isXMLName",
                1,
                (callCx, callScope, thisObj, args) -> js_isXMLName(callCx, callScope, args));
        defineGlobalFunction(
                scope,
                sealed,
                "parseFloat",
                1,
                (callCx, callScope, thisObj, args) -> js_parseFloat(args));
        defineGlobalFunction(
                scope,
                sealed,
                "parseInt",
                2,
                (callCx, callScope, thisObj, args) -> js_parseInt(callCx, args));
        defineGlobalFunction(
                scope,
                sealed,
                "unescape",
                1,
                (callCx, callScope, thisObj, args) -> js_unescape(args));
        defineGlobalFunction(
                scope,
                sealed,
                "uneval",
                1,
                (callCx, callScope, thisObj, args) -> js_uneval(callCx, callScope, args));
        defineGlobalFunctionEval(scope, sealed);

        ScriptableObject.defineProperty(
                scope, "NaN", ScriptRuntime.NaNobj, READONLY | DONTENUM | PERMANENT);
        ScriptableObject.defineProperty(
                scope, "Infinity", Double.POSITIVE_INFINITY, READONLY | DONTENUM | PERMANENT);
        ScriptableObject.defineProperty(
                scope, "undefined", Undefined.instance, READONLY | DONTENUM | PERMANENT);
        ScriptableObject.defineProperty(scope, "globalThis", scope, DONTENUM);

        /*
            Each error constructor gets its own Error object as a prototype,
            with the 'name' property set to the name of the error.
        */
        Scriptable nativeError =
                ScriptableObject.ensureScriptable(ScriptableObject.getProperty(scope, "Error"));
        Scriptable nativeErrorProto =
                ScriptableObject.ensureScriptable(
                        ScriptableObject.getProperty(nativeError, "prototype"));

        for (TopLevel.NativeErrors error : TopLevel.NativeErrors.values()) {
            if (error == TopLevel.NativeErrors.Error) {
                // Error is initialized elsewhere and we should not overwrite it.
                continue;
            }
            String name = error.name();
            Scriptable topLevelScope = ScriptableObject.getTopLevelScope(scope);
            Function builtinErrorCtor =
                    TopLevel.getBuiltinCtor(cx, topLevelScope, TopLevel.Builtins.Error);
            ScriptableObject errorProto = NativeError.makeProto(topLevelScope, builtinErrorCtor);
            errorProto.defineProperty("name", name, DONTENUM);
            errorProto.defineProperty("message", "", DONTENUM);

            BaseFunction ctor;

            // Building errors is complex because of the prototype chain requirements. This is a bit
            // arcane, but it's a combination that makes test262 happy.
            if (error == TopLevel.NativeErrors.AggregateError) {
                var target =
                        new SerializableConstructable() {
                            // We need a reference to the LambdaFunction, so we use this "lateBound"
                            // trick. It ain't great, but it's the only idea I have.
                            private Function lateBoundCtor;

                            @Override
                            public Scriptable construct(
                                    Context callCx, Scriptable callScope, Object[] args) {
                                return NativeError.makeAggregate(
                                        callCx, callScope, lateBoundCtor, args);
                            }
                        };
                ctor =
                        new LambdaConstructor(scope, name, 2, target) {
                            // Necessary to make the test262 case
                            // built-ins/NativeErrors/AggregateError/newtarget-proto-custom.js work
                            // correctly
                            @Override
                            public Scriptable createObject(Context cx, Scriptable scope) {
                                return null;
                            }
                        };
                target.lateBoundCtor = ctor;
            } else {
                var target =
                        new SerializableConstructable() {
                            private Function lateBoundCtor;

                            @Override
                            public Scriptable construct(
                                    Context callCx, Scriptable callScope, Object[] args) {
                                return NativeError.make(callCx, callScope, lateBoundCtor, args);
                            }
                        };
                ctor = new LambdaConstructor(scope, name, 1, target);
                target.lateBoundCtor = ctor;
            }

            ctor.setImmunePrototypeProperty(errorProto);
            ctor.setPrototype(nativeError);
            errorProto.put("constructor", errorProto, ctor);
            errorProto.setAttributes("constructor", ScriptableObject.DONTENUM);
            errorProto.setPrototype(nativeErrorProto);
            if (sealed) {
                errorProto.sealObject();
                ctor.sealObject();
            }
            ctor.setAttributes("name", DONTENUM | READONLY);
            ctor.setAttributes("length", DONTENUM | READONLY);

            ScriptableObject.defineProperty(scope, name, ctor, DONTENUM);
        }
    }