* [jsfm] support to convert buffer to string

Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/3fb8d991
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/3fb8d991
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/3fb8d991

Branch: refs/heads/0.16-dev
Commit: 3fb8d991f303c10204d3ea3072c343ddacc432f2
Parents: 24efe6c
Author: Hanks <zhanghan...@gmail.com>
Authored: Wed Aug 2 12:23:39 2017 +0800
Committer: Hanks <zhanghan...@gmail.com>
Committed: Wed Aug 2 12:23:39 2017 +0800

----------------------------------------------------------------------
 html5/runtime/buffer.js      | 13 +++++++++++++
 html5/runtime/task-center.js |  5 +++--
 2 files changed, 16 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3fb8d991/html5/runtime/buffer.js
----------------------------------------------------------------------
diff --git a/html5/runtime/buffer.js b/html5/runtime/buffer.js
new file mode 100644
index 0000000..ff610d5
--- /dev/null
+++ b/html5/runtime/buffer.js
@@ -0,0 +1,13 @@
+export function bufferToString (buffer) {
+  return String.fromCharCode.apply(null, new Uint16Array(buffer))
+}
+
+export function stringToBuffer (string) {
+  const N = string.length
+  const buffer = new ArrayBuffer(N * 2)
+  const view = new Uint16Array(buffer)
+  for (let i = 0; i < N; i++) {
+    view[i] = string.charCodeAt(i)
+  }
+  return buffer
+}

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/3fb8d991/html5/runtime/task-center.js
----------------------------------------------------------------------
diff --git a/html5/runtime/task-center.js b/html5/runtime/task-center.js
index 6a0d273..f2fbcb5 100644
--- a/html5/runtime/task-center.js
+++ b/html5/runtime/task-center.js
@@ -18,6 +18,7 @@
  */
 import CallbackManager from './callback-manager'
 import Element from './vdom/element'
+import { bufferToString } from './buffer'
 
 let fallback = function () {}
 
@@ -80,7 +81,7 @@ export class TaskCenter {
         return v
 
       case 'ArrayBuffer':
-        return { type, buffer: v }
+        return { type, buffer: v, string: bufferToString(v) }
       case 'Int8Array':
       case 'Uint8Array':
       case 'Uint8ClampedArray':
@@ -90,7 +91,7 @@ export class TaskCenter {
       case 'Uint32Array':
       case 'Float32Array':
       case 'Float64Array':
-        return { type, buffer: v.buffer }
+        return { type, buffer: v.buffer, string: bufferToString(v.buffer) }
 
       case 'Function':
         return this.callbackManager.add(v).toString()

Reply via email to