Working on #4964 and noticed that Buffers have inconsistencies of when arguments are thrown and when they're coerced. This is a proposal to solidify how they should be handled. It has taken care to follow existing practices by browsers in regards to generic argument handling and that of the Typed Array specification:
- Required
numeric arguments throw if undefined.
- Optional
numeric arguments are set as default if undefined.
- Arguments expected to be
numeric will coerce all other values.
- Any
double will be floored to int.
- Throw for all out of range indexes (
index > buffer.length || index < 0) for any read/write methods.
buffer.slice() and buffer.copy() should follow the specification for arraybuffer.slice() (with a few modification for .copy()).
As an example of how to handle this, here are a couple verification functions:
inline uint32_t GetOptionalUintArg(Handle<Value> arg) {
int32_t val_i = arg->Int32Value();
if (val_i < 0)
return ThrowRangeError("index is less than zero");
return static_cast<uint32_t>(val_i);
}
inline uint32_t GetRequiredUintArg(Handle<Value> arg) {
if (arg->IsUndefined())
return ThrowTypeError("argument is undefined");
return GetOptionalUintArg(arg);
}
Working on #4964 and noticed that Buffers have inconsistencies of when arguments are thrown and when they're coerced. This is a proposal to solidify how they should be handled. It has taken care to follow existing practices by browsers in regards to generic argument handling and that of the Typed Array specification:
numericarguments throw ifundefined.numericarguments are set as default ifundefined.numericwill coerce all other values.doublewill be floored toint.index > buffer.length || index < 0) for any read/write methods.buffer.slice()andbuffer.copy()should follow the specification forarraybuffer.slice()(with a few modification for.copy()).As an example of how to handle this, here are a couple verification functions: