Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Unified Diff: src/object-observe.js

Issue 270763003: Object.observe: avoid accessing acceptList properties more than once (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/object-observe.js
diff --git a/src/object-observe.js b/src/object-observe.js
index 532b0d25254c28dfee157e61da3e55c175861002..f4b1f539634a58f583265695ed59e92c72f26f51 100644
--- a/src/object-observe.js
+++ b/src/object-observe.js
@@ -127,9 +127,9 @@ function TypeMapRemoveType(typeMap, type) {
typeMap[type]--;
}
-function TypeMapCreateFromList(typeList) {
+function TypeMapCreateFromList(typeList, length) {
var typeMap = TypeMapCreate();
- for (var i = 0; i < typeList.length; i++) {
+ for (var i = 0; i < length; i++) {
TypeMapAddType(typeMap, typeList[i], true);
}
return typeMap;
@@ -151,14 +151,17 @@ function TypeMapIsDisjointFrom(typeMap1, typeMap2) {
return true;
}
-var defaultAcceptTypes = TypeMapCreateFromList([
- 'add',
- 'update',
- 'delete',
- 'setPrototype',
- 'reconfigure',
- 'preventExtensions'
-]);
+var defaultAcceptTypes = (function() {
+ var defaultTypes = [
+ 'add',
+ 'update',
+ 'delete',
+ 'setPrototype',
+ 'reconfigure',
+ 'preventExtensions'
+ ];
+ return TypeMapCreateFromList(defaultTypes, defaultTypes.length);
+})();
// An Observer is a registration to observe an object by a callback with
// a given set of accept types. If the set of accept types is the default
@@ -170,7 +173,7 @@ function ObserverCreate(callback, acceptList) {
return callback;
var observer = nullProtoObject();
observer.callback = callback;
- observer.accept = TypeMapCreateFromList(acceptList);
+ observer.accept = acceptList;
return observer;
}
@@ -306,16 +309,18 @@ function ObjectInfoGetPerformingTypes(objectInfo) {
return objectInfo.performingCount > 0 ? objectInfo.performing : null;
}
-function AcceptArgIsValid(arg) {
+function ConvertAcceptListToTypeMap(arg) {
+ // We use undefined as a sentinel for the default accept list.
if (IS_UNDEFINED(arg))
- return true;
+ return arg;
- if (!IS_SPEC_OBJECT(arg) ||
- !IS_NUMBER(arg.length) ||
- arg.length < 0)
- return false;
+ if (!IS_SPEC_OBJECT(arg))
+ throw MakeTypeError("observe_accept_invalid");
- return true;
+ var len = ToInteger(arg.length);
+ if (len < 0) len = 0;
+
+ return TypeMapCreateFromList(arg, len);
}
// CallbackInfo's optimized state is just a number which represents its global
@@ -362,15 +367,14 @@ function ObjectObserve(object, callback, acceptList) {
throw MakeTypeError("observe_non_function", ["observe"]);
if (ObjectIsFrozen(callback))
throw MakeTypeError("observe_callback_frozen");
- if (!AcceptArgIsValid(acceptList))
- throw MakeTypeError("observe_accept_invalid");
return %ObjectObserveInObjectContext(object, callback, acceptList);
}
function NativeObjectObserve(object, callback, acceptList) {
var objectInfo = ObjectInfoGetOrCreate(object);
- ObjectInfoAddObserver(objectInfo, callback, acceptList);
+ var typeList = ConvertAcceptListToTypeMap(acceptList);
+ ObjectInfoAddObserver(objectInfo, callback, typeList);
return object;
}
« no previous file with comments | « no previous file | src/runtime.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698