1 /*
2 * Copyright (c) 2012 Joyent, Inc. All rights reserved.
3 */
4
5 /*
6 * 'example' is a native C module that provides a partial implementation of
7 * an unsigned 64-bit integer type. This is a simple consumer that
8 * demonstrates creation and manipulation of these objects, which should be
9 * self-explanatory. See example.c for the implementation.
10 */
11
12 var example = require('./example');
13 var util = require('util');
14 var EventEmitter = require('events').EventEmitter;
15
16 var e = example.create();
17 var f = example.create('8000000000');
18
19 console.log('e = ' + e.toString());
20 console.log('f = ' + f.toString());
21
22 e.add(132);
23 console.log('e = ' + e.toString());
24
25 try {
26 e.set(0x1111111111111111);
27 } catch (e) {
28 console.log('got exception: ' + e.name + ' (' + e.message + ')');
29 }
30
31 e.set('0x1111111111111111');
32 console.log('e = ' + e.toString());
33
34 e.multiply(5);
35 console.log('e = ' + e.toString());
36
37 try {
38 e.toString(33, 'fred');
39 } catch (e) {
40 console.log('got exception: ' + e.name + ' (' + e.message + ')');
41 console.log(util.inspect(e, false, null));
42 }
43
44 e.set(50000000);
45 f.set(22222222);
46 e.multiply(f.toString());
47 console.log('e = ' + e.toString());
48 console.log('f = ' + f.toString());
49
50 e.set(33);
51 e.multiplyAsync(100, function () {
52 console.log('background e = ' + e.toString());
53 });
54
55 function
56 Wrapper(ex)
57 {
58 var self = this;
59
60 ex.__emit = function (name) {
61 var args = Array.prototype.slice.call(arguments);
62 self.emit.apply(self, args);
63 };
64 }
65
66 util.inherits(Wrapper, EventEmitter);
67
68 var g = example.create('543');
69 var gw = new Wrapper(g);
70
71 gw.on('add', function () {
72 console.log('someone added something');
73 });
74
75 g.add(200);
76 g.add(300);
77 g.add(400);
78
79 console.log('1000000000000000000 + 3333333333333333333 = ' +
80 example.static_add('1000000000000000000', '3333333333333333333'));
81
82 console.log(example.static_object());