GNOME Bugzilla – Bug 548897
Wrapping c code segment (snippet) around cfunc in .vapi files
Last modified: 2008-08-23 06:47:12 UTC
Patch follows. To compiler the following program. using GLib; class ClassA: Object { [CCode (snippet="return g_strdup(b);")] public string method(string b); public static void main(string [] args) { ClassA obj = new ClassA(); string test = "hello world"; string b = obj.method(test); message("%s = %s", test, b); } }
Created attachment 117152 [details] [review] To enable this feature. quite primitive. Added a new CCodeNode type CCodeSnippet; Added a new CCode hint snippet; Added a constructor in Scanner to scan a string. SourceFile to accept a null file name ( for the snippet) This is a proof-idea patch, Vala.Scanner is reused to scan the snippet code but nothing is done after the scanning. The variables in the snippet should be translated from vala name to cname after scanning.
Created attachment 117167 [details] [review] updated patch, translating parameter names and 'this', also improved formatting. Still in progress.
Created attachment 117174 [details] [review] Better coding style, both in vala and the generated code. [CCode (snippet="....")] is working now. Please test.
I changed the name of the bug to reflect the change of focus. Now it is fully possible to solve Bug 547236. This is an example line of modified glib-2.0.vapi with this new feature: public class string { [CCode (cname = "strstr")] public weak string? str (string needle); [CCode (cname = "g_str_has_prefix")] public bool has_prefix (string prefix); [CCode (cname = "g_str_has_suffix")] public bool has_suffix (string suffix); [CCode (cname = "g_strdup_printf"), PrintfFormat] public string printf (...); [CCode (cname = "sscanf", cheader_filename = "stdio.h")] public int scanf (...); [CCode (cname = "g_strconcat")] public string concat (string string2, ...); [CCode (cname = "g_strndup")] public string ndup (ulong n); /* FIXME: only UTF-8 */ [CCode (cname = "g_strescape")] public string escape (string exceptions); [CCode (cname = "g_strcompress")] public string compress (); /***********************/ [CCode (cname = "g_strmysplit", snippet = "gchar ** rt = g_strsplit(this, delimiter, max_tokens); *result_length1 = rt?g_strv_length(rt):0; return rt;")] public string[] mysplit (string delimiter, int max_tokens = 0); /**>>>>>>>>>>>>>>>>>>>*/ [CCode (cname = "g_strsplit")] [NoArrayLength] public string[] split (string delimiter, int max_tokens = 0); /**<<<<<<<<<<<<<<<<<<<<*/ ... ...
Created attachment 117182 [details] [review] [CCode snippet ] in .vapi files
Created attachment 117189 [details] [review] Removed duplicated function declarations. 1. Because Vala.CCodeNode now supports a equal method, Vala.CCodeFragment is modified to disalow adding two idential CCodeNodes. Further work against Vala.DynamicMethod can kill the vast number of static wrappers for dynamic methods. (was one function for each) 2. It solves the duplicated wrappers problem 3. now we always use a prefixed name for the wrapper.
Created attachment 117192 [details] [review] array length Array.length is parsed.
last patch (http://bugzilla.gnome.org/attachment.cgi?id=117192&action=view) failed to compile vala/valascanner.vala:56.3-56.16: error: class creation methods only allow property assignment statements _buffer = str; ^^^^^^^^^^^^^^ char* current ; char* end ; ... string _buffer; .... public Scanner.string (string str) { _buffer = str; current = _buffer; end = current + _buffer.size(); } As You can see, property's current, end and _buffer set in constructor but not marked as public XXX {get;construct;}
it compiles with Vala 0.3.5.
vala/valascanner.vala:56.3-56.16: error: class creation methods only allow property assignment statements _buffer = str; ^^^^^^^^^^^^^^ [avl@localhost SPECS]$ valac --version Vala 0.3.5 maybe, it compiles with vala from svn trunk?
No. It compiles with The patch is against vala trunk. and the patched vala is compilable with vala 0.3.5.
No, it compiles only by valac from trunk. e.g. 2 stages 1) build and install vala & vala-devel from trunk 2) apply patch, build and install patched vala Released version 0.3.5 from tarball not suitable for build patched version.
another regress with enabled build option --enable-gen-project: make[2]: Entering directory `/home/avl/RPM/BUILD/vala-0.3.5/gen-project' ../compiler/valac -C --vapidir ./../vapi --pkg config --pkg gtk+-2.0 --basedir .. valaprojectgenerator.vala make[2]: *** [vala-gen-project.vala.stamp] Segmentation fault without patch, build sucessfully with patch, but without --enable-gen-project build sucessfully too