Skip to content

[weex] Sync recent changes of Weex #6028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion packages/weex-vue-framework/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function init (cfg) {
renderer.Element = cfg.Element;
renderer.Comment = cfg.Comment;
renderer.sendTasks = cfg.sendTasks;
renderer.compileBundle = cfg.compileBundle;
}

/**
Expand Down Expand Up @@ -119,7 +120,12 @@ function createInstance (
// deprecated
__weex_require_module__: weexInstanceVar.requireModule // eslint-disable-line
}, timerAPIs);
callFunction(instanceVars, appCode);

if (!callFunctionNative(instanceVars, appCode)) {
// If failed to compile functionBody on native side,
// fallback to 'callFunction()'.
callFunction(instanceVars, appCode);
}

// Send `createFinish` signal to native.
renderer.sendTasks(instanceId + '', [{ module: 'dom', method: 'createFinish', args: [] }], -1);
Expand Down Expand Up @@ -404,6 +410,58 @@ function callFunction (globalObjects, body) {
return result.apply(void 0, globalValues)
}

/**
* Call a new function generated on the V8 native side.
*
* This function helps speed up bundle compiling. Normally, the V8
* engine needs to download, parse, and compile a bundle on every
* visit. If 'compileBundle()' is available on native side,
* the downloding, parsing, and compiling steps would be skipped.
* @param {object} globalObjects
* @param {string} body
* @return {boolean}
*/
function callFunctionNative (globalObjects, body) {
if (typeof renderer.compileBundle !== 'function') {
return false
}

var fn = void 0;
var isNativeCompileOk = false;
var script = '(function (';
var globalKeys = [];
var globalValues = [];
for (var key in globalObjects) {
globalKeys.push(key);
globalValues.push(globalObjects[key]);
}
for (var i = 0; i < globalKeys.length - 1; ++i) {
script += globalKeys[i];
script += ',';
}
script += globalKeys[globalKeys.length - 1];
script += ') {';
script += body;
script += '} )';

try {
var weex = globalObjects.weex || {};
var config = weex.config || {};
fn = renderer.compileBundle(script,
config.bundleUrl,
config.bundleDigest,
config.codeCachePath);
if (fn && typeof fn === 'function') {
fn.apply(void 0, globalValues);
isNativeCompileOk = true;
}
} catch (e) {
console.error(e);
}

return isNativeCompileOk
}

/**
* Convert all type of values into "safe" format to send to native.
* 1. A `function` will be converted into callback id.
Expand Down
60 changes: 59 additions & 1 deletion src/platforms/weex/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function init (cfg) {
renderer.Element = cfg.Element
renderer.Comment = cfg.Comment
renderer.sendTasks = cfg.sendTasks
renderer.compileBundle = cfg.compileBundle
}

/**
Expand Down Expand Up @@ -103,7 +104,12 @@ export function createInstance (
// deprecated
__weex_require_module__: weexInstanceVar.requireModule // eslint-disable-line
}, timerAPIs)
callFunction(instanceVars, appCode)

if (!callFunctionNative(instanceVars, appCode)) {
// If failed to compile functionBody on native side,
// fallback to 'callFunction()'.
callFunction(instanceVars, appCode)
}

// Send `createFinish` signal to native.
renderer.sendTasks(instanceId + '', [{ module: 'dom', method: 'createFinish', args: [] }], -1)
Expand Down Expand Up @@ -367,6 +373,58 @@ function callFunction (globalObjects, body) {
return result(...globalValues)
}

/**
* Call a new function generated on the V8 native side.
*
* This function helps speed up bundle compiling. Normally, the V8
* engine needs to download, parse, and compile a bundle on every
* visit. If 'compileBundle()' is available on native side,
* the downloding, parsing, and compiling steps would be skipped.
* @param {object} globalObjects
* @param {string} body
* @return {boolean}
*/
function callFunctionNative (globalObjects, body) {
if (typeof renderer.compileBundle !== 'function') {
return false
}

let fn = void 0
let isNativeCompileOk = false
let script = '(function ('
const globalKeys = []
const globalValues = []
for (const key in globalObjects) {
globalKeys.push(key)
globalValues.push(globalObjects[key])
}
for (let i = 0; i < globalKeys.length - 1; ++i) {
script += globalKeys[i]
script += ','
}
script += globalKeys[globalKeys.length - 1]
script += ') {'
script += body
script += '} )'

try {
const weex = globalObjects.weex || {}
const config = weex.config || {}
fn = renderer.compileBundle(script,
config.bundleUrl,
config.bundleDigest,
config.codeCachePath)
if (fn && typeof fn === 'function') {
fn(...globalValues)
isNativeCompileOk = true
}
} catch (e) {
console.error(e)
}

return isNativeCompileOk
}

/**
* Convert all type of values into "safe" format to send to native.
* 1. A `function` will be converted into callback id.
Expand Down