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;
}
# 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
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
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
+++ /dev/null
-/* 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();
- }
-}
-
+++ /dev/null
-/* 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