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