aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2013-04-11 00:14:23 -0700
committerJukka Jylänki <jujjyl@gmail.com>2013-04-18 20:08:11 +0300
commit6ca9bffb114f206c13d9954be32d6b8034ce5eef (patch)
tree1bb5f27bfd1cc8adacf9e3b40ab08f32893de27f
parent53c228a260b7a5e90e430860e130ed6006bd328a (diff)
assert if, when trying to convert JS string to std::string, the JS string has code units that do not fit in 8 bits
-rwxr-xr-xsrc/embind/embind.js7
-rwxr-xr-xtests/embind/embind.test.js6
2 files changed, 12 insertions, 1 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js
index f66b0495..f9b15fa3 100755
--- a/src/embind/embind.js
+++ b/src/embind/embind.js
@@ -336,7 +336,12 @@ function __embind_register_std_string(rawType, name) {
var ptr = _malloc(4 + length);
HEAPU32[ptr >> 2] = length;
for (var i = 0; i < length; ++i) {
- HEAPU8[ptr + 4 + i] = value.charCodeAt(i);
+ var charCode = value.charCodeAt(i);
+ if (charCode > 255) {
+ _free(ptr);
+ throwBindingError('String has UTF-16 code units that do not fit in 8 bits');
+ }
+ HEAPU8[ptr + 4 + i] = charCode;
}
destructors.push(_free, ptr);
return ptr;
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js
index 6f63d543..fec7145a 100755
--- a/tests/embind/embind.test.js
+++ b/tests/embind/embind.test.js
@@ -397,6 +397,12 @@ module({
assert.equal(expected, cm.get_non_ascii_string());
});
+ test("passing non-8-bit strings from JS to std::string throws", function() {
+ assert.throws(cm.BindingError, function() {
+ cm.emval_test_take_and_return_std_string("\u1234");
+ });
+ });
+
test("non-ascii wstrings", function() {
var expected = String.fromCharCode(10) +
String.fromCharCode(1234) +