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