📄 function.java
字号:
vars.put(id, var); } int getFormalSize() { return formals == null ? 0 : formals.size(); } Variable getFormal(int j) { return (Variable) formals.get(j); } Expr getReturnType() { return returnType; } void setReturnType(Expr type) { returnType = type; } int getVariableSize() { return variables == null ? 0 : variables.size(); } void addFunction(Function function) { if (functions == null) { functions = new ArrayList(); funMap = new IntMap(); } int pos = funMap.get(function.id); if (pos < 0) { funMap.put(function.id, functions.size()); functions.add(function); } else functions.set(pos, function); } int getFunctionSize() { return functions == null ? 0 : functions.size(); } Function getFunction(int i) { return (Function) functions.get(i); } Function getFunction(ESId id) { if (funMap == null) return null; int index = funMap.get(id); if (index >= 0) { return (Function) functions.get(index); } else return null; } void print(Object value) { if (tail == null) tail = CharBuffer.allocate(); tail.append(String.valueOf(value)); } void println(String value) { if (tail == null) tail = CharBuffer.allocate(); tail.append(String.valueOf(value)); tail.append('\n'); } void addExpr(Expr expr) { if (tail != null) data.add(tail); tail = null; data.add(expr); expr.setUsed(); } void addBoolean(Expr expr) { if (tail != null) data.add(tail); tail = null; data.add(expr.setBoolean()); expr.setUsed(); } Object getTop() { if (tail != null) return tail; else if (data.size() > 0) return data.get(data.size() - 1); else { tail = new CharBuffer(); return tail; } } Object getSwitch() { if (tail != null) data.add(tail); tail = null; hasSwitch = true; return data.get(data.size() - 1); } int mark() { if (tail != null) data.add(tail); tail = null; return data.size(); } void moveChunk(Object topObject, int mark) { if (tail != null) data.add(tail); tail = null; int top; for (top = 0; top < mark; top++) { if (data.get(top) == topObject) break; } top++; int here = data.size(); for (int i = 0; i < here - mark; i++) { Object chunk = data.remove(data.size() - 1); data.add(top, chunk); } } void writeCode(ParseClass cl) throws IOException { cl.print("public "); if (returnType == null) cl.print("ESBase "); else if (returnType.getType() == Expr.TYPE_INTEGER) cl.print("int "); else cl.print("ESBase "); cl.print(name + "(Call _env, int _length"); for (int i = 0; i < getFormalSize(); i++) { Variable formal = (Variable) formals.get(i); if (formal.getTypeExpr() instanceof TypeExpr) { TypeExpr type = (TypeExpr) formal.getTypeExpr(); if (type.getTypeName() != null) cl.print(", " + type.getTypeName() + " " + formal.getId()); } } cl.println(")"); cl.println("throws Throwable"); cl.println("{"); cl.pushDepth(); if (hasCall) cl.println("Call _call = _env.getCall();"); if (hasThis) cl.println("ESObject _this = _env.getThis();"); if (parent != null && functions != null && functions.size() > 0) { needsArguments = true; setNeedsScope(); } // Eval just uses the calling scope if (isEval) { cl.println("ESObject _arg = _env.getEval();"); } else { if (needsScope && parent != null) cl.println("_env.fillScope();"); if (needsArguments && parent != null) { cl.print("ESObject _arg = _env.createArg("); if (getFormalSize() > 0) cl.print("_js._a_" + num); else cl.print("_js._a_null"); cl.println(", _length);"); } } printFormals(); printLocalVariables(); // do closures for (int i = 0; needsArguments && functions != null && i < functions.size(); i++) { if (i == 0) cl.println("ESClosure _closure;"); Function fun = (Function) functions.get(i); Variable var = (Variable) vars.get(fun.id); if (! isGlobal && ! isEval && var != null && ! var.isUsed() && ! useAllVariables) { continue; } cl.print("_closure = new ESClosure("); cl.printLiteral(fun.id); cl.println(", _js, null, " + fun.num + ", _js._a_null, null);"); cl.println("_closure.closure(_env);"); if (! isEval && allowLocals && var != null && var.isUsed() && var.isJavaLocal()) { cl.println("ESBase " + fun.id + " = _closure;"); continue; } else if (! isEval && parent != null) cl.print("_arg.put("); else cl.print("_env.global.put("); cl.printLiteral(fun.id); cl.print(", _closure, "); if (! isEval) cl.print("ESBase.DONT_ENUM|ESBase.DONT_DELETE"); else cl.print("0"); cl.println(");"); } for (int i = 0; i < iterCount; i++) cl.println("java.util.Iterator iter" + i + ";"); for (int i = 0; i < tempCount; i++) cl.println("ESBase temp" + i + ";"); if (needsStatementResults()) cl.println("ESBase _val0 = ESBase.esUndefined;"); for (int i = 1; i <= stmtTop; i++) cl.println("ESBase _val" + i + " = ESBase.esUndefined;"); if (hasSwitch) { cl.println("int _switchcode;"); cl.println("ESBase _switchtemp;"); } for (int i = 0; i < data.size(); i++) { Object d = data.get(i); if (d instanceof CharBuffer) cl.print((CharBuffer) d); else if (d instanceof Expr) { Expr expr = (Expr) d; //cl.setLine(expr.getFilename(), expr.getLine()); expr.printExpr(); } } if (tail != null) cl.print(tail); cl.popDepth(); cl.println("}"); } void printFormals() throws IOException { formal: for (int i = 0; formals != null && i < formals.size(); i++) { Variable formal = (Variable) formals.get(i); // Functions have priority if (funMap != null && funMap.get(formal.getId()) >= 0) continue formal; // Duplicate formals use the last one for (int j = i + 1; j < formals.size(); j++) { if (formal.getId() == ((Variable) formals.get(j)).getId()) continue formal; } if (! allowLocals) { } else if (formal.getTypeExpr() instanceof JavaTypeExpr) { } else if (formal.getType() != Expr.TYPE_INTEGER) { cl.print("ESBase " + formal.getId()); cl.println(" = _env.getArg(" + i + ", _length);"); } } } /** * Initializes the local variables for a function. */ void printLocalVariables() throws IOException { for (int i = 0; i < variables.size(); i++) { Variable var = (Variable) variables.get(i); if (funMap != null && funMap.get(var.getId()) >= 0) { var.killLocal(); continue; } if (var.isJavaGlobal()) { // already initialized } else if ((! allowLocals || ! var.isJavaLocal()) && isGlobal()) { cl.print("_env.global.setProperty("); cl.printLiteral(var.getId()); cl.println(", ESBase.esUndefined);"); } else if (! var.isUsed() && ! useAllVariables()) { } else if (! allowLocals || ! var.isJavaLocal()) { if (isEval) { cl.print("if (_arg.getProperty("); cl.printLiteral(var.getId()); cl.println(") == ESBase.esEmpty)"); cl.print(" "); } if (! var.hasInit()) { cl.print("_arg.put("); cl.printLiteral(var.getId()); cl.print(", ESBase.esUndefined, "); if (! isEval) cl.print("ESBase.DONT_ENUM|ESBase.DONT_DELETE"); else cl.print("0"); cl.println(");"); } } else if (var.getType() == Expr.TYPE_BOOLEAN) { cl.println("boolean " + var.getId() + ";"); if (! var.hasInit()) cl.println(var.getId() + " = false;"); } else if (var.getType() == Expr.TYPE_INTEGER) { cl.println("int " + var.getId() + ";"); if (! var.hasInit()) cl.println(var.getId() + " = 0;"); } else if (var.getType() == Expr.TYPE_NUMBER) { cl.println("double " + var.getId() + ";"); if (! var.hasInit()) cl.println(var.getId() + " = Double.NaN;"); } else if (var.getType() == Expr.TYPE_STRING) { cl.println("ESString " + var.getId() + ";"); if (! var.hasInit()) cl.println(var.getId() + " = null;"); } else if (var.getType() == Expr.TYPE_JAVA && var.getTypeExpr() != null) { TypeExpr type = (TypeExpr) var.getTypeExpr(); cl.println(type.getTypeName() + " " + var.getId() + " = null;"); } else if (var.isLocal()) { if (! var.hasInit()) cl.println("ESBase " + var.getId() + " = ESBase.esUndefined;"); else cl.println("ESBase " + var.getId() + ";"); } else { cl.print(" _env.global.setProperty("); cl.printLiteral(var.getId()); cl.println(", ESBase.esUndefined);"); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -