@ -730,15 +730,37 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) {
template < typename T , enum Endianness endianness >
template < typename T , enum Endianness endianness >
uint32_t WriteFloatGeneric ( const FunctionCallbackInfo < Value > & args ) {
void WriteFloatGeneric ( const FunctionCallbackInfo < Value > & args ) {
SPREAD_ARG ( args [ 0 ] , ts_obj ) ;
Environment * env = Environment : : GetCurrent ( args ) ;
bool should_assert = args . Length ( ) < 4 ;
if ( should_assert ) {
THROW_AND_RETURN_UNLESS_BUFFER ( env , args [ 0 ] ) ;
}
Local < Uint8Array > ts_obj = args [ 0 ] . As < Uint8Array > ( ) ;
ArrayBuffer : : Contents ts_obj_c = ts_obj - > Buffer ( ) - > GetContents ( ) ;
const size_t ts_obj_offset = ts_obj - > ByteOffset ( ) ;
const size_t ts_obj_length = ts_obj - > ByteLength ( ) ;
char * const ts_obj_data =
static_cast < char * > ( ts_obj_c . Data ( ) ) + ts_obj_offset ;
if ( ts_obj_length > 0 )
CHECK_NE ( ts_obj_data , nullptr ) ;
T val = args [ 1 ] - > NumberValue ( env - > context ( ) ) . FromMaybe ( 0 ) ;
size_t offset = args [ 2 ] - > IntegerValue ( env - > context ( ) ) . FromMaybe ( 0 ) ;
T val = args [ 1 ] - > NumberValue ( ) ;
uint32_t offset = args [ 2 ] - > Uint32Value ( ) ;
size_t memcpy_num = sizeof ( T ) ;
size_t memcpy_num = sizeof ( T ) ;
if ( offset + sizeof ( T ) > ts_obj_length )
if ( offset + sizeof ( T ) > ts_obj_length )
memcpy_num = ts_obj_length - offset ;
memcpy_num = ts_obj_length - offset ;
if ( should_assert ) {
CHECK_NOT_OOB ( offset + memcpy_num > = memcpy_num ) ;
CHECK_NOT_OOB ( offset + memcpy_num < = ts_obj_length ) ;
}
CHECK_LE ( offset + memcpy_num , ts_obj_length ) ;
union NoAlias {
union NoAlias {
T val ;
T val ;
char bytes [ sizeof ( T ) ] ;
char bytes [ sizeof ( T ) ] ;
@ -749,31 +771,26 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
if ( endianness ! = GetEndianness ( ) )
if ( endianness ! = GetEndianness ( ) )
Swizzle ( na . bytes , sizeof ( na . bytes ) ) ;
Swizzle ( na . bytes , sizeof ( na . bytes ) ) ;
memcpy ( ptr , na . bytes , memcpy_num ) ;
memcpy ( ptr , na . bytes , memcpy_num ) ;
return offset + memcpy_num ;
}
}
void WriteFloatLE ( const FunctionCallbackInfo < Value > & args ) {
void WriteFloatLE ( const FunctionCallbackInfo < Value > & args ) {
THROW_AND_RETURN_UNLESS_BUFFER ( Environment : : GetCurrent ( args ) , args [ 0 ] ) ;
WriteFloatGeneric < float , kLittleEndian > ( args ) ;
args . GetReturnValue ( ) . Set ( WriteFloatGeneric < float , kLittleEndian > ( args ) ) ;
}
}
void WriteFloatBE ( const FunctionCallbackInfo < Value > & args ) {
void WriteFloatBE ( const FunctionCallbackInfo < Value > & args ) {
THROW_AND_RETURN_UNLESS_BUFFER ( Environment : : GetCurrent ( args ) , args [ 0 ] ) ;
WriteFloatGeneric < float , kBigEndian > ( args ) ;
args . GetReturnValue ( ) . Set ( WriteFloatGeneric < float , kBigEndian > ( args ) ) ;
}
}
void WriteDoubleLE ( const FunctionCallbackInfo < Value > & args ) {
void WriteDoubleLE ( const FunctionCallbackInfo < Value > & args ) {
THROW_AND_RETURN_UNLESS_BUFFER ( Environment : : GetCurrent ( args ) , args [ 0 ] ) ;
WriteFloatGeneric < double , kLittleEndian > ( args ) ;
args . GetReturnValue ( ) . Set ( WriteFloatGeneric < double , kLittleEndian > ( args ) ) ;
}
}
void WriteDoubleBE ( const FunctionCallbackInfo < Value > & args ) {
void WriteDoubleBE ( const FunctionCallbackInfo < Value > & args ) {
THROW_AND_RETURN_UNLESS_BUFFER ( Environment : : GetCurrent ( args ) , args [ 0 ] ) ;
WriteFloatGeneric < double , kBigEndian > ( args ) ;
args . GetReturnValue ( ) . Set ( WriteFloatGeneric < double , kBigEndian > ( args ) ) ;
}
}