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 { declare global {
namespace NodeJS { namespace NodeJS {
interface Global { interface Global {
@ -5,10 +7,12 @@ declare global {
debug: boolean; debug: boolean;
noop: Function; noop: Function;
console: Console; console: Console;
XMLHttpRequest: typeof xhr;
} }
} }
var root: string; var root: string;
var base: Core; var base: Core;
var XMLHttpRequest: typeof xhr;
var ScriptEngineContextHolder: any; var ScriptEngineContextHolder: any;
function engineLoad(str: string): any; function engineLoad(str: string): any;
interface Core { interface Core {

View File

@ -3,10 +3,10 @@ import '@ms/core'
* *
* Created by on 2017/2/9 0009. * Created by on 2017/2/9 0009.
*/ */
var JavaClass = Java.type('java.lang.Class'); const JavaClass = Java.type('java.lang.Class');
var JavaObject = Java.type('java.lang.Object') const JavaObject = Java.type('java.lang.Object')
var NoSuchFieldException = Java.type('java.lang.NoSuchFieldException'); const NoSuchFieldException = Java.type('java.lang.NoSuchFieldException');
var methodCache = []; const methodCache = [];
class Reflect { class Reflect {
private obj: any; private obj: any;
@ -35,7 +35,7 @@ class Reflect {
field(name): Reflect { field(name): Reflect {
try { try {
// Try getting a public field // Try getting a public field
var field = this.class.field(name); let field = this.class.field(name);
return on(field.get(this.obj)); return on(field.get(this.obj));
} catch (ex) { } catch (ex) {
// Try again, getting a non-public field // Try again, getting a non-public field
@ -48,14 +48,14 @@ class Reflect {
} }
values(declared = false) { values(declared = false) {
var cache = {}; let cache = {};
this.fields(declared).forEach(fed => cache[fed.name] = this.field(fed.name).get()) this.fields(declared).forEach(fed => cache[fed.name] = this.field(fed.name).get())
return cache; return cache;
} }
call(...args): Reflect { call(...args): Reflect {
var params = args.slice(1); let params = args.slice(1);
var method = accessible(declaredMethod(this.class, args[0], types(params))); let method = accessible(declaredMethod(this.class, args[0], types(params)));
let result = method.invoke(this.get(), params); let result = method.invoke(this.get(), params);
return result && on(result); return result && on(result);
}; };
@ -82,7 +82,7 @@ function types(values, def?) {
if (values === null) { if (values === null) {
return []; return [];
} }
var result = []; let result: any[] = [];
values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class)); values.forEach(t => result.push((t || def) ? JavaObject.class : t instanceof JavaClass ? t : t.class));
return result; return result;
} }
@ -98,7 +98,7 @@ function accessible(accessible) {
} }
function declaredConstructor(clazz, param) { function declaredConstructor(clazz, param) {
var constructor; let constructor;
try { try {
constructor = clazz.getDeclaredConstructor(types(param)); constructor = clazz.getDeclaredConstructor(types(param));
} catch (ex) { } catch (ex) {
@ -112,8 +112,8 @@ function declaredConstructor(clazz, param) {
} }
function declaredField(clazz, name) { function declaredField(clazz, name) {
var clazzt = clazz; let clazzt = clazz;
var field = null; let field = null;
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
while (clazzt !== JavaObject.class) { while (clazzt !== JavaObject.class) {
try { try {
@ -132,7 +132,7 @@ function declaredField(clazz, name) {
} }
function declaredMethod(clazz, name, clazzs) { function declaredMethod(clazz, name, clazzs) {
var key = clazz.name + '.' + name + ':' + (clazzs || []).join(':'); let key = clazz.name + '.' + name + ':' + (clazzs || []).join(':');
if (!methodCache[key]) { if (!methodCache[key]) {
try { try {
methodCache[key] = clazz.getMethod(name, clazzs); methodCache[key] = clazz.getMethod(name, clazzs);
@ -156,21 +156,21 @@ function declaredMethods(clazz) {
return clazz.declaredMethods; return clazz.declaredMethods;
} }
var classMethodsCache = []; let classMethodsCache: any[] = [];
function mapToObject(javaObj) { function mapToObject(javaObj) {
if (!javaObj || !javaObj.class) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) } if (!javaObj || !javaObj.class) { throw new TypeError(`参数 ${javaObj} 不是一个Java对象!`) }
var target = {}; let target = {};
getJavaObjectMethods(javaObj).forEach(t => mapMethod(target, javaObj, t)); getJavaObjectMethods(javaObj).forEach(t => mapMethod(target, javaObj, t));
return target; return target;
} }
function getJavaObjectMethods(javaObj) { function getJavaObjectMethods(javaObj) {
var className = javaObj.class.name; let className = javaObj.class.name;
if (!classMethodsCache[className]) { if (!classMethodsCache[className]) {
var names = []; let names: any[] = [];
var methods = javaObj.class.methods; let methods = javaObj.class.methods;
for (var i in methods) { for (let i in methods) {
names.push(methods[i].name); names.push(methods[i].name);
} }
classMethodsCache[className] = names; classMethodsCache[className] = names;

View File

@ -1,25 +1,30 @@
function Template(tpl: string) { class Template {
var match: RegExpExecArray; private renderFunc: Function;
var code = ['var r=[];'];
var re = /\{\{\s*([a-zA-Z\.\_0-9()]+)\s*\}\}/m; constructor(tpl: string) {
function addLine(text: string) { var match: RegExpExecArray;
code.push('r.push(\'' + text.replace(/\'/g, '\\\'').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '\');'); var code = ['var r=[];'];
}; var re = /\{\{\s*([a-zA-Z\.\_0-9()]+)\s*\}\}/m;
while (match = re.exec(tpl)) {
if (match.index > 0) { while (match = re.exec(tpl)) {
addLine(tpl.slice(0, match.index)); 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] + ');'); this.addLine(code, tpl);
tpl = tpl.substring(match.index + match[0].length); 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参数 // 用render()调用函数并绑定this参数
this.render = function(model) { render(model: object) {
return fn.apply(model); return this.renderFunc.apply(model);
}; }
} }
export = { export = {