@@ -54,57 +54,69 @@ function clear (obj) {
54
54
* @param {string } appCode
55
55
* @param {object } config
56
56
* @param {object } data
57
+ * @param {object } env { info, config, services }
57
58
*/
58
59
export function createInstance (
59
- instanceId , appCode = '' , config = { } /* {bundleUrl, debug} */ , data ) {
60
+ instanceId ,
61
+ appCode = '' ,
62
+ config = { } ,
63
+ data ,
64
+ env = { }
65
+ ) {
60
66
// Set active instance id and put some information into `instances` map.
61
67
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
+
62
81
instances [ instanceId ] = {
63
82
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
73
84
}
74
85
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
88
99
// all top-level public APIs.
89
100
const subVue = Vue . extend ( { } )
101
+
90
102
// ensure plain-object components are extended from the subVue
91
103
subVue . options . _base = subVue
104
+
92
105
// expose global utility
93
106
; [ 'util' , 'set' , 'delete' , 'nextTick' ] . forEach ( name => {
94
107
subVue [ name ] = Vue [ name ]
95
108
} )
96
109
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 )
100
118
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.
108
120
renderer . sendTasks ( instanceId + '' , [ { module : 'dom' , method : 'createFinish' , args : [ ] } ] , - 1 )
109
121
}
110
122
@@ -266,6 +278,7 @@ Vue.mixin({
266
278
} )
267
279
268
280
/**
281
+ * @deprecated Just instance variable `weex.config`
269
282
* Get instance config.
270
283
* @return {object }
271
284
*/
@@ -340,6 +353,25 @@ function getInstanceTimer (instanceId, moduleGetter) {
340
353
return timerAPIs
341
354
}
342
355
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
+
343
375
/**
344
376
* Convert all type of values into "safe" format to send to native.
345
377
* 1. A `function` will be converted into callback id.
0 commit comments