Browse Source

New WeakRef type added!

Mark Sibly 7 years ago
parent
commit
9fe25a1b77

+ 1 - 0
modules/monkey/monkey.monkey2

@@ -10,6 +10,7 @@ Namespace monkey
 #Import "native/bbgc.cpp"
 #Import "native/bbgc.cpp"
 #Import "native/bbobject.cpp"
 #Import "native/bbobject.cpp"
 #Import "native/bbdebug.cpp"
 #Import "native/bbdebug.cpp"
+#Import "native/bbweakref.cpp"
 #Import "native/bbvariant.cpp"
 #Import "native/bbvariant.cpp"
 #Import "native/bbtypeinfo.cpp"
 #Import "native/bbtypeinfo.cpp"
 #Import "native/bbdeclinfo.cpp"
 #Import "native/bbdeclinfo.cpp"

+ 18 - 0
modules/monkey/native/bbgc.cpp

@@ -1,4 +1,6 @@
+
 #include "bbgc.h"
 #include "bbgc.h"
+#include "bbweakref.h"
 
 
 namespace bbDB{
 namespace bbDB{
 
 
@@ -99,6 +101,22 @@ namespace bbGC{
 			
 			
 			remove( p );
 			remove( p );
 			
 			
+			if( p->flags & 2 ){
+
+				//printf( "deleting weak refs for: %s %p\n",p->typeName(),p );fflush( stdout );
+				
+				bbGCWeakRef **pred=&bbGC::weakRefs,*curr;
+				
+				while( curr=*pred ){
+					if( curr->target==p ){
+						curr->target=0;
+						*pred=curr->succ;
+					}else{
+						pred=&curr->succ;
+					}
+				}
+			}
+			
 			if( p->flags & 1 ){
 			if( p->flags & 1 ){
 				
 				
 				//printf( "finalizing: %s %p\n",p->typeName(),p );fflush( stdout );
 				//printf( "finalizing: %s %p\n",p->typeName(),p );fflush( stdout );

+ 1 - 0
modules/monkey/native/bbmonkey.h

@@ -11,6 +11,7 @@
 #include "bbarray.h"
 #include "bbarray.h"
 #include "bbfunction.h"
 #include "bbfunction.h"
 #include "bbobject.h"
 #include "bbobject.h"
+#include "bbweakref.h"
 #include "bbvariant.h"
 #include "bbvariant.h"
 #include "bbtypeinfo_t.h"
 #include "bbtypeinfo_t.h"
 #include "bbdeclinfo.h"
 #include "bbdeclinfo.h"

+ 4 - 0
modules/monkey/native/bbweakref.cpp

@@ -0,0 +1,4 @@
+
+#include "bbweakref.h"
+
+bbGCWeakRef *bbGC::weakRefs;

+ 31 - 0
modules/monkey/native/bbweakref.h

@@ -0,0 +1,31 @@
+
+
+#ifndef BB_WEAKREF_H
+#define BB_WEAKREF_H
+
+#include "bbobject.h"
+
+struct bbGCWeakRef;
+
+namespace bbGC{
+
+	extern bbGCWeakRef *weakRefs;
+}
+
+struct bbGCWeakRef : public bbObject{
+
+	bbGCWeakRef *succ;	
+	bbObject *target;
+	
+	bbGCWeakRef( bbObject *p ):succ( bbGC::weakRefs ),target( p ){
+		bbGC::weakRefs=this;
+		target->flags|=2;
+	}
+	
+	bbObject *getTarget(){
+		return target;
+	}
+	
+};
+
+#endif

+ 20 - 0
modules/monkey/types.monkey2

@@ -777,3 +777,23 @@ Class DeclInfo Extends Void="bbDeclInfo"
 	Method Invoke:Variant( instance:Variant,params:Variant[] )="invoke"
 	Method Invoke:Variant( instance:Variant,params:Variant[] )="invoke"
 End
 End
 
 
+#rem monkeydoc Weak reference class.
+
+A weak reference is an object that contains a reference to another object, but without preventing the other object from being garbage collected.
+
+The [[Target]] property returns the object being referenced, or null if the object has been garbage collected.
+
+A weak reference must be contructed with the object it references.
+
+#end
+Class WeakRef="bbGCWeakRef"
+	
+	Method New( target:Object )
+		
+	Property Target:Object()="getTarget"
+End
+
+	
+
+
+

+ 2 - 2
src/mx2cc/mx2cc.monkey2

@@ -23,9 +23,9 @@ Global opts_time:Bool
 Global StartDir:String
 Global StartDir:String
 
 
 'Const TestArgs:="mx2cc makemods -verbose=1 -clean -target=emscripten pyro-framework"
 'Const TestArgs:="mx2cc makemods -verbose=1 -clean -target=emscripten pyro-framework"
-Const TestArgs:="mx2cc makemods -verbose -target=desktop pyro-framework"
+'Const TestArgs:="mx2cc makemods -verbose -target=desktop pyro-framework"
  
  
-'Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
+Const TestArgs:="mx2cc makeapp src/mx2cc/test.monkey2"
 
 
 'Const TestArgs:="mx2cc makeapp -parse -geninfo src/mx2cc/test.monkey2"
 'Const TestArgs:="mx2cc makeapp -parse -geninfo src/mx2cc/test.monkey2"
 
 

+ 16 - 13
src/mx2cc/test.monkey2

@@ -1,24 +1,27 @@
 
 
-Class C
+Global weakRef:WeakRef
 
 
-	Method Update()
-		Print "Update!"
-	End
-		
+Class C
 End
 End
 
 
-Class D
-
-	Method NewC:C()
-		
-		Return New C
-	End
+Function Test()
+	
+	weakRef=New WeakRef( New C )
 End
 End
 
 
 Function Main()
 Function Main()
 	
 	
+	Test()
 	
 	
-	(New D).NewC().Update()
+'	Local tmp:=weakRef.Target
 	
 	
-End
+	Print "weakRef valid="+(weakRef.Target<>Null)
+	
+	GCCollect()
+	GCCollect()
 
 
+	Print "weakRef valid="+(weakRef.Target<>Null)
+	
+	Print "Hello World"
+	
+End