2007-08-19 Mathias Hasselmann <mathias.hasselmann@gmx.de>
+ * gobject/valacodegenerator.vala: also use _vala_array_free to
+ release string arrays as g_strfreev cannot handle Vala's sized
+ arrays, which have a _length variable, but no NULL sentinel.
+ * gobject/valacodegeneratorsourcefile.vala: enhance _vala_array_free
+ to handle unsized (NULL sentinel terminated) arrays.
+ * tests/test-022.*: test behaviour of _vala_array_free. currently
+ fails because Vala forgets to release old element members when
+ assigning a new value.
+
+2007-08-19 Mathias Hasselmann <mathias.hasselmann@gmx.de>
+
* gobject/valacodegeneratorsourcefile.vala: move code generation
for _vala_array_free and _vala_array_move into separate methods
for readability.
ccall.add_argument (new CCodeConstant ("TRUE"));
} else if (type.data_type is Array) {
var arr = (Array) type.data_type;
- if (arr.element_type == string_type.data_type) {
- ccall.call = new CCodeIdentifier ("g_strfreev");
- } else if (arr.element_type == null || arr.element_type.is_reference_type ()) {
+ if (arr.element_type == null || arr.element_type.is_reference_type ()) {
requires_array_free = true;
bool first = true;
ma.ccodenode = element_expr;
element_expr = get_ref_expression (ma);
+ var clendecl = new CCodeDeclaration ("int");
+ clendecl.add_declarator (CCodeVariableDeclarator.with_initializer (get_array_length_cname (collection_backup.name, 1), array_len));
+ cblock.add_statement (clendecl);
+
var cfrag = new CCodeFragment ();
append_temp_decl (cfrag, temp_vars);
cbody.add_statement (cfrag);
return new CCodeIncludeDirective (filename, context.library == null);
}
+ private CCodeForStatement get_vala_array_free_loop (bool have_length) {
+ var cbody = new CCodeBlock ();
+ var cptrarray = new CCodeCastExpression (new CCodeIdentifier ("array"), "gpointer*");
+ var cea = new CCodeElementAccess (cptrarray, new CCodeIdentifier ("i"));
+
+ var cfreecall = new CCodeFunctionCall (new CCodeIdentifier ("destroy_func"));
+ cfreecall.add_argument (cea);
+
+ CCodeExpression cforcond;
+
+ if (have_length) {
+ var cfreecond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
+ cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("array_length"));
+ cbody.add_statement (new CCodeIfStatement (cfreecond, new CCodeExpressionStatement (cfreecall)));
+ } else {
+ cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
+ cbody.add_statement (new CCodeExpressionStatement (cfreecall));
+ }
+
+ var cfor = new CCodeForStatement (cforcond, cbody);
+ cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
+ cfor.add_iterator (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeIdentifier ("i"), new CCodeConstant ("1"))));
+
+ return cfor;
+ }
+
private void append_vala_array_free () {
var fun = new CCodeFunction ("_vala_array_free", "void");
fun.modifiers = CCodeModifiers.STATIC;
var citdecl = new CCodeDeclaration ("int");
citdecl.add_declarator (new CCodeVariableDeclarator ("i"));
cdofree.add_statement (citdecl);
-
- var cbody = new CCodeBlock ();
-
- var cptrarray = new CCodeCastExpression (new CCodeIdentifier ("array"), "gpointer*");
- var cea = new CCodeElementAccess (cptrarray, new CCodeIdentifier ("i"));
- var cfreecond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cea, new CCodeConstant ("NULL"));
- var cfreecall = new CCodeFunctionCall (new CCodeIdentifier ("destroy_func"));
- cfreecall.add_argument (cea);
- cbody.add_statement (new CCodeIfStatement (cfreecond, new CCodeExpressionStatement (cfreecall)));
- var cforcond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier ("i"), new CCodeIdentifier ("array_length"));
-
- var cfor = new CCodeForStatement (cforcond, cbody);
- cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeConstant ("0")));
- cfor.add_iterator (new CCodeAssignment (new CCodeIdentifier ("i"), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeIdentifier ("i"), new CCodeConstant ("1"))));
- cdofree.add_statement (cfor);
+ var clencheck = new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN_OR_EQUAL, new CCodeIdentifier ("array_length"), new CCodeConstant ("0"));
+ var ciflen = new CCodeIfStatement (clencheck, get_vala_array_free_loop (true), get_vala_array_free_loop (false));
+ cdofree.add_statement (ciflen);
var ccondarr = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("array"), new CCodeConstant ("NULL"));
var ccondfunc = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("destroy_func"), new CCodeConstant ("NULL"));
One dimensional array creation and assignment: 1 2 3 4 5 6 7 8 9 10 11
+String array creation and assignment: 1 2 3 4 5 6 7 8 9 10 11
+Object array creation and assignment: 1 2 3 4 5 6 7 8 9 10 11 c b a b a c a b c
using GLib;
class Maman.Foo {
- static int main (string[] args) {
+ public Foo (construct string bar) {
+ }
+
+ public ~Foo () {
+ stdout.printf (" %s", _bar);
+ }
+
+ public string bar { get; set construct; }
+
+ static void test_integer_array () {
stdout.printf ("One dimensional array creation and assignment: 1");
-
+
int[] a = new int[4] {1,2};
stdout.printf (" 2");
stdout.printf (" %d", a.length);
stdout.printf (" 11\n");
+ }
+
+ [NoArrayLength ()]
+ static string[] create_unsized_string_array () {
+ return new string[] { "a", "b", "c" };
+ }
+
+ static void test_string_array () {
+ stdout.printf ("String array creation and assignment: 1");
+
+ var a = new string[3] { "a", "b", "c" };
+ var b = new string[] { "a", "b", "c" };
+ var c = create_unsized_string_array ();
+
+ if (3 == a.length) {
+ stdout.printf (" 2");
+ }
+ if (3 == b.length) {
+ stdout.printf (" 3");
+ }
+ if (-1 == c.length) {
+ stdout.printf (" 4");
+ }
+ if (null == c[3]) {
+ stdout.printf (" 5");
+ }
+
+ for (int i = 0; i < a.length; ++i) {
+ if (a[i] == b[i]) {
+ stdout.printf (" %d", i * 2 + 6);
+ }
+ if (a[i] == c[i]) {
+ stdout.printf (" %d", i * 2 + 7);
+ }
+ }
+
+ a[2] = null;
+ b[1] = null;
+
+ stdout.printf ("\n");
+ }
+
+ [NoArrayLength ()]
+ static Foo[] create_unsized_object_array () {
+ return new Foo[] { new Foo ("a"), new Foo ("b"), new Foo ("c") };
+ }
+
+ static void test_object_array () {
+ stdout.printf ("Object array creation and assignment: 1");
+
+ do {
+ var a = new Foo[3] { new Foo ("a"), new Foo ("b"), new Foo ("c") };
+ var b = new Foo[] { new Foo ("a"), new Foo ("b"), new Foo ("c") };
+ var c = create_unsized_object_array ();
+
+ if (3 == a.length) {
+ stdout.printf (" 2");
+ }
+ if (3 == b.length) {
+ stdout.printf (" 3");
+ }
+ if (-1 == c.length) {
+ stdout.printf (" 4");
+ }
+ if (null == c[3]) {
+ stdout.printf (" 5");
+ }
+
+ for (int i = 0; i < a.length; ++i) {
+ if (a[i].bar == b[i].bar) {
+ stdout.printf (" %d", i * 2 + 6);
+ }
+ if (a[i].bar == c[i].bar) {
+ stdout.printf (" %d", i * 2 + 7);
+ }
+ }
+
+ a[2] = null;
+ b[1] = null;
+ } while (false);
+
+ stdout.printf ("\n");
+ }
- return 0;
+ static void main (string[] args) {
+ test_integer_array ();
+ test_string_array ();
+ test_object_array ();
}
}