1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var Node = require('./node');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var Function = module.exports = function Function(name, params, body){
|
24 | Node.call(this);
|
25 | this.name = name;
|
26 | this.params = params;
|
27 | this.block = body;
|
28 | if ('function' == typeof params) this.fn = params;
|
29 | };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | Function.prototype.__defineGetter__('arity', function(){
|
39 | return this.params.length;
|
40 | });
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | Function.prototype.__proto__ = Node.prototype;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | Function.prototype.__defineGetter__('hash', function(){
|
56 | return 'function ' + this.name;
|
57 | });
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | Function.prototype.clone = function(parent){
|
67 | if (this.fn) {
|
68 | var clone = new Function(
|
69 | this.name
|
70 | , this.fn);
|
71 | } else {
|
72 | var clone = new Function(this.name);
|
73 | clone.params = this.params.clone(parent, clone);
|
74 | clone.block = this.block.clone(parent, clone);
|
75 | }
|
76 | clone.lineno = this.lineno;
|
77 | clone.column = this.column;
|
78 | clone.filename = this.filename;
|
79 | return clone;
|
80 | };
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | Function.prototype.toString = function(){
|
90 | if (this.fn) {
|
91 | return this.name
|
92 | + '('
|
93 | + this.fn.toString()
|
94 | .match(/^function *\w*\((.*?)\)/)
|
95 | .slice(1)
|
96 | .join(', ')
|
97 | + ')';
|
98 | } else {
|
99 | return this.name
|
100 | + '('
|
101 | + this.params.nodes.join(', ')
|
102 | + ')';
|
103 | }
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | Function.prototype.toJSON = function(){
|
114 | var json = {
|
115 | __type: 'Function',
|
116 | name: this.name,
|
117 | lineno: this.lineno,
|
118 | column: this.column,
|
119 | filename: this.filename
|
120 | };
|
121 | if (this.fn) {
|
122 | json.fn = this.fn;
|
123 | } else {
|
124 | json.params = this.params;
|
125 | json.block = this.block;
|
126 | }
|
127 | return json;
|
128 | };
|