2019-12-19 12:45:40 +08:00
|
|
|
import { jsObtainContext, jsCallResolve, jsCallReject } from 'doric/src/runtime/sandbox'
|
2019-12-19 11:52:15 +08:00
|
|
|
import { acquireJSBundle, acquirePlugin } from './DoricRegistry'
|
2019-12-19 12:45:40 +08:00
|
|
|
import { getDoricContext } from './DoricContext'
|
|
|
|
import { DoricPlugin } from './DoricPlugin'
|
2019-12-19 11:52:15 +08:00
|
|
|
|
2019-12-19 12:45:40 +08:00
|
|
|
let __scriptId__ = 0
|
2019-12-19 11:52:15 +08:00
|
|
|
function getScriptId() {
|
2019-12-19 12:45:40 +08:00
|
|
|
return `script_${__scriptId__++}`
|
2019-12-19 11:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-19 12:45:40 +08:00
|
|
|
let __contextId__ = 0
|
2019-12-19 11:52:15 +08:00
|
|
|
export function getContextId() {
|
2019-12-19 12:45:40 +08:00
|
|
|
return `context_${__contextId__++}`
|
2019-12-19 11:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function injectGlobalObject(name: string, value: any) {
|
|
|
|
Reflect.set(window, name, value, window)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadJS(script: string) {
|
|
|
|
const scriptElement = document.createElement('script')
|
|
|
|
scriptElement.text = script
|
|
|
|
scriptElement.id = getScriptId()
|
|
|
|
document.body.appendChild(scriptElement)
|
|
|
|
}
|
|
|
|
|
|
|
|
function packageModuleScript(name: string, content: string) {
|
|
|
|
return `Reflect.apply(doric.jsRegisterModule,this,[${name},Reflect.apply(function(__module){(function(module,exports,require){
|
|
|
|
${content}
|
|
|
|
})(__module,__module.exports,doric.__require__);
|
|
|
|
return __module.exports;},this,[{exports:{}}])])`
|
|
|
|
}
|
|
|
|
|
|
|
|
function packageCreateContext(contextId: string, content: string) {
|
|
|
|
return `Reflect.apply(function(doric,context,Entry,require,exports){
|
|
|
|
${content}
|
|
|
|
},doric.jsObtainContext("${contextId}"),[undefined,doric.jsObtainContext("${contextId}"),doric.jsObtainEntry("${contextId}"),doric.__require__,{}])`
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function initDoric() {
|
|
|
|
injectGlobalObject('nativeLog', (type: 'd' | 'w' | 'e', message: string) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'd':
|
|
|
|
console.log(message)
|
|
|
|
break
|
|
|
|
case 'w':
|
|
|
|
console.warn(message)
|
|
|
|
break
|
|
|
|
case 'e':
|
|
|
|
console.error(message)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
injectGlobalObject('nativeRequire', (moduleName: string) => {
|
|
|
|
const bundle = acquireJSBundle(moduleName)
|
|
|
|
if (bundle === undefined || bundle.length === 0) {
|
|
|
|
console.log(`Cannot require JS Bundle :${moduleName}`)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
loadJS(packageModuleScript(moduleName, packageModuleScript(name, bundle)))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
2019-12-19 12:45:40 +08:00
|
|
|
injectGlobalObject('nativeBridge', (contextId: string, namespace: string, method: string, callbackId: string, args?: any) => {
|
2019-12-19 11:52:15 +08:00
|
|
|
const pluginClass = acquirePlugin(namespace)
|
2019-12-19 12:45:40 +08:00
|
|
|
const doricContext = getDoricContext(contextId)
|
|
|
|
if (pluginClass === undefined) {
|
|
|
|
console.error(`Cannot find Plugin:${namespace}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (doricContext === undefined) {
|
|
|
|
console.error(`Cannot find Doric Context:${contextId}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let plugin = doricContext.pluginInstances.get(namespace)
|
|
|
|
if (plugin === undefined) {
|
|
|
|
plugin = new pluginClass(doricContext) as DoricPlugin
|
|
|
|
doricContext.pluginInstances.set(namespace, plugin)
|
|
|
|
}
|
|
|
|
if (!Reflect.has(plugin, method)) {
|
|
|
|
console.error(`Cannot find Method:${method} in plugin ${namespace}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const pluginMethod = Reflect.get(plugin, method, plugin)
|
|
|
|
if (typeof pluginMethod !== 'function') {
|
|
|
|
console.error(`Plugin ${namespace}'s property ${method}'s type is ${typeof pluginMethod} not function,`)
|
|
|
|
}
|
|
|
|
const ret = Reflect.apply(pluginMethod, plugin, [args])
|
|
|
|
if (ret instanceof Promise) {
|
|
|
|
ret.then(
|
|
|
|
e => {
|
|
|
|
jsCallResolve(contextId, callbackId, e)
|
|
|
|
},
|
|
|
|
e => {
|
|
|
|
jsCallReject(contextId, callbackId, e)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
jsCallResolve(contextId, callbackId, ret)
|
|
|
|
}
|
2019-12-19 11:52:15 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
initDoric()
|