}
CCodeExpression length_expr = new CCodeIdentifier ("length");
+ CCodeBinaryOperator length_check_op;
// add extra item to have array NULL-terminated for all reference types
if (array_type.element_type.type_symbol != null && array_type.element_type.type_symbol.is_reference_type ()) {
length_expr = new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, length_expr, new CCodeConstant ("1"));
+ length_check_op = CCodeBinaryOperator.GREATER_THAN_OR_EQUAL;
+ } else {
+ length_check_op = CCodeBinaryOperator.GREATER_THAN;
}
gnew.add_argument (length_expr);
gnew.add_argument (csizeof);
}
+ // only attempt to dup if length >=/> 0, this deals with negative lengths and returns NULL
+ var length_check = new CCodeBinaryExpression (length_check_op, new CCodeIdentifier ("length"), new CCodeConstant ("0"));
+ ccode.open_if (length_check);
+
ccode.add_declaration (get_ccode_name (array_type), cvardecl);
ccode.add_assignment (new CCodeIdentifier ("result"), gnew);
ccode.close ();
ccode.add_return (new CCodeIdentifier ("result"));
+
+ ccode.close ();
+ ccode.add_return (new CCodeIdentifier ("NULL"));
} else {
+ // only dup if length > 0, this deals with negative lengths and returns NULL
+ var length_check = new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN, new CCodeIdentifier ("length"), new CCodeConstant ("0"));
+ ccode.open_if (length_check);
+
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeIdentifier (get_ccode_name (array_type.element_type)));
var length_expr = new CCodeIdentifier ("length");
ccode.add_return (dup_call);
}
+
+ ccode.close ();
+ ccode.add_return (new CCodeIdentifier ("NULL"));
}
// append to file
arrays/cast-silent-invalid.test \
arrays/class-field-length-cname.vala \
arrays/constant-element-access.vala \
+ arrays/empty-length-0.vala \
arrays/expression-bracket.test \
arrays/fixed-length-init0-not-allowed.vala \
arrays/field-global-length-cname.vala \
POSIX_TESTS = \
basic-types/arrays.vala \
+ arrays/empty-length-0.vala \
structs/struct_only.vala \
delegates/delegate_only.vala \
enums/enum_only.vala \
--- /dev/null
+struct Manam {
+ string s;
+}
+
+string[] foo;
+int[] bar;
+Manam[] manam;
+
+string[] get_foo () {
+ return foo;
+}
+
+int[] get_bar () {
+ return bar;
+}
+
+Manam[] get_manam () {
+ return manam;
+}
+
+void main () {
+ {
+ foo = new string[0];
+ assert (foo != null);
+ assert (get_foo () != null);
+ }
+ {
+ foo = {};
+ assert (foo != null);
+ assert (get_foo () != null);
+ }
+ {
+ bar = new int[0];
+ //assert (bar != null);
+ assert (get_bar () == null);
+ }
+ {
+ bar = {};
+ //assert (bar != null);
+ assert (get_bar () == null);
+ }
+ {
+ manam = new Manam[0];
+ //assert (manam != null);
+ assert (get_manam () == null);
+ }
+ {
+ manam = {};
+ //assert (manam != null);
+ assert (get_manam () == null);
+ }
+}