Fix #8039 - library support and add back roopacker dependancy
[roobuilder] / src / Palete / Javascript.vala
1 /*
2 extern JSCore.Value jscore_object_call_as_function(
3         JSCore.Context ctx,
4         JSCore.Object object, 
5         JSCore.Object thisObject,
6         string  val,
7         out JSCore.Value exception
8         );
9         
10 */
11   
12
13 namespace Palete {
14
15         public errordomain JavascriptError {
16                 MISSING_METHOD,
17                 MISSING_FILE
18                 
19         }
20
21         Javascript instance = null;
22         
23         public class Javascript {
24  
25                 public static Javascript singleton()
26                 {
27                         if (instance == null) {
28                                 instance = new Javascript();
29                         }
30                         return instance;
31                 }
32                 public Javascript()
33                 {
34                         //var goc = new JSCore.Class(  class_definition ); 
35                         //this.js_global_context = new JSCore.GlobalContext(goc);
36                         
37
38                 }
39                 
40
41                 
42                 public void validate(string code, JsRender.JsRender file)
43                 {
44  
45                         JSC.Exception? ex;
46                         var  ctx = new JSC.Context();
47                         
48                         //GLib.debug("Check syntax %s", code);
49                         
50                         ctx.check_syntax(code, code.length, JSC.CheckSyntaxMode.SCRIPT, "", 1 ,out ex);
51                         var ar = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
52  
53                         if (ex == null) {
54 //                              file.updateErrors( ar ); // clear old errors ?? 
55                                 this.compressionErrors.begin(code, file.path, (obj, res) => {
56                                         ar = this.compressionErrors.end(res);
57                                         file.updateErrors( ar );
58                                 });
59
60                                 return ;  
61                                  
62                         }
63                         
64                         //GLib.debug("go        t error %d %s", (int)ex.get_line_number() , ex.get_message() );
65                         var diag = new Lsp.Diagnostic.simple((int) ex.get_line_number() -1 , 1, ex.get_message());
66
67                         ar.add(diag);
68                         file.updateErrors( ar );
69                          
70                         
71                 }
72                  
73                 bool packer_running = false;
74                 public  async  new Gee.ArrayList<Lsp.Diagnostic>  compressionErrors(string code , string fn) throws ThreadError
75                 {
76                         // this uses the roojspacker code to try and compress the code.
77                         // it should highlight errors before we actually push live the code.
78                         SourceFunc callback = compressionErrors.callback;
79                         Json.Object ret = new Json.Object();
80                     var ar = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
81                         
82                         if (this.packer_running) {
83                                 return ar;
84                         }
85                         this.packer_running = true;
86                     
87                     ThreadFunc<bool> run = () => {
88
89                         // standard error format:  file %s, line %s, Error 
90                           
91                                 var cfg = new JSDOC.PackerRun();
92                                 cfg.opt_keep_whitespace = false;
93                                 cfg.opt_skip_scope = false;
94                                 cfg.opt_dump_tokens = false;                    
95                                 cfg.opt_clean_cache = false;
96                                 
97
98                                 var p = new JSDOC.Packer(cfg);
99                                  
100                           
101                                 p.packFile(code, fn,"");
102                                  
103                                 ret = p.result;
104                                 Idle.add((owned) callback); 
105                                 return true;
106                         };
107                         new Thread<bool>("roopacker", run);
108                         yield;
109                         this.packer_running = false;                    
110                         if (!ret.has_member(fn)) {
111                                 return ar;
112                         }
113                         var jar = ret.get_array_member(fn);
114                         for(var i = 0; i < jar.get_length(); i++ ){
115                                 var d = jar.get_object_element(i);
116                                 
117                                 ar.add(
118                                         new Lsp.Diagnostic.simple((int) d.get_int_member("line") , 1, d.get_string_member("message"))
119                                 );
120                         }
121                         
122                         return ar;
123                 
124                 
125                 }
126                  
127          
128                 
129                 /**
130                  * extension API concept..
131                  * javascript file.. loaded into jscore, 
132                  * then a method is called, with a string argument (json encoded)
133                  * 
134                  */
135                  /*
136                 public string executeFile(string fname, string call_method, string js_data)
137                                 throws JavascriptError
138                 {
139                         
140                         string file_data;
141                         if (!FileUtils.test (fname, FileTest.EXISTS)) {
142                                 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
143                         }
144                         try {
145                                 FileUtils.get_contents(fname, out file_data);
146                         } catch (GLib.Error e) {
147                                 GLib.debug("Error file load failed: %s", e.message);
148                                 return "";
149                         }
150                         var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
151                         var jmethod = new JSCore.String.with_utf8_c_string(call_method);
152                         //var json_args = new JSCore.String.with_utf8_c_string(js_data);
153                         
154                         JSCore.Value exa;
155                         JSCore.Value exb;
156                         unowned JSCore.Value exc;
157                            JSCore.Value exd;
158                            unowned JSCore.Value exe;
159                         
160                         var goc = new JSCore.Class(  class_definition ); 
161                         var ctx = new JSCore.GlobalContext(goc);
162                         var othis = ctx.get_global_object();
163                         
164                         ctx.evaluate_script (
165                                                 jfile_data,
166                                                 othis,
167                                                 null,
168                                 0,
169                                 out exa
170                                 );
171                         
172                         
173                         if (!othis.has_property(ctx,jmethod)) {
174                                 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", call_method);
175                          
176                         }
177                         
178                         var val =  othis.get_property (ctx, jmethod, out exb);
179                         
180                         if (!val.is_object(ctx)) {
181                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", call_method);
182                         }
183                         var oval = val.to_object(ctx,  out  exc);
184                         
185                         if (!oval.is_function(ctx)) {
186                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", call_method);
187                         }
188                         throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
189                         return "";
190                  
191                          var res = jscore_object_call_as_function(
192                                 ctx, oval, othis, js_data, out exd
193                                 );
194                      // this will never work, as we can not create arrays of Values - due to no 
195                      // free function being available..
196                          //var args =  new JSCore.Value[1] ;
197                          //args[0] = new JSCore.Value.string(ctx,json_args) ;
198                          
199                          //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
200                         // extract the text value from res...
201                          JSCore.String  sv = res.to_string_copy ( ctx,  out  exe);
202                          var length = sv.get_maximum_utf8_c_string_size();
203                          var buf = new char[length];
204                         
205                          sv.get_utf8_c_string( buf, length);
206                          
207                          
208                          print("ret:%s\n",(string)  buf);
209                          return (string) buf;
210                         
211                 }
212                 */
213                  
214                  
215         }
216         
217         
218
219
220 }
221