📄 global.java
字号:
case JAVAX: return snap(id, pkg.getProperty("javax")); default: ESBase value = prototype == null ? null : prototype.getProperty(id); Object obj; if (value != null && value != esEmpty) return snap(id, value); else if (properties != null && (obj = properties.get(id.toString())) != null) return snap(id, objectWrap(obj)); else if ((value = (ESBase) globalProperties.get(id)) != null) return snap(id, value); else { return esEmpty; } } } private ESBase snap(ESString id, ESBase value) { if (value == null) throw new RuntimeException(); global.put(id, value, DONT_ENUM); return value; } ESRegexpWrapper getRegexp() { if (regExp != null) return regExp; else if (root.regExp != null) { regExp = root.regExp; return regExp; } initRegexp(); return regExp; } ESRegexp getRegexpProto() { if (regexpProto != null) return regexpProto; else if (root.regexpProto != null) { regexpProto = root.regexpProto; return regexpProto; } initRegexp(); return regexpProto; } private void initRegexp() { root.regexpProto = (ESRegexp) goldGlobal.regexpProto.resinCopy(); root.regexpProto.prototype = root.objProto; root.regExp = (ESRegexpWrapper) goldGlobal.regExp.resinCopy(); root.regExp.prototype = root.funProto; root.regExp.regexp = root.regexpProto; regexpProto = root.regexpProto; regExp = root.regExp; } /** * Sets a running script. * * @param name classname of the script. * @param script the script itself. */ public void addScript(String name, Script script) { if (_runtimeScripts != null) _runtimeScripts.put(name, new SoftReference<Script>(script)); } /** * Returns the line map for the named class to translate Java line * numbers to javascript line numbers. * * @param className class throwing the error. * @return the line map. */ LineMap getLineMap(String className) { try { int p = className.indexOf('$'); if (p > 0) className = className.substring(0, p); Script script = null; if (_runtimeScripts != null) { SoftReference<Script> ref = _runtimeScripts.get(className); if (ref != null) script = ref.get(); } if (script != null) return script.getLineMap(); else return null; } catch (Exception e) { return null; } } /** * Returns the named script. If the script has already been loaded, * return the old script. */ Script findScript(String className) throws Throwable { Script script = (Script) importScripts.get(className); if (script != null) return script; Parser parser = new Parser(); parser.setScriptPath(getScriptPath()); parser.setClassLoader(getClassLoader()); parser.setWorkDir(getClassDir()); return parser.parse(className); } /** * Returns the global prototype for the current thread. */ public static Global getGlobalProto() { return (Global) _globals.get(); } /** * Starts execution of a JavaScript thread. * * @return the old global context for the thread. */ Global begin() { Global oldGlobal = (Global) _globals.get(); _globals.set(this); return oldGlobal; } /** * Completes execution of a JavaScript thread, restoring the global context. * * @param oldGlobal the old global context for the thread. */ static void end(Global oldGlobal) { _globals.set(oldGlobal); } Call getCall() { Call call = _freeCalls.allocate(); if (call == null) return new Call(); else { call.clear(); return call; } } void freeCall(Call call) { call.free(); _freeCalls.free(call); } ESBase objectWrap(Object object) throws Throwable { if (object == null) return ESBase.esNull; Class cl = object.getClass(); String clName = cl.getName(); if (object instanceof ESBase) return (ESBase) object; if (clName.equals("java.lang.String")) return new ESString(object.toString()); if (clName.equals("java.lang.Double")) return ESNumber.create(((Double) object).doubleValue()); if (clName.equals("java.util.Date")) return convertDate(object); ESBase wrapper; synchronized (_staticWraps) { wrapper = _staticWraps.get(cl); } if (wrapper == null || ((DynamicClassLoader) wrapper.getClass().getClassLoader()).isDestroyed()) { ESBase []values = Wrapper.bean(this, cl); if (values == null) return ESBase.esNull; ESBase clWrapper = values[0]; wrapper = values[1]; if (wrapper.getClass().getClassLoader().getParent().equals(getClass().getClassLoader())) { synchronized (_staticWraps) { _staticClassWraps.put(cl, clWrapper); _staticWraps.put(cl, wrapper); } } } if (wrapper instanceof ESJavaWrapper) return ((ESJavaWrapper) wrapper).wrap(object); else { return ((ESBeanWrapper) wrapper).wrap(object); } } private ESBase convertDate(Object object) { return ESDate.create(((Date) object).getTime()); } // XXX: backwards -- s/b wrap and staticWrap public static ESBase wrap(Object object) throws Throwable { return getGlobalProto().objectWrap(object); } ESBase classWrap(Class cl) throws Throwable { if (cl == null) throw new RuntimeException(); ESBase clWrapper; synchronized (_staticWraps) { clWrapper = _staticClassWraps.get(cl); } if (clWrapper == null || ((DynamicClassLoader) clWrapper.getClass().getClassLoader()).isDestroyed()) { ESBase []values = Wrapper.bean(this, cl); clWrapper = values[0]; ESBase wrapper = values[1]; synchronized (_staticWraps) { _staticWraps.put(cl, wrapper); _staticClassWraps.put(cl, clWrapper); } } return clWrapper; } public ClassLoader getClassLoader() { return loader != null ? loader : root.loader; } public ClassLoader getParentLoader() { return parentLoader != null ? parentLoader : root.parentLoader; } public Path getClassDir() { return classDir != null ? classDir : root.classDir; } public Path getScriptPath() { return scriptPath != null ? scriptPath : root.scriptPath; } public void importScript(ESObject global, String name) throws Throwable { if (importScripts == null) { importScripts = new HashMap(); importGlobals = new HashMap(); } ESGlobal scriptGlobal = (ESGlobal) importGlobals.get(name); if (scriptGlobal == null) { if (importScripts.get(name) != null) return; Parser parser = new Parser(); parser.setScriptPath(getScriptPath()); parser.setClassLoader(getClassLoader()); parser.setWorkDir(getClassDir()); Script script = parser.parse(name); importScripts.put(name, script); scriptGlobal = script.initClass(this); importGlobals.put(name, scriptGlobal); scriptGlobal.execute(); } scriptGlobal.export(global); } ESGlobal getGlobal() { return global; } public void setGlobal(ESGlobal global) { this.global = global; } public Object toJavaObject() throws ESException { Object o = prototype.toJavaObject(); return (o == null) ? this : o; } /* * Somewhat bogus for creating based on globals. * "this" is the global */ public ESObject createObject() { return new ESObject("Object", objProto); } /** * Somewhat bogus for creating based on globals. * "this" is the global */ ESArray createArray() { ESArray array = new ESArray(); array.prototype = arrayProto; return array; } void clearMark() { markCount = 0; } int addMark() { return ++markCount; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -