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 {
25 public static Javascript singleton()
27 if (instance == null) {
28 instance = new Javascript();
34 //var goc = new JSCore.Class( class_definition );
35 //this.js_global_context = new JSCore.GlobalContext(goc);
42 public void validate(string code, JsRender.JsRender file)
46 var ctx = new JSC.Context();
48 //GLib.debug("Check syntax %s", code);
50 ctx.check_syntax(code, code.length, JSC.CheckSyntaxMode.SCRIPT, "", 1 ,out ex);
51 var ar = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
54 // file.updateErrors( ar ); // clear old errors ??
55 this.compressionErrors.begin(code, file.path, (obj, res) => {
56 ar = this.compressionErrors.end(res);
57 file.updateErrors( ar );
64 //GLib.debug("go t error %d %s", (int)ex.get_line_number() , ex.get_message() );
65 var diag = new Lsp.Diagnostic.simple((int) ex.get_line_number() -1 , 1, ex.get_message());
68 file.updateErrors( ar );
73 bool packer_running = false;
74 public async new Gee.ArrayList<Lsp.Diagnostic> compressionErrors(string code , string fn) throws ThreadError
76 // this uses the roojspacker code to try and compress the code.
77 // it should highlight errors before we actually push live the code.
78 SourceFunc callback = compressionErrors.callback;
79 Json.Object ret = new Json.Object();
80 var ar = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
82 if (this.packer_running) {
85 this.packer_running = true;
87 ThreadFunc<bool> run = () => {
89 // standard error format: file %s, line %s, Error
91 var cfg = new JSDOC.PackerRun();
92 cfg.opt_keep_whitespace = false;
93 cfg.opt_skip_scope = false;
94 cfg.opt_dump_tokens = false;
95 cfg.opt_clean_cache = false;
98 var p = new JSDOC.Packer(cfg);
101 p.packFile(code, fn,"");
104 Idle.add((owned) callback);
107 new Thread<bool>("roopacker", run);
109 this.packer_running = false;
110 if (!ret.has_member(fn)) {
113 var jar = ret.get_array_member(fn);
114 for(var i = 0; i < jar.get_length(); i++ ){
115 var d = jar.get_object_element(i);
118 new Lsp.Diagnostic.simple((int) d.get_int_member("line") , 1, d.get_string_member("message"))
130 * extension API concept..
131 * javascript file.. loaded into jscore,
132 * then a method is called, with a string argument (json encoded)
136 public string executeFile(string fname, string call_method, string js_data)
137 throws JavascriptError
141 if (!FileUtils.test (fname, FileTest.EXISTS)) {
142 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
145 FileUtils.get_contents(fname, out file_data);
146 } catch (GLib.Error e) {
147 GLib.debug("Error file load failed: %s", e.message);
150 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
151 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
152 //var json_args = new JSCore.String.with_utf8_c_string(js_data);
156 unowned JSCore.Value exc;
158 unowned JSCore.Value exe;
160 var goc = new JSCore.Class( class_definition );
161 var ctx = new JSCore.GlobalContext(goc);
162 var othis = ctx.get_global_object();
164 ctx.evaluate_script (
173 if (!othis.has_property(ctx,jmethod)) {
174 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
178 var val = othis.get_property (ctx, jmethod, out exb);
180 if (!val.is_object(ctx)) {
181 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
183 var oval = val.to_object(ctx, out exc);
185 if (!oval.is_function(ctx)) {
186 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
188 throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
191 var res = jscore_object_call_as_function(
192 ctx, oval, othis, js_data, out exd
194 // this will never work, as we can not create arrays of Values - due to no
195 // free function being available..
196 //var args = new JSCore.Value[1] ;
197 //args[0] = new JSCore.Value.string(ctx,json_args) ;
199 //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
200 // extract the text value from res...
201 JSCore.String sv = res.to_string_copy ( ctx, out exe);
202 var length = sv.get_maximum_utf8_c_string_size();
203 var buf = new char[length];
205 sv.get_utf8_c_string( buf, length);
208 print("ret:%s\n",(string) buf);