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.08k stars 1.56k forks source link

js-interop + DDC: Can't use ES6 Proxy #31407

Open matanlurey opened 6 years ago

matanlurey commented 6 years ago

I've been trying to play around with Proxy:

// Copyright 2017, Google Inc.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@JS()
library dart_es6_proxy;

import 'package:js/js.dart';

dynamic proxify<T>(T target, Handler<T> handler) {
  final proxy = new Proxy(target, handler);
  return proxy as dynamic;
}

@JS('Proxy')
abstract class Proxy {
  external factory Proxy(dynamic target, Handler<dynamic> handler);
}

@JS()
@anonymous
abstract class Handler<T> {
  external factory Handler({
    void Function(T target, String property, T receiver) get,
  });
}
// Copyright 2017, Google Inc.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:dart_es6_proxy/proxy.dart';

void main() {
  final foo = new Foo();
  final Foo proxy = proxify(foo, new Handler<Foo>(
    get: (a, b, c) {
      print('a: $a, b: $b, c: $c');
    },
  ));
  foo.method();
  proxy.method();
}

class Foo {
  void method() {
    print('Hello World');
  }
}

It looks like though, DDC's type checks fail when trying to pretend a Proxy is actually the type that was passed into it. I'm not sure if I'm doing something silly or if this is just something I'll have to wait for DDC/Dart2JS to implement (i.e. force type success or something).

jmesserly commented 6 years ago

We probably should add a Proxy class (in Dart) in the JS package itself, and have some special handling of that type in DDC. It's a bit too special of a JS type to use the existing vanilla JS interop.

matanlurey commented 6 years ago

Hey @jmesserly we miss you at DartConf!

We probably should add a Proxy class (in Dart) in the JS package itself, and have some special handling of that type in DDC.

Yeah, that's what I figured. Not a priority, but once we are Dart2-ified it can go on the backlog.