📄 parseclass.java
字号:
Expr expr = fun.getReturnType(); boolean hasCoerce = false; if (expr == null) { } else if (expr.getType() == Expr.TYPE_INTEGER || expr.getType() == Expr.TYPE_NUMBER) { hasCoerce = true; print("ESNumber.create("); } else if (expr instanceof JavaTypeExpr) { print("call.global.wrap("); } print("_js_object."); print(fun.name + "(call, length"); for (int j = 0; j < fun.getFormalSize(); j++) { Variable formal = fun.getFormal(j); if (formal.getTypeExpr() instanceof JavaTypeExpr) { TypeExpr type = (TypeExpr) formal.getTypeExpr(); print(", (" + type.getTypeName() + ") call.getArgObject(" + j + ", length)"); } else if (formal.getType() == Expr.TYPE_INTEGER) print(", call.getArgInt32(" + j + ", length)"); } print(")"); if (hasCoerce) print(")"); println(";"); } println(" default:"); println(" throw new RuntimeException();"); println(" }"); println("}"); } void printGetProperty() throws IOException { if (variables.size() == 0) return; println(); println("public ESBase getProperty(ESString key) throws Throwable"); println("{"); pushDepth(); println("switch (propNames.get(key)) {"); Iterator iter = variables.values().iterator(); int i = 0; while (iter.hasNext()) { Variable var = (Variable) iter.next(); println("case " + i + ":"); Class javaClass = var.getTypeExpr().getJavaClass(); if (ESBase.class.isAssignableFrom(javaClass)) println(" return _js_object." + var.getId() + ";"); else println(" return wrap(_js_object." + var.getId() + ");"); i++; } println("default:"); println(" return super.getProperty(key);"); println("}"); popDepth(); println("}"); } void printSetProperty() throws IOException { if (variables.size() == 0) return; println(); println("public void setProperty(ESString key, ESBase value) throws Throwable"); println("{"); pushDepth(); println("switch (propNames.get(key)) {"); Iterator iter = variables.values().iterator(); int i = 0; while (iter.hasNext()) { Variable var = (Variable) iter.next(); println("case " + i + ":"); Class javaClass = var.getTypeExpr().getJavaClass(); if (ESBase.class.isAssignableFrom(javaClass)) println(" _js_object." + var.getId() + " = (" + javaClass.getName() + ") value.toJavaObject();"); else println(" _js_object." + var.getId() + " = value;"); println(" break;"); i++; } println("default:"); println(" return super.setProperty(key, value);"); println("}"); popDepth(); println("}"); } void printPropNames() throws IOException { if (variables.size() == 0) return; println(); println("private static com.caucho.util.IntMap propNames;"); println(); println("static {"); pushDepth(); println("propNames = new com.caucho.util.IntMap();"); Iterator iter = variables.values().iterator(); int i = 0; while (iter.hasNext()) { Variable var = (Variable) iter.next(); println("propNames.put(ESId.intern(\"" + var.getId() + "\"), " + i + ");"); i++; } popDepth(); println("}"); } void writeInit() throws IOException { println(); println("public void _init(Global resin, ESObject global) throws Throwable"); println("{"); pushDepth(); for (int i = 0; i < imports.size(); i++) { String importName = (String) imports.get(i); print("resin.importScript(this, \""); printString(importName); println("\");"); } println("ESClosure fun;"); for (int i = 0; global.functions != null && i < global.functions.size(); i++) { Function fun = (Function) global.functions.get(i); print("fun = new ESClosure("); print(getMangledLiteral(fun.id)); print(", this, null, " + fun.num + ", "); if (fun.getFormalSize() == 0) print("_a_null"); else print("_a_" + fun.num); println(", global);"); println("global.put(\"" + fun.id + "\", fun, ESBase.DONT_ENUM);"); } println("ESObject protoProto;"); println("ESObject proto;"); for (int i = 0; i < classes.size(); i++) { ParseClass cl = (ParseClass) classes.get(i); Function fun = cl.getFunction(ESId.intern(cl.name)); println("{"); pushDepth(); println(cl.name + "_es jsClass;"); println("jsClass = new " + cl.name + "_es();"); if (cl.proto != null) { print("proto = new ESObject(\"Object\", getProperty("); print(getMangledLiteral(cl.proto)); println(").getProperty("); printLiteral(ESId.intern("prototype")); println("));"); } else println("proto = resin.createObject();"); println("jsClass._init(resin, proto);"); print("fun = new ESClosure("); print(getMangledLiteral(ESId.intern(cl.name))); print(", jsClass, proto, " + fun.num + ", "); if (fun.getFormalSize() == 0) print("_a_null"); else print("_a_" + (i + functions.size())); println(", null);"); println("setProperty(\"" + cl.name + "\", fun);"); popDepth(); println("}"); } popDepth(); println("}"); } void writeLastModified() throws IOException { println(); println("public boolean isModified()"); println("{"); if (sourcePath == null || sourcePath.getLastModified() <= 0) println(" return false;"); else if (getScriptPath().lookup(sourcePath.getUserPath()).exists()) { print(" return scriptPath.lookup(\""); printString(sourcePath.getUserPath()); println("\").getLastModified() != " + sourcePath.getLastModified() + "L;"); } else { print(" return com.caucho.vfs.Vfs.lookup(\""); printString(sourcePath.getFullPath()); println("\").getLastModified() != " + sourcePath.getLastModified() + "L;"); } println("}"); } Function getFunction(ESId name) { for (int i = 0; i < functions.size(); i++) { Function fun = (Function) functions.get(i); if (fun.id == name) return fun; } return null; } boolean hasFunction(ArrayList functions, ESId var) { for (int i = 2; i < functions.size(); i++) { Function fun = (Function) functions.get(i); if (fun.id == var) return true; } return false; } void writeStaticInit() throws IOException { Iterator iter = literals.keySet().iterator(); if (iter.hasNext()) println(); while (iter.hasNext()) { Object o = iter.next(); String name = (String) literals.get(o); if (o instanceof ESId) { print("static ESId " + name); print(" = ESId.intern(\""); printString(String.valueOf(o)); println("\");"); } else if (o instanceof ESString) { print("static ESString " + name); print(" = ESString.create(\""); printString(String.valueOf(o)); println("\");"); } else if (o instanceof ESNumber) { print("static ESNumber " + name); double v = ((ESNumber) o).toNum(); if (Double.isInfinite(v)) println(" = ESNumber.create(Double.POSITIVE_INFINITY);"); else if (Double.isInfinite(-v)) println(" = ESNumber.create(Double.NEGATIVE_INFINITY);"); else if (Double.isNaN(v)) throw new RuntimeException(); else println(" = ESNumber.create(" + o + "D);"); } else throw new RuntimeException(); } println("static ESId[] _a_null = new ESId[0];"); for (int i = 0; i < functions.size(); i++) { Function fun = (Function) functions.get(i); printFormals(fun, i); } for (int i = 0; i < classes.size(); i++) { ParseClass cl = (ParseClass) classes.get(i); Function fun = cl.getFunction(ESId.intern(cl.name)); printFormals(fun, i + functions.size()); } } void setLine(String filename, int line) { lineMap.add(filename, line, destLine); } /** * Prints the mapping between java lines and the original *.js lines. */ void printLineMap() throws IOException { String dst = name + ".java"; String src = srcFilename; String srcTail = srcFilename; int p = srcTail.lastIndexOf('/'); if (p >= 0) srcTail = srcTail.substring(p + 1); p = srcTail.lastIndexOf('\\'); if (p >= 0) srcTail = srcTail.substring(p + 1); println(); println("public com.caucho.java.LineMap getLineMap()"); println("{"); pushDepth(); print("com.caucho.java.LineMap lineMap = new com.caucho.java.LineMap(\""); printString(dst); print("\", \""); printString(srcTail); println("\");"); println("lineMap.add(1, 1);"); Iterator iter = lineMap.iterator(); while (iter.hasNext()) { LineMap.Line line = (LineMap.Line) iter.next(); if (line.getSourceFilename() == src) { println("lineMap.add(" + line.getSourceLine() + ", " + line.getDestinationLine() + ");"); } else { src = line.getSourceFilename(); srcTail = src; p = srcTail.lastIndexOf('/'); if (p >= 0) srcTail = srcTail.substring(p + 1); p = srcTail.lastIndexOf('\\'); if (p >= 0) srcTail = srcTail.substring(p + 1); print("lineMap.add(\""); printString(srcTail); println("\", " + line.getSourceLine() + ", " + line.getDestinationLine() + ");"); } } println("return lineMap;"); popDepth(); println("}"); } private void printFormals(Function fun, int i) throws IOException { if (fun.getFormalSize() > 0) { print(" private static ESId[] _a_" + i + " = new ESId[] {"); for (int j = 0; fun.formals != null && j < fun.formals.size(); j++) { if (j != 0) print(","); Variable var = (Variable) fun.formals.get(j); print(" ESId.intern(\"" + var.getId() + "\")"); } println("};"); } } /** * Escapes the string so the Java compiler can properly understand it. */ void printString(String s) throws IOException { for (int i = 0; i < s.length(); i++) { if (i > 0 && i % (16 * 1024) == 0) os.print("\" + \""); char ch = s.charAt(i); switch (ch) { case '\\': os.print("\\\\"); break; case '\n': os.print("\\n"); break; case '\r': os.print("\\r"); break; case '\t': os.print("\\t"); break; case '"': os.print("\\\""); break; default: if (ch >= 32 && ch < 127) os.print(ch); else { os.print("\\u"); printHex(ch >> 12); printHex(ch >> 8); printHex(ch >> 4); printHex(ch >> 0); } break; } } } void printHex(int i) throws IOException { i &= 0xf; if (i < 10) { os.print(i); } else { os.print((char) ('a' + i - 10)); } } void pushDepth() { printDepth += 2; } void popDepth() { printDepth -= 2; } void print(boolean b) throws IOException { if (isFirst) printSpaces(); root.os.print(b); } void print(int i) throws IOException { if (isFirst) printSpaces(); root.os.print(i); } void print(char c) throws IOException { if (isFirst) printSpaces(); root.os.print(c); if (c == '\n') destLine++; } void print(String s) throws IOException { if (isFirst) printSpaces(); if (s == null) s = "null"; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '\n') { isFirst = true; destLine++; } else isFirst = false; } root.os.print(s); } void print(Object o) throws IOException { if (isFirst) printSpaces(); print(String.valueOf(o)); } void println() throws IOException { if (isFirst) printSpaces(); root.os.println(); destLine++; isFirst = true; } void println(String s) throws IOException { print(s); println(); } void println(Object o) throws IOException { print(String.valueOf(o)); println(); } void printSpaces() throws IOException { for (int i = 0; i < printDepth; i++) root.os.print(' '); isFirst = false; } static class Location { String filename; int line; Location(String filename, int line) { this.filename = filename; this.line = line; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -