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