]> err.no Git - sope/blob - Recycler/NGJavaScript/NGJavaScriptRuntime.m
added Kolab sample data
[sope] / Recycler / NGJavaScript / NGJavaScriptRuntime.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "NGJavaScriptRuntime.h"
24 #include "NGJavaScriptContext.h"
25 #include "common.h"
26 #include "globals.h"
27
28 extern NSMapTable *jsctxToObjC;
29
30 @implementation NGJavaScriptRuntime
31
32 static JSBool jsGCCallback(JSContext *cx, JSGCStatus status)
33      __attribute__((unused));
34
35 + (void)initialize {
36   static BOOL didInit = NO;
37
38   if (!didInit) {
39     NSUserDefaults *ud;
40     didInit = YES;
41     
42     ud = [NSUserDefaults standardUserDefaults];
43     
44     NGJavaScriptBridge_TRACK_FINALIZATION =
45       [ud boolForKey:@"JSTrackFinalization"];
46     NGJavaScriptBridge_TRACK_NOINFO_MEMORY =
47       [ud boolForKey:@"JSTrackNoInfoMemory"];
48     NGJavaScriptBridge_TRACK_MEMORY =
49       [ud boolForKey:@"JSTrackMemory"];
50     NGJavaScriptBridge_TRACK_MEMORY_RC =
51       [ud boolForKey:@"JSTrackMemoryRC"];
52     NGJavaScriptBridge_TRACK_FORGET =
53       [ud boolForKey:@"JSTrackForget"];
54
55     NGJavaScriptBridge_LOG_PROP_DEFINITION =
56       [ud boolForKey:@"JSTrackPropDefinition"];
57     NGJavaScriptBridge_LOG_FUNC_DEFINITION =
58       [ud boolForKey:@"JSTrackFuncDefinition"];
59
60     NGJavaScriptBridge_LOG_PROP_GET = [ud boolForKey:@"JSTrackPropGet"];
61     NGJavaScriptBridge_LOG_PROP_SET = [ud boolForKey:@"JSTrackPropSet"];
62     NGJavaScriptBridge_LOG_PROP_DEL = [ud boolForKey:@"JSTrackPropDel"];
63     NGJavaScriptBridge_LOG_PROP_ADD = [ud boolForKey:@"JSTrackPropAdd"];
64   }
65 }
66
67 + (id)standardJavaScriptRuntime {
68   static id stdrt = nil;
69
70   if (stdrt == nil) {
71     stdrt = [[self alloc] init];
72   }
73   return stdrt;
74 }
75
76 - (id)initWithGCCollectSize:(unsigned)_size {
77   self->handle = JS_NewRuntime(_size ? _size : 1024 * 1024 /* 1 MB default */);
78   if (self->handle == NULL) {
79     RELEASE(self);
80     return nil;
81   }
82
83   // JSSetGCCallbackRT(self->handle, jsGCCallback);
84   
85   return self;
86 }
87 - (id)init {
88   return [self initWithGCCollectSize:
89                  [[NSUserDefaults standardUserDefaults]
90                                   integerForKey:@"JSGCCollectSize"]];
91 }
92
93 - (void)dealloc {
94   if (self->handle)
95     JS_DestroyRuntime(self->handle);
96   [super dealloc];
97 }
98
99 - (void *)handle {
100   return self->handle;
101 }
102
103 /* named roots */
104
105 static void rootDumper(const char *name, void *rp, void *data)
106      __attribute__((unused));
107
108 static void rootDumper(const char *name, void *rp, void *data) {
109   printf("ROOT: %s rp=0x%08X rt=0x%08X\n", name, (unsigned)rp, (unsigned)data);
110 }
111
112 - (void)dumpNamedRoots {
113 #if DEBUG && 0
114   JS_DumpNamedRoots(self->handle, rootDumper, self);
115 #endif
116 }
117
118 /* garbage collector */
119
120 - (BOOL)beginGarbageCollectionInContext:(NGJavaScriptContext *)_ctx {
121   return YES;
122 }
123 - (BOOL)endGarbageCollectionInContext:(NGJavaScriptContext *)_ctx {
124   return YES;
125 }
126
127 /* contexts */
128
129 - (NSArray *)contexts {
130   JSContext *ctx, *iterp;
131   NSMutableArray *cts;
132
133   cts = [NSMutableArray array];
134   
135   for (iterp = NULL; (ctx = JS_ContextIterator(self->handle, &iterp)) != NULL;) {
136     NGJavaScriptContext *octx;
137
138     octx = NSMapGet(jsctxToObjC, ctx);
139     [cts addObject:octx];
140   }
141   return AUTORELEASE([cts copy]);
142 }
143
144 /* globals */
145
146 + (NSString *)javaScriptImplementationVersion {
147   return [NSString stringWithCString:JS_GetImplementationVersion()];
148 }
149
150 /* description */
151
152 - (NSString *)description {
153   return [NSString stringWithFormat:
154                      @"<%@[0x%08X]: handle=0x%08X>",
155                      NSStringFromClass([self class]), self,
156                      [self handle]
157                    ];
158 }
159
160 /* statics */
161
162 static JSBool jsGCCallback(JSContext *cx, JSGCStatus status) {
163   NGJavaScriptContext *ctx;
164   NGJavaScriptRuntime *rt;
165   
166   ctx = NSMapGet(jsctxToObjC, cx);
167   rt  = [ctx runtime];
168   
169   return (status == JSGC_BEGIN)
170     ? ([rt beginGarbageCollectionInContext:ctx] ? JSVAL_TRUE : JSVAL_FALSE)
171     : ([rt endGarbageCollectionInContext:ctx]   ? JSVAL_TRUE : JSVAL_FALSE);
172 }
173
174 @end /* NGJavaScriptRuntime */