src/Palete/Javascript.vala
[app.Builder.js] / src / Palete / Javascript.vala
1
2  
3
4 namespace Palete {
5
6         public errordomain JavascriptError {
7                 MISSING_METHOD,
8                 MISSING_FILE
9                 
10         }
11
12         Javascript instance = null;
13         
14         public class Javascript {
15
16
17                 public static JSCore.Object class_constructor(
18                                 JSCore.Context ctx, 
19                                 JSCore.Object constructor,  
20                                 JSCore.Value[] arguments, 
21                               out JSCore.Value exception) 
22                 {
23                         var c = new JSCore.Class (class_definition);
24                         var o = new JSCore.Object (ctx, c, null);
25                                 exception = null;
26                         return o;
27                 }
28                 static const JSCore.StaticFunction[] class_functions = {
29                          { null, null, 0 }
30                 };
31                 static const JSCore.ClassDefinition class_definition = {
32                     0,
33                     JSCore.ClassAttribute.None,
34                     "App",
35                     null,
36
37                     null,
38                     class_functions,
39
40                     null,
41                     null,
42
43                     null,
44                     null,
45                     null,
46                     null,
47
48                     null,
49                     null,
50                     class_constructor,
51                     null,
52                     null
53                 };
54
55                 
56                 public JSCore.GlobalContext js_global_context =  null;
57
58                 public static Javascript singleton()
59                 {
60                         if (instance == null) {
61                                 instance = new Javascript();
62                         }
63                         return instance;
64                 }
65                 public Javascript()
66                 {
67                         var goc = new JSCore.Class(  class_definition ); 
68                         this.js_global_context = new JSCore.GlobalContext(goc);
69                         
70
71                 }
72                 public int validate(string code, out string res)
73                 {
74                         JSCore.Value ex;
75                         unowned   JSCore.GlobalContext ctx = this.js_global_context;
76                         var ret = this.js_global_context.check_script_syntax(
77                                    new JSCore.String.with_utf8_c_string(code),
78                                    null,
79                                    0,
80                                    out ex
81                         );
82                         res = ""; 
83                         if (ex.is_null(ctx)) {
84                                 return -1;
85                         }
86
87                         
88                         var exo = ex.to_object(ctx, null);
89                         unowned JSCore.PropertyNameArray property_names = exo.copy_property_names (ctx);
90
91                         
92                         
93                          
94                         var js_string = new JSCore.String.with_utf8_c_string("line");
95                         var line = exo.get_property(ctx, js_string, null).to_number(ctx,null);
96                         
97                         
98
99                         // see if we can convert exception string
100                         char *c_string = new char[1024];
101                         var err_string = ex.to_string_copy (ctx, null);
102                         err_string.get_utf8_c_string (c_string, 1023);
103                         res = (string)c_string;
104                         //print ("Error on line %d\n%s\n", (int)line, res); 
105                         
106                         var rline = (int) line;
107                         
108                         return rline > 0 ? rline -1 : 0;
109                 
110                         
111                 }
112                 /**
113                  * extension API concept..
114                  * javascript file.. loaded into jscore, 
115                  * then a method is called, with a string argument (json encoded)
116                  * 
117                  */
118                 public void executeFile(string fname, string method, string json)
119                 {
120                         string file_data;
121                         if (!FileUtils.test (fname, FileTest.EXISTS)) {
122                                 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
123                         }
124                 
125                         FileUtils.get_contents(fname, out file_data);
126                         
127                         var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
128                         var jmethod = new JSCore.String.with_utf8_c_string(method);
129                         var json_args = new JSCore.String.with_utf8_c_string(json);
130                         
131                         JSCore.Value ex;
132                         
133                         var goc = new JSCore.Class(  class_definition ); 
134                         var ctx = new JSCore.GlobalContext(goc);
135                         var othis = ctx.get_global_object();
136                         
137                         var eval = ctx.evaluate_script (
138                                                 jfile_data,
139                                                 othis,
140                                                 null,
141                                 0,
142                                 out ex
143                                 );
144                         
145                         
146                         if (!othis.has_property(ctx,jmethod)) {
147                                 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", method);
148                                 return;
149                         }
150                         
151                         var val =  othis.get_property (ctx, jmethod, out ex);
152                         
153                         if (!val.is_object(ctx)) {
154                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", method);
155                         }
156                         var oval = val.to_object(ctx, out ex);
157                         
158                         if (!oval.is_function(ctx)) {
159                                 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", method);
160                         }
161                         JSCore.Value[] args = {};
162                         args += new JSCore.Value.string(ctx,json_args);
163                          
164                         var res = oval.call_as_function(ctx, othis, args, out ex);
165                         
166                         
167                         
168                         
169                 }
170                 
171                 
172
173         }
174         
175         
176
177
178 }
179