ni: fix use of multiple native global refs and according test
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 23 Feb 2013 14:42:47 +0000 (09:42 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 21 Mar 2013 18:32:53 +0000 (14:32 -0400)
Fix a bug occuring when the number of global references from the
C code was higher than one.

The according test is moved from the native interface format to
the FFI. It also has a more complex test case.

Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

clib/nit_main.c
tests/sav/test_ffi_c_global_ref.sav [moved from tests/sav/test_ni_global_ref.sav with 50% similarity]
tests/test_ffi_c_global_ref.nit [moved from tests/test_ni_global_ref.nit with 52% similarity]
tests/test_ni_global_ref.nit.c [deleted file]
tests/test_ni_global_ref.nit.h [deleted file]

index 0be5918..9b20edb 100644 (file)
@@ -231,13 +231,12 @@ void nitni_global_ref_list_init() {
 void nitni_global_ref_add( struct nitni_ref *ref ) {
        if ( nitni_global_ref_list->head == NULL ) {
                nitni_global_ref_list->head = ref;
-               nitni_global_ref_list->tail = ref;
-
                ref->prev = NULL;
        } else {
                nitni_global_ref_list->tail->next = ref;
                ref->prev = nitni_global_ref_list->tail;
        }
+       nitni_global_ref_list->tail = ref;
 
        ref->next = NULL;
 }
similarity index 52%
rename from tests/test_ni_global_ref.nit
rename to tests/test_ffi_c_global_ref.nit
index 94a21ca..daac6f2 100644 (file)
@@ -1,6 +1,6 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
-# Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
+# Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+module test_ffi_c_global_ref
+
+`{
+ToBePreserved global_tbps[3] = {NULL,NULL,NULL};
+`}
+
 class ToBePreserved
        var id : String
 
@@ -21,9 +27,24 @@ class ToBePreserved
 end
 
 class A
-       fun save_as_global( tbp : ToBePreserved ) is extern import ToBePreserved::output
-       fun recover_unsafe : ToBePreserved is extern
-       fun recover : nullable ToBePreserved is extern import ToBePreserved as nullable
+       fun save_as_global( tbp : ToBePreserved, i : Int ) import ToBePreserved::output `{
+               if ( global_tbps[i] != NULL )
+                       ToBePreserved_decr_ref( global_tbps[i] );
+
+               global_tbps[i] = tbp;
+
+               ToBePreserved_incr_ref( tbp );
+       `}
+       fun recover_unsafe( i : Int ) : ToBePreserved `{
+               return global_tbps[i];
+       `}
+       fun recover( i : Int ) : nullable ToBePreserved import ToBePreserved as nullable `{
+               if ( global_tbps[i] != NULL ) {
+                       return ToBePreserved_as_nullable( global_tbps[i] );
+               } else {
+                       return null_ToBePreserved();
+               }
+       `}
 
        fun launch_gc do sys.force_garbage_collection
 end
@@ -32,15 +53,18 @@ var x = new ToBePreserved( "x" )
 var y = new ToBePreserved( "y" )
 var a = new A
 
-a.save_as_global( y )
+a.save_as_global( y, 0 )
 sys.force_garbage_collection
-var r = a.recover
+var r = a.recover(0)
 if r != null then
        r.output
 else
        print "null :("
 end
 
-a.save_as_global( x )
+a.save_as_global( x, 1 )
+a.save_as_global( y, 2 )
 sys.force_garbage_collection
-a.recover_unsafe.output
+a.recover_unsafe(0).output
+a.recover_unsafe(1).output
+a.recover_unsafe(2).output
diff --git a/tests/test_ni_global_ref.nit.c b/tests/test_ni_global_ref.nit.c
deleted file mode 100644 (file)
index 61d0606..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/* This file is part of NIT ( http://www.nitlanguage.org ).
- *
- * Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
- *
- * This file is free software, which comes along with NIT.  This software is
- * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
- * PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
- * is kept unaltered, and a notification of the changes is added.
- * You  are  allowed  to  redistribute it and sell it, alone or is a part of
- * another product.
- */
-
-#include "test_ni_global_ref.nit.h"
-
-ToBePreserved global_tbp = NULL;
-
-/*
-C implementation of test_ni_references::A::save_as_global
-
-Imported methods signatures:
-       void ToBePreserved_output( ToBePreserved recv ) for test_ni_references::ToBePreserved::(kernel::Object::output)
-*/
-void A_save_as_global___impl( A recv, ToBePreserved tbp ) {
-       if ( global_tbp != NULL ) {
-               ToBePreserved_decr_ref( global_tbp );
-       }
-
-       global_tbp = tbp;
-       ToBePreserved_incr_ref( global_tbp );
-}
-
-/*
-C implementation of test_ni_references::A::recover_unsafe
-*/
-ToBePreserved A_recover_unsafe___impl( A recv ) {
-       return global_tbp;
-}
-
-/*
-C implementation of test_ni_references::A::recover
-
-Imported methods signatures:
-       nullable_ToBePreserved ToBePreserved_as_nullable( ToBePreserved value ) to cast from ToBePreserved to nullable ToBePreserved
-*/
-nullable_ToBePreserved A_recover___impl( A recv ) {
-       if ( global_tbp != NULL ) {
-               return ToBePreserved_as_nullable( global_tbp );
-       } else {
-               return null_ToBePreserved();
-       }
-}
-
diff --git a/tests/test_ni_global_ref.nit.h b/tests/test_ni_global_ref.nit.h
deleted file mode 100644 (file)
index 85110f0..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/* This file is part of NIT ( http://www.nitlanguage.org ).
- *
- * Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
- *
- * This file is free software, which comes along with NIT.  This software is
- * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
- * PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
- * is kept unaltered, and a notification of the changes is added.
- * You  are  allowed  to  redistribute it and sell it, alone or is a part of
- * another product.
- */
-
-#ifndef TEST_NI_GLOBAL_REF_NIT_H
-#define TEST_NI_GLOBAL_REF_NIT_H
-
-#include <test_ni_global_ref._nitni.h>
-
-void A_save_as_global___impl( A recv, ToBePreserved tbp );
-ToBePreserved A_recover_unsafe___impl( A recv );
-nullable_ToBePreserved A_recover___impl( A recv );
-
-#endif