Oolite
Loading...
Searching...
No Matches
OOJSInterfaceDefinition.m
Go to the documentation of this file.
1/*
2
3OOJSInterfaceDefinition.m (QuickJS-ng)
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 = JS_UNDEFINED;
35 _callbackThis = JS_UNDEFINED;
36
38
39 // Stays registered until dealloc, so values set after a reset are also released on the next one.
40 [[NSNotificationCenter defaultCenter] addObserver:self
41 selector:@selector(deleteJSPointers)
42 name:kOOJavaScriptEngineWillResetNotification
44
45 return self;
46}
47
48- (void)deleteJSPointers
49{
50 JSContext* context = OOJSAcquireContext();
51 JS_FreeValue(context, _callback);
52 _callback = JS_UNDEFINED;
53 JS_FreeValue(context, _callbackThis);
54 _callbackThis = JS_UNDEFINED;
55 OOJSRelinquishContext(context);
56}
57
58- (void)dealloc
59{
60 [[NSNotificationCenter defaultCenter] removeObserver:self
61 name:kOOJavaScriptEngineWillResetNotification
63
64 [_owningScript release];
65 [_title release];
66 [_summary release];
67 [_category release];
68
69 [self deleteJSPointers];
70
71 [super dealloc];
72}
73
74- (NSString*)title
75{
76 return _title;
77}
78
79- (void)setTitle:(NSString*)title
80{
81 [_title autorelease];
82 _title = [title retain];
83}
84
85- (NSString*)category
86{
87 return _category;
88}
89
90- (void)setCategory:(NSString*)category
91{
92 [_category autorelease];
93 _category = [category retain];
94}
95
96- (NSString*)summary
97{
98 return _summary;
99}
100
101- (void)setSummary:(NSString*)summary
102{
103 [_summary autorelease];
104 _summary = [summary retain];
105}
106
107- (JSValue)callback
108{
109 return JS_DupValue(OOJSAcquireContext(), _callback);
110}
111
112- (void)setCallback:(JSValueConst)callback
113{
114 JSContext* context = OOJSAcquireContext();
115 JSValue old = _callback;
116 _callback = JS_DupValue(context, callback);
117 JS_FreeValue(context, old);
118 OOJSRelinquishContext(context);
119}
120
121- (JSValue)callbackThis
122{
123 return JS_DupValue(OOJSAcquireContext(), _callbackThis);
124}
125
126- (void)setCallbackThis:(JSValueConst)callbackThis
127{
128 JSContext* context = OOJSAcquireContext();
129 JSValue old = _callbackThis;
130 _callbackThis = JS_DupValue(context, callbackThis);
131 JS_FreeValue(context, old);
132 OOJSRelinquishContext(context);
133}
134
135- (void)runCallback:(NSString*)key
136{
138 JSContext* context = OOJSAcquireContext();
139 JSValue rval = JS_UNDEFINED;
140
141 if (!JS_IsFunction(context, _callback)) {
142 // The callback was released by an engine reset.
143 OOJSRelinquishContext(context);
144 return;
145 }
146
147 JSValue cKey = OOJSValueFromNativeObject(context, key);
148
149 OOJSScript* owner = [_owningScript retain]; // local copy needed
150 [OOJSScript pushScript:owner];
151
152 // Hold our own references in case the callback replaces them.
153 JSValue callback = JS_DupValue(context, _callback);
154 JSValue callbackThis = JS_IsObject(_callbackThis) ? JS_DupValue(context, _callbackThis) : JS_UNDEFINED;
155 [engine callJSFunction:callback
156 forObject:callbackThis
157 argc:1
158 argv:&cKey
159 result:&rval];
160 JS_FreeValue(context, callback);
161 JS_FreeValue(context, callbackThis);
162
163 [OOJSScript popScript:owner];
164 [owner release];
165
166 JS_FreeValue(context, rval);
167 JS_FreeValue(context, cKey);
168 OOJSRelinquishContext(context);
169}
170
171- (NSComparisonResult)interfaceCompare:(OOJSInterfaceDefinition*)other
172{
173 NSComparisonResult byCategory = [_category caseInsensitiveCompare:[other category]];
174 if (byCategory == NSOrderedSame) {
175 return [_title caseInsensitiveCompare:[other title]];
176 } else {
177 return byCategory;
178 }
179}
180
181@end
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
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()