Oolite
Loading...
Searching...
No Matches
OOJSGuiScreenKeyDefinition.m
Go to the documentation of this file.
1/*
2
3OOJSGuiScreenKeyDefinition.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 OOJSGuiScreenKeyDefinition
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 [_name release];
66 [_registerKeys release];
67
68 [self deleteJSPointers];
69
70 [super dealloc];
71}
72
73- (NSString*)name
74{
75 return _name;
76}
77
78- (void)setName:(NSString*)name
79{
80 [_name autorelease];
81 _name = [name retain];
82}
83
84- (NSDictionary*)registerKeys
85{
86 return _registerKeys;
87}
88
89- (void)setRegisterKeys:(NSDictionary*)registerKeys
90{
91 [_registerKeys release];
92 _registerKeys = [registerKeys copy];
93}
94
95- (JSValue)callback
96{
97 return JS_DupValue(OOJSAcquireContext(), _callback);
98}
99
100- (void)setCallback:(JSValueConst)callback
101{
102 JSContext* context = OOJSAcquireContext();
103 JSValue old = _callback;
104 _callback = JS_DupValue(context, callback);
105 JS_FreeValue(context, old);
106 OOJSRelinquishContext(context);
107}
108
109- (JSValue)callbackThis
110{
111 return JS_DupValue(OOJSAcquireContext(), _callbackThis);
112}
113
114- (void)setCallbackThis:(JSValueConst)callbackThis
115{
116 JSContext* context = OOJSAcquireContext();
117 JSValue old = _callbackThis;
118 _callbackThis = JS_DupValue(context, callbackThis);
119 JS_FreeValue(context, old);
120 OOJSRelinquishContext(context);
121}
122
123- (void)runCallback:(NSString*)key
124{
126 JSContext* context = OOJSAcquireContext();
127 JSValue rval = JS_UNDEFINED;
128
129 if (!JS_IsFunction(context, _callback)) {
130 // The callback was released by an engine reset.
131 OOJSRelinquishContext(context);
132 return;
133 }
134
135 JSValue cKey = OOJSValueFromNativeObject(context, key);
136
137 OOJSScript* owner = [_owningScript retain]; // local copy needed
138 [OOJSScript pushScript:owner];
139
140 // Hold our own references in case the callback replaces them.
141 JSValue callback = JS_DupValue(context, _callback);
142 JSValue callbackThis = JS_IsObject(_callbackThis) ? JS_DupValue(context, _callbackThis) : JS_UNDEFINED;
143 [engine callJSFunction:callback
144 forObject:callbackThis
145 argc:1
146 argv:&cKey
147 result:&rval];
148 JS_FreeValue(context, callback);
149 JS_FreeValue(context, callbackThis);
150
151 [OOJSScript popScript:owner];
152 [owner release];
153
154 JS_FreeValue(context, rval);
155 JS_FreeValue(context, cKey);
156 OOJSRelinquishContext(context);
157}
158
159- (NSComparisonResult)interfaceCompare:(OOJSGuiScreenKeyDefinition*)other
160{
161 return [_name caseInsensitiveCompare:[other name]];
162}
163
164@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()