From e2f32fd0bf6bf3c519a1961537e72a9a60628564 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 1 Feb 2020 02:44:23 +0800 Subject: [PATCH] feat: move global declare to @ms/ployfill Signed-off-by: MiaoWoo --- packages/api/src/console.ts | 8 ++++++- packages/api/src/index.ts | 2 +- .../src/typings => ployfill/src}/global.ts | 11 +++++----- packages/ployfill/src/index.ts | 22 ++++++++++++++----- 4 files changed, 30 insertions(+), 13 deletions(-) rename packages/{api/src/typings => ployfill/src}/global.ts (76%) diff --git a/packages/api/src/console.ts b/packages/api/src/console.ts index f2d6afcc..d6165378 100644 --- a/packages/api/src/console.ts +++ b/packages/api/src/console.ts @@ -17,7 +17,7 @@ export class MiaoScriptConsole implements Console { Console: NodeJS.ConsoleConstructor; private _name: string = ''; - private _level: LogLevel = global.debug ? LogLevel.DEBUG : LogLevel.INFO; + private _level: LogLevel = LogLevel.INFO; protected logger: any; protected prefix: string = '§6[§bMiaoScript§6]§r '; @@ -25,6 +25,12 @@ export class MiaoScriptConsole implements Console { constructor(name?: string) { this.name = name; this.logger = global.logger; + if (global.debug) { + this._level = LogLevel.DEBUG + } + if (global.level?.toUpperCase() === "TRACE") { + this._level = LogLevel.TRACE + } } get name() { diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index 4290f60b..58a23905 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -1,4 +1,4 @@ -/// +import '@ms/ployfill' export * from './task' export * from './event' diff --git a/packages/api/src/typings/global.ts b/packages/ployfill/src/global.ts similarity index 76% rename from packages/api/src/typings/global.ts rename to packages/ployfill/src/global.ts index 5ca641be..d6efabb7 100644 --- a/packages/api/src/typings/global.ts +++ b/packages/ployfill/src/global.ts @@ -1,13 +1,14 @@ -import { XMLHttpRequest as xhr } from '@ms/ployfill' - +import { XMLHttpRequest as xhr } from './xml-http-request' declare global { namespace NodeJS { interface Global { logger: any; debug: boolean; - noop: Function; + level: string; + NashornEngineStartTime: number; + setGlobal: (key: string, value: any) => void; + noop: () => void; console: Console; - XMLHttpRequest: typeof xhr; } } var root: string; @@ -30,4 +31,4 @@ declare global { console(...args: any): void; } } -export { } +export { } \ No newline at end of file diff --git a/packages/ployfill/src/index.ts b/packages/ployfill/src/index.ts index b0361cd3..a7aca07d 100644 --- a/packages/ployfill/src/index.ts +++ b/packages/ployfill/src/index.ts @@ -1,7 +1,17 @@ -import { XMLHttpRequest as xhr } from './xml-http-request' +import './global' +import '@ms/nashorn' +import { XMLHttpRequest } from './xml-http-request' -var XMLHttpRequest = xhr; - -export { - XMLHttpRequest -} \ No newline at end of file +let ployfillStartTime = new Date().getTime(); +console.info('Initialization Java Nashorn ployfill. Please wait...'); +// String contains ployfill +Object.defineProperty(String.prototype, 'contains', { + value: function(searchString: string, position: number) { + return String.prototype.indexOf.call(this, searchString, position) > -1; + } +}) +// ES6 Map Symbol ployfill +require('es6-map/implement'); +require('es6-symbol/implement'); +global.setGlobal('XMLHttpRequest', XMLHttpRequest) +console.info('Java Nashorn ployfill loading completed... Cost (' + (new Date().getTime() - ployfillStartTime) / 1000 + 's)!');