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

Side by Side Diff: src/object-observe.js

Issue 11225058: Initial JS stub implementation of Object.observe. Adds support for .object/.unobserve/.notify/.deli… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: install functions on Object, not Object.prototype Created 8 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 "use strict";
29
30 var InternalObjectIsFrozen = $Object.isFrozen;
31 var InternalObjectFreeze = $Object.freeze;
32
33 var InternalWeakMapProto = {
34 __proto__: null,
35 set: $WeakMap.prototype.set,
36 get: $WeakMap.prototype.get,
37 has: $WeakMap.prototype.has
38 }
arv (Not doing code reviews) 2012/10/25 20:43:44 semicolon
39
40 function createInternalWeakMap() {
41 var map = new $WeakMap;
42 map.__proto__ = InternalWeakMapProto;
43 return map;
44 }
45
46 var observerInfoMap = createInternalWeakMap();
47 var objectInfoMap = createInternalWeakMap();
48
49 function ObjectObserve(object, callback) {
50 if (!IS_SPEC_OBJECT(object))
51 throw MakeTypeError("observe_non_object", ["observe"]);
52 if (!IS_SPEC_FUNCTION(callback))
53 throw MakeTypeError("observe_non_function", ["observe"]);
54 if (InternalObjectIsFrozen(callback))
55 throw MakeTypeError("observe_callback_frozen");
56
57 if (!observerInfoMap.has(callback)) {
58 // TODO: setup observerInfo.priority.
59 observerInfoMap.set(callback, {
60 pendingChangeRecords: null
61 });
62 }
63
64 var objectInfo = objectInfoMap.get(object);
65 if (IS_UNDEFINED(objectInfo)) {
66 // TODO: setup objectInfo.notifier
67 objectInfo = {
68 changeObservers: new InternalArray(callback)
69 };
70 objectInfoMap.set(object, objectInfo);
71 return;
72 }
73
74 var changeObservers = objectInfo.changeObservers;
75 if (changeObservers.indexOf(callback) >= 0)
76 return;
77
78 changeObservers.push(callback);
79 }
80
81 function ObjectUnobserve(object, callback) {
82 if (!IS_SPEC_OBJECT(object))
83 throw MakeTypeError("observe_non_object", ["unobserve"]);
84
85 var objectInfo = objectInfoMap.get(object);
86 if (IS_UNDEFINED(objectInfo))
87 return;
88
89 var changeObservers = objectInfo.changeObservers;
90 var index = changeObservers.indexOf(callback);
91 if (index < 0)
92 return;
93
94 changeObservers.splice(index, 1);
arv (Not doing code reviews) 2012/10/25 20:43:44 This can be a Set. Sets are ordered and we will ge
rafaelw 2012/10/26 05:34:14 Unfortunately, sets don't have iteration yet. On
95 }
96
97 function EnqueueChangeRecord(changeRecord, observers) {
98 for (var i = 0; i < observers.length; i++) {
99 var observer = observers[i];
100 var observerInfo = observerInfoMap.get(observer);
101
102 // TODO: "activate" the observer
103
104 if (IS_NULL(observerInfo.pendingChangeRecords)) {
105 observerInfo.pendingChangeRecords = new InternalArray(changeRecord);
106 } else {
107 observerInfo.pendingChangeRecords.push(changeRecord);
108 }
109 }
110 }
111
112 function ObjectNotify(object, changeRecord) {
113 // TODO: notifier needs to be [[THIS]]
114 if (!IS_STRING(changeRecord.type))
115 throw MakeTypeError("observe_type_non_string");
116
117 var objectInfo = objectInfoMap.get(object);
118 if (IS_UNDEFINED(objectInfo))
119 return;
120
121 var newRecord = {
122 object: object // TODO: Needs to be 'object' retreived from notifier
123 };
124 for (var prop in changeRecord) {
125 if (prop === 'object')
126 continue;
127 newRecord[prop] = changeRecord[prop];
128 }
129 InternalObjectFreeze(newRecord);
130
131 EnqueueChangeRecord(newRecord, objectInfo.changeObservers);
132 }
133
134 function ObjectDeliverChangeRecords(callback) {
135 if (!IS_SPEC_FUNCTION(callback))
136 throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]);
137
138 var observerInfo = observerInfoMap.get(callback);
139 if (IS_UNDEFINED(observerInfo))
140 return;
141
142 var pendingChangeRecords = observerInfo.pendingChangeRecords;
143 if (IS_NULL(pendingChangeRecords))
144 return;
145
146 observerInfo.pendingChangeRecords = null;
147 var delivered = [];
148 %MoveArrayContents(pendingChangeRecords, delivered);
149 try {
150 %Call(void 0, delivered, callback);
151 } catch (ex) {}
152 }
153
154 function SetupObjectObserve() {
155 %CheckIsBootstrapping();
156 InstallFunctions($Object, DONT_ENUM, $Array(
157 "deliverChangeRecords", ObjectDeliverChangeRecords,
158 "notify", ObjectNotify, // TODO: Remove when getNotifier is implemented.
159 "observe", ObjectObserve,
160 "unobserve", ObjectUnobserve
161 ));
162 }
163
164 SetupObjectObserve();
OLDNEW
« src/messages.js ('K') | « src/messages.js ('k') | test/mjsunit/harmony/object-observe.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698