@@ -54,57 +54,69 @@ function clear (obj) {
5454 * @param {string } appCode
5555 * @param {object } config
5656 * @param {object } data
57+ * @param {object } env { info, config, services }
5758 */
5859export function createInstance (
59- instanceId , appCode = '' , config = { } /* {bundleUrl, debug} */ , data ) {
60+ instanceId ,
61+ appCode = '' ,
62+ config = { } ,
63+ data ,
64+ env = { }
65+ ) {
6066 // Set active instance id and put some information into `instances` map.
6167 activeId = instanceId
68+
69+ // Virtual-DOM object.
70+ const document = new renderer . Document ( instanceId , config . bundleUrl )
71+
72+ // All function/callback of parameters before sent to native
73+ // will be converted as an id. So `callbacks` is used to store
74+ // these real functions. When a callback invoked and won't be
75+ // called again, it should be removed from here automatically.
76+ const callbacks = [ ]
77+
78+ // The latest callback id, incremental.
79+ const callbackId = 1
80+
6281 instances [ instanceId ] = {
6382 instanceId, config, data,
64- // Virtual-DOM object.
65- document : new renderer . Document ( instanceId , config . bundleUrl ) ,
66- // All function/callback of parameters before sent to native
67- // will be converted as an id. So `callbacks` is used to store
68- // these real functions. When a callback invoked and won't be
69- // called again, it should be removed from here automatically.
70- callbacks : [ ] ,
71- // The latest callback id, incremental.
72- callbackId : 1
83+ document, callbacks, callbackId
7384 }
7485
75- // The function which create a closure the JS Bundle will run in .
76- // It will declare some global variables like `Vue`, HTML5 Timer APIs,
77- // and native module getter.
78- const start = new Function (
79- 'Vue' ,
80- '__weex_require_module__' ,
81- 'setTimeout' ,
82- 'setInterval' ,
83- 'clearTimeout' ,
84- 'clearInterval' ,
85- appCode )
86-
87- // Each instance has a independent `Vue` object and it should have
86+ // Prepare native module getter and HTML5 Timer APIs .
87+ const moduleGetter = genModuleGetter ( instanceId )
88+ const timerAPIs = getInstanceTimer ( instanceId , moduleGetter )
89+
90+ // Prepare `weex` instance variable.
91+ const weexInstanceVar = {
92+ config ,
93+ document ,
94+ require : moduleGetter
95+ }
96+ Object . freeze ( weexInstanceVar )
97+
98+ // Each instance has a independent `Vue` variable and it should have
8899 // all top-level public APIs.
89100 const subVue = Vue . extend ( { } )
101+
90102 // ensure plain-object components are extended from the subVue
91103 subVue . options . _base = subVue
104+
92105 // expose global utility
93106 ; [ 'util' , 'set' , 'delete' , 'nextTick' ] . forEach ( name => {
94107 subVue [ name ] = Vue [ name ]
95108 } )
96109
97- // Prepare native module getter and HTML5 Timer APIs.
98- const moduleGetter = genModuleGetter ( instanceId )
99- const timerAPIs = getInstanceTimer ( instanceId , moduleGetter )
110+ // The function which create a closure the JS Bundle will run in.
111+ // It will declare some instance variables like `Vue`, HTML5 Timer APIs etc.
112+ const instanceVars = Object . assign ( {
113+ Vue : subVue ,
114+ weex : weexInstanceVar ,
115+ __weex_require_module__ : weexInstanceVar . require // deprecated
116+ } , timerAPIs )
117+ callFunction ( instanceVars , appCode )
100118
101- // Run the JS Bundle and send `createFinish` signal to native.
102- start (
103- subVue , moduleGetter ,
104- timerAPIs . setTimeout ,
105- timerAPIs . setInterval ,
106- timerAPIs . clearTimeout ,
107- timerAPIs . clearInterval )
119+ // Send `createFinish` signal to native.
108120 renderer . sendTasks ( instanceId + '' , [ { module : 'dom' , method : 'createFinish' , args : [ ] } ] , - 1 )
109121}
110122
@@ -266,6 +278,7 @@ Vue.mixin({
266278} )
267279
268280/**
281+ * @deprecated Just instance variable `weex.config`
269282 * Get instance config.
270283 * @return {object }
271284 */
@@ -340,6 +353,25 @@ function getInstanceTimer (instanceId, moduleGetter) {
340353 return timerAPIs
341354}
342355
356+ /**
357+ * Call a new function body with some global objects.
358+ * @param {object } globalObjects
359+ * @param {string } code
360+ * @return {any }
361+ */
362+ function callFunction ( globalObjects , body ) {
363+ const globalKeys = [ ]
364+ const globalValues = [ ]
365+ for ( const key in globalObjects ) {
366+ globalKeys . push ( key )
367+ globalValues . push ( globalObjects [ key ] )
368+ }
369+ globalKeys . push ( body )
370+
371+ const result = new Function ( ...globalKeys )
372+ return result ( ...globalValues )
373+ }
374+
343375/**
344376 * Convert all type of values into "safe" format to send to native.
345377 * 1. A `function` will be converted into callback id.
0 commit comments