Show More
@@ -0,0 +1,61 b'' | |||||
|
1 | // Copyright (c) IPython Development Team. | |||
|
2 | // Distributed under the terms of the Modified BSD License. | |||
|
3 | ||||
|
4 | define([ | |||
|
5 | 'jquery', | |||
|
6 | 'components/utf8/utf8' | |||
|
7 | ], function ($, utf8) { | |||
|
8 | "use strict"; | |||
|
9 | ||||
|
10 | var _deserialize_binary = function(blob, callback) { | |||
|
11 | // deserialize the binary message format | |||
|
12 | // callback will be called with a message whose buffers attribute | |||
|
13 | // will be an array of DataViews. | |||
|
14 | var reader = new FileReader(); | |||
|
15 | reader.onload = function () { | |||
|
16 | var buf = this.result; // an ArrayBuffer | |||
|
17 | var data = new DataView(buf); | |||
|
18 | // read the header: 1 + nbufs 32b integers | |||
|
19 | var nbufs = data.getInt32(0); | |||
|
20 | var offsets = []; | |||
|
21 | var i; | |||
|
22 | for (i = 1; i <= nbufs; i++) { | |||
|
23 | offsets.push(data.getInt32(i * 4)); | |||
|
24 | } | |||
|
25 | // have to convert array to string for utf8.js | |||
|
26 | var bytestring = String.fromCharCode.apply(null, | |||
|
27 | new Uint8Array(buf.slice(offsets[0], offsets[1])) | |||
|
28 | ); | |||
|
29 | var msg = $.parseJSON( | |||
|
30 | utf8.decode( | |||
|
31 | bytestring | |||
|
32 | ) | |||
|
33 | ); | |||
|
34 | // the remaining chunks are stored as DataViews in msg.buffers | |||
|
35 | msg.buffers = []; | |||
|
36 | var start, stop; | |||
|
37 | for (i = 1; i < nbufs; i++) { | |||
|
38 | start = offsets[i]; | |||
|
39 | stop = offsets[i+1] || buf.byteLength; | |||
|
40 | msg.buffers.push(new DataView(buf.slice(start, stop))); | |||
|
41 | } | |||
|
42 | callback(msg); | |||
|
43 | }; | |||
|
44 | reader.readAsArrayBuffer(blob); | |||
|
45 | }; | |||
|
46 | ||||
|
47 | var deserialize = function (data, callback) { | |||
|
48 | // deserialize a message and pass the unpacked message object to callback | |||
|
49 | if (typeof data === "string") { | |||
|
50 | // text JSON message | |||
|
51 | callback($.parseJSON(data)); | |||
|
52 | } else { | |||
|
53 | // binary message | |||
|
54 | _deserialize_binary(data, callback); | |||
|
55 | } | |||
|
56 | }; | |||
|
57 | ||||
|
58 | return { | |||
|
59 | deserialize : deserialize | |||
|
60 | }; | |||
|
61 | }); No newline at end of file |
@@ -554,43 +554,6 b' define([' | |||||
554 | ); |
|
554 | ); | |
555 | }; |
|
555 | }; | |
556 |
|
556 | |||
557 | var decode_utf8 = function (array) { |
|
|||
558 | // Decode UTF8 Uint8Array to String |
|
|||
559 | // I can't believe Javascript makes us do this |
|
|||
560 | // From http://stackoverflow.com/questions/17191945 |
|
|||
561 |
|
||||
562 | var out, i, len, c; |
|
|||
563 | var char2, char3; |
|
|||
564 |
|
||||
565 | out = ""; |
|
|||
566 | len = array.length; |
|
|||
567 | i = 0; |
|
|||
568 | while(i < len) { |
|
|||
569 | c = array[i++]; |
|
|||
570 | switch(c >> 4) { |
|
|||
571 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
|
|||
572 | // 0xxxxxxx |
|
|||
573 | out += String.fromCharCode(c); |
|
|||
574 | break; |
|
|||
575 | case 12: case 13: |
|
|||
576 | // 110x xxxx 10xx xxxx |
|
|||
577 | char2 = array[i++]; |
|
|||
578 | out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); |
|
|||
579 | break; |
|
|||
580 | case 14: |
|
|||
581 | // 1110 xxxx 10xx xxxx 10xx xxxx |
|
|||
582 | char2 = array[i++]; |
|
|||
583 | char3 = array[i++]; |
|
|||
584 | out += String.fromCharCode(((c & 0x0F) << 12) | |
|
|||
585 | ((char2 & 0x3F) << 6) | |
|
|||
586 | ((char3 & 0x3F) << 0)); |
|
|||
587 | break; |
|
|||
588 | } |
|
|||
589 | } |
|
|||
590 |
|
||||
591 | return out; |
|
|||
592 | }; |
|
|||
593 |
|
||||
594 | var utils = { |
|
557 | var utils = { | |
595 | regex_split : regex_split, |
|
558 | regex_split : regex_split, | |
596 | uuid : uuid, |
|
559 | uuid : uuid, | |
@@ -616,7 +579,6 b' define([' | |||||
616 | ajax_error_msg : ajax_error_msg, |
|
579 | ajax_error_msg : ajax_error_msg, | |
617 | log_ajax_error : log_ajax_error, |
|
580 | log_ajax_error : log_ajax_error, | |
618 | requireCodeMirrorMode : requireCodeMirrorMode, |
|
581 | requireCodeMirrorMode : requireCodeMirrorMode, | |
619 | decode_utf8: decode_utf8, |
|
|||
620 | }; |
|
582 | }; | |
621 |
|
583 | |||
622 | // Backwards compatability. |
|
584 | // Backwards compatability. |
@@ -7,7 +7,8 b' define([' | |||||
7 | 'base/js/utils', |
|
7 | 'base/js/utils', | |
8 | 'services/kernels/js/comm', |
|
8 | 'services/kernels/js/comm', | |
9 | 'widgets/js/init', |
|
9 | 'widgets/js/init', | |
10 | ], function(IPython, $, utils, comm, widgetmanager) { |
|
10 | './serialize' | |
|
11 | ], function(IPython, $, utils, comm, widgetmanager, serialize) { | |||
11 | "use strict"; |
|
12 | "use strict"; | |
12 |
|
13 | |||
13 | /** |
|
14 | /** | |
@@ -846,57 +847,11 b' define([' | |||||
846 | } |
|
847 | } | |
847 | }; |
|
848 | }; | |
848 |
|
849 | |||
849 |
|
||||
850 | Kernel.prototype._deserialize_binary_message = function(blob, callback) { |
|
|||
851 | // deserialize the binary message format |
|
|||
852 | // callback will be called with a message whose buffers attribute |
|
|||
853 | // will be an array of DataViews. |
|
|||
854 | var reader = new FileReader(); |
|
|||
855 | reader.onload = function(e) { |
|
|||
856 | var data = new DataView(this.result); |
|
|||
857 | // read the header: 1 + nbufs 32b integers |
|
|||
858 | var nbufs = data.getInt32(0); |
|
|||
859 | var offsets = []; |
|
|||
860 | var i; |
|
|||
861 | for (i = 1; i <= nbufs; i++) { |
|
|||
862 | offsets.push(data.getInt32(i * 4)); |
|
|||
863 | } |
|
|||
864 | // the first chunk is the message as utf-8 JSON |
|
|||
865 | var msg = $.parseJSON( |
|
|||
866 | utis.decode_utf8( |
|
|||
867 | new Uint8Array(this.result.slice(offsets[0], offsets[1])) |
|
|||
868 | ) |
|
|||
869 | ); |
|
|||
870 | // the remaining chunks are stored as DataViews in msg.buffers |
|
|||
871 | msg.buffers = []; |
|
|||
872 | var start, stop; |
|
|||
873 | for (i = 1; i < nbufs; i++) { |
|
|||
874 | start = offsets[i]; |
|
|||
875 | stop = offsets[i+1]; |
|
|||
876 | msg.buffers.push(new DataView(this.result.slice(start, stop))); |
|
|||
877 | } |
|
|||
878 | callback(msg); |
|
|||
879 | }; |
|
|||
880 | reader.readAsArrayBuffer(blob); |
|
|||
881 | }; |
|
|||
882 |
|
||||
883 |
|
||||
884 | Kernel.prototype._deserialize_msg = function (e, callback) { |
|
|||
885 | // deserialze a message and pass the unpacked message object to callback |
|
|||
886 | if (typeof e.data === "string") { |
|
|||
887 | // text JSON message |
|
|||
888 | callback($.parseJSON(e.data)); |
|
|||
889 | } else { |
|
|||
890 | // binary message |
|
|||
891 | this._deserialize_binary_message(e.data, callback); |
|
|||
892 | } |
|
|||
893 | }; |
|
|||
894 |
|
||||
895 | /** |
|
850 | /** | |
896 | * @function _handle_shell_reply |
|
851 | * @function _handle_shell_reply | |
897 | */ |
|
852 | */ | |
898 | Kernel.prototype._handle_shell_reply = function (e) { |
|
853 | Kernel.prototype._handle_shell_reply = function (e) { | |
899 |
|
|
854 | serialize.deserialize(e.data, $.proxy(this._finish_shell_reply, this)); | |
900 | }; |
|
855 | }; | |
901 |
|
856 | |||
902 | Kernel.prototype._finish_shell_reply = function (reply) { |
|
857 | Kernel.prototype._finish_shell_reply = function (reply) { | |
@@ -1027,7 +982,7 b' define([' | |||||
1027 | * @function _handle_iopub_message |
|
982 | * @function _handle_iopub_message | |
1028 | */ |
|
983 | */ | |
1029 | Kernel.prototype._handle_iopub_message = function (e) { |
|
984 | Kernel.prototype._handle_iopub_message = function (e) { | |
1030 |
|
|
985 | serialize.deserialize(e.data, $.proxy(this._finish_iopub_message, this)); | |
1031 | }; |
|
986 | }; | |
1032 |
|
987 | |||
1033 |
|
988 | |||
@@ -1042,7 +997,7 b' define([' | |||||
1042 | * @function _handle_input_request |
|
997 | * @function _handle_input_request | |
1043 | */ |
|
998 | */ | |
1044 | Kernel.prototype._handle_input_request = function (e) { |
|
999 | Kernel.prototype._handle_input_request = function (e) { | |
1045 |
|
|
1000 | serialize.deserialize(e.data, $.proxy(this._finish_input_request, this)); | |
1046 | }; |
|
1001 | }; | |
1047 |
|
1002 | |||
1048 |
|
1003 |
General Comments 0
You need to be logged in to leave comments.
Login now