src/Palete/Javascript.vala
[app.Builder.js] / src / Palete / Javascript.vala
index d2995a3..137fcc1 100644 (file)
@@ -3,6 +3,12 @@
 
 namespace Palete {
 
+       public errordomain JavascriptError {
+               MISSING_METHOD,
+               MISSING_FILE
+               
+       }
+
        Javascript instance = null;
        
        public class Javascript {
@@ -109,31 +115,63 @@ namespace Palete {
                 * then a method is called, with a string argument (json encoded)
                 * 
                 */
-               void executeFile(string filename, string method, string json)
+               public void executeFile(string fname, string method, string json)
                {
+                       string file_data;
+                       if (!FileUtils.test (fname, FileTest.EXISTS)) {
+                               throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
+                       }
+               
+                       FileUtils.get_contents(fname, out file_data);
+                       
+                       var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
+                       var jmethod = new JSCore.String.with_utf8_c_string(method);
+                       var json_args = new JSCore.String.with_utf8_c_string(json);
+                       
                        JSCore.Value ex;
                        
                        var goc = new JSCore.Class(  class_definition ); 
                        var ctx = new JSCore.GlobalContext(goc);
+                       var othis = ctx.get_global_object();
                        
+                       var eval = ctx.evaluate_script (
+                                               jfile_data,
+                                               othis,
+                                               null,
+                               0,
+                               out ex
+                               );
                        
                        
-                       var jmethod = new JSCore.String.with_utf8_c_string(method)
-                       var othis = ctx.get_global_object();
                        if (!othis.has_property(ctx,jmethod)) {
-                               throw new JavascriptErr.MISSING_METHOD ("Plugin excute - missing  %s", method);
-                
+                               throw new JavascriptError.MISSING_METHOD ("Plugin: missing method  %s", method);
                                return;
                        }
-                       var val =  othis.get_property (ctx, 
-                                       
-                                 out ex);
+                       
+                       var val =  othis.get_property (ctx, jmethod, out ex);
+                       
+                       if (!val.is_object(ctx)) {
+                               throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found  %s", method);
+                       }
+                       var oval = val.to_object(ctx, out ex);
+                       
+                       if (!oval.is_function(ctx)) {
+                               throw new JavascriptError.MISSING_METHOD ("Plugin: not a method  %s", method);
+                       }
+                       JSCore.Value[] args = {};
+                       args += new JSCore.Value.string(ctx,json_args);
+                        
+                       var res = oval.call_as_function(ctx, othis, args, out ex);
+                       
+                       
+                       
+                       
                }
                
                
 
        }
-
+