src/Palete/Javascript.vala
[app.Builder.js] / 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                 static const JSCore.StaticFunction[] class_functions = {
38                          { null, null, 0 }
39                 };
40                 static 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                         var ret = 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 void executeFile(string fname, string call_method, string js_data)
128                 {
129                         string file_data;
130                         if (!FileUtils.test (fname, FileTest.EXISTS)) {
131                                 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
132                         }
133                 
134                         FileUtils.get_contents(fname, out file_data);
135                         
136                         var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
137                         var jmethod = new JSCore.String.with_utf8_c_string(call_method);
138                         //var json_args = new JSCore.String.with_utf8_c_string(js_data);
139                         
140                              JSCore.Value exa;
141                           JSCore.Value exb;
142                         unowned JSCore.Value exc;
143                            JSCore.Value exd;
144                            unowned JSCore.Value exe;
145                         
146                         var goc = new JSCore.Class(  class_definition ); 
147                         var ctx = new JSCore.GlobalContext(goc);
148                         var othis = ctx.get_global_object();
149                         
150                         var eval = ctx.evaluate_script (
151                                                 jfile_data,
152                                                 othis,
153                                                 null,
154                                 0,
155                                 out exa
156                                 );
157                         
158                         
159                         if (!othis.has_property(ctx,jmethod)) {
160                                 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", call_method);
161                                 return;
162                         }
163                         
164                         var val =  othis.get_property (ctx, jmethod, out exb);
165                         
166                         if (!val.is_object(ctx)) {
167                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", call_method);
168                         }
169                         var oval = val.to_object(ctx,  out  exc);
170                         
171                         if (!oval.is_function(ctx)) {
172                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", call_method);
173                         }
174                         
175                  
176                          var res = jscore_object_call_as_function(
177                                 ctx, oval, othis, js_data, out exd
178                                 );
179                      // this will never work, as we can not create arrays of Values - due to no 
180                      // free function being available..
181                          //var args =  new JSCore.Value[1] ;
182                          //args[0] = new JSCore.Value.string(ctx,json_args) ;
183                          
184                          //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
185                         // extract the text value from res...
186                          JSCore.String  sv = res.to_string_copy ( ctx,  out  exe);
187                          var length = sv.get_maximum_utf8_c_string_size();
188                          var buf = new char[length];
189                         
190                          sv.get_utf8_c_string( buf, length);
191                          
192                          
193                          print("ret:%s\n",(string)  buf);
194                          
195                         
196                 }
197                 
198                 
199
200         }
201         
202         
203
204
205 }
206