The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / file.js @ c8e20fbf
History | View | Annotate | Download (2.31 KB)
1 |
// Some explanation of the File binding.
|
---|---|
2 |
//
|
3 |
// All file operations are blocking. To get around this they are executed
|
4 |
// in a thread pool in C++ (libeio).
|
5 |
//
|
6 |
// The ordering of method calls to a file should be preserved, so they are
|
7 |
// only executed one at a time. A queue, called _actionQueue is employed.
|
8 |
//
|
9 |
// The constructor File() is implemented in C++. It initlizes
|
10 |
// the member _actionQueue = []
|
11 |
//
|
12 |
// Any of the methods called on a file are put into this queue. When they
|
13 |
// reach the head of the queue they will be executed. C++ calles the
|
14 |
// method _pollActions each time it becomes idle. If there is no action
|
15 |
// currently being executed then _pollActions will not be called. Thus when
|
16 |
// actions are added to an empty _actionQueue, they should be immediately
|
17 |
// executed.
|
18 |
//
|
19 |
// When an action has completed, the C++ side is going to look at the first
|
20 |
// element of _actionQueue in order to get a handle on the callback
|
21 |
// function. Only after that completion callback has been made can the
|
22 |
// action be shifted out of the queue.
|
23 |
|
24 |
File.prototype.puts = function (data, callback) { |
25 |
this.write(data + "\n", callback); |
26 |
}; |
27 |
|
28 |
File.prototype.open = function (path, mode, callback) { |
29 |
this._addAction("open", [path, mode], callback); |
30 |
}; |
31 |
|
32 |
File.prototype.close = function (callback) { |
33 |
this._addAction("close", [], callback); |
34 |
}; |
35 |
|
36 |
File.prototype.write = function (buf, callback) { |
37 |
this._addAction("write", [buf], callback); |
38 |
}; |
39 |
|
40 |
File.prototype.read = function (length, callback) { |
41 |
this._addAction("read", [length], callback); |
42 |
}; |
43 |
|
44 |
File.prototype._addAction = function (method, args, callback) { |
45 |
this._actionQueue.push({ method: method |
46 |
, callback: callback
|
47 |
, args: args
|
48 |
}); |
49 |
if (this._actionQueue.length == 1) this._act(); |
50 |
} |
51 |
|
52 |
File.prototype._act = function () { |
53 |
var action = this._actionQueue[0]; |
54 |
if (action)
|
55 |
this["_ffi_" + action.method].apply(this, action.args); |
56 |
}; |
57 |
|
58 |
// called from C++ after each action finishes
|
59 |
// (i.e. when it returns from the thread pool)
|
60 |
File.prototype._pollActions = function () { |
61 |
this._actionQueue.shift();
|
62 |
this._act();
|
63 |
}; |
64 |
|
65 |
var stdout = new File(); |
66 |
stdout.fd = File.STDOUT_FILENO; |
67 |
|
68 |
var stderr = new File(); |
69 |
stderr.fd = File.STDERR_FILENO; |
70 |
|
71 |
|
72 |
|
73 |
Array.prototype.encodeUtf8 = function () { |
74 |
return String.fromCharCode.apply(String, this); |
75 |
} |