bb8390f28d085fff42d11d2ccd36ea2892b6d300
[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                         //state.showCompileResult(p.result);
128                         /*
129                         var g = new Json.Generator ();
130
131                         g.pretty = true;
132                         g.indent = 2;
133                         var n = new Json.Node(Json.NodeType.OBJECT);
134                         n.set_object(p.result);
135                         g.set_root (n);
136
137                         GLib.debug("got %s", g.to_data (null));
138                         */
139                         return p.result;
140                          
141                 }
142                 
143                 /**
144                  * extension API concept..
145                  * javascript file.. loaded into jscore, 
146                  * then a method is called, with a string argument (json encoded)
147                  * 
148                  */
149                  /*
150                 public string executeFile(string fname, string call_method, string js_data)
151                                 throws JavascriptError
152                 {
153                         
154                         string file_data;
155                         if (!FileUtils.test (fname, FileTest.EXISTS)) {
156                                 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
157                         }
158                         try {
159                                 FileUtils.get_contents(fname, out file_data);
160                         } catch (GLib.Error e) {
161                                 GLib.debug("Error file load failed: %s", e.message);
162                                 return "";
163                         }
164                         var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
165                         var jmethod = new JSCore.String.with_utf8_c_string(call_method);
166                         //var json_args = new JSCore.String.with_utf8_c_string(js_data);
167                         
168                         JSCore.Value exa;
169                         JSCore.Value exb;
170                         unowned JSCore.Value exc;
171                            JSCore.Value exd;
172                            unowned JSCore.Value exe;
173                         
174                         var goc = new JSCore.Class(  class_definition ); 
175                         var ctx = new JSCore.GlobalContext(goc);
176                         var othis = ctx.get_global_object();
177                         
178                         ctx.evaluate_script (
179                                                 jfile_data,
180                                                 othis,
181                                                 null,
182                                 0,
183                                 out exa
184                                 );
185                         
186                         
187                         if (!othis.has_property(ctx,jmethod)) {
188                                 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", call_method);
189                          
190                         }
191                         
192                         var val =  othis.get_property (ctx, jmethod, out exb);
193                         
194                         if (!val.is_object(ctx)) {
195                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", call_method);
196                         }
197                         var oval = val.to_object(ctx,  out  exc);
198                         
199                         if (!oval.is_function(ctx)) {
200                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", call_method);
201                         }
202                         throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
203                         return "";
204                  
205                          var res = jscore_object_call_as_function(
206                                 ctx, oval, othis, js_data, out exd
207                                 );
208                      // this will never work, as we can not create arrays of Values - due to no 
209                      // free function being available..
210                          //var args =  new JSCore.Value[1] ;
211                          //args[0] = new JSCore.Value.string(ctx,json_args) ;
212                          
213                          //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
214                         // extract the text value from res...
215                          JSCore.String  sv = res.to_string_copy ( ctx,  out  exe);
216                          var length = sv.get_maximum_utf8_c_string_size();
217                          var buf = new char[length];
218                         
219                          sv.get_utf8_c_string( buf, length);
220                          
221                          
222                          print("ret:%s\n",(string)  buf);
223                          return (string) buf;
224                         
225                 }
226                 */
227                  
228                  
229         }
230         
231         
232
233
234 }
235