diff options
author | Chad Austin <chad@imvu.com> | 2013-05-03 18:20:26 -0700 |
---|---|---|
committer | Chad Austin <chad@imvu.com> | 2013-05-17 12:56:48 -0700 |
commit | fd8cbaa853ec9ea1f2c0c689e1729126c52368e4 (patch) | |
tree | 5359cad8a12b53f4d74dcd129945c6d08825a3c8 /tests | |
parent | 7f83ab2926947fda7181a7e67e76425f02da19c4 (diff) |
Add support for (fast?) memory_view objects. If C++ passes a memory_view to JS, it gets converted into a typed array object on the other side. Intended for WebGL.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/embind/embind.test.js | 22 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 19 |
2 files changed, 41 insertions, 0 deletions
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 52b2cad8..f9f035f2 100644 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -1728,6 +1728,28 @@ module({ e.delete(); f.delete(); }); + + BaseFixture.extend("memory view", function() { + test("can pass memory view from C++ to JS", function() { + var views = []; + cm.callWithMemoryView(function(view) { + views.push(view); + }); + assert.equal(3, views.length); + + assert.instanceof(views[0], Uint8Array); + assert.equal(8, views[0].length); + assert.deepEqual([0, 1, 2, 3, 4, 5, 6, 7], [].slice.call(new Uint8Array(views[0]))); + + assert.instanceof(views[1], Float32Array); + assert.equal(4, views[1].length); + assert.deepEqual([1.5, 2.5, 3.5, 4.5], [].slice.call(views[1])); + + assert.instanceof(views[2], Int16Array); + assert.equal(4, views[2].length); + assert.deepEqual([1000, 100, 10, 1], [].slice.call(views[2])); + }); + }); }); /* global run_all_tests */ diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index 23761efc..e8ca0338 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -1148,6 +1148,25 @@ EMSCRIPTEN_BINDINGS(interface_tests) { function("callDifferentArguments", &callDifferentArguments); } +template<typename T, size_t sizeOfArray> +constexpr size_t getElementCount(T (&)[sizeOfArray]) { + return sizeOfArray; +} + +static void callWithMemoryView(val v) { + // static so the JS test can read the memory after callTakeMemoryView runs + static unsigned char data[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + v(memory_view(getElementCount(data), data)); + static float f[] = { 1.5f, 2.5f, 3.5f, 4.5f }; + v(typed_memory_view(getElementCount(f), f)); + static short s[] = { 1000, 100, 10, 1 }; + v(typed_memory_view(getElementCount(s), s)); +} + +EMSCRIPTEN_BINDINGS(memory_view_tests) { + function("callWithMemoryView", &callWithMemoryView); +} + class HasExternalConstructor { public: HasExternalConstructor(const std::string& str) |