Bug 142408 - [ES6] Implement WeakSet
Summary: [ES6] Implement WeakSet
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Yusuke Suzuki
URL:
Keywords:
Depends on: 142348 142556
Blocks: 143919
  Show dependency treegraph
 
Reported: 2015-03-06 13:13 PST by Yusuke Suzuki
Modified: 2015-04-19 11:09 PDT (History)
3 users (show)

See Also:


Attachments
Patch (55.34 KB, patch)
2015-04-18 18:10 PDT, Yusuke Suzuki
darin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yusuke Suzuki 2015-03-06 13:13:59 PST
During upgrade of Map/Set/WeakMap constructor interface, I've found that WeakSet implementation is missing.
Comment 1 Yusuke Suzuki 2015-03-10 19:04:11 PDT
Since MapData change becomes a little bit large, at first, I'll create the separate patch to integrate MapData into JSMap / JSSet.
https://bugs.webkit.org/show_bug.cgi?id=142556
Comment 2 Yusuke Suzuki 2015-03-14 00:49:58 PDT
OK, MapData is done.
So I'll perform the same thing on WeakMapData :D
Comment 3 Yusuke Suzuki 2015-04-18 11:49:02 PDT
At first, implement ES6 WeakSet and after that, optimize WeakMapData.
Comment 4 Yusuke Suzuki 2015-04-18 11:49:28 PDT
Since WeakMap / WeakSet doesn't have iterators, optimization is a little bit easier than MapData.
Comment 5 Yusuke Suzuki 2015-04-18 18:10:57 PDT
Created attachment 251103 [details]
Patch
Comment 6 Darin Adler 2015-04-19 09:23:39 PDT
Comment on attachment 251103 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=251103&action=review

> Source/JavaScriptCore/runtime/JSWeakSet.cpp:2
> + * Copyright (C) 2013 Apple, Inc. All rights reserved.

Copyright is based on date of publication (the time we publish in the open source repository). So it should be 2015 unless this code was published in 2013.

> Source/JavaScriptCore/runtime/JSWeakSet.h:2
> + * Copyright (C) 2013 Apple, Inc. All rights reserved.

Same issue with copyright date.

> Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:88
>      if (!key.isObject())
> -        return JSValue::encode(throwTypeError(callFrame, WTF::ASCIILiteral("A WeakMap cannot have a non-object key")));
> +        return JSValue::encode(jsBoolean(false));
>      return JSValue::encode(jsBoolean(map->remove(asObject(key))));

Now that this is always a boolean, I think we can write it more cleanly:

    return JSValue::encode(jsBoolean(key.isObject() && map->remove(asObject(key)));

No need for the three-line version with the separate early return.

> Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:98
> +        return JSValue::encode(jsUndefined());

Should this be undefined, or null? The change log comment mentions only "false".

> Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:110
>      if (!key.isObject())
> -        return JSValue::encode(throwTypeError(callFrame, WTF::ASCIILiteral("A WeakMap cannot have a non-object key")));
> +        return JSValue::encode(jsBoolean(false));
>      return JSValue::encode(jsBoolean(map->contains(asObject(key))));

Now that this is always a boolean, I think we can write it more cleanly:

    return JSValue::encode(jsBoolean(key.isObject() && map->contains(asObject(key)));

No need for the three-line version with the separate early return.

> Source/JavaScriptCore/runtime/WeakSetConstructor.cpp:2
> + * Copyright (C) 2013 Apple, Inc. All rights reserved.

Same issue with copyright.

> Source/JavaScriptCore/runtime/WeakSetConstructor.h:2
> + * Copyright (C) 2013 Apple, Inc. All rights reserved.

Same issue with copyright.

> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:2
> + * Copyright (C) 2013 Apple, Inc. All rights reserved.

Same issue with copyright.

> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:57
> +        return 0;

nullptr

> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:64
> +    return 0;

nullptr

> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:75
> +    JSValue key = callFrame->argument(0);
> +    if (!key.isObject())
> +        return JSValue::encode(jsBoolean(false));
> +    return JSValue::encode(jsBoolean(map->remove(asObject(key))));

Now that this is always a boolean, I think we can write it more cleanly:

    return JSValue::encode(jsBoolean(key.isObject() && map->remove(asObject(key)));

No need for the three-line version with the separate early return.

> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:86
> +    if (!key.isObject())
> +        return JSValue::encode(jsBoolean(false));
> +    return JSValue::encode(jsBoolean(map->contains(asObject(key))));

Now that this is always a boolean, I think we can write it more cleanly:

    return JSValue::encode(jsBoolean(key.isObject() && map->contains(asObject(key)));

No need for the three-line version with the separate early return.

> Source/JavaScriptCore/runtime/WeakSetPrototype.h:2
> + * Copyright (C) 2013 Apple, Inc. All rights reserved.

Same issue with copyright date.
Comment 7 Yusuke Suzuki 2015-04-19 10:46:00 PDT
Comment on attachment 251103 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=251103&action=review

Thank you for your review and detailed comments!

>> Source/JavaScriptCore/runtime/JSWeakSet.cpp:2
>> + * Copyright (C) 2013 Apple, Inc. All rights reserved.
> 
> Copyright is based on date of publication (the time we publish in the open source repository). So it should be 2015 unless this code was published in 2013.

Thanks! I'll fix it.

>> Source/JavaScriptCore/runtime/JSWeakSet.h:2
>> + * Copyright (C) 2013 Apple, Inc. All rights reserved.
> 
> Same issue with copyright date.

Done

>> Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:88
>>      return JSValue::encode(jsBoolean(map->remove(asObject(key))));
> 
> Now that this is always a boolean, I think we can write it more cleanly:
> 
>     return JSValue::encode(jsBoolean(key.isObject() && map->remove(asObject(key)));
> 
> No need for the three-line version with the separate early return.

OK. I'll follow it :)

>> Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:98
>> +        return JSValue::encode(jsUndefined());
> 
> Should this be undefined, or null? The change log comment mentions only "false".

Oops! Thank you. The code is correct. We need to fix change log comment.
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap.prototype.get

>> Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:110
>>      return JSValue::encode(jsBoolean(map->contains(asObject(key))));
> 
> Now that this is always a boolean, I think we can write it more cleanly:
> 
>     return JSValue::encode(jsBoolean(key.isObject() && map->contains(asObject(key)));
> 
> No need for the three-line version with the separate early return.

Done.

>> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:57
>> +        return 0;
> 
> nullptr

Nice! I also fixed them in WeakMapPrototype.cpp.

>> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:64
>> +    return 0;
> 
> nullptr

Done.

>> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:75
>> +    return JSValue::encode(jsBoolean(map->remove(asObject(key))));
> 
> Now that this is always a boolean, I think we can write it more cleanly:
> 
>     return JSValue::encode(jsBoolean(key.isObject() && map->remove(asObject(key)));
> 
> No need for the three-line version with the separate early return.

Done.

>> Source/JavaScriptCore/runtime/WeakSetPrototype.cpp:86
>> +    return JSValue::encode(jsBoolean(map->contains(asObject(key))));
> 
> Now that this is always a boolean, I think we can write it more cleanly:
> 
>     return JSValue::encode(jsBoolean(key.isObject() && map->contains(asObject(key)));
> 
> No need for the three-line version with the separate early return.

Done.
Comment 8 Yusuke Suzuki 2015-04-19 11:09:23 PDT
Committed r182994: <http://trac.webkit.org/changeset/182994>