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

📄 nodetransformer.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, 1999. * * 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): *   Norris Boyd *   Igor Bukanov *   Bob Jervis *   Roger Lawrence *   Mike McCabe * * 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;import java.util.ArrayList;import java.util.List;/** * This class transforms a tree to a lower-level representation for codegen. * * @see Node * @author Norris Boyd */public class NodeTransformer{    public NodeTransformer()    {    }    public final void transform(ScriptOrFnNode tree)    {        transformCompilationUnit(tree);        for (int i = 0; i != tree.getFunctionCount(); ++i) {            FunctionNode fn = tree.getFunctionNode(i);            transform(fn);        }    }    private void transformCompilationUnit(ScriptOrFnNode tree)    {        loops = new ObjArray();        loopEnds = new ObjArray();        // to save against upchecks if no finally blocks are used.        hasFinally = false;        // Flatten all only if we are not using scope objects for block scope        boolean createScopeObjects = tree.getType() != Token.FUNCTION ||                                  ((FunctionNode)tree).requiresActivation();        tree.flattenSymbolTable(!createScopeObjects);        //uncomment to print tree before transformation        //if (Token.printTrees) System.out.println(tree.toStringTree(tree));        transformCompilationUnit_r(tree, tree, tree, createScopeObjects);    }    private void transformCompilationUnit_r(final ScriptOrFnNode tree,                                            final Node parent,                                            Node.Scope scope,                                            boolean createScopeObjects)    {        Node node = null;      siblingLoop:        for (;;) {            Node previous = null;            if (node == null) {                node = parent.getFirstChild();            } else {                previous = node;                node = node.getNext();            }            if (node == null) {                break;            }            int type = node.getType();            if (createScopeObjects &&                (type == Token.BLOCK || type == Token.LOOP ||                 type == Token.ARRAYCOMP) &&                (node instanceof Node.Scope))            {                Node.Scope newScope = (Node.Scope) node;                if (newScope.symbolTable != null) {                    // transform to let statement so we get a with statement                    // created to contain scoped let variables                    Node let = new Node(type == Token.ARRAYCOMP ? Token.LETEXPR                                                                : Token.LET);                    Node innerLet = new Node(Token.LET);                    let.addChildToBack(innerLet);                    for (String name: newScope.symbolTable.keySet()) {                        innerLet.addChildToBack(Node.newString(Token.NAME, name));                    }                    newScope.symbolTable = null; // so we don't transform again                    Node oldNode = node;                    node = replaceCurrent(parent, previous, node, let);                    type = node.getType();                    let.addChildToBack(oldNode);                }            }                        switch (type) {              case Token.LABEL:              case Token.SWITCH:              case Token.LOOP:                loops.push(node);                loopEnds.push(((Node.Jump)node).target);                break;              case Token.WITH:              {                loops.push(node);                Node leave = node.getNext();                if (leave.getType() != Token.LEAVEWITH) {                    Kit.codeBug();                }                loopEnds.push(leave);                break;              }              case Token.TRY:              {                Node.Jump jump = (Node.Jump)node;                Node finallytarget = jump.getFinally();                if (finallytarget != null) {                    hasFinally = true;                    loops.push(node);                    loopEnds.push(finallytarget);                }                break;              }              case Token.TARGET:              case Token.LEAVEWITH:                if (!loopEnds.isEmpty() && loopEnds.peek() == node) {                    loopEnds.pop();                    loops.pop();                }                break;              case Token.YIELD:                ((FunctionNode)tree).addResumptionPoint(node);                break;              case Token.RETURN:              {                boolean isGenerator = tree.getType() == Token.FUNCTION                    && ((FunctionNode)tree).isGenerator();                if (isGenerator) {                    node.putIntProp(Node.GENERATOR_END_PROP, 1);                }                /* If we didn't support try/finally, it wouldn't be                 * necessary to put LEAVEWITH nodes here... but as                 * we do need a series of JSR FINALLY nodes before                 * each RETURN, we need to ensure that each finally                 * block gets the correct scope... which could mean                 * that some LEAVEWITH nodes are necessary.                 */                if (!hasFinally)                    break;     // skip the whole mess.                Node unwindBlock = null;                for (int i=loops.size()-1; i >= 0; i--) {                    Node n = (Node) loops.get(i);                    int elemtype = n.getType();                    if (elemtype == Token.TRY || elemtype == Token.WITH) {                        Node unwind;                        if (elemtype == Token.TRY) {                            Node.Jump jsrnode = new Node.Jump(Token.JSR);                            Node jsrtarget = ((Node.Jump)n).getFinally();                            jsrnode.target = jsrtarget;                            unwind = jsrnode;                        } else {                            unwind = new Node(Token.LEAVEWITH);                        }                        if (unwindBlock == null) {                            unwindBlock = new Node(Token.BLOCK,                                                   node.getLineno());                        }                        unwindBlock.addChildToBack(unwind);                    }                }                if (unwindBlock != null) {                    Node returnNode = node;                    Node returnExpr = returnNode.getFirstChild();                    node = replaceCurrent(parent, previous, node, unwindBlock);                    if (returnExpr == null || isGenerator) {                        unwindBlock.addChildToBack(returnNode);                    } else {                        Node store = new Node(Token.EXPR_RESULT, returnExpr);                        unwindBlock.addChildToFront(store);                        returnNode = new Node(Token.RETURN_RESULT);                        unwindBlock.addChildToBack(returnNode);                        // transform return expression                        transformCompilationUnit_r(tree, store, scope,                                                    createScopeObjects);                    }                    // skip transformCompilationUnit_r to avoid infinite loop                    continue siblingLoop;                }                break;              }              case Token.BREAK:              case Token.CONTINUE:              {                Node.Jump jump = (Node.Jump)node;                Node.Jump jumpStatement = jump.getJumpStatement();                if (jumpStatement == null) Kit.codeBug();                for (int i = loops.size(); ;) {                    if (i == 0) {                        // Parser/IRFactory ensure that break/continue                        // always has a jump statement associated with it                        // which should be found                        throw Kit.codeBug();                    }                    --i;                    Node n = (Node) loops.get(i);                    if (n == jumpStatement) {                        break;                    }                    int elemtype = n.getType();                    if (elemtype == Token.WITH) {                        Node leave = new Node(Token.LEAVEWITH);                        previous = addBeforeCurrent(parent, previous, node,                                                    leave);                    } else if (elemtype == Token.TRY) {                        Node.Jump tryNode = (Node.Jump)n;                        Node.Jump jsrFinally = new Node.Jump(Token.JSR);                        jsrFinally.target = tryNode.getFinally();                        previous = addBeforeCurrent(parent, previous, node,                                                    jsrFinally);                    }                }                if (type == Token.BREAK) {                    jump.target = jumpStatement.target;                } else {                    jump.target = jumpStatement.getContinue();                }                jump.setType(Token.GOTO);                break;              }              case Token.CALL:                visitCall(node, tree);                break;              case Token.NEW:

⌨️ 快捷键说明

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