Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOJSGuiScreenKeyDefinition.m
Go to the documentation of this file.
1/*
2
3OOJSGuiScreenKeyDefinition.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
27//#import "OOJavaScriptEngine.h"
28
29
30@implementation OOJSGuiScreenKeyDefinition
31
32- (id) init {
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
47- (void) deleteJSPointers
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
64- (void) dealloc
65{
66 [_owningScript release];
67
68 [self deleteJSPointers];
69
70 [super dealloc];
71}
72
73- (NSString *)name
74{
75 return _name;
76}
77
78
79- (void)setName:(NSString *)name
80{
81 [_name autorelease];
82 _name = [name retain];
83}
84
85
86- (NSDictionary *)registerKeys
87{
88 return _registerKeys;
89}
90
91
92- (void)setRegisterKeys:(NSDictionary *)registerKeys
93{
94 [_registerKeys release];
95 _registerKeys = [registerKeys copy];
96}
97
98
99- (jsval)callback
100{
101 return _callback;
102}
103
104
105- (void)setCallback:(jsval)callback
106{
107 JSContext *context = OOJSAcquireContext();
108 JS_RemoveValueRoot(context, &_callback);
110 OOJSAddGCValueRoot(context, &_callback, "OOJSGuiScreenKeyDefinition callback function");
111 OOJSRelinquishContext(context);
112}
113
114
115- (JSObject *)callbackThis
116{
117 return _callbackThis;
118}
119
120
121- (void)setCallbackThis:(JSObject *)callbackThis
122{
123 JSContext *context = OOJSAcquireContext();
124 JS_RemoveObjectRoot(context, &_callbackThis);
126 OOJSAddGCObjectRoot(context, &_callbackThis, "OOJSGuiScreenKeyDefinition callback this");
127 OOJSRelinquishContext(context);
128}
129
130
131- (void)runCallback:(NSString *)key
132{
134 JSContext *context = OOJSAcquireContext();
135 jsval rval = JSVAL_VOID;
136
137 jsval cKey = OOJSValueFromNativeObject(context, key);
138
139 OOJSScript *owner = [_owningScript retain]; // local copy needed
140 [OOJSScript pushScript:owner];
141
142 [engine callJSFunction:_callback
143 forObject:_callbackThis
144 argc:1
145 argv:&cKey
146 result:&rval];
147
148 [OOJSScript popScript:owner];
149 [owner release];
150
151 OOJSRelinquishContext(context);
152}
153
154
155- (NSComparisonResult)interfaceCompare:(OOJSGuiScreenKeyDefinition *)other
156{
157 return [_name caseInsensitiveCompare:[other name]];
158}
159
160@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:537
OOJSScript * currentlyRunningScript()
Definition OOJSScript.m:339
void popScript:(OOJSScript *script)
Definition OOJSScript.m:550
BOOL callJSFunction:forObject:argc:argv:result:(jsval function,[forObject] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *outResult)
OOJavaScriptEngine * sharedEngine()