Auto merge of #20265 - Xanewok:fix-js-objects-in-unions, r=jdm

Fix JS object conversion in unions

<!-- Please describe your changes on the following line: -->
Requires safe `Heap::boxed` constructor from https://github.com/servo/rust-mozjs/pull/395 (more info on it is in the PR).

Since unions currently assume that their respective members root themselves and can be stored on heap, I modified the union member object conversion branch to convert to a `RootedTraceableBox<Heap<*mut JSObject>>` (which is the currently generated type for objects in said unions).

I did it only for Unions and not dictionaries, since some dictionaries had bare `*mut JSObject` members - is this a mistake and something that needs further fixing?

Does this need a test, e.g. passing a union with object to a function that returns said object, and comparing the results for equality?

r? @jdm

Generated code with this patch (for `StringOrObject`):
```rust
impl FromJSValConvertible for StringOrObject {
    type Config = ();
    unsafe fn from_jsval(cx: *mut JSContext,
                         value: HandleValue,
                         _option: ())
                         -> Result<ConversionResult<StringOrObject>, ()> {
        if value.get().is_object() {
            match StringOrObject::TryConvertToObject(cx, value) {
                Err(_) => return Err(()),
                Ok(Some(value)) => return Ok(ConversionResult::Success(StringOrObject::Object(value))),
                Ok(None) => (),
            }

        }
        // (...)
    }
}

impl StringOrObject {
    // (...)
    unsafe fn TryConvertToObject(cx: *mut JSContext, value: HandleValue) -> Result<Option<RootedTraceableBox<Heap<*mut JSObject>>>, ()> {
        Ok(Some(RootedTraceableBox::from_box(Heap::boxed(value.get().to_object()))))
    }
}
```

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #17011 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20265)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-03-14 12:04:23 -04:00 committed by GitHub
commit e597cd9e1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 68 additions and 18 deletions

View file

@ -31972,6 +31972,12 @@
{}
]
],
"mozilla/codegen_unions.html": [
[
"/_mozilla/mozilla/codegen_unions.html",
{}
]
],
"mozilla/collections.html": [
[
"/_mozilla/mozilla/collections.html",
@ -64751,6 +64757,10 @@
"5183977a0d29ba4d74d049c9391090e3c27264a8",
"testharness"
],
"mozilla/codegen_unions.html": [
"7f772fffb75acc92f9c949a482d387b3ed18d0ed",
"testharness"
],
"mozilla/collections.html": [
"d0bebe808ebb45b6c853f4b88e1a6ebbf9b91345",
"testharness"

View file

@ -0,0 +1,3 @@
[codegen_unions.html]
type: testharness
prefs: [dom.testbinding.enabled:true]

View file

@ -0,0 +1,21 @@
<!doctype html>
<html>
<meta charset="utf-8">
<title>WebIDL conversions are performed correctly and don't lose values</title>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<script>
test(function() {
var t = new TestBinding;
// (DOMString or object) receiveUnionIdentity((DOMString or object) arg)
var obj = { 'some': 'key', 'num': 42 };
assert_equals(t.receiveUnionIdentity(obj), obj);
var str = "myString";
assert_equals(t.receiveUnionIdentity(str), str);
}, "(DOMString or object) conversion is performed correctly");
</script>
</html>