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 = file.getErrors("ERR");
93 return ; // this.compressionErrors(code, fn); << to slow on large files?
97 //GLib.debug("go t error %d %s", (int)ex.get_line_number() , ex.get_message() );
100 var ret = new CompileError.new_jserror(file, "ERR", (int) ex.get_line_number() -1 , ex.get_message());
109 public Json.Object compressionErrors(string code , string fn)
111 // this uses the roojspacker code to try and compress the code.
112 // it should highlight errors before we actually push live the code.
114 // standard error format: file %s, line %s, Error
117 var cfg = new JSDOC.PackerRun();
118 cfg.opt_keep_whitespace = false;
119 cfg.opt_skip_scope = false;
120 cfg.opt_dump_tokens = false;
121 cfg.opt_clean_cache = false;
124 var p = new JSDOC.Packer(cfg);
127 p.packFile(code, fn,"");
128 //state.showCompileResult(p.result);
130 var g = new Json.Generator ();
134 var n = new Json.Node(Json.NodeType.OBJECT);
135 n.set_object(p.result);
138 GLib.debug("got %s", g.to_data (null));
145 * extension API concept..
146 * javascript file.. loaded into jscore,
147 * then a method is called, with a string argument (json encoded)
151 public string executeFile(string fname, string call_method, string js_data)
152 throws JavascriptError
156 if (!FileUtils.test (fname, FileTest.EXISTS)) {
157 throw new JavascriptError.MISSING_FILE("Plugin: file not found %s", fname);
160 FileUtils.get_contents(fname, out file_data);
161 } catch (GLib.Error e) {
162 GLib.debug("Error file load failed: %s", e.message);
165 var jfile_data = new JSCore.String.with_utf8_c_string(file_data);
166 var jmethod = new JSCore.String.with_utf8_c_string(call_method);
167 //var json_args = new JSCore.String.with_utf8_c_string(js_data);
171 unowned JSCore.Value exc;
173 unowned JSCore.Value exe;
175 var goc = new JSCore.Class( class_definition );
176 var ctx = new JSCore.GlobalContext(goc);
177 var othis = ctx.get_global_object();
179 ctx.evaluate_script (
188 if (!othis.has_property(ctx,jmethod)) {
189 throw new JavascriptError.MISSING_METHOD ("Plugin: missing method %s", call_method);
193 var val = othis.get_property (ctx, jmethod, out exb);
195 if (!val.is_object(ctx)) {
196 throw new JavascriptError.MISSING_METHOD ("Plugin: not a property not found %s", call_method);
198 var oval = val.to_object(ctx, out exc);
200 if (!oval.is_function(ctx)) {
201 throw new JavascriptError.MISSING_METHOD ("Plugin: not a method %s", call_method);
203 throw new JavascriptError.MISSING_METHOD ("Plugin: not supported anymore");
206 var res = jscore_object_call_as_function(
207 ctx, oval, othis, js_data, out exd
209 // this will never work, as we can not create arrays of Values - due to no
210 // free function being available..
211 //var args = new JSCore.Value[1] ;
212 //args[0] = new JSCore.Value.string(ctx,json_args) ;
214 //unowned JSCore.Value res = oval.call_as_function(ctx, othis, null, out exd);
215 // extract the text value from res...
216 JSCore.String sv = res.to_string_copy ( ctx, out exe);
217 var length = sv.get_maximum_utf8_c_string_size();
218 var buf = new char[length];
220 sv.get_utf8_c_string( buf, length);
223 print("ret:%s\n",(string) buf);