-
Notifications
You must be signed in to change notification settings - Fork 22
/
example.js
executable file
·346 lines (306 loc) · 10.9 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function moduleDidLoad() {
common.hideModule();
}
function $(id) {
return document.getElementById(id);
}
// Called by the common.js module.
function domContentLoaded(name, tc, config, width, height) {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024 * 1024,
function(bytes) {
common.updateStatus(
'Allocated ' + bytes + ' bytes of persistent storage. Running the first time will take 17 seconds to load');
common.attachDefaultListeners();
common.createNaClModule(name, tc, config, width, height);
},
function(e) { alert('Failed to allocate space') });
}
// Called by the common.js module.
function attachListeners() {
var radioEls = document.querySelectorAll('input[type="radio"]');
for (var i = 0; i < radioEls.length; ++i) {
radioEls[i].addEventListener('click', onRadioClicked);
}
// Wire up the 'click' event for each function's button.
var functionEls = document.querySelectorAll('.function');
for (var i = 0; i < functionEls.length; ++i) {
var functionEl = functionEls[i];
var id = functionEl.getAttribute('id');
var buttonEl = functionEl.querySelector('button');
// The function name matches the element id.
var func = window[id];
buttonEl.addEventListener('click', func);
}
//$('pipe_input_box').addEventListener('keypress', onPipeInput)
//$('pipe_output').disabled = true;
//$('pipe_name').addEventListener('change', function() { $('pipe_output').value = ''; })
}
// 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 = {
pipe: pipeName,
operation: 'write',
payload: buffer,
};
nacl_module.postMessage(message);
e.preventDefault();
return false;
}
function onRadioClicked(e) {
var divId = this.id.slice(5); // skip "radio"
var functionEls = document.querySelectorAll('.function');
for (var i = 0; i < functionEls.length; ++i) {
var visible = functionEls[i].id === divId;
if (functionEls[i].id === divId)
functionEls[i].removeAttribute('hidden');
else
functionEls[i].setAttribute('hidden', '');
}
}
function addNameToSelectElements(cssClass, handle, name) {
var text = '[' + handle + '] ' + name;
var selectEls = document.querySelectorAll(cssClass);
for (var i = 0; i < selectEls.length; ++i) {
var optionEl = document.createElement('option');
optionEl.setAttribute('value', handle);
optionEl.appendChild(document.createTextNode(text));
selectEls[i].appendChild(optionEl);
}
}
function removeNameFromSelectElements(cssClass, handle) {
var optionEls = document.querySelectorAll(cssClass + ' > option');
for (var i = 0; i < optionEls.length; ++i) {
var optionEl = optionEls[i];
if (optionEl.value == handle) {
var selectEl = optionEl.parentNode;
selectEl.removeChild(optionEl);
}
}
}
var filehandle_map = {};
var dirhandle_map = {};
function fopen(e) {
var filename = $('fopenFilename').value;
var access = $('fopenMode').value;
postCall('fopen', filename, access, function(filename, filehandle) {
filehandle_map[filehandle] = filename;
addNameToSelectElements('.file-handle', filehandle, filename);
common.logMessage('File ' + filename + ' opened successfully.');
});
}
function fclose(e) {
var filehandle = parseInt($('fcloseHandle').value, 10);
postCall('fclose', filehandle, function(filehandle) {
var filename = filehandle_map[filehandle];
removeNameFromSelectElements('.file-handle', filehandle, filename);
common.logMessage('File ' + filename + ' closed successfully.');
});
}
function fread(e) {
var filehandle = parseInt($('freadHandle').value, 10);
var numBytes = parseInt($('freadBytes').value, 10);
postCall('fread', filehandle, numBytes, function(filehandle, data) {
var filename = filehandle_map[filehandle];
common.logMessage('Read "' + data + '" from file ' + filename + '.');
});
}
function fwrite(e) {
var filehandle = parseInt($('fwriteHandle').value, 10);
var data = $('fwriteData').value;
postCall('fwrite', filehandle, data, function(filehandle, bytesWritten) {
var filename = filehandle_map[filehandle];
common.logMessage('Wrote ' + bytesWritten + ' bytes to file ' + filename +
'.');
});
}
function fseek(e) {
var filehandle = parseInt($('fseekHandle').value, 10);
var offset = parseInt($('fseekOffset').value, 10);
var whence = parseInt($('fseekWhence').value, 10);
postCall('fseek', filehandle, offset, whence, function(filehandle, filepos) {
var filename = filehandle_map[filehandle];
common.logMessage('Seeked to location ' + filepos + ' in file ' + filename +
'.');
});
}
function fflush(e) {
var filehandle = parseInt($('fflushHandle').value, 10);
postCall('fflush', filehandle, function(filehandle, filepos) {
var filename = filehandle_map[filehandle];
common.logMessage('flushed ' + filename + '.');
});
}
function stat(e) {
var filename = $('statFilename').value;
postCall('stat', filename, function(filename, size) {
common.logMessage('File ' + filename + ' has size ' + size + '.');
});
}
function opendir(e) {
var dirname = $('opendirDirname').value;
postCall('opendir', dirname, function(dirname, dirhandle) {
dirhandle_map[dirhandle] = dirname;
addNameToSelectElements('.dir-handle', dirhandle, dirname);
common.logMessage('Directory ' + dirname + ' opened successfully.');
});
}
function readdir(e) {
var dirhandle = parseInt($('readdirHandle').value, 10);
postCall('readdir', dirhandle, function(dirhandle, ino, name) {
var dirname = dirhandle_map[dirhandle];
if (ino === undefined) {
common.logMessage('End of directory.');
} else {
common.logMessage('Read entry ("' + name + '", ino = ' + ino +
') from directory ' + dirname + '.');
}
});
}
function closedir(e) {
var dirhandle = parseInt($('closedirHandle').value, 10);
postCall('closedir', dirhandle, function(dirhandle) {
var dirname = dirhandle_map[dirhandle];
delete dirhandle_map[dirhandle];
removeNameFromSelectElements('.dir-handle', dirhandle, dirname);
common.logMessage('Directory ' + dirname + ' closed successfully.');
});
}
function mkdir(e) {
var dirname = $('mkdirDirname').value;
var mode = parseInt($('mkdirMode').value, 10);
postCall('mkdir', dirname, mode, function(dirname) {
common.logMessage('Directory ' + dirname + ' created successfully.');
});
}
function rmdir(e) {
var dirname = $('rmdirDirname').value;
postCall('rmdir', dirname, function(dirname) {
common.logMessage('Directory ' + dirname + ' removed successfully.');
});
}
function chdir(e) {
var dirname = $('chdirDirname').value;
postCall('chdir', dirname, function(dirname) {
common.logMessage('Changed directory to: ' + dirname + '.');
});
}
function getcwd(e) {
postCall('getcwd', function(dirname) {
common.logMessage('getcwd: ' + dirname + '.');
});
}
function getaddrinfo(e) {
var name = $('getaddrinfoName').value;
var family = $('getaddrinfoFamily').value;
postCall('getaddrinfo', name, family, function(name, addrType) {
common.logMessage('getaddrinfo returned successfully');
common.logMessage('ai_cannonname = ' + name + '.');
var count = 1;
for (var i = 1; i < arguments.length; i+=2) {
var msg = 'Address number ' + count + ' = ' + arguments[i] +
' (' + arguments[i+1] + ')';
common.logMessage(msg);
count += 1;
}
});
}
function gethostbyname(e) {
var name = $('gethostbynameName').value;
postCall('gethostbyname', name, function(name, addrType) {
common.logMessage('gethostbyname returned successfully');
common.logMessage('h_name = ' + name + '.');
common.logMessage('h_addr_type = ' + addrType + '.');
for (var i = 2; i < arguments.length; i++) {
common.logMessage('Address number ' + (i-1) + ' = ' + arguments[i] + '.');
}
});
}
function connect(e) {
var host = $('connectHost').value;
var port = parseInt($('connectPort').value, 10);
postCall('connect', host, port, function(sockhandle) {
common.logMessage('connected');
addNameToSelectElements('.sock-handle', sockhandle, '[socket]');
});
}
function recv(e) {
var handle = parseInt($('recvHandle').value, 10);
var bufferSize = parseInt($('recvBufferSize').value, 10);
postCall('recv', handle, bufferSize, function(messageLen, message) {
common.logMessage("received " + messageLen + ' bytes: ' + message);
});
}
function send(e) {
var handle = parseInt($('sendHandle').value, 10);
var message = $('sendMessage').value;
postCall('send', handle, message, function(sentBytes) {
common.logMessage("sent bytes: " + sentBytes);
});
}
function close(e) {
var handle = parseInt($('closeHandle').value, 10);
postCall('close', handle, function(sock) {
removeNameFromSelectElements('.sock-handle', sock, "[socket]");
common.logMessage("closed socket: " + sock);
});
}
var funcToCallback = {};
function postCall(func) {
var callback = arguments[arguments.length - 1];
funcToCallback[func] = callback;
nacl_module.postMessage({
cmd: func,
args: Array.prototype.slice.call(arguments, 1, -1)
});
}
function ArrayBufferToString(buf) { return String.fromCharCode.apply(null, new Uint16Array(buf)); }
// 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 if (data instanceof Object)
{
var pipeName = data['pipe']
if ( pipeName !== undefined )
{
// Message for JavaScript I/O pipe
var operation = data['operation'];
if (operation == 'write') {
$('pipe_output').value += ArrayBufferToString(data['payload']);
} else if (operation == 'ack') {
common.logMessage(pipeName + ": ack:" + data['payload']);
} else {
common.logMessage('Got unexpected pipe operation: ' + operation);
}
}
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);
}
} else {
common.logMessage('Error: Unknow message `' + data + '` received from NaCl module.');
}
}