use utf8.js

move message serialization to kernel.serialize module
pull/37/head
MinRK 12 years ago
parent c9c131ebf9
commit 6cef7fcb86

@ -554,43 +554,6 @@ define([
);
};
var decode_utf8 = function (array) {
// Decode UTF8 Uint8Array to String
// I can't believe Javascript makes us do this
// From http://stackoverflow.com/questions/17191945
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
};
var utils = {
regex_split : regex_split,
uuid : uuid,
@ -616,7 +579,6 @@ define([
ajax_error_msg : ajax_error_msg,
log_ajax_error : log_ajax_error,
requireCodeMirrorMode : requireCodeMirrorMode,
decode_utf8: decode_utf8,
};
// Backwards compatability.

@ -7,7 +7,8 @@ define([
'base/js/utils',
'services/kernels/js/comm',
'widgets/js/init',
], function(IPython, $, utils, comm, widgetmanager) {
'./serialize'
], function(IPython, $, utils, comm, widgetmanager, serialize) {
"use strict";
/**
@ -846,57 +847,11 @@ define([
}
};
Kernel.prototype._deserialize_binary_message = function(blob, callback) {
// deserialize the binary message format
// callback will be called with a message whose buffers attribute
// will be an array of DataViews.
var reader = new FileReader();
reader.onload = function(e) {
var data = new DataView(this.result);
// read the header: 1 + nbufs 32b integers
var nbufs = data.getInt32(0);
var offsets = [];
var i;
for (i = 1; i <= nbufs; i++) {
offsets.push(data.getInt32(i * 4));
}
// the first chunk is the message as utf-8 JSON
var msg = $.parseJSON(
utis.decode_utf8(
new Uint8Array(this.result.slice(offsets[0], offsets[1]))
)
);
// the remaining chunks are stored as DataViews in msg.buffers
msg.buffers = [];
var start, stop;
for (i = 1; i < nbufs; i++) {
start = offsets[i];
stop = offsets[i+1];
msg.buffers.push(new DataView(this.result.slice(start, stop)));
}
callback(msg);
};
reader.readAsArrayBuffer(blob);
};
Kernel.prototype._deserialize_msg = function (e, callback) {
// deserialze a message and pass the unpacked message object to callback
if (typeof e.data === "string") {
// text JSON message
callback($.parseJSON(e.data));
} else {
// binary message
this._deserialize_binary_message(e.data, callback);
}
};
/**
* @function _handle_shell_reply
*/
Kernel.prototype._handle_shell_reply = function (e) {
this._deserialize_msg(e, $.proxy(this._finish_shell_reply, this));
serialize.deserialize(e.data, $.proxy(this._finish_shell_reply, this));
};
Kernel.prototype._finish_shell_reply = function (reply) {
@ -1027,7 +982,7 @@ define([
* @function _handle_iopub_message
*/
Kernel.prototype._handle_iopub_message = function (e) {
this._deserialize_msg(e, $.proxy(this._finish_iopub_message, this));
serialize.deserialize(e.data, $.proxy(this._finish_iopub_message, this));
};
@ -1042,7 +997,7 @@ define([
* @function _handle_input_request
*/
Kernel.prototype._handle_input_request = function (e) {
this._deserialize_msg(e, $.proxy(this._finish_input_request, this));
serialize.deserialize(e.data, $.proxy(this._finish_input_request, this));
};

@ -0,0 +1,61 @@
// Copyright (c) IPython Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'components/utf8/utf8'
], function ($, utf8) {
"use strict";
var _deserialize_binary = function(blob, callback) {
// deserialize the binary message format
// callback will be called with a message whose buffers attribute
// will be an array of DataViews.
var reader = new FileReader();
reader.onload = function () {
var buf = this.result; // an ArrayBuffer
var data = new DataView(buf);
// read the header: 1 + nbufs 32b integers
var nbufs = data.getInt32(0);
var offsets = [];
var i;
for (i = 1; i <= nbufs; i++) {
offsets.push(data.getInt32(i * 4));
}
// have to convert array to string for utf8.js
var bytestring = String.fromCharCode.apply(null,
new Uint8Array(buf.slice(offsets[0], offsets[1]))
);
var msg = $.parseJSON(
utf8.decode(
bytestring
)
);
// the remaining chunks are stored as DataViews in msg.buffers
msg.buffers = [];
var start, stop;
for (i = 1; i < nbufs; i++) {
start = offsets[i];
stop = offsets[i+1] || buf.byteLength;
msg.buffers.push(new DataView(buf.slice(start, stop)));
}
callback(msg);
};
reader.readAsArrayBuffer(blob);
};
var deserialize = function (data, callback) {
// deserialize a message and pass the unpacked message object to callback
if (typeof data === "string") {
// text JSON message
callback($.parseJSON(data));
} else {
// binary message
_deserialize_binary(data, callback);
}
};
return {
deserialize : deserialize
};
});
Loading…
Cancel
Save