RE: Modulo in the web idl spec

Hi all,


I think I understood how browser proceed to convert an ECMAScript object to an octet (assume no attributes like [Clamp] or [EnforceRange] were specified). 

  function toOctet(input) {

    // convert into a number
    var nbr = +input;

    // abort non-integers numbers
    if(Number.isNaN(nbr) || Number.isInfinite(nbr)) return 0;

    // extract the lowest-order bits
    return nbr & 255;

  }

which can be simplified as just

  function toOctet(input) {

    return +input & 255;

  }

(I tested, that works okay even for NaN and +/- Infinity)


Can we get the spec clarified? We would like to be sure our implementation is correct.
François

_____________________
PS: performance test: 
http://jsperf.com/conversion-to-octet/2


----------------------------------------
> From: francois.remy.dev@outlook.com
> To: cam@mcc.id.au
> CC: public-nextweb@w3.org
> Date: Mon, 14 Jan 2013 19:24:46 +0100
> Subject: Modulo in the web idl spec
>
> Hi,
>
> Marcos Caceres seemed quite confused about the modulo used in the web idl spec. I said him that he could use the % symbol of ECMAScript to perform the operation even if that was not a true modulo, because this is what the spec seems to imply. Can you confirm me it is indeed the case?
>
> Right now, I must admit I'm a bit confused too because I ran the following test in all modern browsers (Chrome Canary, Aurora, Opera Snapshots and IE10) and it returned the result I would expect from a true modulo, not from the JS one:
>
> >> t = new ArrayBuffer(1)
> >> v = new DataView(t)
> >> v.setUint8(0,-1)
> >> v.getUint8(0)
> 255
>
> It would probably be good to actually clarify which modulo is actually used by browsers and use that one in the spec?
>
> Best regards,
> François
>
> [PS] The definition of setUint8 is as follows:
> ---------------------------------------------------------------
> void setUint8(unsigned long byteOffset, octet value);
> --------------------------------------------------------------- 		 	   		  

Received on Wednesday, 16 January 2013 14:13:39 UTC