dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

JS interop: jsify and dartify #21311

Open tfortes opened 10 years ago

tfortes commented 10 years ago

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 commented 10 years ago

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

justinfagnani commented 10 years ago

Moved to GitHub: https://github.com/dart-lang/js-interop/issues/188

adeel41 commented 7 years ago

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