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