2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
23 #include "NGJavaScriptRuntime.h"
24 #include "NGJavaScriptContext.h"
28 extern NSMapTable *jsctxToObjC;
30 @implementation NGJavaScriptRuntime
32 static JSBool jsGCCallback(JSContext *cx, JSGCStatus status)
33 __attribute__((unused));
36 static BOOL didInit = NO;
42 ud = [NSUserDefaults standardUserDefaults];
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"];
55 NGJavaScriptBridge_LOG_PROP_DEFINITION =
56 [ud boolForKey:@"JSTrackPropDefinition"];
57 NGJavaScriptBridge_LOG_FUNC_DEFINITION =
58 [ud boolForKey:@"JSTrackFuncDefinition"];
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"];
67 + (id)standardJavaScriptRuntime {
68 static id stdrt = nil;
71 stdrt = [[self alloc] init];
76 - (id)initWithGCCollectSize:(unsigned)_size {
77 self->handle = JS_NewRuntime(_size ? _size : 1024 * 1024 /* 1 MB default */);
78 if (self->handle == NULL) {
83 // JSSetGCCallbackRT(self->handle, jsGCCallback);
88 return [self initWithGCCollectSize:
89 [[NSUserDefaults standardUserDefaults]
90 integerForKey:@"JSGCCollectSize"]];
95 JS_DestroyRuntime(self->handle);
105 static void rootDumper(const char *name, void *rp, void *data)
106 __attribute__((unused));
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);
112 - (void)dumpNamedRoots {
114 JS_DumpNamedRoots(self->handle, rootDumper, self);
118 /* garbage collector */
120 - (BOOL)beginGarbageCollectionInContext:(NGJavaScriptContext *)_ctx {
123 - (BOOL)endGarbageCollectionInContext:(NGJavaScriptContext *)_ctx {
129 - (NSArray *)contexts {
130 JSContext *ctx, *iterp;
133 cts = [NSMutableArray array];
135 for (iterp = NULL; (ctx = JS_ContextIterator(self->handle, &iterp)) != NULL;) {
136 NGJavaScriptContext *octx;
138 octx = NSMapGet(jsctxToObjC, ctx);
139 [cts addObject:octx];
141 return AUTORELEASE([cts copy]);
146 + (NSString *)javaScriptImplementationVersion {
147 return [NSString stringWithCString:JS_GetImplementationVersion()];
152 - (NSString *)description {
153 return [NSString stringWithFormat:
154 @"<%@[0x%08X]: handle=0x%08X>",
155 NSStringFromClass([self class]), self,
162 static JSBool jsGCCallback(JSContext *cx, JSGCStatus status) {
163 NGJavaScriptContext *ctx;
164 NGJavaScriptRuntime *rt;
166 ctx = NSMapGet(jsctxToObjC, cx);
169 return (status == JSGC_BEGIN)
170 ? ([rt beginGarbageCollectionInContext:ctx] ? JSVAL_TRUE : JSVAL_FALSE)
171 : ([rt endGarbageCollectionInContext:ctx] ? JSVAL_TRUE : JSVAL_FALSE);
174 @end /* NGJavaScriptRuntime */