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.11k stars 1.57k forks source link

[dartdevc] Dart2js allows apps to share a Dart map in DDC, but not deployed #38023

Open edbrims opened 5 years ago

edbrims commented 5 years ago

I'm sharing data between two apps using Dart2js's context. The first app runs code like this:

import 'dart:js'; ... final map = context['__thing'] ??= {}; map['string_key'] = true;

The second app does this:

import 'dart:js'; ... final map = context['__thing'] ??= {}; return map['string_key'] ?? false;

It works as intended in DDC: The first app creates context['__thing'] as a Dart map and populates it. The second app finds it and reads its values.

But when you deploy the code, Dart2js obfuscates the variable names, and you end up looking at different objects. The second app finds the map empty.

I can fix it by using a JsObject map instead of a Dart map: final map = context['__thing'] ??= JsObject.jsify({});

Should DDC somehow replicate the obfuscation in order to break in the same way?

vsmenon commented 5 years ago

This has to do with Dart2JS compiling / optimizing / tree-shaking each app separately.

We might consider having DDC warn (possibly at runtime) that a Dart object (a Dart Map in this case) leaked to JavaScript - somewhat akin to requiring allowInterop for functions.