⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 這是一個javascript 的 interpreter是了解 web browser的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Rhino code, released * May 6, 1998. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1997-1999 * the Initial Developer. All Rights Reserved. * * Contributor(s): *   Patrick Beard *   Norris Boyd *   Igor Bukanov *   Rob Ginda *   Kurt Westerfeld * * Alternatively, the contents of this file may be used under the terms of * the GNU General Public License Version 2 or later (the "GPL"), in which * case the provisions of the GPL are applicable instead of those above. If * you wish to allow use of your version of this file only under the terms of * the GPL and not to allow others to use your version of this file under the * MPL, indicate your decision by deleting the provisions above and replacing * them with the notice and other provisions required by the GPL. If you do * not delete the provisions above, a recipient may use your version of this * file under either the MPL or the GPL. * * ***** END LICENSE BLOCK ***** */package org.mozilla.javascript.tools.shell;import java.io.*;import java.net.URL;import java.net.URLConnection;import java.net.MalformedURLException;import java.util.*;import org.mozilla.javascript.*;import org.mozilla.javascript.tools.ToolErrorReporter;/** * The shell program. * * Can execute scripts interactively or in batch mode at the command line. * An example of controlling the JavaScript engine. * * @author Norris Boyd */public class Main{    public static ShellContextFactory        shellContextFactory = new ShellContextFactory();    public static Global global = new Global();    static protected ToolErrorReporter errorReporter;    static protected int exitCode = 0;    static private final int EXITCODE_RUNTIME_ERROR = 3;    static private final int EXITCODE_FILE_NOT_FOUND = 4;    static boolean processStdin = true;    static Vector fileList = new Vector(5);    private static SecurityProxy securityImpl;    static {        global.initQuitAction(new IProxy(IProxy.SYSTEM_EXIT));    }    /**     * Proxy class to avoid proliferation of anonymous classes.     */    private static class IProxy implements ContextAction, QuitAction    {        private static final int PROCESS_FILES = 1;        private static final int EVAL_INLINE_SCRIPT = 2;        private static final int SYSTEM_EXIT = 3;        private int type;        String[] args;        String scriptText;        IProxy(int type)        {            this.type = type;        }        public Object run(Context cx)        {            if (type == PROCESS_FILES) {                processFiles(cx, args);            } else if (type == EVAL_INLINE_SCRIPT) {                Script script = loadScriptFromSource(cx, scriptText,                                                     "<command>", 1, null);                if (script != null) {                    evaluateScript(script, cx, getGlobal());                }            } else {                throw Kit.codeBug();            }            return null;        }        public void quit(Context cx, int exitCode)        {            if (type == SYSTEM_EXIT) {                System.exit(exitCode);                return;            }            throw Kit.codeBug();        }    }    /**     * Main entry point.     *     * Process arguments as would a normal Java program. Also     * create a new Context and associate it with the current thread.     * Then set up the execution environment and begin to     * execute scripts.     */    public static void main(String args[]) {        try {            if (Boolean.getBoolean("rhino.use_java_policy_security")) {                initJavaPolicySecuritySupport();            }        } catch (SecurityException ex) {            ex.printStackTrace(System.err);        }        int result = exec(args);        if (result != 0) {            System.exit(result);        }    }    /**     *  Execute the given arguments, but don't System.exit at the end.     */    public static int exec(String origArgs[])    {        errorReporter = new ToolErrorReporter(false, global.getErr());        shellContextFactory.setErrorReporter(errorReporter);        String[] args = processOptions(origArgs);        if (processStdin)            fileList.addElement(null);        if (!global.initialized) {            global.init(shellContextFactory);        }        IProxy iproxy = new IProxy(IProxy.PROCESS_FILES);        iproxy.args = args;        shellContextFactory.call(iproxy);        return exitCode;    }    static void processFiles(Context cx, String[] args)    {        // define "arguments" array in the top-level object:        // need to allocate new array since newArray requires instances        // of exactly Object[], not ObjectSubclass[]        Object[] array = new Object[args.length];        System.arraycopy(args, 0, array, 0, args.length);        Scriptable argsObj = cx.newArray(global, array);        global.defineProperty("arguments", argsObj,                              ScriptableObject.DONTENUM);        for (int i=0; i < fileList.size(); i++) {            processSource(cx, (String) fileList.elementAt(i));        }    }    public static Global getGlobal()    {        return global;    }    /**     * Parse arguments.     */    public static String[] processOptions(String args[])    {        String usageError;        goodUsage: for (int i = 0; ; ++i) {            if (i == args.length) {                return new String[0];            }            String arg = args[i];            if (!arg.startsWith("-")) {                processStdin = false;                fileList.addElement(arg);                String[] result = new String[args.length - i - 1];                System.arraycopy(args, i+1, result, 0, args.length - i - 1);                return result;            }            if (arg.equals("-version")) {                if (++i == args.length) {                    usageError = arg;                    break goodUsage;                }                int version;                try {                    version = Integer.parseInt(args[i]);                } catch (NumberFormatException ex) {                    usageError = args[i];                    break goodUsage;                }                if (!Context.isValidLanguageVersion(version)) {                    usageError = args[i];                    break goodUsage;                }                shellContextFactory.setLanguageVersion(version);                continue;            }            if (arg.equals("-opt") || arg.equals("-O")) {                if (++i == args.length) {                    usageError = arg;                    break goodUsage;                }                int opt;                try {                    opt = Integer.parseInt(args[i]);                } catch (NumberFormatException ex) {                    usageError = args[i];                    break goodUsage;                }                if (opt == -2) {                    // Compatibility with Cocoon Rhino fork                    opt = -1;                } else if (!Context.isValidOptimizationLevel(opt)) {                    usageError = args[i];                    break goodUsage;                }                shellContextFactory.setOptimizationLevel(opt);                continue;            }            if (arg.equals("-strict")) {                shellContextFactory.setStrictMode(true);                errorReporter.setIsReportingWarnings(true);                continue;            }            if (arg.equals("-fatal-warnings")) {                shellContextFactory.setWarningAsError(true);                continue;            }            if (arg.equals("-e")) {                processStdin = false;                if (++i == args.length) {                    usageError = arg;                    break goodUsage;                }                if (!global.initialized) {                    global.init(shellContextFactory);                }                IProxy iproxy = new IProxy(IProxy.EVAL_INLINE_SCRIPT);                iproxy.scriptText = args[i];                shellContextFactory.call(iproxy);                continue;            }            if (arg.equals("-w")) {                errorReporter.setIsReportingWarnings(true);                continue;            }            if (arg.equals("-f")) {                processStdin = false;                if (++i == args.length) {                    usageError = arg;                    break goodUsage;                }                fileList.addElement(args[i].equals("-") ? null : args[i]);                continue;            }            if (arg.equals("-sealedlib")) {                global.setSealedStdLib(true);                continue;            }            if (arg.equals("-debug")) {                shellContextFactory.setGeneratingDebug(true);                continue;            }            if (arg.equals("-?") ||                arg.equals("-help")) {                // print usage message                global.getOut().println(                    ToolErrorReporter.getMessage("msg.shell.usage", Main.class.getName()));                System.exit(1);            }            usageError = arg;            break goodUsage;        }        // print error and usage message        global.getOut().println(            ToolErrorReporter.getMessage("msg.shell.invalid", usageError));        global.getOut().println(            ToolErrorReporter.getMessage("msg.shell.usage", Main.class.getName()));        System.exit(1);        return null;    }    private static void initJavaPolicySecuritySupport()    {        Throwable exObj;        try {            Class cl = Class.forName                ("org.mozilla.javascript.tools.shell.JavaPolicySecurity");            securityImpl = (SecurityProxy)cl.newInstance();            SecurityController.initGlobal(securityImpl);            return;        } catch (ClassNotFoundException ex) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -