-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement data manipulation methods for dataview
This change implements additional methods for manipulating data in the dataview object. This change includes the following things: - Get/Set${type} methods to manipulate data - Tests for the methods PR-URL: nodejs/node-addon-api#218 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
1 parent
1ef83f5
commit 853b1c9
Showing
7 changed files
with
336 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
static Value GetFloat32(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetFloat32(byteOffset)); | ||
} | ||
|
||
static Value GetFloat64(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetFloat64(byteOffset)); | ||
} | ||
|
||
static Value GetInt8(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetInt8(byteOffset)); | ||
} | ||
|
||
static Value GetInt16(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetInt16(byteOffset)); | ||
} | ||
|
||
static Value GetInt32(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetInt32(byteOffset)); | ||
} | ||
|
||
static Value GetUint8(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetUint8(byteOffset)); | ||
} | ||
|
||
static Value GetUint16(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetUint16(byteOffset)); | ||
} | ||
|
||
static Value GetUint32(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return Number::New(info.Env(), info[0].As<DataView>().GetUint32(byteOffset)); | ||
} | ||
|
||
static void SetFloat32(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().FloatValue(); | ||
info[0].As<DataView>().SetFloat32(byteOffset, value); | ||
} | ||
|
||
static void SetFloat64(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().DoubleValue(); | ||
info[0].As<DataView>().SetFloat64(byteOffset, value); | ||
} | ||
|
||
static void SetInt8(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().Int32Value(); | ||
info[0].As<DataView>().SetInt8(byteOffset, value); | ||
} | ||
|
||
static void SetInt16(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().Int32Value(); | ||
info[0].As<DataView>().SetInt16(byteOffset, value); | ||
} | ||
|
||
static void SetInt32(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().Int32Value(); | ||
info[0].As<DataView>().SetInt32(byteOffset, value); | ||
} | ||
|
||
static void SetUint8(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().Uint32Value(); | ||
info[0].As<DataView>().SetUint8(byteOffset, value); | ||
} | ||
|
||
static void SetUint16(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().Uint32Value(); | ||
info[0].As<DataView>().SetUint16(byteOffset, value); | ||
} | ||
|
||
static void SetUint32(const CallbackInfo& info) { | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
auto value = info[2].As<Number>().Uint32Value(); | ||
info[0].As<DataView>().SetUint32(byteOffset, value); | ||
} | ||
|
||
Object InitDataViewReadWrite(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["getFloat32"] = Function::New(env, GetFloat32); | ||
exports["getFloat64"] = Function::New(env, GetFloat64); | ||
exports["getInt8"] = Function::New(env, GetInt8); | ||
exports["getInt16"] = Function::New(env, GetInt16); | ||
exports["getInt32"] = Function::New(env, GetInt32); | ||
exports["getUint8"] = Function::New(env, GetUint8); | ||
exports["getUint16"] = Function::New(env, GetUint16); | ||
exports["getUint32"] = Function::New(env, GetUint32); | ||
|
||
exports["setFloat32"] = Function::New(env, SetFloat32); | ||
exports["setFloat64"] = Function::New(env, SetFloat64); | ||
exports["setInt8"] = Function::New(env, SetInt8); | ||
exports["setInt16"] = Function::New(env, SetInt16); | ||
exports["setInt32"] = Function::New(env, SetInt32); | ||
exports["setUint8"] = Function::New(env, SetUint8); | ||
exports["setUint16"] = Function::New(env, SetUint16); | ||
exports["setUint32"] = Function::New(env, SetUint32); | ||
|
||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`../build/${buildType}/binding.node`)); | ||
test(require(`../build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
function expected(type, value) { | ||
return eval(`(new ${type}Array([${value}]))[0]`); | ||
} | ||
|
||
function nativeReadDataView(dataview, type, offset, value) { | ||
return eval(`binding.dataview_read_write.get${type}(dataview, offset)`); | ||
} | ||
|
||
function nativeWriteDataView(dataview, type, offset, value) { | ||
eval(`binding.dataview_read_write.set${type}(dataview, offset, value)`); | ||
} | ||
|
||
function jsReadDataView(dataview, type, offset, value) { | ||
return eval(`dataview.get${type}(offset, true)`); | ||
} | ||
|
||
function jsWriteDataView(dataview, type, offset, value) { | ||
eval(`dataview.set${type}(offset, value, true)`); | ||
} | ||
|
||
function testReadData(dataview, type, offset, value) { | ||
jsWriteDataView(dataview, type, offset, 0); | ||
assert.strictEqual(jsReadDataView(dataview, type, offset), 0); | ||
|
||
jsWriteDataView(dataview, type, offset, value); | ||
assert.strictEqual( | ||
nativeReadDataView(dataview, type, offset), expected(type, value)); | ||
} | ||
|
||
function testWriteData(dataview, type, offset, value) { | ||
jsWriteDataView(dataview, type, offset, 0); | ||
assert.strictEqual(jsReadDataView(dataview, type, offset), 0); | ||
|
||
nativeWriteDataView(dataview, type, offset, value); | ||
assert.strictEqual( | ||
jsReadDataView(dataview, type, offset), expected(type, value)); | ||
} | ||
|
||
function testInvalidOffset(dataview, type, offset, value) { | ||
assert.throws(() => { | ||
nativeReadDataView(dataview, type, offset); | ||
}, RangeError); | ||
|
||
assert.throws(() => { | ||
nativeWriteDataView(dataview, type, offset, value); | ||
}, RangeError); | ||
} | ||
|
||
const dataview = new DataView(new ArrayBuffer(22)); | ||
|
||
testReadData(dataview, 'Float32', 0, 10.2); | ||
testReadData(dataview, 'Float64', 4, 20.3); | ||
testReadData(dataview, 'Int8', 5, 120); | ||
testReadData(dataview, 'Int16', 7, 15000); | ||
testReadData(dataview, 'Int32', 11, 200000); | ||
testReadData(dataview, 'Uint8', 12, 128); | ||
testReadData(dataview, 'Uint16', 14, 32768); | ||
testReadData(dataview, 'Uint32', 18, 1000000); | ||
|
||
testWriteData(dataview, 'Float32', 0, 10.2); | ||
testWriteData(dataview, 'Float64', 4, 20.3); | ||
testWriteData(dataview, 'Int8', 5, 120); | ||
testWriteData(dataview, 'Int16', 7, 15000); | ||
testWriteData(dataview, 'Int32', 11, 200000); | ||
testWriteData(dataview, 'Uint8', 12, 128); | ||
testWriteData(dataview, 'Uint16', 14, 32768); | ||
testWriteData(dataview, 'Uint32', 18, 1000000); | ||
|
||
testInvalidOffset(dataview, 'Float32', 22, 10.2); | ||
testInvalidOffset(dataview, 'Float64', 22, 20.3); | ||
testInvalidOffset(dataview, 'Int8', 22, 120); | ||
testInvalidOffset(dataview, 'Int16', 22, 15000); | ||
testInvalidOffset(dataview, 'Int32', 22, 200000); | ||
testInvalidOffset(dataview, 'Uint8', 22, 128); | ||
testInvalidOffset(dataview, 'Uint16', 22, 32768); | ||
testInvalidOffset(dataview, 'Uint32', 22, 1000000); | ||
} |
Oops, something went wrong.