Chromium Code Reviews| Index: native_client_sdk/src/examples/demo/nacl_io/example.js |
| diff --git a/native_client_sdk/src/examples/demo/nacl_io/example.js b/native_client_sdk/src/examples/demo/nacl_io/example.js |
| index 1a13580c6d7145cece2e2edb729f86d1fd839e8d..e2cc1fa666f2c197ab7ce982cf4eb18c9cd76b1f 100644 |
| --- a/native_client_sdk/src/examples/demo/nacl_io/example.js |
| +++ b/native_client_sdk/src/examples/demo/nacl_io/example.js |
| @@ -40,6 +40,32 @@ function attachListeners() { |
| var func = window[id]; |
| buttonEl.addEventListener('click', func); |
| } |
| + |
| + $('pipe_input_box').addEventListener('keypress', onPipeInput) |
| + $('pipe_output').disabled = true; |
| + |
| + var onChange = function(e) { |
|
binji
2014/05/01 20:22:31
this function is small enough to inline, IMO
Sam Clegg
2014/05/01 22:16:55
Done.
|
| + $('pipe_output').value = ''; |
| + } |
| + $('pipe_name').addEventListener('change', onChange); |
| +} |
| + |
| +// Called with keypress events on the pipe input box |
| +function onPipeInput(e) { |
| + // Create an arraybuffer containing the 16-bit char code |
| + // from the keypress event. |
| + var buffer = new ArrayBuffer(1*2); |
| + var bufferView = new Uint16Array(buffer); |
| + bufferView[0] = e.charCode; |
| + |
| + // Pass the buffer in a dictionary over the NaCl module |
| + var pipeSelect = $('pipe_name'); |
| + var pipeName = pipeSelect[pipeSelect.selectedIndex].value; |
| + var message = {}; |
| + message[pipeName] = ['write', buffer]; |
| + nacl_module.postMessage(message); |
| + e.preventDefault(); |
| + return false; |
| } |
| function onRadioClicked(e) { |
| @@ -279,24 +305,46 @@ function postCall(func) { |
| }); |
| } |
| +function ArrayBufferToString(buf) { |
| + return String.fromCharCode.apply(null, new Uint16Array(buf)); |
|
binji
2014/05/01 20:22:31
probably not an issue here, but this fails for lar
|
| +} |
| + |
| // Called by the common.js module. |
| function handleMessage(message_event) { |
| var data = message_event.data; |
| if ((typeof(data) === 'string' || data instanceof String)) { |
| common.logMessage(data); |
| - } else { |
| - // Result from a function call. |
| - var params = data.args; |
| - var funcName = data.cmd; |
| - var callback = funcToCallback[funcName]; |
| - |
| - if (!callback) { |
| - common.logMessage('Error: Bad message ' + funcName + |
| - ' received from NaCl module.'); |
| - return; |
| + } else if (data instanceof Object) { |
| + var channelName = Object.keys(data)[0] |
|
binji
2014/05/01 20:22:31
This seems clunky to me. It would be nicer to have
Sam Clegg
2014/05/01 22:16:55
I guess it will make the code slightly large and t
|
| + if (channelName == 'jspipe1' || |
|
binji
2014/05/01 20:22:31
nit: === here and everywhere
Sam Clegg
2014/05/01 22:16:55
Done.
|
| + channelName == 'jspipe2' || |
| + channelName == 'jspipe3') { |
| + // Message for JavaScript I/O pipe |
| + var command = data[channelName]; |
| + if (command[0] == 'write') { |
| + $('pipe_output').value += ArrayBufferToString(command[1]); |
| + } else if (command[0] == 'ack') { |
| + common.logMessage(channelName + ": ack:" + command[1]); |
| + } else { |
| + common.logMessage('Got unexpected command: ' + command); |
| + } |
| + } else { |
| + // Result from a function call. |
| + var params = data.args; |
| + var funcName = data.cmd; |
| + var callback = funcToCallback[funcName]; |
| + |
| + if (!callback) { |
| + common.logMessage('Error: Bad message ' + funcName + |
| + ' received from NaCl module.'); |
| + return; |
| + } |
| + |
| + delete funcToCallback[funcName]; |
| + callback.apply(null, params); |
| } |
| - |
| - delete funcToCallback[funcName]; |
| - callback.apply(null, params); |
| + } else { |
| + common.logMessage('Error: Unknow message `' + data + |
| + '` received from NaCl module.'); |
| } |
| } |