JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / Introspect / Basic.js
1 //<script type="text/javascript">
2 //Gtk = imports.gi.Gtk;
3 GI      = imports.gi.GIRepository;
4 GLib    = imports.gi.GLib;
5 xml     = imports.libxml;
6 //GObject = imports.gi.GObject;
7
8 XObject = imports.XObject.XObject;
9 console = imports.console.console;
10
11
12
13 /**
14  * Base class for Properties, functions, methods etc.
15  */
16
17
18 Basic = XObject.define(
19     function( ) {
20          // never called?
21     },
22     Object,
23     {
24         
25         typeToName : function (type_info) {
26             var ty = GI.type_tag_to_string( GI.type_info_get_tag(type_info));
27             
28             if ((ty == 'void') && GI.type_info_is_pointer(type_info)) {
29                 return 'void*'; // it's a fake string?!?!!? - slightly naughty...
30             }
31             if (ty == 'array') {
32                 // array of what!?!?
33                 var param_type = GI.type_info_get_param_type (type_info, 0);
34                 var atype = GI.type_info_get_tag(param_type);
35                 if (atype == GI.TypeTag.UINT8) {
36                     return 'utf8';
37                 }
38                 
39                             
40                 return ty;
41             }
42             if (ty != 'interface') {
43                 return ty;
44             }
45             var interface_info = GI.type_info_get_interface (type_info);        
46             var interface_type = GI.base_info_get_type  ?
47                 GI.base_info_get_type (interface_info) : interface_info.get_type() ;
48                 
49             if (interface_type  == GI.InfoType.CALLBACK) {
50                 // callback.. 
51                 var Callback = imports.Callback.Callback ;
52                 var ret=  new Callback(interface_info, this, false, false);
53                 ret.alias = interface_info.get_namespace() + '.' + interface_info.get_name();
54                 return ret;
55                  
56                 
57             }
58
59             return  interface_info.get_namespace() + '.' + interface_info.get_name();
60             
61         },
62         
63         directions : [ "in", "out"," inout"],
64         
65         argsToArrays : function(m,  returnArray) 
66         {
67             
68             var outIntoReturn = false;
69             if (returnArray && returnArray.length == 1 && returnArray[0].type == 'void') {
70                 outIntoReturn = true;
71             }
72             
73             var outArg = {
74                 name : 'out_values',
75                 ns : '',
76                 type : 'Object',
77                 direction : 'out',
78                 be_null :  true, // in theory..
79                 desc : "Return Object, use an empty Object, and the properties will get set as follows",
80                 properties : []
81             };
82             
83             var args = [];
84             var firstOut = -1;
85             for(var a_i  =0; a_i   < GI.callable_info_get_n_args(m); a_i++) {
86                 var arg = GI.callable_info_get_arg(m, a_i);
87                 
88                 var direction = this.directions[GI.arg_info_get_direction(arg)];
89                 
90                 var add = {
91                     name : arg.get_name(),
92                     ns : arg.get_namespace(),
93                     type : this.typeToName(GI.arg_info_get_type(arg)),
94                     direction : direction,
95                     be_null :  GI.arg_info_may_be_null(arg) || GI.arg_info_is_optional(arg),
96                     desc : arg.get_attribute('doc') || ''
97                 };
98                 
99                 
100                 add.alias = add.ns + '.' + add.name;
101                 
102                 if (direction == 'out') {
103                     firstOut = firstOut  > -1 ? firstOut : a_i;
104                     if (outIntoReturn) {
105                         returnArray.push(add);
106                     } else {
107                         outArg.properties.push(add);
108                     }
109                 }
110                 
111                 
112                 args.push(add);
113             }
114             
115             // drop all the 'out args from the end of args..
116             if (returnArray) {
117                     
118                 while (true) {
119                     if (!args.length) {
120                         break;
121                     }
122                     var l = args.pop(); // drop!
123                     if (l.direction == 'out') {
124                         // got an out arg..
125                         continue;
126                     }
127                     args.push(l); // put it back in (it's not a out).
128                     break;
129                     
130                 }
131             }
132             if (outArg.properties.length) {
133                 // put the out arg in there...
134                 if (firstOut >= args.length) {
135                     args.push(outArg);
136                 } else {
137                     args[firstOut]  = outArg;
138                 }
139             }
140             
141              
142             // remove void return....
143             
144             if (returnArray && returnArray.length > 1 && returnArray[0].type == 'void') {
145                 returnArray.shift();
146             }
147             
148             return args;
149         }
150 });