diff --git a/DebugInfos.js b/DebugInfos.js
index 930b805621..d0bedc3aa4 100644
--- a/DebugInfos.js
+++ b/DebugInfos.js
@@ -277,7 +277,7 @@ if (CC_DEBUG) {
"3621": "Unknown type of %s.%s, maybe you want is '%s'.", //CCClass
"3622": "Unknown type of %s.%s, property should be defined in 'properties' or 'ctor'", //CCClass_2
"3623": "Can not use 'editor' attribute, '%s' not inherits from Components.", //CCClass_3
- "3624": "'%s' overrided '%s' but '%s' is defined as 'false' so the super method will not be called. You can set '%s' to null to disable this warning.", //CCClass_4
+ "3624": "'%s' overwrote '%s' but '%s' is defined as 'false' so the super method will not be called. You can set '%s' to null to disable this warning.", //CCClass_4
"3625": "[isChildClassOf] superclass should be function type, not", //isChildClassOf
"3626": "Can't remove '%s' because '%s' depends on it.", //destroy
"3627": "Should not add renderer component (%s) to a Canvas node.", //_registSizeProvider
@@ -432,7 +432,7 @@ if (CC_DEBUG) {
"5514": "The 'default' value of '%s.%s' should not be used with a 'set' function.", //default_2
"5515": "The 'default' value of '%s.%s' can not be an constructor. Set default to null please.", //default_3
"5516": "Property '%s.%s' must define at least one of 'default', 'get' or 'set'.", //default_4
- "5517": "'%s.%s' hides inherited property '%s.%s'. To make the current property override that implementation, add the `override: true` attribute please.", //default_5
+ "5517": "'%s.%s' hides inherited property '%s.%s'. To make the current property overwrite that implementation, add the `override: true` attribute please.", //default_5
//Find: 5600
"5600": "Argument must be non-nil", //find
"5601": "Can not get current scene.", //find_2
diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js
index b25333ecc0..a4bbb7ee7c 100644
--- a/cocos2d/core/CCNode.js
+++ b/cocos2d/core/CCNode.js
@@ -1100,7 +1100,7 @@ var Node = cc.Class({
* It's the recommended way to register touch/mouse event for Node,
* please do not use cc.eventManager directly for Node.
* !#zh
- * 在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的调用者。
+ * 在节点上注册指定类型的回调函数,也可以设置 target 用于绑定响应函数的 this 对象。
* 同时您可以将事件派发到父节点或者通过调用 stopPropagation 拦截它。
* 推荐使用这种方式来监听节点上的触摸或鼠标事件,请不要在节点上直接使用 cc.eventManager。
* @method on
@@ -1109,7 +1109,7 @@ var Node = cc.Class({
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
- * @param {Object} [target] - The target to invoke the callback, can be null
+ * @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
@@ -1184,7 +1184,7 @@ var Node = cc.Class({
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} callback - The callback to remove.
- * @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
+ * @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
diff --git a/cocos2d/core/components/CCAnimation.js b/cocos2d/core/components/CCAnimation.js
index e70f645625..9f38ff55ce 100644
--- a/cocos2d/core/components/CCAnimation.js
+++ b/cocos2d/core/components/CCAnimation.js
@@ -508,7 +508,7 @@ var Animation = cc.Class({
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
- * @param {Object} [target] - The target to invoke the callback, can be null
+ * @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
@@ -556,7 +556,7 @@ var Animation = cc.Class({
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} callback - The callback to remove.
- * @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
+ * @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
diff --git a/cocos2d/core/event/event-listeners.js b/cocos2d/core/event/event-listeners.js
index 8bb5d9ffa2..67b316da99 100644
--- a/cocos2d/core/event/event-listeners.js
+++ b/cocos2d/core/event/event-listeners.js
@@ -26,8 +26,6 @@
var JS = cc.js;
var CallbacksHandler = require('../platform/callbacks-invoker').CallbacksHandler;
-var REMOVE_PLACEHOLDER = CallbacksHandler.REMOVE_PLACEHOLDER;
-
// Extends CallbacksHandler to handle and invoke event callbacks.
function EventListeners () {
CallbacksHandler.call(this);
@@ -35,50 +33,32 @@ function EventListeners () {
JS.extend(EventListeners, CallbacksHandler);
EventListeners.prototype.invoke = function (event, captureListeners) {
- var key = event.type,
- list = this._callbackTable[key],
- i, endIndex,
- callingFunc, target, hasTarget;
-
- this._invoking[key] = true;
-
+ var key = event.type;
+ var list = this._callbackTable[key];
if (list) {
- if (list.length === 1) {
- callingFunc = list[0];
- if (callingFunc !== REMOVE_PLACEHOLDER) {
- callingFunc.call(event.currentTarget, event, captureListeners);
+ var rootInvoker = !list.isInvoking;
+ list.isInvoking = true;
+
+ var callbacks = list.callbacks;
+ var targets = list.targets;
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
+ var callback = callbacks[i];
+ if (callback) {
+ var target = targets[i] || event.currentTarget;
+ callback.call(target, event, captureListeners);
+ if (event._propagationImmediateStopped) {
+ break;
+ }
}
}
- else {
- endIndex = list.length - 1;
- for (i = 0; i <= endIndex;) {
- callingFunc = list[i];
- var increment = 1;
- // cheap detection for function
- if (callingFunc !== REMOVE_PLACEHOLDER) {
- target = list[i+1];
- hasTarget = target && typeof target === 'object';
- if (hasTarget) {
- callingFunc.call(target, event, captureListeners);
- increment = 2;
- }
- else {
- callingFunc.call(event.currentTarget, event, captureListeners);
- }
- if (event._propagationImmediateStopped || i + increment > endIndex) {
- break;
- }
- }
-
- i += increment;
+ if (rootInvoker) {
+ list.isInvoking = false;
+ if (list.containCanceled) {
+ list.purgeCanceled();
}
}
}
- this._invoking[key] = false;
-
- // Delay removing
- this._clearToRemove(key);
};
module.exports = EventListeners;
diff --git a/cocos2d/core/event/event-target.js b/cocos2d/core/event/event-target.js
index 5078e392b3..1a15626fcc 100644
--- a/cocos2d/core/event/event-target.js
+++ b/cocos2d/core/event/event-target.js
@@ -162,7 +162,7 @@ proto.hasEventListener = function (type, checkCapture) {
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
- * @param {Object} [target] - The target to invoke the callback, can be null
+ * @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
@@ -213,7 +213,7 @@ proto.on = function (type, callback, target, useCapture) {
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} [callback] - The callback to remove.
- * @param {Object} [target] - The target to invoke the callback, if it's not given, only callback without target will be removed
+ * @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not.
* If not specified, useCapture defaults to false. If a callback was registered twice,
* one with capture and one without, each must be removed separately. Removal of a capturing callback
@@ -283,7 +283,7 @@ proto.targetOff = function (target) {
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {Event} callback.event event
- * @param {Object} [target] - The target to invoke the callback, can be null
+ * @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
* When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE.
diff --git a/cocos2d/core/load-pipeline/loading-items.js b/cocos2d/core/load-pipeline/loading-items.js
index c988695511..ebc5f7d7ae 100644
--- a/cocos2d/core/load-pipeline/loading-items.js
+++ b/cocos2d/core/load-pipeline/loading-items.js
@@ -728,7 +728,8 @@ proto.itemComplete = function (id) {
this.onProgress(dep ? dep.completed.length : this.completedCount, dep ? dep.deps.length : this.totalCount, item);
}
- this.invokeAndRemove(id, item);
+ this.invoke(id, item);
+ this.removeAll(id);
// All completed
if (!this._appending && this.completedCount >= this.totalCount) {
diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js
index 19944465e3..aaaad37ca5 100644
--- a/cocos2d/core/platform/CCClass.js
+++ b/cocos2d/core/platform/CCClass.js
@@ -863,8 +863,7 @@ function CCClass (options) {
continue;
}
var func = options[funcName];
- // TODO: call validateMethod on es6
- if (!preprocess.validateMethod(func, funcName, name, cls, base)) {
+ if (!preprocess.validateMethodWithProps(func, funcName, name, cls, base)) {
continue;
}
// use value to redefine some super method defined as getter
diff --git a/cocos2d/core/platform/CCClassDecorator.js b/cocos2d/core/platform/CCClassDecorator.js
index 6bad048f9b..c0cddccacb 100644
--- a/cocos2d/core/platform/CCClassDecorator.js
+++ b/cocos2d/core/platform/CCClassDecorator.js
@@ -166,6 +166,7 @@ var ccclass = checkCtorArgument(function (ctor, name) {
if (base === Object) {
base = null;
}
+
var proto = {
name,
extends: base,
@@ -177,11 +178,23 @@ var ccclass = checkCtorArgument(function (ctor, name) {
JS.mixin(proto, decoratedProto);
ctor.__ccclassProto__ = undefined;
}
+
var res = cc.Class(proto);
- // if (FIX_BABEL6) {
- // for (var method in Object.getOwnPropertyNames(ctor.prototype)) {
- // }
- // }
+
+ // validate methods
+ if (CC_DEV) {
+ var methods = Object.getOwnPropertyNames(ctor.prototype);
+ for (var i = 0; i < methods.length; ++i) {
+ var methodName = methods[i];
+ if (methodName !== 'constructor') {
+ var func = ctor.prototype[methodName];
+ if (typeof func === 'function') {
+ Preprocess.doValidateMethodWithProps_DEV(func, methodName, JS.getClassName(ctor), ctor, base);
+ }
+ }
+ }
+ }
+
return res;
});
diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js
index 7e028a2dc0..380d912be2 100644
--- a/cocos2d/core/platform/callbacks-invoker.js
+++ b/cocos2d/core/platform/callbacks-invoker.js
@@ -23,7 +23,60 @@
THE SOFTWARE.
****************************************************************************/
-var JS = require('./js');
+const JS = require('./js');
+const fastRemoveAt = JS.array.fastRemoveAt;
+
+function CallbackList () {
+ this.callbacks = [];
+ this.targets = []; // same length with callbacks, nullable
+ this.isInvoking = false;
+ this.containCanceled = false;
+}
+var proto = CallbackList.prototype;
+
+proto.removeBy = function (array, value) {
+ var callbacks = this.callbacks;
+ var targets = this.targets;
+ for (var i = 0; i < array.length; ++i) {
+ if (array[i] === value) {
+ fastRemoveAt(callbacks, i);
+ fastRemoveAt(targets, i);
+ --i;
+ }
+ }
+};
+
+proto.cancel = function (index) {
+ this.callbacks[index] = this.targets[index] = null;
+ this.containCanceled = true;
+};
+
+proto.cancelAll = function () {
+ let callbacks = this.callbacks;
+ let targets = this.targets;
+ for (let i = 0; i < callbacks.length; i++) {
+ callbacks[i] = targets[i] = null;
+ }
+ this.containCanceled = true;
+};
+
+// filter all removed callbacks and compact array
+proto.purgeCanceled = function () {
+ this.removeBy(this.callbacks, null);
+ this.containCanceled = false;
+};
+
+
+const MAX_SIZE = 16;
+const callbackListPool = new JS.Pool(function (list) {
+ list.callbacks.length = 0;
+ list.targets.length = 0;
+ list.isInvoking = false;
+ list.containCanceled = false;
+}, MAX_SIZE);
+callbackListPool.get = function () {
+ return this._get() || new CallbackList();
+};
/**
* The CallbacksHandler is an abstract class that can register and unregister callbacks by key.
@@ -32,70 +85,24 @@ var JS = require('./js');
*
* @private
*/
-var CallbacksHandler = (function () {
- this._callbackTable = {};
- this._invoking = {};
- this._toRemove = {};
- this._toRemoveAll = null;
-});
-
-// Avoid to equal to user set target (null for example)
-var REMOVE_PLACEHOLDER = {};
-CallbacksHandler.REMOVE_PLACEHOLDER = REMOVE_PLACEHOLDER;
-
-CallbacksHandler.prototype._clearToRemove = function (key) {
- var list = this._callbackTable[key];
- if (this._toRemove[key] && list) {
- // filter all REMOVE_PLACEHOLDER and compact array
- var firstRemovedIndex = list.indexOf(REMOVE_PLACEHOLDER);
- var nextIndex = firstRemovedIndex;
- for (var i = firstRemovedIndex + 1; i < list.length; ++i) {
- var item = list[i];
- if (item !== REMOVE_PLACEHOLDER) {
- list[nextIndex] = item;
- ++nextIndex;
- }
- }
- list.length = nextIndex;
- this._toRemove[key] = false;
- }
- if (this._toRemoveAll) {
- this.removeAll(this._toRemoveAll);
- this._toRemoveAll = null;
- }
-};
+function CallbacksHandler () {
+ this._callbackTable = JS.createMap(true);
+}
+proto = CallbacksHandler.prototype;
/**
* @method add
* @param {String} key
* @param {Function} callback
* @param {Object} [target] - can be null
- * @return {Boolean} whether the key is new
*/
-CallbacksHandler.prototype.add = function (key, callback, target) {
+proto.add = function (key, callback, target) {
var list = this._callbackTable[key];
- if (typeof list !== 'undefined') {
- if (typeof target === 'object') {
- // append the target after callback
- list.push(callback, target);
- }
- else {
- list.push(callback);
- }
- return false;
- }
- else {
- // new key
- if (typeof target === 'object') {
- // Just append the target after callback
- list = [callback, target];
- }
- else {
- list = [callback];
- }
- this._callbackTable[key] = list;
- return true;
+ if (!list) {
+ list = this._callbackTable[key] = callbackListPool.get();
}
+ list.callbacks.push(callback);
+ list.targets.push(target || null);
};
/**
@@ -107,78 +114,68 @@ CallbacksHandler.prototype.add = function (key, callback, target) {
* @param {Object} [target]
* @return {Boolean}
*/
-CallbacksHandler.prototype.has = function (key, callback, target) {
- if (this._toRemoveAll === key) {
- return false;
- }
- var list = this._callbackTable[key], callbackTarget, index;
+proto.has = function (key, callback, target) {
+ var list = this._callbackTable[key];
if (!list) {
return false;
}
- // callback not given, but key found
- if (!callback) {
- if (this._toRemove[key]) {
- for (index = 0; index < list.length; index++) {
- if (list[index] !== REMOVE_PLACEHOLDER) {
- return true;
- }
- }
- return false;
- }
- else {
- return list.length > 0;
- }
- }
- // wrong callback type, can't found anything
- else if (typeof callback !== 'function')
- return false;
- // Search callback, target pair in the list
- index = list.indexOf(callback);
- while (index !== -1) {
- callbackTarget = list[index+1];
- if (typeof callbackTarget !== 'object') {
- callbackTarget = undefined;
+ // check any valid callback
+ var callbacks = list.callbacks;
+ if (!callback) {
+ for (let i = 0; i < callbacks.length; i++) {
+ if (callbacks[i]) {
+ return true;
+ }
}
- if (callbackTarget === target) {
+ return false;
+ }
+
+ target = target || null;
+ var targets = list.targets;
+ for (let i = 0; i < callbacks.length; ++i) {
+ if (callbacks[i] === callback && targets[i] === target) {
return true;
}
- index = cc.js.array.indexOf.call(list, callback, index + 1);
}
- // callback given but not found
return false;
};
/**
* Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
* @method removeAll
- * @param {String|Object} key - The event key to be removed or the target to be removed
+ * @param {String|Object} keyOrTarget - The event key to be removed or the target to be removed
*/
-CallbacksHandler.prototype.removeAll = function (key) {
- // Delay removing
- if (this._invoking[key]) {
- this._toRemoveAll = key;
- return;
- }
- if (typeof key === 'object') {
- var target = key, list, index, callback;
- // loop for all event types
- for (key in this._callbackTable) {
- list = this._callbackTable[key];
- index = list.lastIndexOf(target);
- while (index !== -1) {
- callback = list[index-1];
- if (typeof callback === 'function')
- list.splice(index-1, 2);
- else
- list.splice(index, 1);
- index = list.lastIndexOf(target);
+proto.removeAll = function (keyOrTarget) {
+ if (typeof keyOrTarget === 'string') {
+ // remove by key
+ let list = this._callbackTable[keyOrTarget];
+ if (list) {
+ if (list.isInvoking) {
+ list.cancelAll();
+ }
+ else {
+ callbackListPool.put(list);
+ delete this._callbackTable[keyOrTarget];
}
}
}
- else {
- delete this._callbackTable[key];
- delete this._toRemove[key];
+ else if (keyOrTarget) {
+ // remove by target
+ for (let key in this._callbackTable) {
+ let list = this._callbackTable[key];
+ if (list.isInvoking) {
+ let targets = list.targets;
+ for (let i = 0; i < targets.length; ++i) {
+ if (targets[i] === keyOrTarget) {
+ list.cancel(i);
+ }
+ }
+ }
+ else {
+ list.removeBy(list.targets, keyOrTarget);
+ }
+ }
}
};
@@ -186,37 +183,27 @@ CallbacksHandler.prototype.removeAll = function (key) {
* @method remove
* @param {String} key
* @param {Function} callback
- * @param {Object} target
- * @return {Boolean} removed
+ * @param {Object} [target]
*/
-CallbacksHandler.prototype.remove = function (key, callback, target) {
- var list = this._callbackTable[key], index, callbackTarget;
+proto.remove = function (key, callback, target) {
+ var list = this._callbackTable[key];
if (list) {
- index = list.indexOf(callback);
- while (index !== -1) {
- callbackTarget = list[index+1];
- if (typeof callbackTarget !== 'object') {
- callbackTarget = undefined;
- }
- if (callbackTarget === target) {
- // Delay removing
- if (CC_JSB || this._invoking[key]) {
- list[index] = REMOVE_PLACEHOLDER;
- callbackTarget && (list[index+1] = REMOVE_PLACEHOLDER);
- this._toRemove[key] = true;
+ target = target || null;
+ var callbacks = list.callbacks;
+ var targets = list.targets;
+ for (var i = 0; i < callbacks.length; ++i) {
+ if (callbacks[i] === callback && targets[i] === target) {
+ if (list.isInvoking) {
+ list.cancel(i);
}
else {
- list.splice(index, callbackTarget ? 2 : 1);
+ fastRemoveAt(callbacks, i);
+ fastRemoveAt(targets, i);
}
break;
}
-
- // indexOf have bug on some mobile browsers
- index = cc.js.array.indexOf.call(list, callback, index + 1);
}
- return true;
}
- return false;
};
@@ -246,102 +233,33 @@ if (CC_TEST) {
* @param {any} [p5]
*/
CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) {
- this._invoking[key] = true;
var list = this._callbackTable[key];
if (list) {
- var i, endIndex = list.length - 1;
- for (i = 0; i <= endIndex;) {
- var callingFunc = list[i];
- var increment = 1;
- // cheap detection for function
- if (callingFunc !== REMOVE_PLACEHOLDER) {
- var target = list[i + 1];
- var hasTarget = target && typeof target === 'object';
- if (hasTarget) {
- callingFunc.call(target, p1, p2, p3, p4, p5);
- increment = 2;
+ var rootInvoker = !list.isInvoking;
+ list.isInvoking = true;
+
+ var callbacks = list.callbacks;
+ var targets = list.targets;
+ for (var i = 0, len = callbacks.length; i < len; ++i) {
+ var callback = callbacks[i];
+ if (callback) {
+ var target = targets[i];
+ if (target) {
+ callback.call(target, p1, p2, p3, p4, p5);
}
else {
- callingFunc(p1, p2, p3, p4, p5);
+ callback(p1, p2, p3, p4, p5);
}
}
+ }
- i += increment;
+ if (rootInvoker) {
+ list.isInvoking = false;
+ if (list.containCanceled) {
+ list.purgeCanceled();
+ }
}
}
- this._invoking[key] = false;
-
- // Delay removing
- this._clearToRemove(key);
-};
-
-/**
- * @method invokeAndRemove
- * @param {String} key
- * @param {any} [p1]
- * @param {any} [p2]
- * @param {any} [p3]
- * @param {any} [p4]
- * @param {any} [p5]
- */
-CallbacksInvoker.prototype.invokeAndRemove = function (key, p1, p2, p3, p4, p5) {
- this._invoking[key] = true;
- // this.invoke(key, p1, p2, p3, p4, p5);
- // 这里不直接调用invoke仅仅是为了减少调用堆栈的深度,方便调试
- var list = this._callbackTable[key], i, l, increment, callingFunc, target;
- if (list) {
- for (i = 0, l = list.length; i < l;) {
- callingFunc = list[i];
- increment = 1;
- // cheap detection for function
- if (callingFunc !== REMOVE_PLACEHOLDER) {
- target = list[i+1];
- if (target && typeof target === 'object') {
- callingFunc.call(target, p1, p2, p3, p4, p5);
- increment = 2;
- }
- else {
- callingFunc(p1, p2, p3, p4, p5);
- }
- }
-
- i += increment;
- }
- }
- this._invoking[key] = false;
- this._toRemove[key] = false;
- this.removeAll(key);
-};
-
-/**
- * @method bindKey
- * @param {String} key
- * @param {Boolean} [remove=false] - remove callbacks after invoked
- * @return {Function} the new callback which will invoke all the callbacks binded with the same supplied key
- */
-CallbacksInvoker.prototype.bindKey = function (key, remove) {
- var self = this;
- return function bindedInvocation (p1, p2, p3, p4, p5) {
- // this.invoke(key, p1, p2, p3, p4, p5);
- // 这里不直接调用invoke仅仅是为了减少调用堆栈的深度,方便调试
- var list = self._callbackTable[key], i, l, target;
- if (list) {
- for (i = 0, l = list.length; i < l;) {
- target = list[i+1];
- if (target && typeof target === 'object') {
- list[i].call(target, p1, p2, p3, p4, p5);
- i += 2;
- }
- else {
- list[i](p1, p2, p3, p4, p5);
- ++i;
- }
- }
- }
- if (remove) {
- self.removeAll(key);
- }
- };
};
CallbacksInvoker.CallbacksHandler = CallbacksHandler;
diff --git a/cocos2d/core/platform/instantiate-jit.js b/cocos2d/core/platform/instantiate-jit.js
index f5a19bc8b9..64ebb33bc3 100644
--- a/cocos2d/core/platform/instantiate-jit.js
+++ b/cocos2d/core/platform/instantiate-jit.js
@@ -474,6 +474,9 @@ proto.instantiateObj = function (obj) {
else if (ctor === Object) {
createCode = new Declaration(LOCAL_OBJ, '{}');
}
+ else if (!ctor) {
+ createCode = new Declaration(LOCAL_OBJ, 'Object.create(null)');
+ }
else {
// do not clone unknown type
return this.getObjRef(obj);
diff --git a/cocos2d/core/platform/instantiate.js b/cocos2d/core/platform/instantiate.js
index 95a6341bf6..54a6a6422a 100644
--- a/cocos2d/core/platform/instantiate.js
+++ b/cocos2d/core/platform/instantiate.js
@@ -134,14 +134,17 @@ function doInstantiate (obj, parent) {
var clone;
if (obj._iN$t) {
- clone = obj._iN$t;
+ // User can specify an existing object by assigning the "_iN$t" property.
// enumerateObject will always push obj to objsToClearTmpVar
+ clone = obj._iN$t;
}
- else {
- // User can specify an existing object by assigning the "_iN$t" property
+ else if (obj.constructor) {
var klass = obj.constructor;
clone = new klass();
}
+ else {
+ clone = Object.create(null);
+ }
enumerateObject(obj, clone, parent);
@@ -268,12 +271,18 @@ function instantiateObj (obj, parent) {
}
}
}
+ clone = new ctor();
}
- else if (ctor !== Object) {
+ else if (ctor === Object) {
+ clone = {};
+ }
+ else if (!ctor) {
+ clone = Object.create(null);
+ }
+ else {
// unknown type
return obj;
}
- clone = new ctor();
enumerateObject(obj, clone, parent);
return clone;
}
diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js
index ae47d2c852..cddfcdf7c8 100644
--- a/cocos2d/core/platform/js.js
+++ b/cocos2d/core/platform/js.js
@@ -631,8 +631,9 @@ js.shiftArguments = function () {
js.createMap = function (forceDictMode) {
var map = Object.create(null);
if (forceDictMode) {
- map["__"] = undefined;
- delete map["__"];
+ const INVALID_IDENTIFIER = '.';
+ map[INVALID_IDENTIFIER] = true;
+ delete map[INVALID_IDENTIFIER];
}
return map;
};
@@ -746,20 +747,14 @@ function appendObjectsAt (array, addObjs, index) {
}
/**
- * Exact same function as Array.prototype.indexOf.
- * HACK: ugliy hack for Baidu mobile browser compatibility,
- * stupid Baidu guys modify Array.prototype.indexOf for all pages loaded,
- * their version changes strict comparison to non-strict comparison,
- * it also ignores the second parameter of the original API,
- * and this will cause event handler enter infinite loop.
- * Baidu developers, if you ever see this documentation,
- * here is the standard: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
- * Seriously !
+ * Exact same function as Array.prototype.indexOf.
+ * HACK: ugliy hack for Baidu mobile browser compatibility, stupid Baidu guys modify Array.prototype.indexOf for all pages loaded, their version changes strict comparison to non-strict comparison, it also ignores the second parameter of the original API, and this will cause event handler enter infinite loop.
+ * Baidu developers, if you ever see this documentation, here is the standard: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf, Seriously!
*
* @method indexOf
- * @param {any} searchElement Element to locate in the array.
- * @param {Number} [fromIndex=0] The index to start the search at
- * @return {Number} returns the first index at which a given element can be found in the array, or -1 if it is not present.
+ * @param {any} searchElement - Element to locate in the array.
+ * @param {Number} [fromIndex=0] - The index to start the search at
+ * @return {Number} - the first index at which a given element can be found in the array, or -1 if it is not present.
*/
var indexOf = Array.prototype.indexOf;
@@ -771,7 +766,7 @@ var indexOf = Array.prototype.indexOf;
* @return {Boolean}
*/
function contains (array, value) {
- return indexOf.call(array, value) >= 0;
+ return array.indexOf(value) >= 0;
}
/**
@@ -788,16 +783,16 @@ function copy (array) {
}
js.array = {
- remove: remove,
- fastRemove: fastRemove,
- removeAt: removeAt,
- fastRemoveAt: fastRemoveAt,
- contains: contains,
- verifyType: verifyType,
- removeArray: removeArray,
- appendObjectsAt: appendObjectsAt,
- copy: copy,
- indexOf: indexOf,
+ remove,
+ fastRemove,
+ removeAt,
+ fastRemoveAt,
+ contains,
+ verifyType,
+ removeArray,
+ appendObjectsAt,
+ copy,
+ indexOf,
MutableForwardIterator: require('../utils/mutable-forward-iterator')
};
diff --git a/cocos2d/core/platform/preprocess-class.js b/cocos2d/core/platform/preprocess-class.js
index f9c931083a..b30ad221fe 100644
--- a/cocos2d/core/platform/preprocess-class.js
+++ b/cocos2d/core/platform/preprocess-class.js
@@ -262,17 +262,32 @@ exports.preprocessAttrs = function (properties, className, cls, es6) {
}
};
-module.exports.validateMethod = function (func, funcName, className, cls, base) {
+if (CC_DEV) {
+ const CALL_SUPER_DESTROY_REG_DEV = /\b\._super\b|destroy\s*\.\s*call\s*\(\s*\w+\s*[,|)]/;
+ exports.doValidateMethodWithProps_DEV = function (func, funcName, className, cls, base) {
+ if (cls.__props__ && cls.__props__.indexOf(funcName) >= 0) {
+ // find class that defines this method as a property
+ var baseClassName = cc.js.getClassName(getBaseClassWherePropertyDefined_DEV(funcName, cls));
+ cc.errorID(3648, className, funcName, baseClassName);
+ return false;
+ }
+ if (funcName === 'destroy' &&
+ cc.isChildClassOf(base, cc.Component) &&
+ !CALL_SUPER_DESTROY_REG_DEV.test(func)
+ ) {
+ cc.error(`Overwriting '${funcName}' function in '${className}' class without calling super is not allowed. Call the super function in '${funcName}' please.`);
+ }
+ };
+}
+
+exports.validateMethodWithProps = function (func, funcName, className, cls, base) {
if (CC_DEV && funcName === 'constructor') {
cc.errorID(3643, className);
return false;
}
if (typeof func === 'function' || func === null) {
- if (CC_DEV && cls.__props__ && cls.__props__.indexOf(funcName) >= 0) {
- // find class that defines this method as a property
- var baseClassName = cc.js.getClassName(getBaseClassWherePropertyDefined_DEV(funcName, cls));
- cc.errorID(3648, className, funcName, baseClassName);
- return false;
+ if (CC_DEV) {
+ this.doValidateMethodWithProps_DEV(func, funcName, className, cls, base);
}
}
else {
diff --git a/cocos2d/core/platform/requiring-frame.js b/cocos2d/core/platform/requiring-frame.js
index 2e800176ce..dde5c05629 100644
--- a/cocos2d/core/platform/requiring-frame.js
+++ b/cocos2d/core/platform/requiring-frame.js
@@ -63,10 +63,3 @@ if (CC_EDITOR) {
requiringFrames = [];
};
}
-
-cc._RFpush = cc._RF.push;
-cc._RFpop = cc._RF.pop;
-cc._RFpeek = cc._RF.peek;
-if (CC_EDITOR) {
- cc._RFreset = cc._RF.reset;
-}
diff --git a/cocos2d/core/platform/utils.js b/cocos2d/core/platform/utils.js
index 21d427e29e..b0528365f6 100644
--- a/cocos2d/core/platform/utils.js
+++ b/cocos2d/core/platform/utils.js
@@ -104,8 +104,9 @@ if (CC_DEV) {
return true;
};
module.exports.cloneable_DEV = function (obj) {
- return obj && typeof obj.clone === 'function' &&
- (obj.constructor.prototype.hasOwnProperty('clone') || obj.hasOwnProperty('clone'));
+ return obj &&
+ typeof obj.clone === 'function' &&
+ ( (obj.constructor && obj.constructor.prototype.hasOwnProperty('clone')) || obj.hasOwnProperty('clone') );
};
}
diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js
index 6e8fec562f..659b08ae8f 100644
--- a/jsb/jsb-director.js
+++ b/jsb/jsb-director.js
@@ -182,9 +182,11 @@ cc.js.mixin(cc.director, {
// detach persist nodes
var game = cc.game;
- var persistNodes = game._persistRootNodes;
- for (let id in persistNodes) {
- let node = persistNodes[id];
+ var persistNodeList = Object.keys(game._persistRootNodes).map(function (x) {
+ return game._persistRootNodes[x];
+ });
+ for (let i = 0; i < persistNodeList.length; i++) {
+ let node = persistNodeList[i];
game._ignoreRemovePersistNode = node;
node.parent = null;
game._ignoreRemovePersistNode = null;
@@ -195,7 +197,7 @@ cc.js.mixin(cc.director, {
// auto release assets
console.time(AUTO_RELEASE);
var autoReleaseAssets = oldScene && oldScene.autoReleaseAssets && oldScene.dependAssets;
- AutoReleaseUtils.autoRelease(cc.loader, autoReleaseAssets, scene.dependAssets);
+ AutoReleaseUtils.autoRelease(autoReleaseAssets, scene.dependAssets, persistNodeList);
console.timeEnd(AUTO_RELEASE);
// unload scene
@@ -224,9 +226,9 @@ cc.js.mixin(cc.director, {
// Re-attach or replace persist nodes
console.time(ATTACH_PERSIST);
- for (let id in persistNodes) {
- let node = persistNodes[id];
- var existNode = scene.getChildByUuid(id);
+ for (let i = 0; i < persistNodeList.length; i++) {
+ let node = persistNodeList[i];
+ var existNode = scene.getChildByUuid(node.uuid);
if (existNode) {
// scene also contains the persist node, select the old one
var index = existNode.getSiblingIndex();
diff --git a/test/qunit/unit-es5/test-callbacks-invoker.js b/test/qunit/unit-es5/test-callbacks-invoker.js
index e79d502195..6a29155ca5 100644
--- a/test/qunit/unit-es5/test-callbacks-invoker.js
+++ b/test/qunit/unit-es5/test-callbacks-invoker.js
@@ -3,15 +3,15 @@ largeModule('CallbacksInvoker');
test('test', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback();
- var cb2 = new Callback();
- var cb3 = new Callback();
- strictEqual(ci.add('a', cb1), true, 'first cb key');
+ var cb1 = Callback();
+ var cb2 = Callback();
+ var cb3 = Callback();
+ ci.add('a', cb1);
strictEqual(ci.has('a', function () {}), false, '`has` should return false if the callback not exists');
strictEqual(ci.has('a', cb1), true, '`has` should return true if the callback exists');
- strictEqual(ci.add('a', cb2), false, 'not first key');
- strictEqual(ci.add('b', cb3), true, 'another first key');
- strictEqual(ci.add('nil', undefined), true, 'null callback should also return true');
+ ci.add('a', cb2);
+ ci.add('b', cb3);
+ ci.add('nil', undefined);
cb1.enable();
cb2.enable();
@@ -19,11 +19,6 @@ test('test', function () {
cb1.once('1 should be called');
cb2.once('2 should be called');
- var invokeA = ci.bindKey('a');
- invokeA();
- cb1.once('1 should be called again').disable();
- cb2.once('2 should be called again').disable();
-
cb3.enable();
ci.invoke('b');
cb3.once('3 should be called');
@@ -45,10 +40,10 @@ test('test', function () {
test('remove self during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback(function () {
+ var cb1 = Callback(function () {
ci.remove('eve', cb1);
}).enable();
- var cb2 = new Callback().enable();
+ var cb2 = Callback().enable();
ci.add('eve', cb1);
ci.add('eve', cb2);
@@ -61,10 +56,10 @@ test('remove self during invoking', function () {
test('remove self with target during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback(function () {
+ var cb1 = Callback(function () {
ci.remove('eve', cb1, target);
}).enable();
- var cb2 = new Callback().enable();
+ var cb2 = Callback().enable();
var target = {};
ci.add('eve', cb1, target);
@@ -78,8 +73,8 @@ test('remove self with target during invoking', function () {
test('remove previous during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback().enable();
- var cb2 = new Callback(function () {
+ var cb1 = Callback().enable();
+ var cb2 = Callback(function () {
ci.remove('eve', cb1);
}).enable();
@@ -93,8 +88,8 @@ test('remove previous during invoking', function () {
test('remove previous with target during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback().enable();
- var cb2 = new Callback(function () {
+ var cb1 = Callback().enable();
+ var cb2 = Callback(function () {
ci.remove('eve', cb1, target);
}).enable();
var target = {};
@@ -109,8 +104,8 @@ test('remove previous with target during invoking', function () {
test('remove last during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback().enable();
- var cb2 = new Callback(function () {
+ var cb1 = Callback().enable();
+ var cb2 = Callback(function () {
ci.remove('eve', cb2);
}).enable();
@@ -124,8 +119,8 @@ test('remove last during invoking', function () {
test('remove last with target during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback().enable();
- var cb2 = new Callback(function () {
+ var cb1 = Callback().enable();
+ var cb2 = Callback(function () {
ci.remove('eve', cb2, target);
}).enable();
var target = {};
@@ -139,12 +134,12 @@ test('remove last with target during invoking', function () {
test('remove multiple callbacks during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback().enable();
- var cb2 = new Callback(function () {
+ var cb1 = Callback().enable();
+ var cb2 = Callback(function () {
ci.remove('eve', cb1);
ci.remove('eve', cb3, target);
}).enable();
- var cb3 = new Callback(function () {
+ var cb3 = Callback(function () {
ci.remove('eve', cb2, target);
}).enable();
var target = {};
@@ -166,11 +161,11 @@ test('remove multiple callbacks during invoking', function () {
test('remove all callbacks during invoking', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback().enable();
- var cb2 = new Callback(function () {
+ var cb1 = Callback().enable();
+ var cb2 = Callback(function () {
ci.removeAll('eve');
}).enable();
- var cb3 = new Callback(function () {
+ var cb3 = Callback(function () {
ci.remove('eve', cb2, target);
}).enable();
var target = {};
@@ -183,7 +178,7 @@ test('remove all callbacks during invoking', function () {
cb1.expect(2, 'first callback should be invoked twice');
cb2.expect(1, 'second callback should be invoked once');
- cb3.expect(2, 'third callback should be invoked twice');
+ cb3.expect(0, 'third callback should never invoked');
strictEqual(ci.has('eve'), false, 'All callbacks should be removed');
});
@@ -239,8 +234,8 @@ test('CallbacksInvoker support target', function () {
this.count++;
};
cb1.count = 0;
- var cb2 = new Callback();
- var cb3 = new Callback();
+ var cb2 = Callback();
+ var cb3 = Callback();
var target1 = {
name: 'CallbackTarget1',
@@ -252,7 +247,7 @@ test('CallbacksInvoker support target', function () {
};
ci.add('a', cb1);
- strictEqual(ci.add('a', cb1, target1), false, 'can add callback with target');
+ ci.add('a', cb1, target1);
ci.add('a', cb1);
ci.add('a', cb1, target2);
ci.add('a', cb1, target2);
@@ -260,8 +255,8 @@ test('CallbacksInvoker support target', function () {
ci.add('a', cb2, target1);
ci.add('a', cb3);
ci.add('a', cb3, target1);
+ ci.add('b', cb1, target1);
- strictEqual(ci.add('b', cb1, target1), true, 'can add callback with target for new event key');
strictEqual(ci.has('a', cb2), false, '`has` should return false if the callback without target not exists');
strictEqual(ci.has('a', cb2, target1), true, '`has` should return true if the callback with correct target exists');
strictEqual(ci.has('a', cb3), true, '`has` should return true if the callback without target exists');
@@ -304,9 +299,9 @@ test('CallbacksInvoker support target', function () {
test('CallbacksInvoker remove target', function () {
var ci = new cc._Test.CallbacksInvoker();
- var cb1 = new Callback();
- var cb2 = new Callback();
- var cb3 = new Callback();
+ var cb1 = Callback();
+ var cb2 = Callback();
+ var cb3 = Callback();
var target1 = {
name: 'CallbackTarget1',
@@ -358,3 +353,99 @@ test('CallbacksInvoker remove target', function () {
cb2.expect(0, 'removed one, callback2 should not be called');
cb3.expect(1, 'callback3 should be called once');
});
+
+test('event type conflict with object prototype', function () {
+ var ci = new cc._Test.CallbacksInvoker();
+ var cb1 = Callback(function () {
+ ci.remove('toString', cb1);
+ }).enable();
+ var cb2 = Callback().enable();
+
+ ci.add('toString', cb1);
+ ci.add('toString', cb2);
+ ci.invoke('toString');
+ cb2.once('it should be called correctly if previous callback deregistered itself');
+
+ cb1.disable('should not call after removed');
+ ci.invoke('toString');
+});
+
+test('nest invoke', function () {
+ var ci = new cc._Test.CallbacksInvoker();
+
+ var actualSequence = [];
+ var isParentLoop = true;
+
+ var cb1 = function () {
+ actualSequence.push(cb1);
+ if (isParentLoop) {
+ isParentLoop = false;
+ ci.invoke('visit');
+ }
+ };
+ var cb2 = function () {
+ actualSequence.push(cb2);
+ };
+
+ ci.add('visit', cb1);
+ ci.add('visit', cb2);
+ ci.invoke('visit');
+
+ deepEqual(actualSequence, [cb1, cb1, cb2, cb2], 'support nest invoke');
+});
+
+test('remove during nest invoke', function () {
+ var ci = new cc._Test.CallbacksInvoker();
+
+ var actualSequence = [];
+ var isParentLoop = true;
+
+ var cb1 = function () {
+ actualSequence.push(cb1);
+ if (isParentLoop) {
+ isParentLoop = false;
+ ci.invoke('visit');
+ }
+ else {
+ ci.remove('visit', cb1);
+ strictEqual(ci.has('visit'), false, 'all callbacks removed');
+ }
+ };
+
+ ci.add('visit', cb1);
+ ci.invoke('visit');
+
+ deepEqual(actualSequence, [cb1, cb1], 'invoke sequence');
+});
+
+test('remove many during nest invoke', function () {
+ var ci = new cc._Test.CallbacksInvoker();
+
+ var actualSequence = [];
+ var isParentLoop = true;
+
+ var cb1 = function () {
+ actualSequence.push(cb1);
+ };
+ var cb2 = function () {
+ actualSequence.push(cb2);
+ if (isParentLoop) {
+ isParentLoop = false;
+ ci.invoke('visit');
+ }
+ else {
+ ci.remove('visit', cb1);
+ ci.remove('visit', cb2);
+ }
+ };
+ var cb3 = function () {
+ actualSequence.push(cb3);
+ };
+
+ ci.add('visit', cb1);
+ ci.add('visit', cb2);
+ ci.add('visit', cb3);
+ ci.invoke('visit');
+
+ deepEqual(actualSequence, [cb1, cb2, cb1, cb2, cb3, cb3], 'invoke sequence');
+});