-
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.
src: Initial implementation of DataView class
This is an initial implementation of DataView class. This change includes the following things: - DataView::New() methods - Getters for DataView (ArrayBuffer(), ByteOffset(), ByteLength()) - Tests for them PR-URL: nodejs/node-addon-api#205 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Kyle Farnung <[email protected]>
- Loading branch information
1 parent
daa0ad4
commit 649bd1d
Showing
7 changed files
with
200 additions
and
0 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,46 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
static Value CreateDataView1(const CallbackInfo& info) { | ||
ArrayBuffer arrayBuffer = info[0].As<ArrayBuffer>(); | ||
return DataView::New(info.Env(), arrayBuffer); | ||
} | ||
|
||
static Value CreateDataView2(const CallbackInfo& info) { | ||
ArrayBuffer arrayBuffer = info[0].As<ArrayBuffer>(); | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
return DataView::New(info.Env(), arrayBuffer, byteOffset); | ||
} | ||
|
||
static Value CreateDataView3(const CallbackInfo& info) { | ||
ArrayBuffer arrayBuffer = info[0].As<ArrayBuffer>(); | ||
size_t byteOffset = info[1].As<Number>().Uint32Value(); | ||
size_t byteLength = info[2].As<Number>().Uint32Value(); | ||
return DataView::New(info.Env(), arrayBuffer, byteOffset, byteLength); | ||
} | ||
|
||
static Value GetArrayBuffer(const CallbackInfo& info) { | ||
return info[0].As<DataView>().ArrayBuffer(); | ||
} | ||
|
||
static Value GetByteOffset(const CallbackInfo& info) { | ||
return Number::New(info.Env(), info[0].As<DataView>().ByteOffset()); | ||
} | ||
|
||
static Value GetByteLength(const CallbackInfo& info) { | ||
return Number::New(info.Env(), info[0].As<DataView>().ByteLength()); | ||
} | ||
|
||
Object InitDataView(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["createDataView1"] = Function::New(env, CreateDataView1); | ||
exports["createDataView2"] = Function::New(env, CreateDataView2); | ||
exports["createDataView3"] = Function::New(env, CreateDataView3); | ||
exports["getArrayBuffer"] = Function::New(env, GetArrayBuffer); | ||
exports["getByteOffset"] = Function::New(env, GetByteOffset); | ||
exports["getByteLength"] = Function::New(env, GetByteLength); | ||
|
||
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,38 @@ | ||
'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 testDataViewCreation(factory, arrayBuffer, offset, length) { | ||
const view = factory(arrayBuffer, offset, length); | ||
offset = offset ? offset : 0; | ||
assert.ok(dataview.getArrayBuffer(view) instanceof ArrayBuffer); | ||
assert.strictEqual(dataview.getArrayBuffer(view), arrayBuffer); | ||
assert.strictEqual(dataview.getByteOffset(view), offset); | ||
assert.strictEqual(dataview.getByteLength(view), | ||
length ? length : arrayBuffer.byteLength - offset); | ||
} | ||
|
||
function testInvalidRange(factory, arrayBuffer, offset, length) { | ||
assert.throws(() => { | ||
factory(arrayBuffer, offset, length); | ||
}, RangeError); | ||
} | ||
|
||
const dataview = binding.dataview; | ||
const arrayBuffer = new ArrayBuffer(10); | ||
|
||
testDataViewCreation(dataview.createDataView1, arrayBuffer); | ||
testDataViewCreation(dataview.createDataView2, arrayBuffer, 2); | ||
testDataViewCreation(dataview.createDataView2, arrayBuffer, 10); | ||
testDataViewCreation(dataview.createDataView3, arrayBuffer, 2, 4); | ||
testDataViewCreation(dataview.createDataView3, arrayBuffer, 10, 0); | ||
|
||
testInvalidRange(dataview.createDataView2, arrayBuffer, 11); | ||
testInvalidRange(dataview.createDataView3, arrayBuffer, 11, 0); | ||
testInvalidRange(dataview.createDataView3, arrayBuffer, 6, 5); | ||
} |
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