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