aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/embind/embind.js17
-rwxr-xr-xtests/embind/embind.test.js10
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) +