diff --git a/packages/api/src/typings/global.ts b/packages/api/src/typings/global.ts index a6c2144b..ca054194 100644 --- a/packages/api/src/typings/global.ts +++ b/packages/api/src/typings/global.ts @@ -1,3 +1,5 @@ +import { XMLHttpRequest as xhr } from '@ms/ployfill' + declare global { namespace NodeJS { interface Global { @@ -5,10 +7,12 @@ declare global { debug: boolean; noop: Function; console: Console; + XMLHttpRequest: typeof xhr; } } var root: string; var base: Core; + var XMLHttpRequest: typeof xhr; var ScriptEngineContextHolder: any; function engineLoad(str: string): any; interface Core { diff --git a/packages/common/src/reflect.ts b/packages/common/src/reflect.ts index 6c375461..1f5ae601 100644 --- a/packages/common/src/reflect.ts +++ b/packages/common/src/reflect.ts @@ -3,10 +3,10 @@ import '@ms/core' * 反射工具类 * Created by 蒋天蓓 on 2017/2/9 0009. */ -var JavaClass = Java.type('java.lang.Class'); -var JavaObject = Java.type('java.lang.Object') -var NoSuchFieldException = Java.type('java.lang.NoSuchFieldException'); -var methodCache = []; +const JavaClass = Java.type('java.lang.Class'); +const JavaObject = Java.type('java.lang.Object') +const NoSuchFieldException = Java.type('java.lang.NoSuchFieldException'); +const methodCache = []; class Reflect { private obj: any; @@ -35,7 +35,7 @@ class Reflect { field(name): Reflect { try { // Try getting a public field - var field = this.class.field(name); + let field = this.class.field(name); return on(field.get(this.obj)); } catch (ex) { // Try again, getting a non-public field @@ -48,14 +48,14 @@ class Reflect { } values(declared = false) { - var cache = {}; + let cache = {}; this.fields(declared).forEach(fed => cache[fed.name] = this.field(fed.name).get()) return cache; } call(...args): Reflect { - var params = args.slice(1); - var method = accessible(declaredMethod(this.class, args[0], types(params))); + let params = args.slice(1); + let method = accessible(declaredMethod(this.class, args[0], types(params))); let result = method.invoke(this.get(), params); return result && on(result); }; @@ -82,7 +82,7 @@ function types(values, def?) { if (values === null) { return []; } - var result = []; + let result: any[] = []; values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class)); return result; } @@ -98,7 +98,7 @@ function accessible(accessible) { } function declaredConstructor(clazz, param) { - var constructor; + let constructor; try { constructor = clazz.getDeclaredConstructor(types(param)); } catch (ex) { @@ -112,8 +112,8 @@ function declaredConstructor(clazz, param) { } function declaredField(clazz, name) { - var clazzt = clazz; - var field = null; + let clazzt = clazz; + let field = null; // noinspection JSUnresolvedVariable while (clazzt !== JavaObject.class) { try { @@ -132,7 +132,7 @@ function declaredField(clazz, name) { } function declaredMethod(clazz, name, clazzs) { - var key = clazz.name + '.' + name + ':' + (clazzs || []).join(':'); + let key = clazz.name + '.' + name + ':' + (clazzs || []).join(':'); if (!methodCache[key]) { try { methodCache[key] = clazz.getMethod(name, clazzs); @@ -156,21 +156,21 @@ function declaredMethods(clazz) { return clazz.declaredMethods; } -var classMethodsCache = []; +let classMethodsCache: any[] = []; function mapToObject(javaObj) { if (!javaObj || !javaObj.class) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) } - var target = {}; + let target = {}; getJavaObjectMethods(javaObj).forEach(t => mapMethod(target, javaObj, t)); return target; } function getJavaObjectMethods(javaObj) { - var className = javaObj.class.name; + let className = javaObj.class.name; if (!classMethodsCache[className]) { - var names = []; - var methods = javaObj.class.methods; - for (var i in methods) { + let names: any[] = []; + let methods = javaObj.class.methods; + for (let i in methods) { names.push(methods[i].name); } classMethodsCache[className] = names; diff --git a/packages/common/src/template.ts b/packages/common/src/template.ts index cb738350..66dc9a8e 100644 --- a/packages/common/src/template.ts +++ b/packages/common/src/template.ts @@ -1,25 +1,30 @@ -function Template(tpl: string) { - var match: RegExpExecArray; - var code = ['var r=[];']; - var re = /\{\{\s*([a-zA-Z\.\_0-9()]+)\s*\}\}/m; - function addLine(text: string) { - code.push('r.push(\'' + text.replace(/\'/g, '\\\'').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '\');'); - }; - while (match = re.exec(tpl)) { - if (match.index > 0) { - addLine(tpl.slice(0, match.index)); +class Template { + private renderFunc: Function; + + constructor(tpl: string) { + var match: RegExpExecArray; + var code = ['var r=[];']; + var re = /\{\{\s*([a-zA-Z\.\_0-9()]+)\s*\}\}/m; + + while (match = re.exec(tpl)) { + if (match.index > 0) { + this.addLine(code, tpl.slice(0, match.index)); + } + code.push('r.push(this.' + match[1] + ');'); + tpl = tpl.substring(match.index + match[0].length); } - code.push('r.push(this.' + match[1] + ');'); - tpl = tpl.substring(match.index + match[0].length); + this.addLine(code, tpl); + code.push('return r.join(\'\');'); + // 创建函数: + this.renderFunc = new Function(code.join('\n')); + } + addLine(code: string[], text: string) { + code.push('r.push(\'' + text.replace(/\'/g, '\\\'').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '\');'); } - addLine(tpl); - code.push('return r.join(\'\');'); - // 创建函数: - var fn = new Function(code.join('\n')); // 用render()调用函数并绑定this参数: - this.render = function(model) { - return fn.apply(model); - }; + render(model: object) { + return this.renderFunc.apply(model); + } } export = {