6 public errordomain JavascriptError {
12 Javascript instance = null;
14 public class Javascript {
17 public static JSCore.Object class_constructor(
19 JSCore.Object constructor,
20 JSCore.Value[] arguments,
21 out JSCore.Value exception)
23 var c = new JSCore.Class (class_definition);
24 var o = new JSCore.Object (ctx, c, null);
28 static const JSCore.StaticFunction[] class_functions = {
31 static const JSCore.ClassDefinition class_definition = {
33 JSCore.ClassAttribute.None,
56 public JSCore.GlobalContext js_global_context = null;
58 public static Javascript singleton()
60 if (instance == null) {
61 instance = new Javascript();
67 var goc = new JSCore.Class( class_definition );
68 this.js_global_context = new JSCore.GlobalContext(goc);
72 public int validate(string code, out string res)
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),
83 if (ex.is_null(ctx)) {
88 var exo = ex.to_object(ctx, null);
89 unowned JSCore.PropertyNameArray property_names = exo.copy_property_names (ctx);
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);
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);
106 var rline = (int) line;
108 return rline > 0 ? rline -1 : 0;
113 * extension API concept..
114 * javascript file.. loaded into jscore,
115 * then a method is called, with a string argument (json encoded)
118 public void executeFile(string fname, string call_method, string js_data)
121 if (!FileUtils.test (fname, FileTest.EXISTS)) {
122 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
125 FileUtils.get_contents(fname, out file_data);
127 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
128 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
129 var json_args = new JSCore.String.with_utf8_c_string(js_data);
133 unowned JSCore.Value exc;
136 var goc = new JSCore.Class( class_definition );
137 var ctx = new JSCore.GlobalContext(goc);
138 var othis = ctx.get_global_object();
140 var eval = ctx.evaluate_script (
149 if (!othis.has_property(ctx,jmethod)) {
150 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
154 var val = othis.get_property (ctx, jmethod, out exb);
156 if (!val.is_object(ctx)) {
157 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
159 var oval = val.to_object(ctx, out exc);
161 if (!oval.is_function(ctx)) {
162 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
165 // this will never work, as we can not create arrays of Values - due to no
166 // free function being available..
167 var args = new JSCore.Value[1] ;
168 args[0] = new JSCore.Value.string(ctx,json_args) ;
170 unowned JSCore.Value res = oval.call_as_function(ctx, othis, args, out exd);