W3C home > Mailing lists > Public > public-script-coord@w3.org > July to September 2011

[WebIDL] Converting non-objects to Dictionaries.

From: Jonas Sicking <jonas@sicking.cc>
Date: Thu, 21 Jul 2011 12:22:14 -0700
Message-ID: <CA+c2ei_qRT9W2iVA6nbA0iqJ0TQpNVt6EfRsPnyYgw80b9ZtWg@mail.gmail.com>
To: public-script-coord@w3.org
Consider the following WebIDL:

dictionary FooOptions {
 boolean reverse = false;
 unsigned long limit;
};

interface Foo {
  void someFunc(in DOMString name, optional in FooOptions options);
};


This creates an API that is intended to be called like:

myfoo.someFunc("name", { reverse: true });

however, as currently defined, you can call it with:

myfoo.someFunc("name", 4);

The dictionary algorithm will convert the '4' to an object and then
attempt to read properties off that object. However since the object
won't have any properties, all properties in the dictionary will get
their default value. In other words, the above is equivalent to

myfoo.someFunc("name", {});
and
myfoo.someFunc("name");

It seems to very likely that if someone passes in a non-object, they
have a bug in their code and we would do them a bigger service by
throwing an exception than silently ignoring their argument.

This does leave the question what to do for something like:

myfoo.someFunc("name", null);
and
myfoo.someFunc("name", undefined);

It seems useful to me to allow both these and use the default values
in both cases. For example for code that looks like:

function myDoStuffFunc(name, options) {
  getFooObj().someFunc(name, options);
}

/ Jonas
Received on Thursday, 21 July 2011 19:23:11 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:04 UTC