ead98520aa006fc1c64179661fd55ec2dcef9644
[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 Json.Object validate(string code, string fn)
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                  
91                         if (ex == null) {
92                                 return new Json.Object(); // this.compressionErrors(code, fn); << to slow on large files?
93                         
94                                  
95                         }
96  
97                         //GLib.debug("got error %d %s", (int)ex.get_line_number() , ex.get_message() );
98  
99                         var ar  = new Json.Array();
100                         ar.add_string_element(ex.get_message());
101                         
102                         
103
104                         var line_to_err =  new Json.Object();
105
106                         line_to_err.set_array_member(ex.get_line_number().to_string(), ar);
107                         var file_to_line =   new Json.Object();
108                         file_to_line.set_object_member(fn, line_to_err);
109                         var ret =  new Json.Object();
110                         ret.set_object_member("ERR", file_to_line);
111                         
112                         /*
113                         var g = new Json.Generator ();
114
115                         g.pretty = true;
116                         g.indent = 2;
117                         var n = new Json.Node(Json.NodeType.OBJECT);
118                         n.set_object(ret);
119                         g.set_root (n);
120
121                         GLib.debug("got %s", g.to_data (null));
122                         */
123                         return ret;
124                         
125                 }
126                 
127                 
128                 public Json.Object   compressionErrors(string code , string fn)
129                 {
130                         // this uses the roojspacker code to try and compress the code.
131                         // it should highlight errors before we actually push live the code.
132                         
133                         // standard error format:  file %s, line %s, Error 
134                          
135                         
136                         var cfg = new JSDOC.PackerRun();
137                         cfg.opt_keep_whitespace = false;
138                         cfg.opt_skip_scope = false;
139                         cfg.opt_dump_tokens = false;                    
140                         cfg.opt_clean_cache = false;
141                         
142
143                         var p = new JSDOC.Packer(cfg);
144                          
145                   
146                         p.packFile(code, fn,"");
147                         //state.showCompileResult(p.result);
148                         /*
149                         var g = new Json.Generator ();
150
151                         g.pretty = true;
152                         g.indent = 2;
153                         var n = new Json.Node(Json.NodeType.OBJECT);
154                         n.set_object(p.result);
155                         g.set_root (n);
156
157                         GLib.debug("got %s", g.to_data (null));
158                         */
159                         return p.result;
160                          
161                 }
162                 
163                 /**
164                  * extension API concept..
165                  * javascript file.. loaded into jscore, 
166                  * then a method is called, with a string argument (json encoded)
167                  * 
168                  */
169                  /*
170                 public string executeFile(string fname, string call_method, string js_data)
171                                 throws JavascriptError
172                 {
173                         
174                         string file_data;
175                         if (!FileUtils.test (fname, FileTest.EXISTS)) {
176                                 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
177                         }
178                         try {
179                                 FileUtils.get_contents(fname, out file_data);
180                         } catch (GLib.Error e) {
181                                 GLib.debug("Error file load failed: %s", e.message);
182                                 return "";
183                         }
184                         var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
185                         var jmethod = new JSCore.String.with_utf8_c_string(call_method);
186                         //var json_args = new JSCore.String.with_utf8_c_string(js_data);
187                         
188                         JSCore.Value exa;
189                         JSCore.Value exb;
190                         unowned JSCore.Value exc;
191                            JSCore.Value exd;
192                            unowned JSCore.Value exe;
193                         
194                         var goc = new JSCore.Class(  class_definition ); 
195                         var ctx = new JSCore.GlobalContext(goc);
196                         var othis = ctx.get_global_object();
197                         
198                         ctx.evaluate_script (
199                                                 jfile_data,
200                                                 othis,
201                                                 null,
202                                 0,
203                                 out exa
204                                 );
205                         
206                         
207                         if (!othis.has_property(ctx,jmethod)) {
208                                 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", call_method);
209                          
210                         }
211                         
212                         var val =  othis.get_property (ctx, jmethod, out exb);
213                         
214                         if (!val.is_object(ctx)) {
215                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", call_method);
216                         }
217                         var oval = val.to_object(ctx,  out  exc);
218                         
219                         if (!oval.is_function(ctx)) {
220                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", call_method);
221                         }
222                         throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
223                         return "";
224                  
225                          var res = jscore_object_call_as_function(
226                                 ctx, oval, othis, js_data, out exd
227                                 );
228                      // this will never work, as we can not create arrays of Values - due to no 
229                      // free function being available..
230                          //var args =  new JSCore.Value[1] ;
231                          //args[0] = new JSCore.Value.string(ctx,json_args) ;
232                          
233                          //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
234                         // extract the text value from res...
235                          JSCore.String  sv = res.to_string_copy ( ctx,  out  exe);
236                          var length = sv.get_maximum_utf8_c_string_size();
237                          var buf = new char[length];
238                         
239                          sv.get_utf8_c_string( buf, length);
240                          
241                          
242                          print("ret:%s\n",(string)  buf);
243                          return (string) buf;
244                         
245                 }
246                 */
247                  
248                  
249         }
250         
251         
252
253
254 }
255