From 62841321a5dd57b0fc4b8b1f24d05d6dccf1f016 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Thu, 19 Oct 2017 17:32:03 +0800 Subject: [PATCH 1/4] fallback function if not support Function --- cocos2d/core/assets/CCPrefab.js | 16 ++- cocos2d/core/component-scheduler.js | 20 +++- cocos2d/core/node-activator.js | 11 +- cocos2d/core/platform/CCClass.js | 173 +++++++++++++++++++++------ cocos2d/core/platform/CCObject.js | 47 +++++--- cocos2d/core/platform/_CCClass.js | 2 +- cocos2d/core/platform/attribute.js | 2 +- cocos2d/core/platform/deserialize.js | 113 ++++++++++++++++- index.js | 27 +++-- jsb/index.js | 28 +++-- 10 files changed, 346 insertions(+), 93 deletions(-) diff --git a/cocos2d/core/assets/CCPrefab.js b/cocos2d/core/assets/CCPrefab.js index 7d8341ff7c..926b3b789e 100644 --- a/cocos2d/core/assets/CCPrefab.js +++ b/cocos2d/core/assets/CCPrefab.js @@ -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. diff --git a/cocos2d/core/component-scheduler.js b/cocos2d/core/component-scheduler.js index 5f0bbfd910..7a02a2b988 100644 --- a/cocos2d/core/component-scheduler.js +++ b/cocos2d/core/component-scheduler.js @@ -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 = []; diff --git a/cocos2d/core/node-activator.js b/cocos2d/core/node-activator.js index 686c40ce80..adf5a7e7c4 100644 --- a/cocos2d/core/node-activator.js +++ b/cocos2d/core/node-activator.js @@ -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); diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index 4118d0cc5b..4d7674d1a7 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -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,48 @@ 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); + + var args = arguments; + var self = this; + function init () { + if (ctorLen === 1) { + CCClass.__ctors__[0].apply(self, args); + } + else { + var cs = CCClass.__ctors__; + for (var i = 0; i < ctorLen; i++) { + cs[i].apply(self, args);; + } + } + } + + // call user constructors + var ctorLen = ctors.length; + if (ctorLen > 0) { + var useTryCatch = ! (className && className.startsWith('cc.')); + if (useTryCatch) { + try { + init(); + } + catch(e) { + cc._throw(e); + } + } + else { + init(); + } + } + }; +}; function _validateCtor_DEV (ctor, baseClass, className, options) { if (CC_EDITOR && baseClass) { diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index 8c7d193d48..f3e1781f3a 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -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); } /** diff --git a/cocos2d/core/platform/_CCClass.js b/cocos2d/core/platform/_CCClass.js index 2b50d3757a..d66de087d1 100644 --- a/cocos2d/core/platform/_CCClass.js +++ b/cocos2d/core/platform/_CCClass.js @@ -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" + diff --git a/cocos2d/core/platform/attribute.js b/cocos2d/core/platform/attribute.js index d4a809aa71..da5ef14f0f 100644 --- a/cocos2d/core/platform/attribute.js +++ b/cocos2d/core/platform/attribute.js @@ -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'; diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 0289609d85..0074c1f36a 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -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__; diff --git a/index.js b/index.js index 1538bc3456..5bca974237 100644 --- a/index.js +++ b/index.js @@ -110,21 +110,21 @@ 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); // PREDEFINE @@ -138,6 +138,9 @@ Function( */ cc = {}; +// check whether support jit +cc.supportJit = Function('') !== undefined; + // The namespace for original nodes rendering in scene graph. _ccsg = {}; diff --git a/jsb/index.js b/jsb/index.js index 3859bbd5a5..ead122e2c2 100644 --- a/jsb/index.js +++ b/jsb/index.js @@ -25,25 +25,29 @@ 'use strict'; +// check whether support jit +cc.supportJit = Function('') !== undefined; + 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; From dc7ae7fc8f4ac3e8caae576613fc04958fe78efd Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Fri, 20 Oct 2017 09:29:45 +0800 Subject: [PATCH 2/4] refine code --- cocos2d/core/node-activator.js | 2 +- cocos2d/core/platform/CCClass.js | 33 ++++++++++++++++---------------- index.js | 2 +- jsb/index.js | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cocos2d/core/node-activator.js b/cocos2d/core/node-activator.js index adf5a7e7c4..23987801f5 100644 --- a/cocos2d/core/node-activator.js +++ b/cocos2d/core/node-activator.js @@ -47,7 +47,7 @@ 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; + c._objFlags |= IsOnLoadCalled; } // for __preload: use internally, no sort diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index 4d7674d1a7..1f7d159a96 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -622,34 +622,35 @@ var _createCtor = cc.supportJit ? function (ctors, baseClass, className, options this.__initProps__(CCClass); - var args = arguments; - var self = this; - function init () { - if (ctorLen === 1) { - CCClass.__ctors__[0].apply(self, args); - } - else { - var cs = CCClass.__ctors__; - for (var i = 0; i < ctorLen; i++) { - cs[i].apply(self, args);; - } - } - } - // call user constructors var ctorLen = ctors.length; + var cs = CCClass.__ctors__; if (ctorLen > 0) { var useTryCatch = ! (className && className.startsWith('cc.')); if (useTryCatch) { try { - init(); + 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 { - init(); + if (ctorLen === 1) { + cs[0].apply(this, arguments); + } + else { + for (var i = 0; i < ctorLen; i++) { + cs[i].apply(this, arguments);; + } + } } } }; diff --git a/index.js b/index.js index 5bca974237..47cb5d2e27 100644 --- a/index.js +++ b/index.js @@ -139,7 +139,7 @@ defineMacro('CC_BUILD', false); cc = {}; // check whether support jit -cc.supportJit = Function('') !== undefined; +cc.supportJit = typeof Function('') === 'function'; // The namespace for original nodes rendering in scene graph. _ccsg = {}; diff --git a/jsb/index.js b/jsb/index.js index ead122e2c2..a1712c471a 100644 --- a/jsb/index.js +++ b/jsb/index.js @@ -26,7 +26,7 @@ 'use strict'; // check whether support jit -cc.supportJit = Function('') !== undefined; +cc.supportJit = typeof Function('') === 'function'; function defineMacro (name, defaultValue) { // if "global_defs" not preprocessed by uglify, just declare them globally, From 93910fbb9a63558ed3235db1445e8f3f03767dd0 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Fri, 20 Oct 2017 10:29:48 +0800 Subject: [PATCH 3/4] fixed window is not defined when load editor from bash --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 47cb5d2e27..815918e434 100644 --- a/index.js +++ b/index.js @@ -106,16 +106,17 @@ /** * @property {Boolean} CC_TEST - Running in the engine's unit test. */ +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) - if (typeof window[name] == 'undefined') { - window[name] = defaultValue; + if (typeof _global[name] == 'undefined') { + _global[name] = defaultValue; } } function defined (name) { - return typeof window[name] == 'object'; + return typeof _global[name] == 'object'; } defineMacro('CC_TEST', defined('tap') || defined('QUnit')); From cd302bbc031c97cb6cd5cb411d8246ec17663786 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Mon, 23 Oct 2017 15:10:19 +0800 Subject: [PATCH 4/4] add comment for window undefined --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 815918e434..3e08bad500 100644 --- a/index.js +++ b/index.js @@ -106,6 +106,8 @@ /** * @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,