Merge pull request #2055 from 2youyou2/jit

fallback function if not support Function
This commit is contained in:
Jare Guo
2017-10-23 02:17:45 -05:00
committed by GitHub
10 changed files with 350 additions and 93 deletions
+12 -4
View File
@@ -94,10 +94,18 @@ var Prefab = cc.Class({
},
_instantiate: function () {
// instantiate node
var node = this._doInstantiate();
// initialize node
this.data._instantiate(node);
var node;
if (cc.supportJit) {
// instantiate node
node = this._doInstantiate();
// initialize node
this.data._instantiate(node);
}
else {
// instantiate node
node = this.data._instantiate();
}
// link prefab in editor
if (CC_EDITOR || CC_TEST) {
// This operation is not necessary, but some old prefab asset may not contain complete data.
+16 -4
View File
@@ -38,6 +38,18 @@ var callUpdateInTryCatch = CC_EDITOR && callerFunctor('update', 'dt');
var callLateUpdateInTryCatch = CC_EDITOR && callerFunctor('lateUpdate', 'dt');
var callOnDisableInTryCatch = CC_EDITOR && callerFunctor('onDisable');
var supportJit = cc.supportJit;
var callStart = supportJit ? 'c.start();c._objFlags|=' + IsStartCalled : function (c) {
c.start();
c._objFlags |= IsStartCalled;
};
var callUpdate = supportJit ? 'c.update(dt)' : function (c, dt) {
c.update(dt);
};
var callLateUpdate = supportJit ? 'c.lateUpdate(dt)' : function (c, dt) {
c.lateUpdate(dt);
};
function sortedIndex (array, comp) {
var order = comp.constructor._executionOrder;
var id = comp.__instanceId;
@@ -204,7 +216,7 @@ function enableInEditor (comp) {
}
function createInvokeImpl (funcOrCode, useDt) {
if (CC_EDITOR) {
if (typeof funcOrCode === 'function') {
if (useDt) {
return function (iterator, dt) {
var array = iterator.array;
@@ -253,11 +265,11 @@ function createInvokeImpl (funcOrCode, useDt) {
function ctor () {
// invokers
this.startInvoker = new OneOffInvoker(createInvokeImpl(
CC_EDITOR ? callStartInTryCatch : 'c.start();c._objFlags|=' + IsStartCalled));
CC_EDITOR ? callStartInTryCatch : callStart));
this.updateInvoker = new ReusableInvoker(createInvokeImpl(
CC_EDITOR ? callUpdateInTryCatch : 'c.update(dt)', true));
CC_EDITOR ? callUpdateInTryCatch : callUpdate, true));
this.lateUpdateInvoker = new ReusableInvoker(createInvokeImpl(
CC_EDITOR ? callLateUpdateInTryCatch : 'c.lateUpdate(dt)', true));
CC_EDITOR ? callLateUpdateInTryCatch : callLateUpdate, true));
// components deferred to next frame
this.scheduleInNextFrame = [];
+9 -2
View File
@@ -43,6 +43,13 @@ var callResetInTryCatch = CC_EDITOR && callerFunctor('resetInEditor');
var callOnFocusInTryCatch = CC_EDITOR && callerFunctor('onFocusInEditor');
var callOnLostFocusInTryCatch = CC_EDITOR && callerFunctor('onLostFocusInEditor');
var supportJit = cc.supportJit;
var callPreload = supportJit ? 'c.__preload();' : function (c) { c.__preload(); }
var callOnLoad = supportJit ? ('c.onLoad();c._objFlags|=' + IsOnLoadCalled) : function (c) {
c.onLoad();
c._objFlags |= IsOnLoadCalled;
}
// for __preload: use internally, no sort
var UnsortedInvoker = cc.Class({
extends: CompScheduler.LifeCycleInvoker,
@@ -62,10 +69,10 @@ var UnsortedInvoker = cc.Class({
});
var invokePreload = CompScheduler.createInvokeImpl(
CC_EDITOR ? callPreloadInTryCatch : 'c.__preload();'
CC_EDITOR ? callPreloadInTryCatch : callPreload
);
var invokeOnLoad = CompScheduler.createInvokeImpl(
CC_EDITOR ? callOnLoadInTryCatch : ('c.onLoad();c._objFlags|=' + IsOnLoadCalled)
CC_EDITOR ? callOnLoadInTryCatch : callOnLoad
);
var activateTasksPool = new JS.Pool(MAX_POOL_SIZE);
+138 -36
View File
@@ -396,33 +396,37 @@ function getNewValueTypeCode (value) {
var clsName = JS.getClassName(value);
var type = value.constructor;
var res = 'new ' + clsName + '(';
var i;
if (type === cc.Mat3 || type === cc.Mat4) {
var data = value.data;
for (i = 0; i < data.length; i++) {
res += data[i];
if (i < data.length - 1) {
res += ',';
}
for (var i = 0; i < type.__props__.length; i++) {
var prop = type.__props__[i];
var propVal = value[prop];
if (typeof propVal === 'object') {
cc.errorID(3641, clsName);
return 'new ' + clsName + '()';
}
}
else {
for (i = 0; i < type.__props__.length; i++) {
var prop = type.__props__[i];
var propVal = value[prop];
if (typeof propVal === 'object') {
cc.errorID(3641, clsName);
return 'new ' + clsName + '()';
}
res += propVal;
if (i < type.__props__.length - 1) {
res += ',';
}
res += propVal;
if (i < type.__props__.length - 1) {
res += ',';
}
}
return res + ')';
}
function getNewValueType (value) {
var clsName = JS.getClassName(value);
var type = value.constructor;
var res = new type();
for (var i = 0; i < type.__props__.length; i++) {
var prop = type.__props__[i];
var propVal = value[prop];
if (typeof propVal === 'object') {
cc.errorID(3641, clsName);
return res;
}
res[prop] = propVal;
}
return res;
}
// TODO - move escapeForJS, IDENTIFIER_RE, getNewValueTypeCode to misc.js or a new source file
// convert a normal string including newlines, quotes and unicode characters into a string literal
@@ -434,17 +438,7 @@ function escapeForJS (s) {
replace(/\u2029/g, '\\u2029');
}
// simple test variable name
var IDENTIFIER_RE = /^[$A-Za-z_][0-9A-Za-z_$]*$/;
function compileProps (actualClass) {
// init deferred properties
var attrs = Attr.getClassAttrs(actualClass);
var propList = actualClass.__props__;
if (propList === null) {
deferredInitializer.init();
propList = actualClass.__props__;
}
function getInitPropsJit (attrs, propList) {
// functions for generated code
var F = [];
var func = '';
@@ -498,7 +492,6 @@ function compileProps (actualClass) {
// console.log(func);
// }
// Overwite __initProps__ to avoid compile again.
var initProps;
if (F.length === 0) {
initProps = Function(func);
@@ -506,14 +499,81 @@ function compileProps (actualClass) {
else {
initProps = Function('F', 'return (function(){\n' + func + '})')(F);
}
return initProps;
}
function getInitProps (attrs, propList) {
// functions for generated code
function func () {
var F = [];
for (var i = 0; i < propList.length; i++) {
var prop = propList[i];
var attrKey = prop + DELIMETER + 'default';
if (attrKey in attrs) { // getter does not have default
var expression;
var def = attrs[attrKey];
if (typeof def === 'object' && def) {
if (def instanceof cc.ValueType) {
expression = getNewValueType(def);
}
else if (Array.isArray(def)) {
expression = [];
}
else {
expression = {};
}
}
else if (typeof def === 'function') {
var index = F.length;
F.push(def);
expression = F[index]();
if (CC_EDITOR) {
try {
this[prop] = expression;
}
catch(err) {
cc._throw(e);
}
continue;
}
}
else {
// number, boolean, null, undefined, string
expression = def;
}
this[prop] = expression;
}
}
}
return func;
}
// simple test variable name
var IDENTIFIER_RE = /^[$A-Za-z_][0-9A-Za-z_$]*$/;
function compileProps (actualClass) {
// init deferred properties
var attrs = Attr.getClassAttrs(actualClass);
var propList = actualClass.__props__;
if (propList === null) {
deferredInitializer.init();
propList = actualClass.__props__;
}
// Overwite __initProps__ to avoid compile again.
var initProps = cc.supportJit ? getInitPropsJit(attrs, propList) : getInitProps(attrs, propList);
actualClass.prototype.__initProps__ = initProps;
// call instantiateProps immediately, no need to pass actualClass into it anymore
// (use call to manually bind `this` because `this` may not instanceof actualClass)
initProps.call(this);
}
function _createCtor (ctors, baseClass, className, options) {
// bound super calls
var _createCtor = cc.supportJit ? function (ctors, baseClass, className, options) {
var superCallBounded = baseClass && boundSuperCalls(baseClass, options, className);
var ctorName = CC_DEV ? normalizeClassName_DEV(className) : 'CCClass';
@@ -552,7 +612,49 @@ function _createCtor (ctors, baseClass, className, options) {
body += '}';
return Function(body)();
}
} : function (ctors, baseClass, className, options) {
var superCallBounded = baseClass && boundSuperCalls(baseClass, options, className);
return function CCClass () {
if (superCallBounded) {
this._super=null;
}
this.__initProps__(CCClass);
// call user constructors
var ctorLen = ctors.length;
var cs = CCClass.__ctors__;
if (ctorLen > 0) {
var useTryCatch = ! (className && className.startsWith('cc.'));
if (useTryCatch) {
try {
if (ctorLen === 1) {
cs[0].apply(this, arguments);
}
else {
for (var i = 0; i < ctorLen; i++) {
cs[i].apply(this, arguments);;
}
}
}
catch(e) {
cc._throw(e);
}
}
else {
if (ctorLen === 1) {
cs[0].apply(this, arguments);
}
else {
for (var i = 0; i < ctorLen; i++) {
cs[i].apply(this, arguments);;
}
}
}
}
};
};
function _validateCtor_DEV (ctor, baseClass, className, options) {
if (CC_EDITOR && baseClass) {
+30 -17
View File
@@ -339,25 +339,38 @@ function compileDestruct (obj, ctor) {
}
// compile code
var skipId = obj instanceof cc._BaseNode || obj instanceof cc.Component;
var func = '';
for (key in propsToReset) {
if (skipId && key === '_id') {
continue;
if (cc.supportJit) {
var func = '';
for (key in propsToReset) {
if (skipId && key === '_id') {
continue;
}
var statement;
if (CCClass.IDENTIFIER_RE.test(key)) {
statement = 'o.' + key + '=';
}
else {
statement = 'o[' + CCClass.escapeForJS(key) + ']=';
}
var val = propsToReset[key];
if (val === '') {
val = '""';
}
func += (statement + val + ';\n');
}
var statement;
if (CCClass.IDENTIFIER_RE.test(key)) {
statement = 'o.' + key + '=';
}
else {
statement = 'o[' + CCClass.escapeForJS(key) + ']=';
}
var val = propsToReset[key];
if (val === '') {
val = '""';
}
func += (statement + val + ';\n');
return Function('o', func);
}
else {
return function (o) {
for (key in propsToReset) {
if (skipId && key === '_id') {
continue;
}
o[key] = propsToReset[key];
}
};
}
return Function('o', func);
}
/**
+1 -1
View File
@@ -69,7 +69,7 @@ Class.extend = function (props) {
// The dummy Class constructor
var TheClass;
if (cc.game && cc.game.config && cc.game.config[cc.game.CONFIG_KEY.exposeClassName]) {
if (cc.supportJit && cc.game && cc.game.config && cc.game.config[cc.game.CONFIG_KEY.exposeClassName]) {
var ctor =
"return (function " + (props._className || "Class") + "(arg0,arg1,arg2,arg3,arg4) {\n" +
"this.__instanceId = cc.ClassManager.getNewInstanceId();\n" +
+1 -1
View File
@@ -30,7 +30,7 @@ const DELIMETER = '$_$';
function createAttrsSingle (owner, ownerCtor, superAttrs) {
var AttrsCtor;
if (CC_DEV) {
if (CC_DEV && cc.supportJit) {
var ctorName = ownerCtor.name;
if (owner === ownerCtor) {
ctorName += '_ATTRS';
+109 -4
View File
@@ -496,7 +496,7 @@ var _Deserializer = (function () {
// }
// }
function compileObjectType (sources, defaultValue, accessorToSet, propNameLiteralToSet, assumeHavePropIfIsValue) {
var compileObjectType = cc.supportJit ? function (sources, defaultValue, accessorToSet, propNameLiteralToSet, assumeHavePropIfIsValue) {
if (defaultValue instanceof cc.ValueType) {
// fast case
if (!assumeHavePropIfIsValue) {
@@ -518,9 +518,38 @@ var _Deserializer = (function () {
}
sources.push('}else o' + accessorToSet + '=null;');
}
}
} : function (s, o, t, prop, defaultValue, propName, assumeHavePropIfIsValue) {
if (defaultValue instanceof cc.ValueType) {
var ctor = defaultValue.constructor;
if (assumeHavePropIfIsValue) {
s._deserializeTypedObject(o[propName], prop, ctor);
}
else {
if (prop) {
s._deserializeTypedObject(o[propName], prop, ctor);
}
else {
o[propName] = null;
}
}
}
else {
if (prop) {
if (CC_EDITOR || CC_TEST) {
s._deserializeObjField(o, prop, propName, t&&o);
}
else {
s._deserializeObjField(o, prop, propName);
}
}
else {
o[propName] = null;
}
}
};
function compileDeserialize (self, klass) {
var compileDeserialize = cc.supportJit ? function (self, klass) {
var RAW_TYPE = Attr.DELIMETER + 'rawType';
var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly';
var SERIALIZABLE = Attr.DELIMETER + 'serializable';
@@ -619,7 +648,83 @@ var _Deserializer = (function () {
sources.push('s._deserializePrimitiveObject(o._$erialized,d);');
}
return Function('s', 'o', 'd', 'k', 't', sources.join(''));
}
} : function (self, klass) {
var RAW_TYPE = Attr.DELIMETER + 'rawType';
var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly';
var SERIALIZABLE = Attr.DELIMETER + 'serializable';
var DEFAULT = Attr.DELIMETER + 'default';
var SAVE_URL_AS_ASSET = Attr.DELIMETER + 'saveUrlAsAsset';
var FORMERLY_SERIALIZED_AS = Attr.DELIMETER + 'formerlySerializedAs';
var attrs = Attr.getClassAttrs(klass);
var props = klass.__props__;
var fastMode = Misc.BUILTIN_CLASSID_RE.test(JS._getClassId(klass));
return function (s, o, d, k, t) {
var prop;
for (var p = 0; p < props.length; p++) {
var propName = props[p];
var rawType = attrs[propName + RAW_TYPE];
if (!rawType) {
if ((CC_PREVIEW || (CC_EDITOR && self._ignoreEditorOnly)) && attrs[propName + EDITOR_ONLY]) {
var mayUsedInPersistRoot = (propName === '_id' && cc.isChildClassOf(klass, cc.Node));
if (!mayUsedInPersistRoot) {
continue; // skip editor only if in preview
}
}
if (attrs[propName + SERIALIZABLE] === false) {
continue; // skip nonSerialized
}
var propNameToRead = propName;
if (attrs[propName + FORMERLY_SERIALIZED_AS]) {
propNameToRead = attrs[propName + FORMERLY_SERIALIZED_AS];
}
prop = d[propNameToRead];
if (typeof prop !== 'undefined') {
// function undefined object(null) string boolean number
var defaultValue = CCClass.getDefault(attrs[propName + DEFAULT]);
if (fastMode) {
var defaultType = typeof defaultValue;
var isPrimitiveType = (defaultType === 'string' && !attrs[propName + SAVE_URL_AS_ASSET]) ||
defaultType === 'number' ||
defaultType === 'boolean';
if (isPrimitiveType) {
o[propName] = prop;
}
else {
compileObjectType(s, o, t, prop, defaultValue, propName, true);
}
}
else {
if (typeof prop !== 'object') {
o[propName] = prop;
}
else {
compileObjectType(s, o, t, prop, defaultValue, propName, false);
}
}
}
}
else {
// always load raw objects even if property not serialized
// 这里假定每个asset都有uuid,每个json只能包含一个asset,只能包含一个rawProp
if (s.result.rawProp) {
cc.error("not support multi raw object in a file");
}
s.result.rawProp = propName;
}
}
if (props[props.length - 1] === '_$erialized') {
// deep copy original serialized data
o._$erialized=JSON.parse(JSON.stringify(d));
// parse the serialized data as primitive javascript object, so its __id__ will be dereferenced
s._deserializePrimitiveObject(o._$erialized,d);
}
}
};
function unlinkUnusedPrefab (self, serialized, obj) {
var uuid = serialized['asset'] && serialized['asset'].__uuid__;
+18 -12
View File
@@ -106,25 +106,28 @@
/**
* @property {Boolean} CC_TEST - Running in the engine's unit test.
*/
// window may be undefined when first load engine from editor
var _global = typeof window === 'undefined' ? global : window;
function defineMacro (name, defaultValue) {
// if "global_defs" not preprocessed by uglify, just declare them globally,
// this may happened in release version's preview page.
// (use evaled code to prevent mangle by uglify)
return 'if(typeof ' + name + '=="undefined")' +
name + '=' + defaultValue + ';';
if (typeof _global[name] == 'undefined') {
_global[name] = defaultValue;
}
}
function defined (name) {
return 'typeof ' + name + '=="object"';
return typeof _global[name] == 'object';
}
Function(
defineMacro('CC_TEST', defined('tap') + '||' + defined('QUnit')) +
defineMacro('CC_EDITOR', defined('Editor') + '&&' + defined('process') + '&&"electron" in process.versions') +
defineMacro('CC_PREVIEW', '!CC_EDITOR') +
defineMacro('CC_DEV', true) + // (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST
defineMacro('CC_DEBUG', true) + // CC_DEV || Debug Build
defineMacro('CC_JSB', defined('jsb')) +
defineMacro('CC_BUILD', false)
)();
defineMacro('CC_TEST', defined('tap') || defined('QUnit'));
defineMacro('CC_EDITOR', defined('Editor') && defined('process') && ('electron' in process.versions));
defineMacro('CC_PREVIEW', !CC_EDITOR);
defineMacro('CC_DEV', true); // (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST
defineMacro('CC_DEBUG', true); // CC_DEV || Debug Build
defineMacro('CC_JSB', defined('jsb'));
defineMacro('CC_BUILD', false);
// PREDEFINE
@@ -138,6 +141,9 @@ Function(
*/
cc = {};
// check whether support jit
cc.supportJit = typeof Function('') === 'function';
// The namespace for original nodes rendering in scene graph.
_ccsg = {};
+16 -12
View File
@@ -25,25 +25,29 @@
'use strict';
// check whether support jit
cc.supportJit = typeof Function('') === 'function';
function defineMacro (name, defaultValue) {
// if "global_defs" not preprocessed by uglify, just declare them globally,
// this may happened in release version's preview page.
// (use evaled code to prevent mangle by uglify)
return 'if(typeof ' + name + '=="undefined")' +
name + '=' + defaultValue + ';';
if (typeof window[name] == 'undefined') {
window[name] = defaultValue;
}
}
function defined (name) {
return 'typeof ' + name + '=="object"';
return typeof window[name] == 'object';
}
Function(
defineMacro('CC_TEST', defined('tap') + '||' + defined('QUnit')) +
defineMacro('CC_EDITOR', defined('Editor') + '&&' + defined('process') + '&&"electron" in process.versions') +
defineMacro('CC_PREVIEW', '!CC_EDITOR') +
defineMacro('CC_DEV', true) + // (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST
defineMacro('CC_DEBUG', true) + // CC_DEV || Debug Build
defineMacro('CC_JSB', defined('jsb')) +
defineMacro('CC_BUILD', false)
)();
defineMacro('CC_TEST', defined('tap') || defined('QUnit'));
defineMacro('CC_EDITOR', defined('Editor') && defined('process') && ('electron' in process.versions));
defineMacro('CC_PREVIEW', !CC_EDITOR);
defineMacro('CC_DEV', true); // (CC_EDITOR && !CC_BUILD) || CC_PREVIEW || CC_TEST
defineMacro('CC_DEBUG', true); // CC_DEV || Debug Build
defineMacro('CC_JSB', defined('jsb'));
defineMacro('CC_BUILD', false);
if (!cc.ClassManager) {
cc.ClassManager = window.ClassManager;