Line data Source code
1 0 : /* 2 : 3 : OOJSPopulatorDefinition.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 "OOJSPopulatorDefinition.h" 27 : #import "OOJavaScriptEngine.h" 28 : #import "OOMaths.h" 29 : #import "OOJSVector.h" 30 : 31 : @implementation OOJSPopulatorDefinition 32 : 33 0 : - (id) init { 34 : self = [super init]; 35 : _callback = JSVAL_VOID; 36 : _callbackThis = NULL; 37 : 38 : _owningScript = [[OOJSScript currentlyRunningScript] weakRetain]; 39 : 40 : [[NSNotificationCenter defaultCenter] addObserver:self 41 : selector:@selector(deleteJSPointers) 42 : name:kOOJavaScriptEngineWillResetNotification 43 : object:[OOJavaScriptEngine sharedEngine]]; 44 : 45 : return self; 46 : } 47 : 48 0 : - (void) deleteJSPointers 49 : { 50 : 51 : JSContext *context = OOJSAcquireContext(); 52 : _callback = JSVAL_VOID; 53 : _callbackThis = NULL; 54 : JS_RemoveValueRoot(context, &_callback); 55 : JS_RemoveObjectRoot(context, &_callbackThis); 56 : 57 : OOJSRelinquishContext(context); 58 : 59 : [[NSNotificationCenter defaultCenter] removeObserver:self 60 : name:kOOJavaScriptEngineWillResetNotification 61 : object:[OOJavaScriptEngine sharedEngine]]; 62 : 63 : } 64 : 65 0 : - (void) dealloc 66 : { 67 : [_owningScript release]; 68 : 69 : [self deleteJSPointers]; 70 : 71 : [super dealloc]; 72 : } 73 : 74 : - (jsval)callback 75 : { 76 : return _callback; 77 : } 78 : 79 : 80 : - (void)setCallback:(jsval)callback 81 : { 82 : JSContext *context = OOJSAcquireContext(); 83 : JS_RemoveValueRoot(context, &_callback); 84 : _callback = callback; 85 : OOJSAddGCValueRoot(context, &_callback, "OOJSPopulatorDefinition callback function"); 86 : OOJSRelinquishContext(context); 87 : } 88 : 89 : 90 : - (JSObject *)callbackThis 91 : { 92 : return _callbackThis; 93 : } 94 : 95 : 96 : - (void)setCallbackThis:(JSObject *)callbackThis 97 : { 98 : JSContext *context = OOJSAcquireContext(); 99 : JS_RemoveObjectRoot(context, &_callbackThis); 100 : _callbackThis = callbackThis; 101 : OOJSAddGCObjectRoot(context, &_callbackThis, "OOJSPopulatorDefinition callback this"); 102 : OOJSRelinquishContext(context); 103 : } 104 : 105 : 106 : - (void)runCallback:(HPVector)location 107 : { 108 : OOJavaScriptEngine *engine = [OOJavaScriptEngine sharedEngine]; 109 : JSContext *context = OOJSAcquireContext(); 110 : jsval loc, rval = JSVAL_VOID; 111 : 112 : VectorToJSValue(context, HPVectorToVector(location), &loc); 113 : 114 : OOJSScript *owner = [_owningScript retain]; // local copy needed 115 : [OOJSScript pushScript:owner]; 116 : 117 : [engine callJSFunction:_callback 118 : forObject:_callbackThis 119 : argc:1 120 : argv:&loc 121 : result:&rval]; 122 : 123 : [OOJSScript popScript:owner]; 124 : [owner release]; 125 : 126 : OOJSRelinquishContext(context); 127 : } 128 : 129 : @end