Re: "negative count" and unsigned counts

Johnny Stenback wrote:

> Agreed. I don't think there's anything that really can be fixed here, 
> but DOM Level 3 (and maybe arratas to older versions) could explain 
> the intended behavior better here, and IMO the DOM TS should not make 
> an implementation fail if it doesn't throw a *DOM exception* when a 
> negative value is passed as an unsigned type.


Tests that pass negative numbers to parameters declared as unsigned have 
an assertion that they are only applicable to bindings that exclusively 
used signed values.  Typically, it looks something like this:

<test>
   <metadata/>
   <implementationAttribute name="signed" value="true"/>
   <!--  body of test goes here -->
</test>

Both the Java and ECMAScript frameworks accept tests where signed="true" 
since neither supports unsigned values, however frameworks for other 
bindings may dismiss those tests as not applicable.

Unfortunately, most languages that support unsigned integers do not 
raise an exception on an attempt to cast a negative value to unsigned.  
This results in the same programing error having substantially different 
outcomes on different bindings.  For example, if 
CharacterData.substringData(0, -1) will raise an exception on Java and 
ECMAScript implementations, but would return the entire string on 
implementations that silently converted -1 to a very large positive integer.

If I were designing a binding for a language that did not raise an 
exception on a conversion of a negative number to unsigned and also 
allowed function overloading, I would consider creating a signed variant 
of each function that performed range checking an raised the appropriate 
DOMException.  For example,

class CharacterData
{
      //
      //   signed wrapper function, called when uncast expressions or 
literals are used in call
      DOMString substringData(int offset, int count) {
          if (offset < 0) throw DOMException(INDEX_SIZE_ERR);
          if (count < 0) throw DOMException(INDEX_SIZE_ERR);
          substringData((unsigned int) offset, (unsigned int) count);
      }

     DOMString substringData(unsigned int offset, unsigned int count) {
          //  actual implementation
     }
}

Received on Saturday, 20 September 2003 00:09:49 UTC