Skip to content

Dart2JS: How to avoid interceptors for JS interop? #35142

Closed
@matanlurey

Description

@matanlurey

I have code that ~mostly looks like this:

@JS()
library js_interop;

import 'package:js/js.dart';

@JS('self')
external MyGlobals get myGlobals;

@JS()
@anonymous
abstract class MyGlobals {
  external List<String> get names;
  set names(List<String> names);
}

void main() {
  var names = myGlobals.names;
  if (names == null) {
    names = myGlobals.names = [];
  }
}

This fails, mostly mysteriously, with a NoSuchMethodError. While debugging the Dart2JS, I realized that this is going through an Interceptor. The code that is emitted looks something like this:

J.get$names$x = function(receiver) {
  return J.getInterceptor$x(receiver).get$names(receiver);
}

...

JavaScriptObject: {
  get$names: function(obj) {
    return obj.names;
  }
}

...

main: function() {
  var names;
  names = J.get$names$x(self.self);
}

Questions:

  1. In my head, this would work perfectly fine if instead Dart2JS just emitted:
main: function() {
  var names;
  names = self.names;
}

... but I can't tell why it doesn't try to do that. Any pointers? I can point to my internal work.

  1. [Low] Is there a way to avoid reading self.self in this case?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions