6 public errordomain JavascriptError {
11 Javascript instance = null;
13 public class Javascript {
16 public static JSCore.Object class_constructor(
18 JSCore.Object constructor,
19 JSCore.Value[] arguments,
20 out JSCore.Value exception)
22 var c = new JSCore.Class (class_definition);
23 var o = new JSCore.Object (ctx, c, null);
27 static const JSCore.StaticFunction[] class_functions = {
30 static const JSCore.ClassDefinition class_definition = {
32 JSCore.ClassAttribute.None,
55 public JSCore.GlobalContext js_global_context = null;
57 public static Javascript singleton()
59 if (instance == null) {
60 instance = new Javascript();
66 var goc = new JSCore.Class( class_definition );
67 this.js_global_context = new JSCore.GlobalContext(goc);
71 public int validate(string code, out string res)
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),
82 if (ex.is_null(ctx)) {
87 var exo = ex.to_object(ctx, null);
88 unowned JSCore.PropertyNameArray property_names = exo.copy_property_names (ctx);
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);
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);
105 var rline = (int) line;
107 return rline > 0 ? rline -1 : 0;
112 * extension API concept..
113 * javascript file.. loaded into jscore,
114 * then a method is called, with a string argument (json encoded)
117 void executeFile(string fname, string method, string json)
120 if (!FileUtils.test (fname, FileTest.EXISTS)) {
121 throw new JavascriptError.MISSING_FILE(fname + " not found");
124 FileUtils.get_contents(fname, out file_data);
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);
132 var goc = new JSCore.Class( class_definition );
133 var ctx = new JSCore.GlobalContext(goc);
134 var othis = ctx.get_global_object();
136 var val = ctx.evaluate_script (
145 if (!othis.has_property(ctx,jmethod)) {
146 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", method);
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);
154 JSCore.Value[] args = {};
157 var res = val.call_as_function(ctx, othis, args, ex);