Bug 190997 - [WebIDL] Support serializing sequences and FrozenArrays of non-interfaces
Summary: [WebIDL] Support serializing sequences and FrozenArrays of non-interfaces
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Andy Estes
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-10-27 15:39 PDT by Andy Estes
Modified: 2019-02-08 10:18 PST (History)
16 users (show)

See Also:


Attachments
Patch (17.59 KB, patch)
2018-10-27 15:46 PDT, Andy Estes
no flags Details | Formatted Diff | Diff
Patch (17.57 KB, patch)
2018-10-28 05:38 PDT, Andy Estes
no flags Details | Formatted Diff | Diff
Patch (19.86 KB, patch)
2019-02-07 16:04 PST, Andy Estes
no flags Details | Formatted Diff | Diff
Patch (19.85 KB, patch)
2019-02-08 09:41 PST, Andy Estes
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andy Estes 2018-10-27 15:39:05 PDT
[WebIDL] Support serializing sequences and FrozenArrays of non-interfaces
Comment 1 Andy Estes 2018-10-27 15:40:10 PDT
rdar://problem/35983035
Comment 2 Andy Estes 2018-10-27 15:46:10 PDT
Created attachment 353247 [details]
Patch
Comment 3 Wenson Hsieh 2018-10-27 19:14:03 PDT
Comment on attachment 353247 [details]
Patch

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

> Source/WebCore/bindings/scripts/test/TestSerialization.idl:42
> +    attribute sequence<TestSerializationInheritFinal> twelthInterfaceSequenceAttribute;

twelth!
Comment 4 Andy Estes 2018-10-27 19:38:47 PDT
(In reply to Wenson Hsieh from comment #3)
> Comment on attachment 353247 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=353247&action=review
> 
> > Source/WebCore/bindings/scripts/test/TestSerialization.idl:42
> > +    attribute sequence<TestSerializationInheritFinal> twelthInterfaceSequenceAttribute;
> 
> twelth

🤦‍♂️
Comment 5 Andy Estes 2018-10-28 05:38:14 PDT
Created attachment 353254 [details]
Patch
Comment 6 Joseph Pecoraro 2018-10-30 17:30:34 PDT
Comment on attachment 353254 [details]
Patch

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

> Source/WebCore/bindings/scripts/test/JS/JSTestSerialization.cpp:646
> +    auto tenthFrozenArrayAttributeValue = jsTestSerializationTenthFrozenArrayAttributeGetter(state, thisObject, throwScope);
> +    throwScope.assertNoException();
> +    result->putDirect(vm, Identifier::fromString(&vm, "tenthFrozenArrayAttribute"), tenthFrozenArrayAttributeValue);
> +
> +    auto eleventhSequenceAttributeValue = jsTestSerializationEleventhSequenceAttributeGetter(state, thisObject, throwScope);
> +    throwScope.assertNoException();
> +    result->putDirect(vm, Identifier::fromString(&vm, "eleventhSequenceAttribute"), eleventhSequenceAttributeValue);

Is this actually putting the raw array on the serialized object? Should this need to be copied/cloned somewhere?

I'd expect if there is:

    interface TestSerialization {
        attribute sequence<DOMString> seq;
        serializer = { attribute };
    }

That if I have an instance that looked like:

    let instance = new TestSerialization;
    instance.seq = ["one", "two"];

The toJSON's seq should be separate from the instance's seq:

    assert( instance.toJSON().seq !== instance.seq ); // (1)

Such that modifications to the toJSON result should not affect the instance's:

    let json = instance.toJSON();
    assert( instance.seq.length === 2 );
    json.seq.push("test");
    assert( instance.seq.length === 2 ); // (2)

Right now it looks like we are directly putting the value on to the JSON. So I'd think (1) and (2) would fail.

--

Honestly the spec doesn't seem too clear to me on this. (Note serializer became toJSON a while back):
https://heycam.github.io/webidl/#idl-tojson-operation

It's example only uses primitives, where getting the underlying value is a copy. It doesn't show what to do with non-primitives, or I'm missing it.
Comment 7 Andy Estes 2018-10-31 14:08:53 PDT
(In reply to Joseph Pecoraro from comment #6)
> Comment on attachment 353254 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=353254&action=review
> 
> > Source/WebCore/bindings/scripts/test/JS/JSTestSerialization.cpp:646
> > +    auto tenthFrozenArrayAttributeValue = jsTestSerializationTenthFrozenArrayAttributeGetter(state, thisObject, throwScope);
> > +    throwScope.assertNoException();
> > +    result->putDirect(vm, Identifier::fromString(&vm, "tenthFrozenArrayAttribute"), tenthFrozenArrayAttributeValue);
> > +
> > +    auto eleventhSequenceAttributeValue = jsTestSerializationEleventhSequenceAttributeGetter(state, thisObject, throwScope);
> > +    throwScope.assertNoException();
> > +    result->putDirect(vm, Identifier::fromString(&vm, "eleventhSequenceAttribute"), eleventhSequenceAttributeValue);
> 
> Is this actually putting the raw array on the serialized object?
eleventhSequenceAttributeValue (for example) is a newly-created JSValue. jsTestSerializationEleventhSequenceAttributeGetter() calls toJS() on the wrapped object's Vector, which converts the vector's items into JSValues in a MarkedArgumentBuffer then creates a new array with JSC::constructArray().

But now that I think about it, we won't always get a new value if the attribute uses [CachedAttribute], [CustomGetter], or [CustomSetter]. I'll think about how to handle those.

In the meantime, I wrote a layout test to check your questions:

> I'd expect if there is:
> 
>     interface TestSerialization {
>         attribute sequence<DOMString> seq;
>         serializer = { attribute };
>     }
> 
> That if I have an instance that looked like:
> 
>     let instance = new TestSerialization;
>     instance.seq = ["one", "two"];
> 
> The toJSON's seq should be separate from the instance's seq:
> 
>     assert( instance.toJSON().seq !== instance.seq ); // (1)
This assertion is true (the sequences are separate).

> 
> Such that modifications to the toJSON result should not affect the
> instance's:
> 
>     let json = instance.toJSON();
>     assert( instance.seq.length === 2 );
>     json.seq.push("test");
>     assert( instance.seq.length === 2 ); // (2)
These two assertions are also true (the sequences are separate).
Comment 8 Joseph Pecoraro 2018-10-31 14:43:10 PDT
> > The toJSON's seq should be separate from the instance's seq:
> > 
> >     assert( instance.toJSON().seq !== instance.seq ); // (1)
> This assertion is true (the sequences are separate).
> 
> > 
> > Such that modifications to the toJSON result should not affect the
> > instance's:
> > 
> >     let json = instance.toJSON();
> >     assert( instance.seq.length === 2 );
> >     json.seq.push("test");
> >     assert( instance.seq.length === 2 ); // (2)
> These two assertions are also true (the sequences are separate).

Awesome, then this is looking good to me.
Comment 9 Andy Estes 2019-02-07 16:04:56 PST
Created attachment 361462 [details]
Patch
Comment 10 Brent Fulgham 2019-02-08 09:07:14 PST
Comment on attachment 361462 [details]
Patch

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

r=me

> Source/WebCore/ChangeLog:14
> +        left that as a follow-up task, since I don't see any IDLs that currently need this.

If there are specs that support this idea, I think we should file a Bugzilla now that tracks this, and reference it in your FIXME comment.

But, if no IDL needs this at all I guess we don't have to track work that isn't even conceptually needed.

> Source/WebCore/bindings/scripts/CodeGenerator.pm:957
> +        # FIXME: Teach GenerateSerializerDefinition to serialize sequences of interfaces, then remove this line.

FIXME(Bug), please...
Comment 11 Andy Estes 2019-02-08 09:33:44 PST
Comment on attachment 361462 [details]
Patch

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

>> Source/WebCore/ChangeLog:14
>> +        left that as a follow-up task, since I don't see any IDLs that currently need this.
> 
> If there are specs that support this idea, I think we should file a Bugzilla now that tracks this, and reference it in your FIXME comment.
> 
> But, if no IDL needs this at all I guess we don't have to track work that isn't even conceptually needed.

Filed bug #194439 and will add a comment below.

Thanks for reviewing!
Comment 12 Andy Estes 2019-02-08 09:41:06 PST
Created attachment 361511 [details]
Patch
Comment 13 WebKit Commit Bot 2019-02-08 10:18:33 PST
Comment on attachment 361511 [details]
Patch

Clearing flags on attachment: 361511

Committed r241198: <https://trac.webkit.org/changeset/241198>
Comment 14 WebKit Commit Bot 2019-02-08 10:18:35 PST
All reviewed patches have been landed.  Closing bug.