Skip to content

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

Closed
@parnham

Description

@parnham

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

Metadata

Metadata

Assignees

Labels

web-js-interopIssues that impact all js interop

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions