Skip to content

Loading…

Add Map#clear and WeakMap#clear #4

Closed
wants to merge 1 commit into from

2 participants

@eriwen

Simple impl and tests for Map#clear, according to current (10/26/12) spec draft.

@eriwen

Nevermind, should have left it commented out as this won't work in FF :/

@eriwen eriwen closed this
@WebReflection

hey, nice ... with tests too. What is wrong with FF?

@eriwen

Tests don't pass in FF16, when I ran them. Weird because Object.isExtensible(Map) returns true. Maybe you could verify that?

@WebReflection

right ... reason is: FF 16 does not expose clear, neither iterate, nether keys ... neither is possible to use for/of or for/in so I believe it's not possible to implement clear over native FF 16 constructors :-/

@eriwen

I am working on a destructive "shim" to get things working in Firefox until they implement more. Thanks for your work on this project; it's been educational :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 12, 2012
  1. @eriwen
Showing with 57 additions and 9 deletions.
  1. +13 −4 build/es6-collections.js
  2. +1 −1 build/es6-collections.min.js
  3. +13 −4 src/es6-collections.js
  4. +30 −0 tests/es6-collections.js
View
17 build/es6-collections.js
@@ -32,7 +32,9 @@
// WeakMap#has(key:void*):boolean
has: {value: bind.call(sharedHas, NULL, TRUE, keys, values)},
// WeakMap#set(key:void*, value:void*):void
- set: {value: bind.call(sharedSet, NULL, TRUE, keys, values)}
+ set: {value: bind.call(sharedSet, NULL, TRUE, keys, values)},
+ // Map#clear(void):void*
+ clear: {value: bind.call(sharedClear, NULL, FALSE, keys, values)}
});
}
@@ -57,10 +59,13 @@
// Map#has(key:void*):boolean
has: {value: bind.call(sharedHas, NULL, FALSE, keys, values)},
// Map#set(key:void*, value:void*):void
- set: {value: bind.call(sharedSet, NULL, FALSE, keys, values)}
+ set: {value: bind.call(sharedSet, NULL, FALSE, keys, values)},
+ // Map#clear(void):void*
+ clear: {value: bind.call(sharedClear, NULL, FALSE, keys, values)}
/*,
- // Map#size(void):number === Mozilla only so far
- size: {value: bind.call(sharedSize, NULL, keys)},
+ //:was Map#size(void):number - Firefox 16 does this
+ // Map.size:number
+ size: {get: bind.call(sharedSize, NULL, keys)},
// Map#keys(void):Array === not in specs
keys: {value: boundSlice(keys)},
// Map#values(void):Array === not in specs
@@ -124,6 +129,10 @@
return -1 < i;
}
+ function sharedClear(objectOnly, keys, values) {
+ keys.length = values.length = 0;
+ }
+
function sharedGet(objectOnly, keys, values, key/*, d3fault*/) {
return sharedHas(objectOnly, keys, values, key) ? values[i] : undefined; //d3fault;
}
View
2 build/es6-collections.min.js
@@ -1,4 +1,4 @@
/*!
(C) @WebReflection - Mit Style License
*/
-"WeakMap" in this||(function(d){function v(){var D=[],i=[];return q(t,{"delete":{value:C.call(m,A,l,D,i)},get:{value:C.call(y,A,l,D,i)},has:{value:C.call(c,A,l,D,i)},set:{value:C.call(r,A,l,D,i)}})}function f(){var D=[],i=[];return q(e,{"delete":{value:C.call(m,A,u,D,i)},get:{value:C.call(y,A,u,D,i)},has:{value:C.call(c,A,u,D,i)},set:{value:C.call(r,A,u,D,i)}})}function x(){var E=[],i=[],D=C.call(c,A,u,i,E);return q(B,{"delete":{value:C.call(m,A,u,i,E)},has:{value:D},add:{value:C.call(k,A,u,D,i)}})}function m(F,E,i,D){if(c(F,E,i,D)){E.splice(w,1);i.splice(w,1)}return -1<w}function y(F,E,i,D){return c(F,E,i,D)?i[w]:h}function c(F,E,i,D){if(F&&D!==z(D)){throw new TypeError("not a non-null object")}w=a.call(E,D);return -1<w}function r(G,E,i,D,F){c(G,E,i,D)?i[w]=F:i[E.push(D)-1]=F}function k(F,D,i,E){(!D(E)&&!!i.push(E))}function a(i){if(i!=i||i===0){for(w=this.length;w--&&!j(this[w],i);){}}else{w=b.call(this,i)}return w}function p(){}var A=null,l=true,u=false,o=d=="undefined",n=o?this:global,d=o?{}:exports,z=n.Object,t=v.prototype,e=f.prototype,B=x.prototype,g=z.defineProperty,s=[].slice,j=z.is||function(D,i){return D===i?D!==0||1/D==1/i:D!=D&&i!=i},C=v.bind||function C(D,H,F,i){var G=this;return function E(I,J){return G.call(D,H,F,i,I,J)}},q=z.create||function q(E,F){p.prototype=E;var i=new p,D;for(D in F){i[D]=F[D].value}return i},b=[].indexOf||function b(i){for(w=this.length;w--&&this[w]!==i;){}return w},h,w;v.prototype=t=v();f.prototype=e=f();x.prototype=B=x();n.WeakMap=d.WeakMap=n.WeakMap||v;n.Map=d.Map=n.Map||f;n.Set=d.Set=n.Set||x}.call(this,typeof exports));
+"WeakMap" in this||(function(d){function w(){var E=[],i=[];return r(u,{"delete":{value:D.call(n,B,m,E,i)},get:{value:D.call(z,B,m,E,i)},has:{value:D.call(c,B,m,E,i)},set:{value:D.call(s,B,m,E,i)},clear:{value:D.call(f,B,v,E,i)}})}function g(){var E=[],i=[];return r(e,{"delete":{value:D.call(n,B,v,E,i)},get:{value:D.call(z,B,v,E,i)},has:{value:D.call(c,B,v,E,i)},set:{value:D.call(s,B,v,E,i)},clear:{value:D.call(f,B,v,E,i)}})}function y(){var F=[],i=[],E=D.call(c,B,v,i,F);return r(C,{"delete":{value:D.call(n,B,v,i,F)},has:{value:E},add:{value:D.call(l,B,v,E,i)}})}function n(G,F,i,E){if(c(G,F,i,E)){F.splice(x,1);i.splice(x,1)}return -1<x}function f(F,E,i){E.length=i.length=0}function z(G,F,i,E){return c(G,F,i,E)?i[x]:j}function c(G,F,i,E){if(G&&E!==A(E)){throw new TypeError("not a non-null object")}x=a.call(F,E);return -1<x}function s(H,F,i,E,G){c(H,F,i,E)?i[x]=G:i[F.push(E)-1]=G}function l(G,E,i,F){(!E(F)&&!!i.push(F))}function a(i){if(i!=i||i===0){for(x=this.length;x--&&!k(this[x],i);){}}else{x=b.call(this,i)}return x}function q(){}var B=null,m=true,v=false,p=d=="undefined",o=p?this:global,d=p?{}:exports,A=o.Object,u=w.prototype,e=g.prototype,C=y.prototype,h=A.defineProperty,t=[].slice,k=A.is||function(E,i){return E===i?E!==0||1/E==1/i:E!=E&&i!=i},D=w.bind||function D(E,I,G,i){var H=this;return function F(J,K){return H.call(E,I,G,i,J,K)}},r=A.create||function r(F,G){q.prototype=F;var i=new q,E;for(E in G){i[E]=G[E].value}return i},b=[].indexOf||function b(i){for(x=this.length;x--&&this[x]!==i;){}return x},j,x;w.prototype=u=w();g.prototype=e=g();y.prototype=C=y();o.WeakMap=d.WeakMap=o.WeakMap||w;o.Map=d.Map=o.Map||g;o.Set=d.Set=o.Set||y}.call(this,typeof exports));
View
17 src/es6-collections.js
@@ -28,7 +28,9 @@
// WeakMap#has(key:void*):boolean
has: {value: bind.call(sharedHas, NULL, TRUE, keys, values)},
// WeakMap#set(key:void*, value:void*):void
- set: {value: bind.call(sharedSet, NULL, TRUE, keys, values)}
+ set: {value: bind.call(sharedSet, NULL, TRUE, keys, values)},
+ // Map#clear(void):void*
+ clear: {value: bind.call(sharedClear, NULL, FALSE, keys, values)}
});
}
@@ -53,10 +55,13 @@
// Map#has(key:void*):boolean
has: {value: bind.call(sharedHas, NULL, FALSE, keys, values)},
// Map#set(key:void*, value:void*):void
- set: {value: bind.call(sharedSet, NULL, FALSE, keys, values)}
+ set: {value: bind.call(sharedSet, NULL, FALSE, keys, values)},
+ // Map#clear(void):void*
+ clear: {value: bind.call(sharedClear, NULL, FALSE, keys, values)}
/*,
- // Map#size(void):number === Mozilla only so far
- size: {value: bind.call(sharedSize, NULL, keys)},
+ //:was Map#size(void):number - Firefox 16 does this
+ // Map.size:number
+ size: {get: bind.call(sharedSize, NULL, keys)},
// Map#keys(void):Array === not in specs
keys: {value: boundSlice(keys)},
// Map#values(void):Array === not in specs
@@ -120,6 +125,10 @@
return -1 < i;
}
+ function sharedClear(objectOnly, keys, values) {
+ keys.length = values.length = 0;
+ }
+
function sharedGet(objectOnly, keys, values, key/*, d3fault*/) {
return sharedHas(objectOnly, keys, values, key) ? values[i] : undefined; //d3fault;
}
View
30 tests/es6-collections.js
@@ -78,6 +78,21 @@ function wru(wru){
assert("if found and removed returns true", o["delete"](o));
}
}, {
+ name: "WeakMap#clear",
+ test: function () {
+ var
+ o = WeakMap(),
+ generic = {},
+ callback = function () {}
+ ;
+ o.set(callback, generic);
+ o.set(generic, callback);
+ o.set(o, callback);
+ assert("has all of them", o.has(callback) && o.has(generic) && o.has(o));
+ o.clear();
+ assert("has none of them", !o.has(callback) && !o.has(generic) && !o.has(o));
+ }
+ }, {
name: "non object key throws an error",
test: function () {
var o = WeakMap();
@@ -193,6 +208,21 @@ function wru(wru){
assert("if found and removed returns true", o["delete"](o));
}
}, {
+ name: "Map#clear",
+ test: function () {
+ var
+ o = Map(),
+ generic = {},
+ callback = function () {}
+ ;
+ o.set(callback, generic);
+ o.set(generic, callback);
+ o.set(o, callback);
+ assert("has all of them", o.has(callback) && o.has(generic) && o.has(o));
+ o.clear();
+ assert("has none of them", !o.has(callback) && !o.has(generic) && !o.has(o));
+ }
+ }, {
name: "non object key does not throw an error",
test: function () {
var o = Map();
Something went wrong with that request. Please try again.