You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ha...@apache.org on 2017/12/29 11:13:56 UTC

[4/4] incubator-weex git commit: [jsfm] Enhance the multi-instance isolation (sandbox) (resolve #960)

[jsfm] Enhance the multi-instance isolation (sandbox) (resolve #960)

Squashed commit of the following:

commit fa7719f710de1899c85df16a4d57f59518fddac9
Merge: 33ff3f027 11a1f3eaf
Author: Hanks <zh...@gmail.com>
Date:   Fri Dec 29 15:52:39 2017 +0800

    Merge branch 'master' into jsfm-feature-sandbox

commit 33ff3f027b30117dac4feb6569eb0ca1ecbb6494
Merge: 3d589d488 11a1f3eaf
Author: Hanks <zh...@gmail.com>
Date:   Fri Dec 29 15:51:51 2017 +0800

    Merge branch 'master' into jsfm-feature-sandbox

commit 3d589d488794bf4cf52ddbdb4f80e8a2032837d6
Author: Hanks <zh...@gmail.com>
Date:   Fri Dec 29 15:38:47 2017 +0800

    * [jsfm] release v0.23.6

commit f8f8a1506dddd1e11710fe225b3376e5c584748f
Author: Hanks <zh...@gmail.com>
Date:   Thu Dec 28 12:18:12 2017 +0800

    * [jsfm] enhance the multi-instance isolation (sandbox)

    Use "createInstanceContext" instead of "createInstance".
    JS Framework only needs to prepare the instance context and no longer
    execute the js code. The js code will be executed by native.
    This *sandbox* feature requires native to cooperate to achieve.

commit 7c40162445403df8f4f25e35dfa377cba24da217
Author: Hanks <zh...@gmail.com>
Date:   Thu Dec 28 12:15:29 2017 +0800

    * [jsfm] upgrade vue and rax

    upgrade weex-vue-framework to 2.5.11-weex.1
    upgrade weex-rax-framework to 0.4.20


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/dc428443
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/dc428443
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/dc428443

Branch: refs/heads/master
Commit: dc42844309744773266de89094a988f8e0e3e2d2
Parents: 11a1f3e
Author: Hanks <zh...@gmail.com>
Authored: Fri Dec 29 19:12:08 2017 +0800
Committer: Hanks <zh...@gmail.com>
Committed: Fri Dec 29 19:12:08 2017 +0800

----------------------------------------------------------------------
 html5/runtime/api/init.js                   |  119 +-
 html5/runtime/bridge/TaskCenter.js          |    7 +-
 html5/test/unit/runtime/legacy-framework.js |    5 -
 html5/test/unit/runtime/runner/helper.js    |    4 +-
 package.json                                |    6 +-
 packages/weex-js-framework/index.js         | 4988 +++++++++++++---------
 packages/weex-js-framework/index.min.js     |    2 +-
 pre-build/native-bundle-main.js             |    9 +-
 8 files changed, 2998 insertions(+), 2142 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dc428443/html5/runtime/api/init.js
----------------------------------------------------------------------
diff --git a/html5/runtime/api/init.js b/html5/runtime/api/init.js
index 6a95b70..187504a 100644
--- a/html5/runtime/api/init.js
+++ b/html5/runtime/api/init.js
@@ -24,6 +24,7 @@ import { registerComponents } from './component'
 import { services, register, unregister } from './service'
 import { track } from '../bridge/debug'
 import WeexInstance from './WeexInstance'
+import { getDoc } from '../vdom/operation'
 
 let frameworks
 let runtimeConfig
@@ -71,12 +72,42 @@ function createServices (id, env, config) {
   return serviceMap
 }
 
-const instanceMap = {}
-
+const instanceTypeMap = {}
 function getFrameworkType (id) {
-  if (instanceMap[id]) {
-    return instanceMap[id].framework
+  return instanceTypeMap[id]
+}
+
+function createInstanceContext (id, options = {}, data) {
+  const weex = new WeexInstance(id, options)
+  Object.freeze(weex)
+
+  const bundleType = options.bundleType || 'Vue'
+  instanceTypeMap[id] = bundleType
+  const framework = runtimeConfig.frameworks[bundleType]
+  if (!framework) {
+    return new Error(`[JS Framework] Invalid bundle type "${bundleType}".`)
   }
+  track(id, 'bundleType', bundleType)
+
+  // prepare js service
+  const services = createServices(id, { weex, bundleType }, runtimeConfig)
+  Object.freeze(services)
+
+  // prepare runtime context
+  const runtimeContext = Object.create(null)
+  Object.assign(runtimeContext, services, {
+    weex,
+    services // Temporary compatible with some legacy APIs in Rax
+  })
+  Object.freeze(runtimeContext)
+
+  // prepare instance context
+  const instanceContext = Object.create(runtimeContext)
+  if (typeof framework.createInstanceContext === 'function') {
+    Object.assign(instanceContext, framework.createInstanceContext(id, runtimeContext, data))
+  }
+  Object.freeze(instanceContext)
+  return instanceContext
 }
 
 /**
@@ -88,35 +119,22 @@ function getFrameworkType (id) {
  * @param {object} data
  */
 function createInstance (id, code, config, data) {
-  if (instanceMap[id]) {
-    return new Error(`invalid instance id "${id}"`)
+  if (instanceTypeMap[id]) {
+    return new Error(`The instance id "${id}" has already been used!`)
   }
 
   // Init instance info.
   const bundleType = getBundleType(code)
+  instanceTypeMap[id] = bundleType
 
   // Init instance config.
   config = JSON.parse(JSON.stringify(config || {}))
   config.env = JSON.parse(JSON.stringify(global.WXEnvironment || {}))
-
-  const weex = new WeexInstance(id, config)
-  Object.freeze(weex)
-
-  const runtimeEnv = {
-    weex,
-    config, // TODO: deprecated
-    created: Date.now(),
-    framework: bundleType
-  }
-  runtimeEnv.services = createServices(id, runtimeEnv, runtimeConfig)
-  instanceMap[id] = runtimeEnv
-
-  const runtimeContext = Object.create(null)
-  Object.assign(runtimeContext, runtimeEnv.services, { weex })
+  config.bundleType = bundleType
 
   const framework = runtimeConfig.frameworks[bundleType]
   if (!framework) {
-    return new Error(`invalid bundle type "${bundleType}".`)
+    return new Error(`[JS Framework] Invalid bundle type "${bundleType}".`)
   }
   if (bundleType === 'Weex') {
     console.error(`[JS Framework] COMPATIBILITY WARNING: `
@@ -126,14 +144,22 @@ function createInstance (id, code, config, data) {
       + `Please upgrade it to Vue.js or Rax.`)
   }
 
-  track(id, 'bundleType', bundleType)
-
-  // run create instance
-  if (typeof framework.prepareInstanceContext === 'function') {
-    const instanceContext = framework.prepareInstanceContext(runtimeContext)
-    return runInContext(code, instanceContext)
+  const instanceContext = createInstanceContext(id, config, data)
+  if (typeof framework.createInstance === 'function') {
+    // Temporary compatible with some legacy APIs in Rax,
+    // some Rax page is using the legacy ".we" framework.
+    if (bundleType === 'Rax' || bundleType === 'Weex') {
+      const raxInstanceContext = Object.assign({
+        config,
+        created: Date.now(),
+        framework: bundleType
+      }, instanceContext)
+      return framework.createInstance(id, code, config, data, raxInstanceContext)
+    }
+    return framework.createInstance(id, code, config, data, instanceContext)
   }
-  return framework.createInstance(id, code, config, data, runtimeEnv)
+  // console.error(`[JS Framework] Can't find available "createInstance" method in ${bundleType}!`)
+  runInContext(code, instanceContext)
 }
 
 /**
@@ -151,7 +177,6 @@ function runInContext (code, context) {
 
   const bundle = `
     (function (global) {
-      "use strict";
       ${code}
     })(Object.create(this))
   `
@@ -159,8 +184,28 @@ function runInContext (code, context) {
   return (new Function(...keys, bundle))(...args)
 }
 
+/**
+ * Get the JSON object of the root element.
+ * @param {string} instanceId
+ */
+function getRoot (instanceId) {
+  const document = getDoc(instanceId)
+  try {
+    if (document && document.body) {
+      return document.body.toJSON()
+    }
+  }
+  catch (e) {
+    console.error(`[JS Framework] Failed to get the virtual dom tree.`)
+    return
+  }
+}
+
 const methods = {
   createInstance,
+  createInstanceContext,
+  getRoot,
+  getDocument: getDoc,
   registerService: register,
   unregisterService: unregister,
   callJS (id, tasks) {
@@ -200,12 +245,13 @@ function genInstance (methodName) {
             destroy(id, { info, runtime: runtimeConfig })
           }
         })
-        delete instanceMap[id]
+        delete instanceTypeMap[id]
       }
 
       return result
     }
-    return new Error(`invalid instance id "${id}"`)
+    return new Error(`[JS Framework] Using invalid instance id `
+      + `"${id}" when calling ${methodName}.`)
   }
 }
 
@@ -240,14 +286,19 @@ export default function init (config) {
   // `sendTasks(...args)`.
   for (const name in frameworks) {
     const framework = frameworks[name]
-    framework.init(config)
+    if (typeof framework.init === 'function') {
+      try {
+        framework.init(config)
+      }
+      catch (e) {}
+    }
   }
 
   adaptMethod('registerComponents', registerComponents)
   adaptMethod('registerModules', registerModules)
   adaptMethod('registerMethods')
 
-  ; ['destroyInstance', 'refreshInstance', 'getRoot'].forEach(genInstance)
+  ; ['destroyInstance', 'refreshInstance'].forEach(genInstance)
 
   return methods
 }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dc428443/html5/runtime/bridge/TaskCenter.js
----------------------------------------------------------------------
diff --git a/html5/runtime/bridge/TaskCenter.js b/html5/runtime/bridge/TaskCenter.js
index 3a43203..defee6f 100644
--- a/html5/runtime/bridge/TaskCenter.js
+++ b/html5/runtime/bridge/TaskCenter.js
@@ -50,8 +50,11 @@ export class TaskCenter {
     return this.callbackManager.triggerHook(...args)
   }
 
-  updateData (componentId, newData) {
-    this.callModule('dom', 'updateComponentData', [newData])
+  updateData (componentId, newData, callback) {
+    this.send('module', {
+      module: 'dom',
+      method: 'updateComponentData'
+    }, [componentId, newData, callback])
   }
 
   destroyCallback () {

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dc428443/html5/test/unit/runtime/legacy-framework.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/runtime/legacy-framework.js b/html5/test/unit/runtime/legacy-framework.js
index b25d14d..a82d23b 100644
--- a/html5/test/unit/runtime/legacy-framework.js
+++ b/html5/test/unit/runtime/legacy-framework.js
@@ -188,11 +188,6 @@ describe('framework entry', () => {
       }
       expect(json).to.be.deep.equal(expectJSON)
     })
-
-    it('with a non-exist instanceId', () => {
-      const result = framework.getRoot('123')
-      expect(result).to.be.an.instanceof(Error)
-    })
   })
 
   describe('callJS', () => {

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dc428443/html5/test/unit/runtime/runner/helper.js
----------------------------------------------------------------------
diff --git a/html5/test/unit/runtime/runner/helper.js b/html5/test/unit/runtime/runner/helper.js
index 7fdcb80..dce81c7 100644
--- a/html5/test/unit/runtime/runner/helper.js
+++ b/html5/test/unit/runtime/runner/helper.js
@@ -62,12 +62,12 @@ export function execute (type, code) {
   const context = createContext()
   return new Promise(resolve => {
     const id = String(Date.now() + Math.random())
-    const instance = context.createInstance(id, `
+    context.createInstance(id, `
       // { "framework": "${type}" }
       ${code}
     `)
     setTimeout(() => {
-      resolve(instance.document.body.toJSON())
+      resolve(context.getRoot(id))
     }, 10)
   })
 }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/dc428443/package.json
----------------------------------------------------------------------
diff --git a/package.json b/package.json
index 0e86dfa..0282224 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
   "name": "weex",
   "version": "0.12.2",
   "subversion": {
-    "framework": "0.22.4",
+    "framework": "0.23.6",
     "transformer": ">=0.1.5 <0.5"
   },
   "description": "A framework for building Mobile cross-platform UI",
@@ -81,8 +81,8 @@
     "query-string": "^4.2.3",
     "semver": "^5.1.0",
     "weex-picker": "^0.1.0",
-    "weex-rax-framework": "0.4.14",
-    "weex-vue-framework": "2.4.2-weex.6"
+    "weex-rax-framework": "0.4.20",
+    "weex-vue-framework": "2.5.11-weex.1"
   },
   "devDependencies": {
     "@weex-project/weex-picker": "^0.2.4",