tests/TestCompiler.vala
[app.Builder.js] / tests / TestCompiler.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 namespace Palete {
5          
6         
7          
8
9         public class TestCompiler : Vala.CodeVisitor {
10
11                 Vala.CodeContext context;
12                 public TestCompiler(string file) {
13                         base();
14                         this.file = file;
15                         
16
17                 }
18                 public void dumpCode(string str) {
19                         var ls = str.split("\n");
20                         for (var i=0;i < ls.length; i++) {
21                                 print("%d : %s\n", i+1, ls[i]);
22                         }
23                 }
24                 
25                 public Gee.HashMap<int,string> checkFile()
26                 {
27                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
28                 }
29
30                 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
31                                         JsRender.Node node, 
32                                         string prop,
33                                         string ptype,
34                                         string val)
35                 {
36                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
37                         var hash = ptype == "listener" ? node.listeners : node.props;
38                         
39                         // untill we get a smarter renderer..
40                         // we have some scenarios where changing the value does not work
41                         if (prop == "* xns" || prop == "xtype") {
42                                 return ret;
43                         }
44                                 
45                         
46                         var old = hash.get(prop);
47                         var newval = "/*--VALACHECK-START--*/ " + val ;
48                         
49                         hash.set(prop, newval);
50                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
51                         hash.set(prop, old);
52                         //print("%s\n", tmpstring);
53                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
54                         var offset =0;
55                         if (bits.length > 0) {
56                                 offset = bits[0].split("\n").length +1;
57                         }
58                         //this.dumpCode(tmpstring);
59                         //print("offset %d\n", offset);
60                         yield this.checkStringThread(tmpstring);
61                         
62                         // modify report
63                         
64                         var iter = this.report.line_errors.map_iterator();
65                         while (iter.next()) {
66                                 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
67                                 // we have to prefix the error with the fake line number 
68                                 // so that it's a unique mark..
69                                  ret.set(iter.get_key() - offset, 
70                                        "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
71                         }
72                         return ret;
73                         
74                 }
75                 
76                 public async  Gee.HashMap<int,string> checkStringThread(string contents)
77                 {
78                         SourceFunc callback = checkStringThread.callback;
79                         var ret = new Gee.HashMap<int,string>();
80                         ThreadFunc<void*> run = () => {
81                                  
82                                 // Pass back result and schedule callback
83                                 ret = this.checkString(contents);
84                                 Idle.add((owned) callback);
85                                 return null;
86                         };
87                         Thread.create<void*>(run, false);
88
89                         // Wait for background thread to schedule our callback
90                         yield;
91                         return ret;
92                 }
93                 
94                 
95                 
96                 public Gee.HashMap<int,string> checkString(string contents)
97                 {
98                         // init context:
99                         var valac = "valac " ;
100                         
101                         context = new Vala.CodeContext ();
102                         Vala.CodeContext.push (context);
103                 
104                         context.experimental = false;
105                         context.experimental_non_null = false;
106                         
107 #if VALA_0_28
108                         var ver=28;
109 #elif VALA_0_26 
110                         var ver=26;
111 #elif VALA_0_24
112                         var ver=24;
113 #elif VALA_0_22 
114                         var ver=22;
115 #endif
116                         
117                         for (int i = 2; i <= ver; i += 2) {
118                                 context.add_define ("VALA_0_%d".printf (i));
119                         }
120                         
121                         
122                         
123                         
124                         
125                         
126                         
127                         var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
128                         // what's the current version of vala???
129                         
130                         
131                         vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
132                         
133                         for(var i =0 ; i < vapidirs.length; i++) {
134                                 valac += " --vapidir=" + vapidirs[i];
135                         }
136                                 
137                         
138                         // or context.get_vapi_path("glib-2.0"); // should return path..
139                         context.vapi_directories = vapidirs;
140                         context.report.enable_warnings = true;
141                         context.metadata_directories = { };
142                         context.gir_directories = {};
143                         context.thread = true;
144                         
145                         
146                         this.report = new ValaSourceReport(this.file);
147                         context.report = this.report;
148                         
149                         
150                         context.basedir = "/tmp"; //Posix.realpath (".");
151                 
152                         context.directory = context.basedir;
153                 
154
155                         // add default packages:
156                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
157                         context.profile = Vala.Profile.GOBJECT;
158                          
159                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
160                         context.root.add_using_directive (ns_ref);
161
162                         var source_file = new Vala.SourceFile (
163                                 context, 
164                                 Vala.SourceFileType.SOURCE, 
165                                         "~~~~~testfile.vala",
166                                         contents
167                         );
168                         source_file.add_using_directive (ns_ref);
169                         context.add_source_file (source_file);
170                         
171                 // add all the files (except the current one) - this.file.path
172                 var pr = ((Project.Gtk)this.file.project);
173                 if (this.file.build_module.length > 0) {
174                                 var cg =  pr.compilegroups.get(this.file.build_module);
175                                 for (var i = 0; i < cg.sources.size; i++) {
176                                         var path = pr.resolve_path(
177                                                         pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
178                                                         
179                                         if (!FileUtils.test(path, FileTest.EXISTS)) {
180                                                 continue;
181                                         }       
182                          
183                                         if (path == this.file.path.replace(".bjs", ".vala")) {
184                                                 valac += " " + path;
185                                                 continue;
186                                         }
187                                         if (FileUtils.test(path, FileTest.IS_DIR)) {
188                                                 continue;
189                                         }
190                                         //print("Add source file %s\n", path);
191                                         
192                                         valac += " " + path;
193                                         
194                                         if (Regex.match_simple("\\.c$", path)) {
195                                                 context.add_c_source_file(path);
196                                                 continue;
197                                         }
198                                         
199                                         
200                                         var xsf = new Vala.SourceFile (
201                                                 context,
202                                                 Vala.SourceFileType.SOURCE, 
203                                                 path
204                                         );
205                                         xsf.add_using_directive (ns_ref);
206                                         context.add_source_file(xsf);
207                                         
208                                 }
209                         }
210                         // default.. packages..
211                         context.add_external_package ("glib-2.0"); 
212                         context.add_external_package ("gobject-2.0");
213                         // user defined ones..
214                         
215                 var dcg = pr.compilegroups.get("_default_");
216                 for (var i = 0; i < dcg.packages.size; i++) {
217                                 valac += " --pkg " + dcg.packages.get(i);
218                                 context.add_external_package (dcg.packages.get(i));
219                         }
220                 
221                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
222                         
223                         // add the modules...
224                         
225                         
226                         
227                         //context.add_external_package ("libvala-0.24");
228                         
229                         this.report.compile_notice("START", "", 0, "");
230
231                 
232                         //add_documented_files (context, settings.source_files);
233                 
234                         Vala.Parser parser = new Vala.Parser ();
235                         parser.parse (context);
236                         //gir_parser.parse (context);
237                         if (context.report.get_errors () > 0) {
238                                 print("parse got errors");
239                                 ((ValaSourceReport)context.report).dump();
240                                 
241                                 Vala.CodeContext.pop ();
242                                 this.report.compile_notice("END", "", 0, "");
243                                 return this.report.line_errors;
244                         }
245
246
247                         
248                         // check context:
249                         context.check ();
250                         if (context.report.get_errors () > 0) {
251                                 print("check got errors");
252                                 ((ValaSourceReport)context.report).dump();
253                                 Vala.CodeContext.pop ();
254                                 this.report.compile_notice("END", "", 0, "");
255                                 return this.report.line_errors;
256                                 
257                         }
258                         
259                         //context.codegen = new Vala.GDBusServerModule ();
260                         
261                          
262                         context.output = "/tmp/testbuild";
263                         valac += " -o " +context.output;
264                         //context.codegen.emit (context);
265                         /*
266                         var ccompiler = new Vala.CCodeCompiler ();
267                         var cc_command = Environment.get_variable ("CC");
268                         var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
269 #if VALA_0_28
270                         ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
271 #else
272                         ccompiler.compile (context, cc_command, new string[] { });
273 #endif
274                         */
275  
276                         Vala.CodeContext.pop ();
277                         //(new Vala.CodeNode()).get_error_types().clear();
278                         //(new Vala.NullType()).get_type_arguments().clear();
279                         (new Vala.NullType(null)).get_type_arguments().clear();
280                         parser = null;
281                         this.report.compile_notice("END", "", 0, "");
282                         print("%s\n", valac);
283                         print("ALL OK?\n");
284                         return this.report.line_errors;
285                 }
286         //
287                 // startpoint:
288                 //
289          
290         }
291 }
292 /*
293 int main (string[] args) {
294
295         var a = new ValaSource(file);
296         a.create_valac_tree();
297         return 0;
298 }
299 */
300
301