cb3c20aa935237c1ffe0eec9c7439866ee0d8187
[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 int validate(string code, out string res)
82                 {
83                         JSCore.Value ex;
84                         unowned   JSCore.GlobalContext ctx = this.js_global_context;
85                         this.js_global_context.check_script_syntax(
86                                    new JSCore.String.with_utf8_c_string(code),
87                                    null,
88                                    0,
89                                    out ex
90                         );
91                         res = ""; 
92                         if (ex.is_null(ctx)) {
93                                 return -1;
94                         }
95
96                         
97                         var exo = ex.to_object(ctx, null);
98                         //unowned JSCore.PropertyNameArray property_names = exo.copy_property_names (ctx);
99
100                         
101                         
102                          
103                         var js_string = new JSCore.String.with_utf8_c_string("line");
104                         var line = exo.get_property(ctx, js_string, null).to_number(ctx,null);
105                         
106                         
107
108                         // see if we can convert exception string
109                         char *c_string = new char[1024];
110                         var err_string = ex.to_string_copy (ctx, null);
111                         err_string.get_utf8_c_string (c_string, 1023);
112                         res = (string)c_string;
113                         //print ("Error on line %d\n%s\n", (int)line, res); 
114                         
115                         var rline = (int) line;
116                         
117                         return rline > 0 ? rline -1 : 0;
118                 
119                         
120                 }
121                 /**
122                  * extension API concept..
123                  * javascript file.. loaded into jscore, 
124                  * then a method is called, with a string argument (json encoded)
125                  * 
126                  */
127                 public string executeFile(string fname, string call_method, string js_data)
128                                 throws JavascriptError
129                 {
130                         
131                         string file_data;
132                         if (!FileUtils.test (fname, FileTest.EXISTS)) {
133                                 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
134                         }
135                         try {
136                                 FileUtils.get_contents(fname, out file_data);
137                         } catch (GLib.Error e) {
138                                 GLib.debug("Error file load failed: %s", e.message);
139                                 return "";
140                         }
141                         var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
142                         var jmethod = new JSCore.String.with_utf8_c_string(call_method);
143                         //var json_args = new JSCore.String.with_utf8_c_string(js_data);
144                         
145                         JSCore.Value exa;
146                         JSCore.Value exb;
147                         unowned JSCore.Value exc;
148                            JSCore.Value exd;
149                            unowned JSCore.Value exe;
150                         
151                         var goc = new JSCore.Class(  class_definition ); 
152                         var ctx = new JSCore.GlobalContext(goc);
153                         var othis = ctx.get_global_object();
154                         
155                         ctx.evaluate_script (
156                                                 jfile_data,
157                                                 othis,
158                                                 null,
159                                 0,
160                                 out exa
161                                 );
162                         
163                         
164                         if (!othis.has_property(ctx,jmethod)) {
165                                 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", call_method);
166                          
167                         }
168                         
169                         var val =  othis.get_property (ctx, jmethod, out exb);
170                         
171                         if (!val.is_object(ctx)) {
172                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", call_method);
173                         }
174                         var oval = val.to_object(ctx,  out  exc);
175                         
176                         if (!oval.is_function(ctx)) {
177                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", call_method);
178                         }
179                         
180                  
181                          var res = jscore_object_call_as_function(
182                                 ctx, oval, othis, js_data, out exd
183                                 );
184                      // this will never work, as we can not create arrays of Values - due to no 
185                      // free function being available..
186                          //var args =  new JSCore.Value[1] ;
187                          //args[0] = new JSCore.Value.string(ctx,json_args) ;
188                          
189                          //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
190                         // extract the text value from res...
191                          JSCore.String  sv = res.to_string_copy ( ctx,  out  exe);
192                          var length = sv.get_maximum_utf8_c_string_size();
193                          var buf = new char[length];
194                         
195                          sv.get_utf8_c_string( buf, length);
196                          
197                          
198                          print("ret:%s\n",(string)  buf);
199                          return (string) buf;
200                         
201                 }
202                  
203                 
204
205         }
206         
207         
208
209
210 }
211