Skip to content
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

JS interop: jsify and dartify #21311

Open
tfortes opened this issue Oct 12, 2014 · 3 comments
Open

JS interop: jsify and dartify #21311

tfortes opened this issue Oct 12, 2014 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-js type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop

Comments

@tfortes
Copy link

tfortes commented Oct 12, 2014

JsObject.jsify currently takes a map or iterable.

...A scenario I ran into was sending a dynamic value to JS - I currently have to inspect the value to see if it should be sent to jsify or not.

Proposal: a more general jsify method (global in the js package?) that takes any Dart value (including null) and returns the "best" JS representation of it (JsObject, JsArray or unchanged for primitives)

In the opposite direction, a "dartify" method that returns usable Dart values (Map, List, or primitive) would be useful.

Here's my current work-around implementation of jsify and dartify. Note the horrible hacks I had to do.

  dynamic dartify(dynamic arg) {
    if (arg is js.JsArray) {
      return arg.toList();
    }
    if (arg is js.JsObject) {
      // TODO(tfortes): Find a better way to convert the js object to a Map.
      // https://code.google.com/p/dart/issues/detail?id=12997
      String json = js.context['JSON'].callMethod('stringify', [arg]);
      return JSON.decode(json);
    }

    return arg;
  }

  dynamic jsify(dynamic arg) {
    if (arg is Map || arg is Iterable) {
      return new js.JsObject.jsify(arg);
    }
    // Primitives are fine as-is. Other objects
    // will end up as opaque in JavaScript.
    return arg;
  }

@anders-sandholm
Copy link
Contributor

cc @justinfagnani.
Added Area-Library, Library-JS, Triaged labels.

@justinfagnani
Copy link
Contributor

@tfortes tfortes added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-js labels Oct 21, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Mar 1, 2016
@adeel41
Copy link

adeel41 commented Nov 8, 2016

what is the current status of this enhancement? Is there a way to dartify a JsObject automatically?

@vsmenon vsmenon added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jul 20, 2019
@joshualitt joshualitt added the web-js-interop Issues that impact all js interop label Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-js type-enhancement A request for a change that isn't a bug web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

7 participants