2 extern JSCore.Value jscore_object_call_as_function(
5 JSCore.Object thisObject,
7 out JSCore.Value exception
15 public errordomain JavascriptError {
21 Javascript instance = null;
23 public class Javascript {
26 public static JSCore.Object class_constructor(
28 JSCore.Object constructor,
29 JSCore.Value[] arguments,
30 out JSCore.Value exception)
32 var c = new JSCore.Class (class_definition);
33 var o = new JSCore.Object (ctx, c, null);
37 static const JSCore.StaticFunction[] class_functions = {
40 static const JSCore.ClassDefinition class_definition = {
42 JSCore.ClassAttribute.None,
65 public JSCore.GlobalContext js_global_context = null;
67 public static Javascript singleton()
69 if (instance == null) {
70 instance = new Javascript();
76 var goc = new JSCore.Class( class_definition );
77 this.js_global_context = new JSCore.GlobalContext(goc);
81 public int validate(string code, out string res)
84 unowned JSCore.GlobalContext ctx = this.js_global_context;
85 var ret = this.js_global_context.check_script_syntax(
86 new JSCore.String.with_utf8_c_string(code),
92 if (ex.is_null(ctx)) {
97 var exo = ex.to_object(ctx, null);
98 unowned JSCore.PropertyNameArray property_names = exo.copy_property_names (ctx);
103 var js_string = new JSCore.String.with_utf8_c_string("line");
104 var line = exo.get_property(ctx, js_string, null).to_number(ctx,null);
108 // see if we can convert exception string
109 char *c_string = new char[1024];
110 var err_string = ex.to_string_copy (ctx, null);
111 err_string.get_utf8_c_string (c_string, 1023);
112 res = (string)c_string;
113 //print ("Error on line %d\n%s\n", (int)line, res);
115 var rline = (int) line;
117 return rline > 0 ? rline -1 : 0;
122 * extension API concept..
123 * javascript file.. loaded into jscore,
124 * then a method is called, with a string argument (json encoded)
127 public string executeFile(string fname, string call_method, string js_data)
128 throws JavascriptError
131 if (!FileUtils.test (fname, FileTest.EXISTS)) {
132 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
135 FileUtils.get_contents(fname, out file_data);
137 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
138 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
139 //var json_args = new JSCore.String.with_utf8_c_string(js_data);
143 unowned JSCore.Value exc;
145 unowned JSCore.Value exe;
147 var goc = new JSCore.Class( class_definition );
148 var ctx = new JSCore.GlobalContext(goc);
149 var othis = ctx.get_global_object();
151 var eval = ctx.evaluate_script (
160 if (!othis.has_property(ctx,jmethod)) {
161 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
165 var val = othis.get_property (ctx, jmethod, out exb);
167 if (!val.is_object(ctx)) {
168 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
170 var oval = val.to_object(ctx, out exc);
172 if (!oval.is_function(ctx)) {
173 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
177 var res = jscore_object_call_as_function(
178 ctx, oval, othis, js_data, out exd
180 // this will never work, as we can not create arrays of Values - due to no
181 // free function being available..
182 //var args = new JSCore.Value[1] ;
183 //args[0] = new JSCore.Value.string(ctx,json_args) ;
185 //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
186 // extract the text value from res...
187 JSCore.String sv = res.to_string_copy ( ctx, out exe);
188 var length = sv.get_maximum_utf8_c_string_size();
189 var buf = new char[length];
191 sv.get_utf8_c_string( buf, length);
194 print("ret:%s\n",(string) buf);