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 MouseEvent unexpectedly converted to Dart MouseEvent in interop #15216

Closed
DartBot opened this issue Nov 20, 2013 · 6 comments
Closed

JS MouseEvent unexpectedly converted to Dart MouseEvent in interop #15216

DartBot opened this issue Nov 20, 2013 · 6 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant library-js type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@DartBot
Copy link

DartBot commented Nov 20, 2013

This issue was originally filed by @rbishop-bah


What steps will reproduce the problem?

  1. Dart method that receives a JavaScript MouseEvent as a js.Proxy (js pkg, not dart:js)
  2. Forwards the JS mouse event to another Dart method, which tries to get the clientX value
  3. event.clientX works in Dartium but fails after compilation to JS (via pub build... polymer custom element)

What is the expected output? What do you see instead?
I want clientX's value. I get an error.

What version of the product are you using? On what operating system?
Dart SDK 1.0.0.7, Win7x64

Please provide any additional information below.
Same error on both Chrome and Firefox. Here's the relevant Dart, then the JavaScript, then the error:

========================
Dart

    _view.on("mousedown", (js.Proxy event, js.Proxy object) {
      handleMouseDown(event, object);
    });


  void handleMouseDown(js.Proxy event, js.Proxy item) {
      ... some logic
      _dragStartClientX = event.clientX;

========================
JavaScript

Thing_initView_closure2: {"": "Closure;this_3-",
  call$2: function($event, object) {
    this.this_3.handleMouseDown$2($event, object);
    "281,520,147,282,147";
  },
  "+call:2:0": 1,
  $isFunction: true,
  $is_args2: true
},
 _________

  handleMouseDown$2: function($event, item) {
      /// some other logic, blah blah
      this._dragStartClientX = $event.get$clientX();
      // etc.
  }

Running in JavaScript in Chrome or FireFox yields this error:

TypeError {stack:
 "TypeError: Object #<Object> has no method 'get$clientX'
    at Thing.handleMouseDown$2 (https://localhost/redacted/index.html_bootstrap.dart.js:161114:39)
...

If I poke around in Chrome's Watch Expressions I can see that:

$event: GenericEvent
$event.clientX: 333
$event.get$clientX(): <not available> <===== THIS IS WHAT COMPILED CODE USES
$event.get$clientX: undefined
$event.clientX(): <not available>
$event.impl.clientX: 333
$event.impl.get$clientX(): <not available>

@DartBot
Copy link
Author

DartBot commented Nov 21, 2013

This comment was originally written by @rbishop-bah


event.clientX also fails if I put it in the initial closure, so passing the proxy to another method is not the issue.

@DartBot
Copy link
Author

DartBot commented Nov 21, 2013

This comment was originally written by @rbishop-bah


Upon further investigation, this appears to be the result of an unexpected (?) auto-conversion of a JavaScript MouseEvent to a Dart MouseEvent.

Sequence of events:

  • Dart method calls a function of a JavaScript Proxy, passing it a callback function to be executed

  void on(String str, OnCallback func) {
    _proxy.on(str, func);
  }

  where OnCallback is defined as: typedef OnCallback(event, item);

* The callback function is executed -- after a mouse click, for example -- and its event is actually (unexpectedly to me) a Dart MouseEvent and not a proxy to a JavaScript MouseEvent.

* Downstream processing of the event that assumes it's handling a Proxy to a JS MouseEvent crashes (in compiled JS as described above).

So at some point the _proxy processes the JS mouse event and it gets auto converted to a Dart mouse event before the callback is executed.

Armed with this knowledge I have devised successful workarounds -- testing whether the event in the callback is a Dart MouseEvent before assuming it's a proxy to a JS MouseEvent and processing accordingly.

But this is kind of confusing. Is this a bug? If it's not what are the rules for the types in JS callbacks (is there a set of types that get auto-converted from JS to Dart)?

@DartBot
Copy link
Author

DartBot commented Nov 22, 2013

This comment was originally written by @zoechi


I don't know if this changes anything but clientX is deprecated and replaced by client.x

@iposva-google
Copy link
Contributor

Added Area-HTML, Triaged labels.

@kevmoo
Copy link
Member

kevmoo commented Apr 7, 2014

Removed Area-HTML label.
Added Area-Library, Library-Html labels.

@alan-knight
Copy link
Contributor

Given the further investigation in #­2 this seems like a question for dart:js more than for dart:html. This may be stale, given that js.Proxy no longer exists, but I don't know enough about it to say.


Removed Library-Html label.
Added Library-JS label.
Changed the title to: "JS MouseEvent unexpectedly converted to Dart MouseEvent in interop".

@DartBot DartBot added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-js labels Jan 21, 2015
@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed priority-unassigned labels Feb 29, 2016
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 19, 2018
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. closed-obsolete Closed as the reported issue is no longer relevant library-js type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants