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 void validate(string code, JsRender.JsRender file)
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);
90 var ar = new Gee.ArrayList<Lsp.Diagnostic>((a,b) => { return a.equals(b); });
93 file.updateErrors( ar );
94 return ; // this.compressionErrors(code, fn); << to slow on large files?
98 //GLib.debug("go t error %d %s", (int)ex.get_line_number() , ex.get_message() );
99 var diag = new Lsp.Diagnostic.simple((int) ex.get_line_number() -1 , 1, ex.get_message());
102 file.updateErrors( ar );
108 public Json.Object compressionErrors(string code , string fn)
110 // this uses the roojspacker code to try and compress the code.
111 // it should highlight errors before we actually push live the code.
113 // standard error format: file %s, line %s, Error
116 var cfg = new JSDOC.PackerRun();
117 cfg.opt_keep_whitespace = false;
118 cfg.opt_skip_scope = false;
119 cfg.opt_dump_tokens = false;
120 cfg.opt_clean_cache = false;
123 var p = new JSDOC.Packer(cfg);
126 p.packFile(code, fn,"");
134 * extension API concept..
135 * javascript file.. loaded into jscore,
136 * then a method is called, with a string argument (json encoded)
140 public string executeFile(string fname, string call_method, string js_data)
141 throws JavascriptError
145 if (!FileUtils.test (fname, FileTest.EXISTS)) {
146 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
149 FileUtils.get_contents(fname, out file_data);
150 } catch (GLib.Error e) {
151 GLib.debug("Error file load failed: %s", e.message);
154 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
155 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
156 //var json_args = new JSCore.String.with_utf8_c_string(js_data);
160 unowned JSCore.Value exc;
162 unowned JSCore.Value exe;
164 var goc = new JSCore.Class( class_definition );
165 var ctx = new JSCore.GlobalContext(goc);
166 var othis = ctx.get_global_object();
168 ctx.evaluate_script (
177 if (!othis.has_property(ctx,jmethod)) {
178 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
182 var val = othis.get_property (ctx, jmethod, out exb);
184 if (!val.is_object(ctx)) {
185 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
187 var oval = val.to_object(ctx, out exc);
189 if (!oval.is_function(ctx)) {
190 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
192 throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
195 var res = jscore_object_call_as_function(
196 ctx, oval, othis, js_data, out exd
198 // this will never work, as we can not create arrays of Values - due to no
199 // free function being available..
200 //var args = new JSCore.Value[1] ;
201 //args[0] = new JSCore.Value.string(ctx,json_args) ;
203 //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
204 // extract the text value from res...
205 JSCore.String sv = res.to_string_copy ( ctx, out exe);
206 var length = sv.get_maximum_utf8_c_string_size();
207 var buf = new char[length];
209 sv.get_utf8_c_string( buf, length);
212 print("ret:%s\n",(string) buf);