Re: ISSUE-7: FrameRequestCallback interface should be designated as Callback=FunctionOnly [Request Animation Frame]

Boris Zbarsky:
> Consider the case of a DOM with both ES and non-ES bindings, where he
> non-ES code wants to pass a FrameRequestCallback object to the ES code
> and have that ES code register the callback.  Or heck, ES code wants to
> pass a FrameRequestCallback to some other method implemented in a non-ES
> language which will then pass it back into ES which ends up registering it.
> 
> Is such a setup possible in the FunctionOnly case?  If not, do we really
> want to preclude it?

I think it is possible.  Let’s say we have:

  interface WindowTiming {
    long requestAnimationFrame(in FrameRequestCallback callback);
    FrameRequestCallback getLastRegisteredCallback();
  };
  Window implements WindowTiming;

  [Callback=FunctionOnly,NoInterfaceObject]
  interface FrameRequestCallback {
    void sample(in DOMTimeStamp time);
  };

And let’s say we have both JS and Java bindings in our implementation.
We’d have these Java interfaces:

  interface Window { ... }

  interface WindowTiming {
    int requestAnimationFrame(FrameRequestCallback callback);
    FrameRequestCallback getLastRegisteredCallback();
  }

  interface FrameRequestCallback {
    void sample(long time);
  }

Code that registered a callback from Java would look like:

  Window window = ...;
  WindowTiming timing = (WindowTiming) window;
  timing.requestAnimationFrame(new FrameRequestCallback() {
    void sample(long time) {
      // do some animation
    }
  });

If in JS you then did:

  var callback = window.getLastRegisteredCallback();

then callback could be a unique Function object that is the “JS wrapper”
for the Java FrameRequestCallback.  Its [[Call]] would call the sample
method of the wrapped Java object.

I don’t know if Web IDL needs to talk about how in particular objects
from one language are reflected in another.

> What are the benefits of using FunctionOnly?

Simplicity, I guess.  Why allow ({ sample: function() { ... }) when in
JS the idiom is to use a bare Function object?

Now, the reason the ({ sample: ... }) style is allowed for interfaces
with a single operation in Web IDL is because I wanted it to be
consistent with how you would handle implementing interfaces with
multiple operations in JS.  Such cases come up in Web APIs so far,
though.  I added the [Callback=FunctionOnly] annotation because other
spec authors wanted to restrict the JS binding in this way, and weren’t
convinced that allowing both the object and Function way was useful.

-- 
Cameron McCormack ≝ http://mcc.id.au/

Received on Thursday, 2 June 2011 02:24:22 UTC