1 28 29 package com.caucho.es; 30 31 35 class Native extends ESBase { 36 static ESId CONSTRUCTOR = ESId.intern("constructor"); 37 static ESId LENGTH = ESId.intern("length"); 38 static ESId PROTOTYPE = ESId.intern("prototype"); 39 static final int NEW = 1; 40 41 String name; 42 ESString id; 43 ESString []formals; 44 int length; 45 protected int n; 46 protected int newN; 47 48 protected Native(String name, int len) 49 { 50 prototype = esBase; 51 this.name = name; 52 this.length = len; 53 className = "Function"; 54 id = ESId.intern(name); 55 } 56 57 60 public ESBase getProperty(ESString key) 61 { 62 if (key.equals(LENGTH)) 63 return ESNumber.create(length); 64 else 65 return esEmpty; 66 } 67 68 public ESBase delete(ESString key) 69 { 70 return ESBoolean.create(false); 71 } 72 73 public ESBase typeof() throws ESException 74 { 75 return ESString.create("function"); 76 } 77 78 public ESString toStr() 79 { 80 return ESString.create(decompile()); 81 } 82 83 private String decompile() 84 { 85 StringBuffer sbuf = new StringBuffer (); 86 87 sbuf.append("function "); 88 sbuf.append(name); 89 sbuf.append("("); 90 for (int i = 0; formals != null && i < formals.length; i++) { 91 if (i != 0) 92 sbuf.append(", "); 93 sbuf.append(formals[i]); 94 } 95 96 sbuf.append(") "); 97 98 sbuf.append("{ "); 99 sbuf.append("[native code]"); 100 sbuf.append(" }"); 101 102 return sbuf.toString(); 103 } 104 105 public double toNum() 106 { 107 return 0.0/0.0; 108 } 109 110 public boolean toBoolean() 111 { 112 return true; 113 } 114 115 public ESObject toObject() 116 { 117 throw new RuntimeException (); 118 } 120 121 public ESBase construct(Call eval, int length) throws Throwable 122 { 123 if (n != newN) 124 return super.construct(eval, length); 125 126 try { 127 return (ESBase) call(eval, length); 128 } catch (ClassCastException e) { 129 throw new ESException("cannot create " + name); 130 } 131 } 132 133 protected Native create(String name, int n, int len) 134 { 135 throw new RuntimeException ("create not specialized"); 136 } 137 } 138 | Popular Tags |