bluenode 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ module Bluenode
2
+ class NativeModule
3
+ FILES = Dir[File.expand_path(File.join('..', 'shims', '*.js'), __FILE__)].freeze
4
+
5
+ SOURCES = FILES.inject({}) do |memo, filename|
6
+ id = File.basename(filename, '.js')
7
+ fn = lambda { File.read(filename )}
8
+
9
+ memo.merge! id => fn
10
+ end.freeze
11
+
12
+ NAMES = SOURCES.keys.freeze
13
+
14
+ class << self
15
+ def require(context, id)
16
+ return self if id == 'native_module'
17
+
18
+ cached = cache[id]
19
+
20
+ return cached.exports if cached
21
+
22
+ raise LoadError, "no such native module #{id}" unless exist?(id)
23
+
24
+ # process.moduleLoadList.push('NativeModule ' + id)
25
+
26
+ native_module = new(context, id)
27
+ native_module.compile
28
+ native_module.cache
29
+ native_module.exports
30
+ end
31
+
32
+ def exist?(id)
33
+ SOURCES.key?(id)
34
+ end
35
+
36
+ def cache
37
+ @cache ||= {}
38
+ end
39
+
40
+ def sources
41
+ @sources ||= Hash.new do |hash, id|
42
+ hash[id] = SOURCES[id].call
43
+ end
44
+ end
45
+ end
46
+
47
+ attr_accessor :id, :filename, :exports, :loaded
48
+
49
+ def initialize(context, id)
50
+ @context = context
51
+ @filename = id + '.js'
52
+ @id = id
53
+ @exports = context.new_object
54
+ @loaded = false
55
+ end
56
+
57
+ def compile
58
+ wrapped = Module.wrap(self.class.sources[id])
59
+ function = @context.runtime.eval(wrapped, filename)
60
+
61
+ function.call exports, @context.new_function(require_proc), self, filename
62
+
63
+ @loaded = true
64
+ end
65
+
66
+ def cache
67
+ self.class.cache[id] = self
68
+ end
69
+
70
+ private
71
+ def require_proc
72
+ @require_proc ||= lambda do |_, path|
73
+ NativeModule.require @context, path
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,327 @@
1
+ // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
2
+ //
3
+ // THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
4
+ //
5
+ // Originally from narwhal.js (http://narwhaljs.org)
6
+ // Copyright (c) 2009 Thomas Robinson <280north.com>
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ // of this software and associated documentation files (the 'Software'), to
10
+ // deal in the Software without restriction, including without limitation the
11
+ // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
+ // sell copies of the Software, and to permit persons to whom the Software is
13
+ // furnished to do so, subject to the following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be included in
16
+ // all copies or substantial portions of the Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22
+ // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ // UTILITY
26
+ var util = require('util');
27
+ var pSlice = Array.prototype.slice;
28
+
29
+ // 1. The assert module provides functions that throw
30
+ // AssertionError's when particular conditions are not met. The
31
+ // assert module must conform to the following interface.
32
+
33
+ var assert = module.exports = ok;
34
+
35
+ // 2. The AssertionError is defined in assert.
36
+ // new assert.AssertionError({ message: message,
37
+ // actual: actual,
38
+ // expected: expected })
39
+
40
+ assert.AssertionError = function AssertionError(options) {
41
+ this.name = 'AssertionError';
42
+ this.actual = options.actual;
43
+ this.expected = options.expected;
44
+ this.operator = options.operator;
45
+ if (options.message) {
46
+ this.message = options.message;
47
+ this.generatedMessage = false;
48
+ } else {
49
+ this.message = getMessage(this);
50
+ this.generatedMessage = true;
51
+ }
52
+ var stackStartFunction = options.stackStartFunction || fail;
53
+ Error.captureStackTrace(this, stackStartFunction);
54
+ };
55
+
56
+ // assert.AssertionError instanceof Error
57
+ util.inherits(assert.AssertionError, Error);
58
+
59
+ function replacer(key, value) {
60
+ if (util.isUndefined(value)) {
61
+ return '' + value;
62
+ }
63
+ if (util.isNumber(value) && !isFinite(value)) {
64
+ return value.toString();
65
+ }
66
+ if (util.isFunction(value) || util.isRegExp(value)) {
67
+ return value.toString();
68
+ }
69
+ return value;
70
+ }
71
+
72
+ function truncate(s, n) {
73
+ if (util.isString(s)) {
74
+ return s.length < n ? s : s.slice(0, n);
75
+ } else {
76
+ return s;
77
+ }
78
+ }
79
+
80
+ function getMessage(self) {
81
+ return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' +
82
+ self.operator + ' ' +
83
+ truncate(JSON.stringify(self.expected, replacer), 128);
84
+ }
85
+
86
+ // At present only the three keys mentioned above are used and
87
+ // understood by the spec. Implementations or sub modules can pass
88
+ // other keys to the AssertionError's constructor - they will be
89
+ // ignored.
90
+
91
+ // 3. All of the following functions must throw an AssertionError
92
+ // when a corresponding condition is not met, with a message that
93
+ // may be undefined if not provided. All assertion methods provide
94
+ // both the actual and expected values to the assertion error for
95
+ // display purposes.
96
+
97
+ function fail(actual, expected, message, operator, stackStartFunction) {
98
+ throw new assert.AssertionError({
99
+ message: message,
100
+ actual: actual,
101
+ expected: expected,
102
+ operator: operator,
103
+ stackStartFunction: stackStartFunction
104
+ });
105
+ }
106
+
107
+ // EXTENSION! allows for well behaved errors defined elsewhere.
108
+ assert.fail = fail;
109
+
110
+ // 4. Pure assertion tests whether a value is truthy, as determined
111
+ // by !!guard.
112
+ // assert.ok(guard, message_opt);
113
+ // This statement is equivalent to assert.equal(true, !!guard,
114
+ // message_opt);. To test strictly for the value true, use
115
+ // assert.strictEqual(true, guard, message_opt);.
116
+
117
+ function ok(value, message) {
118
+ if (!value) fail(value, true, message, '==', assert.ok);
119
+ }
120
+ assert.ok = ok;
121
+
122
+ // 5. The equality assertion tests shallow, coercive equality with
123
+ // ==.
124
+ // assert.equal(actual, expected, message_opt);
125
+
126
+ assert.equal = function equal(actual, expected, message) {
127
+ if (actual != expected) fail(actual, expected, message, '==', assert.equal);
128
+ };
129
+
130
+ // 6. The non-equality assertion tests for whether two objects are not equal
131
+ // with != assert.notEqual(actual, expected, message_opt);
132
+
133
+ assert.notEqual = function notEqual(actual, expected, message) {
134
+ if (actual == expected) {
135
+ fail(actual, expected, message, '!=', assert.notEqual);
136
+ }
137
+ };
138
+
139
+ // 7. The equivalence assertion tests a deep equality relation.
140
+ // assert.deepEqual(actual, expected, message_opt);
141
+
142
+ assert.deepEqual = function deepEqual(actual, expected, message) {
143
+ if (!_deepEqual(actual, expected)) {
144
+ fail(actual, expected, message, 'deepEqual', assert.deepEqual);
145
+ }
146
+ };
147
+
148
+ function _deepEqual(actual, expected) {
149
+ // 7.1. All identical values are equivalent, as determined by ===.
150
+ if (actual === expected) {
151
+ return true;
152
+
153
+ } else if (util.isBuffer(actual) && util.isBuffer(expected)) {
154
+ if (actual.length != expected.length) return false;
155
+
156
+ for (var i = 0; i < actual.length; i++) {
157
+ if (actual[i] !== expected[i]) return false;
158
+ }
159
+
160
+ return true;
161
+
162
+ // 7.2. If the expected value is a Date object, the actual value is
163
+ // equivalent if it is also a Date object that refers to the same time.
164
+ } else if (util.isDate(actual) && util.isDate(expected)) {
165
+ return actual.getTime() === expected.getTime();
166
+
167
+ // 7.3 If the expected value is a RegExp object, the actual value is
168
+ // equivalent if it is also a RegExp object with the same source and
169
+ // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
170
+ } else if (util.isRegExp(actual) && util.isRegExp(expected)) {
171
+ return actual.source === expected.source &&
172
+ actual.global === expected.global &&
173
+ actual.multiline === expected.multiline &&
174
+ actual.lastIndex === expected.lastIndex &&
175
+ actual.ignoreCase === expected.ignoreCase;
176
+
177
+ // 7.4. Other pairs that do not both pass typeof value == 'object',
178
+ // equivalence is determined by ==.
179
+ } else if (!util.isObject(actual) && !util.isObject(expected)) {
180
+ return actual == expected;
181
+
182
+ // 7.5 For all other Object pairs, including Array objects, equivalence is
183
+ // determined by having the same number of owned properties (as verified
184
+ // with Object.prototype.hasOwnProperty.call), the same set of keys
185
+ // (although not necessarily the same order), equivalent values for every
186
+ // corresponding key, and an identical 'prototype' property. Note: this
187
+ // accounts for both named and indexed properties on Arrays.
188
+ } else {
189
+ return objEquiv(actual, expected);
190
+ }
191
+ }
192
+
193
+ function isArguments(object) {
194
+ return Object.prototype.toString.call(object) == '[object Arguments]';
195
+ }
196
+
197
+ function objEquiv(a, b) {
198
+ if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b))
199
+ return false;
200
+ // an identical 'prototype' property.
201
+ if (a.prototype !== b.prototype) return false;
202
+ //~~~I've managed to break Object.keys through screwy arguments passing.
203
+ // Converting to array solves the problem.
204
+ var aIsArgs = isArguments(a),
205
+ bIsArgs = isArguments(b);
206
+ if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
207
+ return false;
208
+ if (aIsArgs) {
209
+ a = pSlice.call(a);
210
+ b = pSlice.call(b);
211
+ return _deepEqual(a, b);
212
+ }
213
+ try {
214
+ var ka = Object.keys(a),
215
+ kb = Object.keys(b),
216
+ key, i;
217
+ } catch (e) {//happens when one is a string literal and the other isn't
218
+ return false;
219
+ }
220
+ // having the same number of owned properties (keys incorporates
221
+ // hasOwnProperty)
222
+ if (ka.length != kb.length)
223
+ return false;
224
+ //the same set of keys (although not necessarily the same order),
225
+ ka.sort();
226
+ kb.sort();
227
+ //~~~cheap key test
228
+ for (i = ka.length - 1; i >= 0; i--) {
229
+ if (ka[i] != kb[i])
230
+ return false;
231
+ }
232
+ //equivalent values for every corresponding key, and
233
+ //~~~possibly expensive deep test
234
+ for (i = ka.length - 1; i >= 0; i--) {
235
+ key = ka[i];
236
+ if (!_deepEqual(a[key], b[key])) return false;
237
+ }
238
+ return true;
239
+ }
240
+
241
+ // 8. The non-equivalence assertion tests for any deep inequality.
242
+ // assert.notDeepEqual(actual, expected, message_opt);
243
+
244
+ assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
245
+ if (_deepEqual(actual, expected)) {
246
+ fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
247
+ }
248
+ };
249
+
250
+ // 9. The strict equality assertion tests strict equality, as determined by ===.
251
+ // assert.strictEqual(actual, expected, message_opt);
252
+
253
+ assert.strictEqual = function strictEqual(actual, expected, message) {
254
+ if (actual !== expected) {
255
+ fail(actual, expected, message, '===', assert.strictEqual);
256
+ }
257
+ };
258
+
259
+ // 10. The strict non-equality assertion tests for strict inequality, as
260
+ // determined by !==. assert.notStrictEqual(actual, expected, message_opt);
261
+
262
+ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
263
+ if (actual === expected) {
264
+ fail(actual, expected, message, '!==', assert.notStrictEqual);
265
+ }
266
+ };
267
+
268
+ function expectedException(actual, expected) {
269
+ if (!actual || !expected) {
270
+ return false;
271
+ }
272
+
273
+ if (Object.prototype.toString.call(expected) == '[object RegExp]') {
274
+ return expected.test(actual);
275
+ } else if (actual instanceof expected) {
276
+ return true;
277
+ } else if (expected.call({}, actual) === true) {
278
+ return true;
279
+ }
280
+
281
+ return false;
282
+ }
283
+
284
+ function _throws(shouldThrow, block, expected, message) {
285
+ var actual;
286
+
287
+ if (util.isString(expected)) {
288
+ message = expected;
289
+ expected = null;
290
+ }
291
+
292
+ try {
293
+ block();
294
+ } catch (e) {
295
+ actual = e;
296
+ }
297
+
298
+ message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
299
+ (message ? ' ' + message : '.');
300
+
301
+ if (shouldThrow && !actual) {
302
+ fail(actual, expected, 'Missing expected exception' + message);
303
+ }
304
+
305
+ if (!shouldThrow && expectedException(actual, expected)) {
306
+ fail(actual, expected, 'Got unwanted exception' + message);
307
+ }
308
+
309
+ if ((shouldThrow && actual && expected &&
310
+ !expectedException(actual, expected)) || (!shouldThrow && actual)) {
311
+ throw actual;
312
+ }
313
+ }
314
+
315
+ // 11. Expected to throw an error:
316
+ // assert.throws(block, Error_opt, message_opt);
317
+
318
+ assert.throws = function(block, /*optional*/error, /*optional*/message) {
319
+ _throws.apply(this, [true].concat(pSlice.call(arguments)));
320
+ };
321
+
322
+ // EXTENSION! This is annoying to write outside this module.
323
+ assert.doesNotThrow = function(block, /*optional*/message) {
324
+ _throws.apply(this, [false].concat(pSlice.call(arguments)));
325
+ };
326
+
327
+ assert.ifError = function(err) { if (err) {throw err;}};
@@ -0,0 +1,112 @@
1
+ // Copyright Joyent, Inc. and other Node contributors.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a
4
+ // copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ // persons to whom the Software is furnished to do so, subject to the
9
+ // following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included
12
+ // in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ var util = require('util');
23
+
24
+ function Console(stdout, stderr) {
25
+ if (!(this instanceof Console)) {
26
+ return new Console(stdout, stderr);
27
+ }
28
+ if (!stdout || !util.isFunction(stdout.write)) {
29
+ throw new TypeError('Console expects a writable stream instance');
30
+ }
31
+ if (!stderr) {
32
+ stderr = stdout;
33
+ }
34
+ var prop = {
35
+ writable: true,
36
+ enumerable: false,
37
+ configurable: true
38
+ };
39
+ prop.value = stdout;
40
+ Object.defineProperty(this, '_stdout', prop);
41
+ prop.value = stderr;
42
+ Object.defineProperty(this, '_stderr', prop);
43
+ prop.value = {};
44
+ Object.defineProperty(this, '_times', prop);
45
+
46
+ // bind the prototype functions to this Console instance
47
+ var keys = Object.keys(Console.prototype);
48
+ for (var v = 0; v < keys.length; v++) {
49
+ var k = keys[v];
50
+ this[k] = this[k].bind(this);
51
+ }
52
+ }
53
+
54
+ Console.prototype.log = function() {
55
+ this._stdout.write(util.format.apply(this, arguments) + '\n');
56
+ };
57
+
58
+
59
+ Console.prototype.info = Console.prototype.log;
60
+
61
+
62
+ Console.prototype.warn = function() {
63
+ this._stderr.write(util.format.apply(this, arguments) + '\n');
64
+ };
65
+
66
+
67
+ Console.prototype.error = Console.prototype.warn;
68
+
69
+
70
+ Console.prototype.dir = function(object, options) {
71
+ this._stdout.write(util.inspect(object, util._extend({
72
+ customInspect: false
73
+ }, options)) + '\n');
74
+ };
75
+
76
+
77
+ Console.prototype.time = function(label) {
78
+ this._times[label] = Date.now();
79
+ };
80
+
81
+
82
+ Console.prototype.timeEnd = function(label) {
83
+ var time = this._times[label];
84
+ if (!time) {
85
+ throw new Error('No such label: ' + label);
86
+ }
87
+ var duration = Date.now() - time;
88
+ this.log('%s: %dms', label, duration);
89
+ };
90
+
91
+
92
+ Console.prototype.trace = function() {
93
+ // TODO probably can to do this better with V8's debug object once that is
94
+ // exposed.
95
+ var err = new Error;
96
+ err.name = 'Trace';
97
+ err.message = util.format.apply(this, arguments);
98
+ Error.captureStackTrace(err, arguments.callee);
99
+ this.error(err.stack);
100
+ };
101
+
102
+
103
+ Console.prototype.assert = function(expression) {
104
+ if (!expression) {
105
+ var arr = Array.prototype.slice.call(arguments, 1);
106
+ require('assert').ok(false, util.format.apply(this, arr));
107
+ }
108
+ };
109
+
110
+
111
+ module.exports = new Console(process.stdout, process.stderr);
112
+ module.exports.Console = Console;