style: format file

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-27 18:40:35 +08:00
parent 4c0290bf21
commit e1530c49ee
3 changed files with 47 additions and 38 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -1,25 +1,30 @@
function Template(tpl: string) {
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;
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));
this.addLine(code, tpl.slice(0, match.index));
}
code.push('r.push(this.' + match[1] + ');');
tpl = tpl.substring(match.index + match[0].length);
}
addLine(tpl);
this.addLine(code, tpl);
code.push('return r.join(\'\');');
// 创建函数:
var fn = new Function(code.join('\n'));
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') + '\');');
}
// 用render()调用函数并绑定this参数
this.render = function(model) {
return fn.apply(model);
};
render(model: object) {
return this.renderFunc.apply(model);
}
}
export = {