javascripthtmlwebapiidlwebidl

Should the IDL definition of a callback throw an error if the return type does not match?


There is such a definition of IDL:

callback NavigationInterceptHandler = Promise<undefined> ();

This corresponds to the property of the handler argument passed to the intercept method of the navigate event:

navigation.addEventListener('navigate', (event) => {
    event.intercept({handler(){
      /// For corresponding with return type of IDL
      return Promise.resolve(undefined);
    }});
  });

The question is: should an error occur if the return type is other than Promise, and if the result is a Promise, but its result is not undefined, should an error occur?

As another example, consider this IDL:

callback FrameRequestCallback = undefined (DOMHighResTimeStamp time);

This corresponds to the callback argument in the requestAnimationFrame function:

requestAnimationFrame(function(){
  /// For corresponding with return type of IDL
  return undefined;
})

What happens if in this case we return a value other than undefined? Does this break the interface? Should this throw an error?

I tried to find information about return value matching, but WebIDL doesn't seem to say anything like that. If I missed anything let me know.

If in both cases we return inappropriate value types, then no errors will occur, why?


Solution

  • The return_type of the callback function does not represent what the JS function must or should return. Instead it's what the IDL callback function itself will return.

    In invoke a callback function you can see that in the step 13, if everything went well, callResult.[[Value]] (the value returned by your JS function) is converted "to the IDL value of the same type as [the IDL callback function] callable’s return type.". So you as an author don't have to ensure the return value of your JS function will match with this signature, the IDL side will take care of it.

    Might be noted that in case of an abrupt completion (i.e. something did throw while calling the JS callback), even that abrupt completion might be converted to a rejected Promise in case the callback function's return type is a Promise.