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 const JSCore.StaticFunction[] class_functions = {
40 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 Json.Object validate(string code, string fn)
85 var ctx = new JSC.Context();
87 //GLib.debug("Check syntax %s", code);
89 ctx.check_syntax(code, code.length, JSC.CheckSyntaxMode.SCRIPT, "", 1 ,out ex);
92 return new Json.Object(); // this.compressionErrors(code, fn); << to slow on large files?
97 //GLib.debug("got error %d %s", (int)ex.get_line_number() , ex.get_message() );
99 var ar = new Json.Array();
100 ar.add_string_element(ex.get_message());
104 var line_to_err = new Json.Object();
106 line_to_err.set_array_member(ex.get_line_number().to_string(), ar);
107 var file_to_line = new Json.Object();
108 file_to_line.set_object_member(fn, line_to_err);
109 var ret = new Json.Object();
110 ret.set_object_member("ERR", file_to_line);
113 var g = new Json.Generator ();
117 var n = new Json.Node(Json.NodeType.OBJECT);
121 GLib.debug("got %s", g.to_data (null));
128 public Json.Object compressionErrors(string code , string fn)
130 // this uses the roojspacker code to try and compress the code.
131 // it should highlight errors before we actually push live the code.
133 // standard error format: file %s, line %s, Error
136 var cfg = new JSDOC.PackerRun();
137 cfg.opt_keep_whitespace = false;
138 cfg.opt_skip_scope = false;
139 cfg.opt_dump_tokens = false;
140 cfg.opt_clean_cache = false;
143 var p = new JSDOC.Packer(cfg);
146 p.packFile(code, fn,"");
147 //state.showCompileResult(p.result);
149 var g = new Json.Generator ();
153 var n = new Json.Node(Json.NodeType.OBJECT);
154 n.set_object(p.result);
157 GLib.debug("got %s", g.to_data (null));
164 * extension API concept..
165 * javascript file.. loaded into jscore,
166 * then a method is called, with a string argument (json encoded)
170 public string executeFile(string fname, string call_method, string js_data)
171 throws JavascriptError
175 if (!FileUtils.test (fname, FileTest.EXISTS)) {
176 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
179 FileUtils.get_contents(fname, out file_data);
180 } catch (GLib.Error e) {
181 GLib.debug("Error file load failed: %s", e.message);
184 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
185 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
186 //var json_args = new JSCore.String.with_utf8_c_string(js_data);
190 unowned JSCore.Value exc;
192 unowned JSCore.Value exe;
194 var goc = new JSCore.Class( class_definition );
195 var ctx = new JSCore.GlobalContext(goc);
196 var othis = ctx.get_global_object();
198 ctx.evaluate_script (
207 if (!othis.has_property(ctx,jmethod)) {
208 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
212 var val = othis.get_property (ctx, jmethod, out exb);
214 if (!val.is_object(ctx)) {
215 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
217 var oval = val.to_object(ctx, out exc);
219 if (!oval.is_function(ctx)) {
220 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
222 throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
225 var res = jscore_object_call_as_function(
226 ctx, oval, othis, js_data, out exd
228 // this will never work, as we can not create arrays of Values - due to no
229 // free function being available..
230 //var args = new JSCore.Value[1] ;
231 //args[0] = new JSCore.Value.string(ctx,json_args) ;
233 //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
234 // extract the text value from res...
235 JSCore.String sv = res.to_string_copy ( ctx, out exe);
236 var length = sv.get_maximum_utf8_c_string_size();
237 var buf = new char[length];
239 sv.get_utf8_c_string( buf, length);
242 print("ret:%s\n",(string) buf);