Oolite
Loading...
Searching...
No Matches
OOJSInterfaceDefinition.m
Go to the documentation of this file.
1/*
2
3OOJSInterfaceDefinition.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
28
29@implementation OOJSInterfaceDefinition
30
31- (id)init
32{
33 self = [super init];
34 _callback = JSVAL_VOID;
35 _callbackThis = NULL;
36
38
39 [[NSNotificationCenter defaultCenter] addObserver:self
40 selector:@selector(deleteJSPointers)
41 name:kOOJavaScriptEngineWillResetNotification
43
44 return self;
45}
46
48{
49
50 JSContext* context = OOJSAcquireContext();
51 _callback = JSVAL_VOID;
52 _callbackThis = NULL;
53 JS_RemoveValueRoot(context, &_callback);
54 JS_RemoveObjectRoot(context, &_callbackThis);
55
56 OOJSRelinquishContext(context);
57
58 [[NSNotificationCenter defaultCenter] removeObserver:self
59 name:kOOJavaScriptEngineWillResetNotification
61}
62
63- (void)dealloc
64{
65 [_owningScript release];
66
67 [self deleteJSPointers];
68
69 [super dealloc];
70}
71
72- (NSString*)title
73{
74 return _title;
75}
76
77- (void)setTitle:(NSString*)title
78{
79 [_title autorelease];
80 _title = [title retain];
81}
82
83- (NSString*)category
84{
85 return _category;
86}
87
88- (void)setCategory:(NSString*)category
89{
90 [_category autorelease];
91 _category = [category retain];
92}
93
94- (NSString*)summary
95{
96 return _summary;
97}
98
99- (void)setSummary:(NSString*)summary
100{
101 [_summary autorelease];
102 _summary = [summary retain];
103}
104
105- (jsval)callback
106{
107 return _callback;
108}
109
110- (void)setCallback:(jsval)callback
111{
112 JSContext* context = OOJSAcquireContext();
113 JS_RemoveValueRoot(context, &_callback);
115 OOJSAddGCValueRoot(context, &_callback, "OOJSInterfaceDefinition callback function");
116 OOJSRelinquishContext(context);
117}
118
119- (JSObject*)callbackThis
120{
121 return _callbackThis;
122}
123
124- (void)setCallbackThis:(JSObject*)callbackThis
125{
126 JSContext* context = OOJSAcquireContext();
127 JS_RemoveObjectRoot(context, &_callbackThis);
129 OOJSAddGCObjectRoot(context, &_callbackThis, "OOJSInterfaceDefinition callback this");
130 OOJSRelinquishContext(context);
131}
132
133- (void)runCallback:(NSString*)key
134{
135 OOJavaScriptEngine* engine = [OOJavaScriptEngine sharedEngine];
136 JSContext* context = OOJSAcquireContext();
137 jsval rval = JSVAL_VOID;
138
139 jsval cKey = OOJSValueFromNativeObject(context, key);
140
141 OOJSScript* owner = [_owningScript retain]; // local copy needed
142 [OOJSScript pushScript:owner];
143
144 [engine callJSFunction:_callback
145 forObject:_callbackThis
146 argc:1
147 argv:&cKey
148 result:&rval];
149
150 [OOJSScript popScript:owner];
151 [owner release];
152
153 OOJSRelinquishContext(context);
154}
155
156- (NSComparisonResult)interfaceCompare:(OOJSInterfaceDefinition*)other
157{
158 NSComparisonResult byCategory = [_category caseInsensitiveCompare:[other category]];
159 if (byCategory == NSOrderedSame) {
160 return [_title caseInsensitiveCompare:[other title]];
161 } else {
162 return byCategory;
163 }
164}
165
166@end
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
OOINLINE JSContext * OOJSAcquireContext(void)
#define OOJSAddGCObjectRoot(context, root, name)
OOINLINE void OOJSRelinquishContext(JSContext *context)
#define OOJSAddGCValueRoot(context, root, name)
void pushScript:(OOJSScript *script)
Definition OOJSScript.m:480
OOJSScript * currentlyRunningScript()
Definition OOJSScript.m:101
void popScript:(OOJSScript *script)
Definition OOJSScript.m:493
BOOL callJSFunction:forObject:argc:argv:result:(jsval function,[forObject] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *outResult)
OOJavaScriptEngine * sharedEngine()