diff --git a/js-runtime/src/sandbox.ts b/js-runtime/src/sandbox.ts index c6b19b7c..4247a4da 100644 --- a/js-runtime/src/sandbox.ts +++ b/js-runtime/src/sandbox.ts @@ -1,6 +1,6 @@ /** * ``` TypeScript - * // run in global scope + * // load script in global scope * Reflect.apply( * function(hego,context,require){ * //Script content @@ -10,7 +10,7 @@ * hego.obtainContext(id), * hego.__require__, * ]) - * + * // load module in global scope * Reflect.apply(hego.registerModule,this,[ * moduleName, * Reflect.apply(function(__module){ @@ -21,31 +21,101 @@ * * ``` */ +declare function nativeRequire(moduleName: string): boolean -export interface Context { - id: number - asyncCall(module: string, method: string, args: any, callbackId: string): any +declare function nativeBridge(contextId: string, namespace: string, method: string, args?: any, callbackId?: string): boolean + +let __uniqueId__: number = 0 + +function uniqueId(prefix: string) { + return `__${prefix}_${__uniqueId__++}__`; } -const gContexts = new Map -const gModules = new Map +export function log(message: any) { + console.log(message) +} -export function obtainContext(id: string) { +export function loge(message: any) { + console.error(message) +} + +export function logw(message: any) { + console.warn(message) +} + +export function jsCallResolve(contextId: string, callbackId: string, args?: any) { + const context = gContexts.get(contextId) + if (context === undefined) { + loge(`Cannot find context for context id:${contextId}`) + return + } + const callback = context.callbacks.get(callbackId) + if (callback === undefined) { + loge(`Cannot find call for context id:${contextId},callback id:${callbackId}`) + return + } + const argumentsList: any = [] + for (let i = 2; i < arguments.length; i++) { + argumentsList.push(arguments[i]) + } + Reflect.apply(callback.resolve, context, args) +} + +export function jsCallReject(contextId: string, callbackId: string, args?: any) { + const context = gContexts.get(contextId) + if (context === undefined) { + loge(`Cannot find context for context id:${contextId}`) + return + } + const callback = context.callbacks.get(callbackId) + if (callback === undefined) { + loge(`Cannot find call for context id:${contextId},callback id:${callbackId}`) + return + } + const argumentsList: any = [] + for (let i = 2; i < arguments.length; i++) { + argumentsList.push(arguments[i]) + } + Reflect.apply(callback.reject, context, args) +} + +export class Context { + id: string + callbacks: Map = new Map + constructor(id: string) { + this.id = id + } + callNative(namespace: string, method: string, args?: any): Promise { + const callbackId = uniqueId('callback') + + nativeBridge(this.id, namespace, method, args, callbackId) + return new Promise((resolve, reject) => { + this.callbacks.set(callbackId, { + resolve, + reject, + }) + }) + } +} + + +const gContexts: Map = new Map +const gModules: Map = new Map + +export function jsObtainContext(id: string) { if (gContexts.has(id)) { return gContexts.get(id) } else { - const context = { id } + const context: Context = new Context(id) gContexts.set(id, context) return context } } -export function releaseContext(id: string) { +export function jsReleaseContext(id: string) { gContexts.delete(id) } -declare function nativeRequire(name: string): boolean - export function __require__(name: string): any { if (gModules.has(name)) { return gModules.get(name) @@ -57,6 +127,12 @@ export function __require__(name: string): any { } } } -export function registerModule(name: string, moduleObject: any) { + +export function jsRegisterModule(name: string, moduleObject: any) { gModules.set(name, moduleObject) -} \ No newline at end of file +} + +export function jsCall() { + +} +