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

Side by Side Diff: test/mjsunit/harmony/proxies-example-membrane.js

Issue 1417063011: [runtime] support new Proxy() instead of Proxy.create and install getPrototypeOf trap (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: removing unreachable code Created 5 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
« no previous file with comments | « test/mjsunit/harmony/proxies.js ('k') | test/mjsunit/harmony/proxies-for.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 function wrapCall2(fun, that, args) { 148 function wrapCall2(fun, that, args) {
149 if (!enabled) { throw new Error("disabled"); } 149 if (!enabled) { throw new Error("disabled"); }
150 try { 150 try {
151 return wrap(fun.apply(that, Array.prototype.map.call(args, wrap))); 151 return wrap(fun.apply(that, Array.prototype.map.call(args, wrap)));
152 } catch (e) { 152 } catch (e) {
153 throw wrap(e); 153 throw wrap(e);
154 } 154 }
155 } 155 }
156 156
157 var baseHandler = createHandler(obj); 157 var baseHandler = createHandler(obj);
158 var handler = Proxy.create(Object.freeze({ 158 var handler = new Proxy({}, Object.freeze({
159 get: function(receiver, name) { 159 get: function(receiver, name) {
160 return function() { 160 return function() {
161 var arg = (name === "get" || name == "set") ? arguments[1] : ""; 161 var arg = (name === "get" || name == "set") ? arguments[1] : "";
162 print("handler enter", name, arg); 162 print("handler enter", name, arg);
163 var x = wrapCall(baseHandler[name], baseHandler, arguments); 163 var x = wrapCall(baseHandler[name], baseHandler, arguments);
164 print("handler exit", name, arg, "returning", str(x)); 164 print("handler exit", name, arg, "returning", str(x));
165 return x; 165 return x;
166 } 166 }
167 } 167 }
168 })); 168 }));
(...skipping 12 matching lines...) Expand all
181 try { 181 try {
182 function forward(args) { return obj.apply(this, args) } 182 function forward(args) { return obj.apply(this, args) }
183 return wrap(new forward(Array.prototype.map.call(arguments, wrap))); 183 return wrap(new forward(Array.prototype.map.call(arguments, wrap)));
184 } catch (e) { 184 } catch (e) {
185 throw wrap(e); 185 throw wrap(e);
186 } 186 }
187 } 187 }
188 return Proxy.createFunction(handler, callTrap, constructTrap); 188 return Proxy.createFunction(handler, callTrap, constructTrap);
189 } else { 189 } else {
190 var prototype = wrap(Object.getPrototypeOf(obj)); 190 var prototype = wrap(Object.getPrototypeOf(obj));
191 return Proxy.create(handler, prototype); 191 return new Proxy(prototype, handler);
192 } 192 }
193 } 193 }
194 194
195 var gate = Object.freeze({ 195 var gate = Object.freeze({
196 enable: function() { enabled = true; }, 196 enable: function() { enabled = true; },
197 disable: function() { enabled = false; } 197 disable: function() { enabled = false; }
198 }); 198 });
199 199
200 return Object.freeze({ 200 return Object.freeze({
201 wrapper: wrap(target), 201 wrapper: wrap(target),
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 function asDry2(wet) { 304 function asDry2(wet) {
305 if (wet !== Object(wet)) { 305 if (wet !== Object(wet)) {
306 // primitives provide only irrevocable knowledge, so don't 306 // primitives provide only irrevocable knowledge, so don't
307 // bother wrapping it. 307 // bother wrapping it.
308 return wet; 308 return wet;
309 } 309 }
310 var dryResult = wet2dry.get(wet); 310 var dryResult = wet2dry.get(wet);
311 if (dryResult) { return dryResult; } 311 if (dryResult) { return dryResult; }
312 312
313 var wetHandler = createHandler(wet); 313 var wetHandler = createHandler(wet);
314 var dryRevokeHandler = Proxy.create(Object.freeze({ 314 var dryRevokeHandler = new Proxy({}, Object.freeze({
315 get: function(receiver, name) { 315 get: function(receiver, name) {
316 return function() { 316 return function() {
317 var arg = (name === "get" || name == "set") ? arguments[1] : ""; 317 var arg = (name === "get" || name == "set") ? arguments[1] : "";
318 print("dry handler enter", name, arg); 318 print("dry handler enter", name, arg);
319 var optWetHandler = dry2wet.get(dryRevokeHandler); 319 var optWetHandler = dry2wet.get(dryRevokeHandler);
320 try { 320 try {
321 var x = asDry(optWetHandler[name].apply( 321 var x = asDry(optWetHandler[name].apply(
322 optWetHandler, Array.prototype.map.call(arguments, asWet))); 322 optWetHandler, Array.prototype.map.call(arguments, asWet)));
323 print("dry handler exit", name, arg, "returning", str(x)); 323 print("dry handler exit", name, arg, "returning", str(x));
324 return x; 324 return x;
(...skipping 16 matching lines...) Expand all
341 return x; 341 return x;
342 } 342 }
343 function constructTrap() { 343 function constructTrap() {
344 function forward(args) { return wet.apply(this, args) } 344 function forward(args) { return wet.apply(this, args) }
345 return asDry(new forward(Array.prototype.map.call(arguments, asWet))); 345 return asDry(new forward(Array.prototype.map.call(arguments, asWet)));
346 } 346 }
347 dryResult = 347 dryResult =
348 Proxy.createFunction(dryRevokeHandler, callTrap, constructTrap); 348 Proxy.createFunction(dryRevokeHandler, callTrap, constructTrap);
349 } else { 349 } else {
350 dryResult = 350 dryResult =
351 Proxy.create(dryRevokeHandler, asDry(Object.getPrototypeOf(wet))); 351 new Proxy(asDry(Object.getPrototypeOf(wet)), dryRevokeHandler);
352 } 352 }
353 wet2dry.set(wet, dryResult); 353 wet2dry.set(wet, dryResult);
354 dry2wet.set(dryResult, wet); 354 dry2wet.set(dryResult, wet);
355 return dryResult; 355 return dryResult;
356 } 356 }
357 357
358 function asWet(obj) { 358 function asWet(obj) {
359 registerObject(obj) 359 registerObject(obj)
360 print("asWet enter", str(obj)) 360 print("asWet enter", str(obj))
361 try { 361 try {
362 var x = asWet2(obj) 362 var x = asWet2(obj)
363 registerObject(x, "wet") 363 registerObject(x, "wet")
364 print("asWet exit", str(obj), "as", str(x)) 364 print("asWet exit", str(obj), "as", str(x))
365 return x 365 return x
366 } catch(e) { 366 } catch(e) {
367 print("asWet exception", str(e)) 367 print("asWet exception", str(e))
368 throw e 368 throw e
369 } 369 }
370 } 370 }
371 function asWet2(dry) { 371 function asWet2(dry) {
372 if (dry !== Object(dry)) { 372 if (dry !== Object(dry)) {
373 // primitives provide only irrevocable knowledge, so don't 373 // primitives provide only irrevocable knowledge, so don't
374 // bother wrapping it. 374 // bother wrapping it.
375 return dry; 375 return dry;
376 } 376 }
377 var wetResult = dry2wet.get(dry); 377 var wetResult = dry2wet.get(dry);
378 if (wetResult) { return wetResult; } 378 if (wetResult) { return wetResult; }
379 379
380 var dryHandler = createHandler(dry); 380 var dryHandler = createHandler(dry);
381 var wetRevokeHandler = Proxy.create(Object.freeze({ 381 var wetRevokeHandler = new Proxy({}, Object.freeze({
382 get: function(receiver, name) { 382 get: function(receiver, name) {
383 return function() { 383 return function() {
384 var arg = (name === "get" || name == "set") ? arguments[1] : ""; 384 var arg = (name === "get" || name == "set") ? arguments[1] : "";
385 print("wet handler enter", name, arg); 385 print("wet handler enter", name, arg);
386 var optDryHandler = wet2dry.get(wetRevokeHandler); 386 var optDryHandler = wet2dry.get(wetRevokeHandler);
387 try { 387 try {
388 var x = asWet(optDryHandler[name].apply( 388 var x = asWet(optDryHandler[name].apply(
389 optDryHandler, Array.prototype.map.call(arguments, asDry))); 389 optDryHandler, Array.prototype.map.call(arguments, asDry)));
390 print("wet handler exit", name, arg, "returning", str(x)); 390 print("wet handler exit", name, arg, "returning", str(x));
391 return x; 391 return x;
(...skipping 16 matching lines...) Expand all
408 return x; 408 return x;
409 } 409 }
410 function constructTrap() { 410 function constructTrap() {
411 function forward(args) { return dry.apply(this, args) } 411 function forward(args) { return dry.apply(this, args) }
412 return asWet(new forward(Array.prototype.map.call(arguments, asDry))); 412 return asWet(new forward(Array.prototype.map.call(arguments, asDry)));
413 } 413 }
414 wetResult = 414 wetResult =
415 Proxy.createFunction(wetRevokeHandler, callTrap, constructTrap); 415 Proxy.createFunction(wetRevokeHandler, callTrap, constructTrap);
416 } else { 416 } else {
417 wetResult = 417 wetResult =
418 Proxy.create(wetRevokeHandler, asWet(Object.getPrototypeOf(dry))); 418 new Proxy(asWet(Object.getPrototypeOf(dry)), wetRevokeHandler);
419 } 419 }
420 dry2wet.set(dry, wetResult); 420 dry2wet.set(dry, wetResult);
421 wet2dry.set(wetResult, dry); 421 wet2dry.set(wetResult, dry);
422 return wetResult; 422 return wetResult;
423 } 423 }
424 424
425 var gate = Object.freeze({ 425 var gate = Object.freeze({
426 revoke: function() { 426 revoke: function() {
427 dry2wet = wet2dry = Object.freeze({ 427 dry2wet = wet2dry = Object.freeze({
428 get: function(key) { throw new Error("revoked"); }, 428 get: function(key) { throw new Error("revoked"); },
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 assertThrows(function() { w[1] }, Error) 503 assertThrows(function() { w[1] }, Error)
504 assertThrows(function() { w.c }, Error) 504 assertThrows(function() { w.c }, Error)
505 assertThrows(function() { wb.bb }, Error) 505 assertThrows(function() { wb.bb }, Error)
506 assertEquals(3, wr.a) 506 assertEquals(3, wr.a)
507 assertThrows(function() { wf(4) }, Error) 507 assertThrows(function() { wf(4) }, Error)
508 assertEquals(6, wfx.a) 508 assertEquals(6, wfx.a)
509 assertEquals(7, wgx.aa) 509 assertEquals(7, wgx.aa)
510 assertThrows(function() { wh4.q }, Error) 510 assertThrows(function() { wh4.q }, Error)
511 assertThrows(function() { ws5.x }, Error) 511 assertThrows(function() { ws5.x }, Error)
512 assertThrows(function() { ws5x.y }, Error) 512 assertThrows(function() { ws5x.y }, Error)
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/proxies.js ('k') | test/mjsunit/harmony/proxies-for.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698