Skip to content

js-interop:Array serialisation discrepancy between Dart interop and pure javascript #26768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
parnham opened this issue Jun 24, 2016 · 2 comments
Assignees
Labels
web-js-interop Issues that impact all js interop

Comments

@parnham
Copy link

parnham commented Jun 24, 2016

When using the "js" package for javascript interop I am seeing a weird case where running in Dartium gives a different result to compiled javascript running in Chrome.

Dart SDK version: 1.17.1 linux_x64
pkg/js version: 0.6.0

Example code:

<!DOCTYPE html>
<html>
    <head>
        <title>Interop test</title>
        <meta charset="utf-8">
    </head>

    <body>
        <script type="application/dart" src="test.dart"></script>
        <script src="packages/browser/dart.js"></script>
    </body>
</html>
@JS()
library interop.test;

import 'package:js/js.dart';

@JS('JSON.stringify')
external String toJson(dynamic object);

@JS()
@anonymous
class Simple
{
    external List<int> get numbers;
    external set numbers(List<int> numbers);

    external factory Simple({ List<int> numbers });
}

void main()
{
    var simple = new Simple(numbers: [ 1, 2, 3 ]);

    print(toJson(simple));
}

When run in Dartium it produces:

{"numbers":{"0":1,"1":2,"2":3}}

When compiled to javascript and run in Chrome it produces (as expected):

{"numbers":[1,2,3]}

Further experimentation in Dartium reveals that whilst the top-level object appears as this (via a console.log() interop):

Object {numbers: Array[3]}
  numbers: Array[3]
  __proto__: Object

the numbers member itself appears as:

[1, 2, 3]
  [[class]]: List
@floitschG floitschG added the web-js-interop Issues that impact all js interop label Jun 24, 2016
@jacob314 jacob314 self-assigned this Jun 24, 2016
@jacob314
Copy link
Member

Thanks for reporting. We will patch JSON.stringify in dartium to fix this issue.
The dart2js behavior is the correct behavior.
This is part of a class of bugs where we have to try hard to maintain the abstraction that a Dart List looks like a JS Array even though it is not exactly an Array and just looks like an Array as far as any non-native JS APIs can tell.

@jacob314 jacob314 mentioned this issue Jul 8, 2016
19 tasks
@jacob314
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

3 participants