diff options
author | Chad Austin <chad@imvu.com> | 2013-04-15 21:15:13 -0700 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2013-04-18 20:08:14 +0300 |
commit | e53dd06bf9ca763a65f8bc77dd3ae980843e8652 (patch) | |
tree | 14571b59144cf67562a2e2989031bd80ce33b057 | |
parent | 181e6abb2f3abb9c3dc07d769adb84f6bc5982bc (diff) |
allow passing Int8Array and Uint8Array directly to std::string
-rwxr-xr-x | src/embind/embind.js | 17 | ||||
-rwxr-xr-x | tests/embind/embind.test.js | 10 |
2 files changed, 25 insertions, 2 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js index 5fc8c948..078b5c1c 100755 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -348,7 +348,20 @@ function __embind_register_std_string(rawType, name) { return a.join(''); }, toWireType: function(destructors, value) { - if (typeof value !== "string") { + function getTAElement(ta, index) { + return ta[index]; + } + function getStringElement(string, index) { + return string.charCodeAt(index); + } + var getElement; + if (value instanceof Uint8Array) { + getElement = getTAElement; + } else if (value instanceof Int8Array) { + getElement = getTAElement; + } else if (typeof value === 'string') { + getElement = getStringElement; + } else { throwBindingError('Cannot pass non-string to std::string'); } @@ -357,7 +370,7 @@ function __embind_register_std_string(rawType, name) { var ptr = _malloc(4 + length); HEAPU32[ptr >> 2] = length; for (var i = 0; i < length; ++i) { - var charCode = value.charCodeAt(i); + var charCode = getElement(value, i); if (charCode > 255) { _free(ptr); throwBindingError('String has UTF-16 code units that do not fit in 8 bits'); diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 6473573a..821355d2 100755 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -409,6 +409,16 @@ module({ }); }); + test("can pass Uint8Array to std::string", function() { + var e = cm.emval_test_take_and_return_std_string(new Uint8Array([65, 66, 67, 68])); + assert.equal('ABCD', e); + }); + + test("can pass Int8Array to std::string", function() { + var e = cm.emval_test_take_and_return_std_string(new Int8Array([65, 66, 67, 68])); + assert.equal('ABCD', e); + }); + test("non-ascii wstrings", function() { var expected = String.fromCharCode(10) + String.fromCharCode(1234) + |