From b54c819f38c2ab3288213739d16709aa266fda36 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 8 Nov 2016 17:00:37 +0800 Subject: [PATCH] minify code and output warning if _super method undefined --- cocos2d/core/platform/CCClass.js | 257 +++++++++++------------ cocos2d/core/platform/instantiate-jit.js | 4 +- test/qunit/unit/test-audioSource.js | 1 + test/qunit/unit/test-component.js | 3 + test/qunit/unit/test-instantiate.js | 5 +- 5 files changed, 126 insertions(+), 144 deletions(-) diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index 5004cded08..d30e79ab80 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -94,8 +94,8 @@ var deferredInitializer = { // */ function appendProp (cls, name/*, isGetter*/) { if (CC_DEV) { - //var JsVarReg = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/; - //if (!JsVarReg.test(name)) { + //var VAR_REG = /^[$A-Za-z_][0-9A-Za-z_$]*$/; + //if (!VAR_REG.test(name)) { // cc.error('The property name "' + name + '" is not compliant with JavaScript naming standards'); // return; //} @@ -104,9 +104,7 @@ function appendProp (cls, name/*, isGetter*/) { return; } } - - var index = cls.__props__.indexOf(name); - if (index < 0) { + if (cls.__props__.indexOf(name) < 0) { cls.__props__.push(name); } } @@ -280,69 +278,6 @@ function getDefault (defaultVal) { return defaultVal; } -/** - * Checks whether subclass is child of superclass or equals to superclass - * - * @method isChildClassOf - * @param {Function} subclass - * @param {Function} superclass - * @return {Boolean} - */ -cc.isChildClassOf = function (subclass, superclass) { - if (subclass && superclass) { - if (typeof subclass !== 'function') { - return false; - } - if (typeof superclass !== 'function') { - if (CC_DEV) { - cc.warn('[isChildClassOf] superclass should be function type, not', superclass); - } - return false; - } - if (subclass === superclass) { - return true; - } - for (;;) { - if (CC_JSB && subclass.$super) { - subclass = subclass.$super; - } - else { - var proto = subclass.prototype; // binded function do not have prototype - var dunderProto = proto && Object.getPrototypeOf(proto); - subclass = dunderProto && dunderProto.constructor; - } - if (!subclass) { - return false; - } - if (subclass === superclass) { - return true; - } - } - } - return false; -}; - -// get all super classes -function getInheritanceChain (klass) { - var chain = []; - for (;;) { - if (CC_JSB && klass.$super) { - klass = klass.$super; - } - else { - var dunderProto = Object.getPrototypeOf(klass.prototype); - klass = dunderProto && dunderProto.constructor; - } - if (!klass) { - break; - } - if (klass !== Object) { - chain.push(klass); - } - } - return chain; -} - function doDefine (className, baseClass, mixins, constructor, options) { var fireClass = _createCtor(constructor, baseClass, mixins, className, options); @@ -425,26 +360,6 @@ function define (className, baseClasses, mixins, constructor, options) { return doDefine(className, baseClasses, mixins, constructor, options); } -function _checkCtor (ctor, className) { - if (CC_DEV) { - if (CCClass._isCCClass(ctor)) { - cc.error('ctor of "%s" can not be another CCClass', className); - return; - } - if (typeof ctor !== 'function') { - cc.error('ctor of "%s" must be function type', className); - return; - } - if (ctor.length > 0 && !className.startsWith('cc.')) { - // fireball-x/dev#138: To make a unified CCClass serialization process, - // we don't allow parameters for constructor when creating instances of CCClass. - // For advance user, construct arguments can still get from 'arguments'. - cc.warn('Can not instantiate CCClass "%s" with arguments.', className); - return; - } - } -} - function normalizeClassName (className) { if (CC_DEV) { var DefaultName = 'CCClass'; @@ -499,7 +414,8 @@ function cleanEval_F (code, F) { // jshint evil: false } -var NAME_IN_DOT_NOTATION_REG = /^[$A-Za-z_][0-9A-Za-z_$]*$/; +// simple test variable name +var VAR_REG = /^[$A-Za-z_][0-9A-Za-z_$]*$/; function compileProps (actualClass) { // init deferred properties var attrs = Attr.getClassAttrs(actualClass); @@ -518,7 +434,7 @@ function compileProps (actualClass) { var attrKey = prop + Attr.DELIMETER + 'default'; if (attrKey in attrs) { // getter does not have default var statement; - if (NAME_IN_DOT_NOTATION_REG.test(prop)) { + if (VAR_REG.test(prop)) { statement = 'this.' + prop + '='; } else { @@ -600,10 +516,22 @@ function _createCtor (ctor, baseClass, mixins, className, options) { shouldAddProtoCtor = true; } } - var superCallBounded = options && baseClass && boundSuperCalls(baseClass, options); + var superCallBounded = options && baseClass && boundSuperCalls(baseClass, options, className); if (CC_DEV && ctor) { - _checkCtor(ctor, className); + // check ctor + if (CCClass._isCCClass(ctor)) { + cc.error('ctor of "%s" can not be another CCClass', className); + } + if (typeof ctor !== 'function') { + cc.error('ctor of "%s" must be function type', className); + } + if (ctor.length > 0 && !className.startsWith('cc.')) { + // fireball-x/dev#138: To make a unified CCClass serialization process, + // we don't allow parameters for constructor when creating instances of CCClass. + // For advance user, construct arguments can still get from 'arguments'. + cc.warn('Can not instantiate CCClass "%s" with arguments.', className); + } } // get base user constructors var ctors = []; @@ -683,48 +611,48 @@ function _createCtor (ctor, baseClass, mixins, className, options) { return fireClass; } -var SuperCallReg = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/; -function _boundSuperCall (func, funcName, base) { - var superFunc = null; - var pd = JS.getPropertyDescriptor(base.prototype, funcName); - if (pd) { - superFunc = pd.value; - // ignore pd.get, assume that function defined by getter is just for warnings - if (typeof superFunc === 'function') { - var hasSuperCall = SuperCallReg.test(func); - if (hasSuperCall) { - return function () { - var tmp = this._super; - - // Add a new ._super() method that is the same method but on the super-Class - this._super = superFunc; - - var ret = func.apply(this, arguments); - - // The method only need to be bound temporarily, so we remove it when we're done executing - this._super = tmp; - - return ret; - }; - } - } - } - return null; -} - -function boundSuperCalls (baseClass, options) { +var SuperCallReg = /xyz/.test(function(){xyz}) ? /\b_super\b/ : /.*/; +var SuperCallRegStrict = /xyz/.test(function(){xyz}) ? /this\._super\s*\(/ : /(NONE){99}/; +function boundSuperCalls (baseClass, options, className) { var hasSuperCall = false; for (var funcName in options) { - if (BUILTIN_ENTRIES.indexOf(funcName) < 0) { - var func = options[funcName]; - if (typeof func === 'function') { - var bounded = _boundSuperCall(func, funcName, baseClass); - if (bounded) { + if (BUILTIN_ENTRIES.indexOf(funcName) >= 0) { + continue; + } + var func = options[funcName]; + if (typeof func !== 'function') { + continue; + } + var pd = JS.getPropertyDescriptor(baseClass.prototype, funcName); + if (pd) { + var superFunc = pd.value; + // ignore pd.get, assume that function defined by getter is just for warnings + if (typeof superFunc === 'function') { + if (SuperCallReg.test(func)) { hasSuperCall = true; - options[funcName] = bounded; + // boundSuperCall + options[funcName] = (function (superFunc, func) { + return function () { + var tmp = this._super; + + // Add a new ._super() method that is the same method but on the super-Class + this._super = superFunc; + + var ret = func.apply(this, arguments); + + // The method only need to be bound temporarily, so we remove it when we're done executing + this._super = tmp; + + return ret; + }; + })(superFunc, func); } + continue; } } + if (CC_DEV && SuperCallRegStrict.test(func)) { + cc.warn('this._super declared in "%s.%s" but no super method defined', className, funcName); + } } return hasSuperCall; } @@ -966,15 +894,6 @@ CCClass._isCCClass = function (constructor) { return !!constructor && typeof constructor.__ctors__ !== 'undefined'; }; -// -// @method _convertToFireClass -// @param {Function} constructor -// @private -// -//CCClass._convertToFireClass = function (constructor) { -// constructor.prop = _metaClass.prop; -//}; - // // Optimized define function only for internal classes // @@ -999,13 +918,73 @@ CCClass._fastDefine = function (className, constructor, serializableFields) { CCClass.Attr = Attr; CCClass.attr = Attr.attr; +/** + * Checks whether subclass is child of superclass or equals to superclass + * + * @method isChildClassOf + * @param {Function} subclass + * @param {Function} superclass + * @return {Boolean} + */ +cc.isChildClassOf = function (subclass, superclass) { + if (subclass && superclass) { + if (typeof subclass !== 'function') { + return false; + } + if (typeof superclass !== 'function') { + if (CC_DEV) { + cc.warn('[isChildClassOf] superclass should be function type, not', superclass); + } + return false; + } + if (subclass === superclass) { + return true; + } + for (;;) { + if (CC_JSB && subclass.$super) { + subclass = subclass.$super; + } + else { + var proto = subclass.prototype; // binded function do not have prototype + var dunderProto = proto && Object.getPrototypeOf(proto); + subclass = dunderProto && dunderProto.constructor; + } + if (!subclass) { + return false; + } + if (subclass === superclass) { + return true; + } + } + } + return false; +}; + /** * Return all super classes * @method getInheritanceChain * @param {Function} constructor * @return {Function[]} */ -CCClass.getInheritanceChain = getInheritanceChain; +CCClass.getInheritanceChain = function (klass) { + var chain = []; + for (;;) { + if (CC_JSB && klass.$super) { + klass = klass.$super; + } + else { + var dunderProto = Object.getPrototypeOf(klass.prototype); + klass = dunderProto && dunderProto.constructor; + } + if (!klass) { + break; + } + if (klass !== Object) { + chain.push(klass); + } + } + return chain; +}; var PrimitiveTypes = { // Specify that the input value must be integer in Inspector. @@ -1168,7 +1147,7 @@ module.exports = { }, fastDefine: CCClass._fastDefine, getNewValueTypeCode: getNewValueTypeCode, - NAME_IN_DOT_NOTATION_REG: NAME_IN_DOT_NOTATION_REG, + VAR_REG: VAR_REG, }; if (CC_EDITOR) { diff --git a/cocos2d/core/platform/instantiate-jit.js b/cocos2d/core/platform/instantiate-jit.js index b34ee81907..6854533f20 100644 --- a/cocos2d/core/platform/instantiate-jit.js +++ b/cocos2d/core/platform/instantiate-jit.js @@ -34,7 +34,7 @@ var CCClass = require('./CCClass'); var SERIALIZABLE = Attr.DELIMETER + 'serializable'; var DEFAULT = Attr.DELIMETER + 'default'; -var NAME_IN_DOT_NOTATION_REG = CCClass.NAME_IN_DOT_NOTATION_REG; +var VAR_REG = CCClass.VAR_REG; var VAR = 'var '; var LOCAL_OBJ = 'o'; @@ -292,7 +292,7 @@ JS.mixin(Parser.prototype, { writeField: function (codeArray, obj, key, value) { var statement; - if (NAME_IN_DOT_NOTATION_REG.test(key)) { + if (VAR_REG.test(key)) { statement = LOCAL_OBJ + '.' + key + '='; } else { diff --git a/test/qunit/unit/test-audioSource.js b/test/qunit/unit/test-audioSource.js index 5a6ac57d5f..7c1dda39c6 100644 --- a/test/qunit/unit/test-audioSource.js +++ b/test/qunit/unit/test-audioSource.js @@ -12,6 +12,7 @@ if (!isPhantomJS) { audioSource.play(); audioSource.audio.on('load', function () { + clearTimeout(timerId); strictEqual(audioSource.isPlaying, true, 'audio scource play state true after preload'); start(); }); diff --git a/test/qunit/unit/test-component.js b/test/qunit/unit/test-component.js index 23e2809a28..8c71ab6129 100644 --- a/test/qunit/unit/test-component.js +++ b/test/qunit/unit/test-component.js @@ -96,6 +96,9 @@ test('start', function () { cc.game.step(); // run TestComp cc.game.step(); + + // end test + node.active = false; }); test('start should only', function () { diff --git a/test/qunit/unit/test-instantiate.js b/test/qunit/unit/test-instantiate.js index c7187a5565..216e85115b 100644 --- a/test/qunit/unit/test-instantiate.js +++ b/test/qunit/unit/test-instantiate.js @@ -190,9 +190,8 @@ function testInstantiate (module, match, instantiate) { var node = new cc.Node(); var comp = node.addComponent(Script); - var otherComp = node.addComponent(cc.Sprite); - comp.compRef = otherComp; - otherComp.destroy(); + comp.compRef = node.addComponent(cc.Sprite); + comp.compRef.destroy(); cc.Object._deferredDestroy();