Skip to content

Commit 72d006b

Browse files
committed
Move jawaObjectPool into JawaExecutor to make sure that all JawaObjects are garbage-collected when the executor is destroyed.
1 parent 078d02a commit 72d006b

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

JawaScriptExecutive-iOS/JawaExecutor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ extern int release_count;
1919
@interface JawaExecutor : NSObject
2020
{
2121
JawaObjectRef* NULL_CONSTANT;
22-
}
22+
}
2323

2424
@property (weak) NSMutableDictionary* env;
2525
@property NSMutableDictionary* global;
2626
@property NSMutableArray* activations;
2727
@property NSMutableArray* currentActivation;
2828
@property NSMutableDictionary* currentIterationScope;
29+
@property NSMutableArray* jawaObjectPool;
2930
@property BOOL isFromCallExpression;
3031
@property id<JawaExternalCallback> externalCallback;
3132

JawaScriptExecutive-iOS/JawaExecutor.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ -(id)init {
3232

3333
NULL_CONSTANT = [JawaObjectRef RefIn:self];
3434

35-
jawaObjectPool = [[NSMutableArray alloc]init];
35+
_jawaObjectPool = [[NSMutableArray alloc]init];
3636

3737
arrayPrototype = [[NSMutableDictionary alloc]init];
3838
stringPrototype = [[NSMutableDictionary alloc]init];
@@ -205,14 +205,14 @@ -(NSMutableDictionary*)invoke:(NSString *)funcName with:(NSMutableDictionary*)as
205205
setObject:n.boolValue ? @"true" : @"false"
206206
forKey:@"retValue"];
207207
}
208-
//printf("%lu objects are in pool.\n", jawaObjectPool.count);
208+
//printf("%lu objects are in pool.\n", _jawaObjectPool.count);
209209
[self gc];
210210

211211
return retJSON;
212212
}
213213

214214
-(void)gc {
215-
for (JawaObjectRef* obj in jawaObjectPool) {
215+
for (JawaObjectRef* obj in _jawaObjectPool) {
216216
obj.marked = false;
217217
obj.discovered = false;
218218
}
@@ -222,12 +222,14 @@ -(void)gc {
222222
}
223223

224224
NSMutableArray *markedObj = [NSMutableArray array];
225-
for (JawaObjectRef* obj in jawaObjectPool) {
225+
for (JawaObjectRef* obj in _jawaObjectPool) {
226226
if (obj.marked)
227227
[markedObj addObject:obj];
228+
//else
229+
// printf("Recycled: %d\n", obj.obj_id);
228230
}
229-
//printf("%lu objects recycled.\n", jawaObjectPool.count - markedObj.count);
230-
[jawaObjectPool setArray:markedObj];
231+
//printf("%lu objects recycled.\n", _jawaObjectPool.count - markedObj.count);
232+
[_jawaObjectPool setArray:markedObj];
231233
}
232234

233235
-(void)markAndTraverse:(JawaObjectRef*)objRef {
@@ -324,6 +326,10 @@ -(JawaObjectRef*)toJawaArray:(NSArray*)array {
324326
return [JawaObjectRef RefWithJawaArray:jawaArr];
325327
}
326328

329+
//-(void)dealloc {
330+
// printf("Executor Recycled:\n");
331+
//}
332+
327333
-(JawaObjectRef*)evalInExpression:(NSDictionary*)ast {
328334
NSArray* subExpressions = [ast objectForKey:PR_subExpressions];
329335

JawaScriptExecutive-iOS/JawaObjectRef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
@class JawaFunc;
1616
@class JawaObject;
1717

18-
extern NSMutableArray* jawaObjectPool;
1918
extern int release_count;
2019
@interface JawaObjectRef : NSObject
2120
{
@@ -24,6 +23,7 @@ extern int release_count;
2423

2524
@property bool marked;
2625
@property bool discovered;
26+
@property int obj_id;
2727
@property (strong) NSObject* object;
2828
@property (weak) JawaObjectRef* appliedOn;
2929
@property (weak) JawaExecutor* executor;

JawaScriptExecutive-iOS/JawaObjectRef.m

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#import "JawaNumber.h"
1414
#import "JawaObjectRef.h"
1515

16-
NSMutableArray* jawaObjectPool;
1716
int release_count;
17+
int obj_count;
1818

1919
@implementation JawaObjectRef
2020

@@ -23,7 +23,9 @@ -(id)initIn:(JawaExecutor*)ex {
2323
if (self) {
2424
_object = NULL;
2525
_executor = ex;
26-
[jawaObjectPool addObject:self];
26+
_obj_id = obj_count++;
27+
//printf("%d : null\n", _obj_id);
28+
[ex.jawaObjectPool addObject:self];
2729
}
2830
return self;
2931
}
@@ -33,7 +35,9 @@ -(id)initWithNumber:(double)number in:(JawaExecutor*)ex {
3335
if (self) {
3436
_object = [[JawaNumber alloc]init:number];
3537
_executor = ex;
36-
[jawaObjectPool addObject:self];
38+
_obj_id = obj_count++;
39+
//printf("%d : number(%lf)\n", _obj_id, number);
40+
[ex.jawaObjectPool addObject:self];
3741
}
3842
return self;
3943
}
@@ -43,7 +47,9 @@ -(id)initWithString:(NSString*)string in:(JawaExecutor*)ex {
4347
if (self) {
4448
_object = [NSMutableString stringWithString:string];
4549
_executor = ex;
46-
[jawaObjectPool addObject:self];
50+
_obj_id = obj_count++;
51+
//printf("%d : string(%s)\n", _obj_id, [string cStringUsingEncoding: NSUTF8StringEncoding]);
52+
[ex.jawaObjectPool addObject:self];
4753
}
4854
return self;
4955
}
@@ -53,7 +59,9 @@ -(id)initWithBoolean:(bool)tf in:(JawaExecutor*)ex {
5359
if (self) {
5460
_object = [NSNumber numberWithBool:tf];
5561
_executor = ex;
56-
[jawaObjectPool addObject:self];
62+
_obj_id = obj_count++;
63+
//printf("%d : bool(%d)\n", _obj_id, tf);
64+
[ex.jawaObjectPool addObject:self];
5765
}
5866
return self;
5967
}
@@ -63,7 +71,9 @@ -(id)initWithJawaArray:(JawaArray*)array {
6371
if (self) {
6472
_object = array;
6573
_executor = array.executor;
66-
[jawaObjectPool addObject:self];
74+
_obj_id = obj_count++;
75+
//printf("%d : array\n", _obj_id);
76+
[_executor.jawaObjectPool addObject:self];
6777
}
6878
return self;
6979
}
@@ -74,7 +84,9 @@ -(id)initWithJawaFunc:(JawaFunc*)func {
7484
_object = func;
7585
_appliedOn = nil;
7686
_executor = func.executor;
77-
[jawaObjectPool addObject:self];
87+
_obj_id = obj_count++;
88+
//printf("%d : func(%s)\n", _obj_id, [[func.name description]cStringUsingEncoding:NSUTF8StringEncoding]);
89+
[_executor.jawaObjectPool addObject:self];
7890
}
7991
return self;
8092
}
@@ -85,7 +97,9 @@ -(id)initWithJawaFunc:(JawaFunc*)func on:(JawaObjectRef*)obj {
8597
_object = func;
8698
_appliedOn = obj;
8799
_executor = func.executor;
88-
[jawaObjectPool addObject:self];
100+
_obj_id = obj_count++;
101+
//printf("%d : func(%s)\n", _obj_id, [[func.name description]cStringUsingEncoding:NSUTF8StringEncoding]);
102+
[_executor.jawaObjectPool addObject:self];
89103
}
90104
return self;
91105
}
@@ -95,7 +109,9 @@ -(id)initWithJawaObject:(JawaObject*)obj {
95109
if (self) {
96110
_object = obj;
97111
_executor = obj.executor;
98-
[jawaObjectPool addObject:self];
112+
_obj_id = obj_count++;
113+
//printf("%d : object\n", _obj_id);
114+
[_executor.jawaObjectPool addObject:self];
99115
}
100116
return self;
101117
}
@@ -124,9 +140,10 @@ -(id)transfer {
124140
} else
125141
return self.object;
126142
}
143+
127144
-(void)dealloc {
128145
release_count++;
129-
//printf("Releasing %s\n", [NSStringFromClass([self class]) cStringUsingEncoding:NSUTF8StringEncoding]);
146+
//printf("Releasing %s\n", [NSStringFromClass([self class]) cStringUsingEncoding:NSUTF8StringEncoding]);
130147
}
131148

132149
+(id)RefIn:(JawaExecutor *)ex {

JawaScriptExecutive-iOS/test.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,20 @@ BOOL test34() {
828828
return true;
829829
}
830830

831+
BOOL test35() {
832+
JawaExecutor* ex = [[JawaExecutor alloc]init];
833+
NSString *parsed = @"{\"t\":0,\"0\":[{\"t\":1,\"3\":\"test\",\"23\":[],\"24\":{\"t\":2,\"0\":[{\"t\":37,\"33\":[{\"t\":34,\"26\":\"a\",\"27\":{\"t\":28,\"6\":[{\"t\":39,\"4\":\"name\",\"5\":{\"t\":25,\"8\":\"STRING_LITERAL,a\"}}]}}]},{\"t\":37,\"33\":[{\"t\":34,\"26\":\"b\",\"27\":{\"t\":28,\"6\":[{\"t\":39,\"4\":\"name\",\"5\":{\"t\":25,\"8\":\"STRING_LITERAL,b\"}}]}}]},{\"t\":5,\"14\":\"PUNCTUATOR,=\",\"20\":{\"t\":20,\"10\":{\"t\":24,\"3\":\"a\"},\"11\":{\"t\":24,\"3\":\"key\"}},\"21\":{\"t\":24,\"3\":\"b\"}},{\"t\":5,\"14\":\"PUNCTUATOR,=\",\"20\":{\"t\":20,\"10\":{\"t\":24,\"3\":\"b\"},\"11\":{\"t\":24,\"3\":\"key\"}},\"21\":{\"t\":24,\"3\":\"a\"}},{\"t\":36,\"32\":{\"t\":25,\"8\":\"NULL,null\"}}]}}]}";
834+
835+
NSDictionary* prog = jsonToDictionary(parsed);
836+
837+
[ex execute:prog];
838+
[ex invoke:@"test" with:nil];
839+
printf("Test 35 passed.\n");
840+
return true;
841+
}
842+
831843
int main(int argc, char *argv[]) {
832844
@autoreleasepool {
833-
834845
if (!test1()) return -1;
835846
if (!test2()) return -1;
836847
if (!test3()) return -1;
@@ -865,6 +876,7 @@ int main(int argc, char *argv[]) {
865876
if (!test32()) return -1;
866877
if (!test33()) return -1;
867878
if (!test34()) return -1;
879+
if (!test35()) return -1;
868880
}
869881
//printf("%d\n", release_count);
870882
return 0;

0 commit comments

Comments
 (0)