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,"");
127 //state.showCompileResult(p.result);
129 var g = new Json.Generator ();
133 var n = new Json.Node(Json.NodeType.OBJECT);
134 n.set_object(p.result);
137 GLib.debug("got %s", g.to_data (null));
144 * extension API concept..
145 * javascript file.. loaded into jscore,
146 * then a method is called, with a string argument (json encoded)
150 public string executeFile(string fname, string call_method, string js_data)
151 throws JavascriptError
155 if (!FileUtils.test (fname, FileTest.EXISTS)) {
156 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
159 FileUtils.get_contents(fname, out file_data);
160 } catch (GLib.Error e) {
161 GLib.debug("Error file load failed: %s", e.message);
164 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
165 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
166 //var json_args = new JSCore.String.with_utf8_c_string(js_data);
170 unowned JSCore.Value exc;
172 unowned JSCore.Value exe;
174 var goc = new JSCore.Class( class_definition );
175 var ctx = new JSCore.GlobalContext(goc);
176 var othis = ctx.get_global_object();
178 ctx.evaluate_script (
187 if (!othis.has_property(ctx,jmethod)) {
188 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
192 var val = othis.get_property (ctx, jmethod, out exb);
194 if (!val.is_object(ctx)) {
195 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
197 var oval = val.to_object(ctx, out exc);
199 if (!oval.is_function(ctx)) {
200 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
202 throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
205 var res = jscore_object_call_as_function(
206 ctx, oval, othis, js_data, out exd
208 // this will never work, as we can not create arrays of Values - due to no
209 // free function being available..
210 //var args = new JSCore.Value[1] ;
211 //args[0] = new JSCore.Value.string(ctx,json_args) ;
213 //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
214 // extract the text value from res...
215 JSCore.String sv = res.to_string_copy ( ctx, out exe);
216 var length = sv.get_maximum_utf8_c_string_size();
217 var buf = new char[length];
219 sv.get_utf8_c_string( buf, length);
222 print("ret:%s\n",(string) buf);