Oolite
Loading...
Searching...
No Matches
OOJSPopulatorDefinition.m
Go to the documentation of this file.
1/*
2
3OOJSPopulatorDefinition.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 "OOJSVector.h"
29#import "OOMaths.h"
30
31@implementation OOJSPopulatorDefinition
32
33- (id)init
34{
35 self = [super init];
36 _callback = JSVAL_VOID;
37 _callbackThis = NULL;
38
40
41 [[NSNotificationCenter defaultCenter] addObserver:self
42 selector:@selector(deleteJSPointers)
43 name:kOOJavaScriptEngineWillResetNotification
45
46 return self;
47}
48
50{
51
52 JSContext* context = OOJSAcquireContext();
53 _callback = JSVAL_VOID;
54 _callbackThis = NULL;
55 JS_RemoveValueRoot(context, &_callback);
56 JS_RemoveObjectRoot(context, &_callbackThis);
57
58 OOJSRelinquishContext(context);
59
60 [[NSNotificationCenter defaultCenter] removeObserver:self
61 name:kOOJavaScriptEngineWillResetNotification
63}
64
65- (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- (void)setCallback:(jsval)callback
80{
81 JSContext* context = OOJSAcquireContext();
82 JS_RemoveValueRoot(context, &_callback);
84 OOJSAddGCValueRoot(context, &_callback, "OOJSPopulatorDefinition callback function");
85 OOJSRelinquishContext(context);
86}
87
88- (JSObject*)callbackThis
89{
90 return _callbackThis;
91}
92
93- (void)setCallbackThis:(JSObject*)callbackThis
94{
95 JSContext* context = OOJSAcquireContext();
96 JS_RemoveObjectRoot(context, &_callbackThis);
98 OOJSAddGCObjectRoot(context, &_callbackThis, "OOJSPopulatorDefinition callback this");
99 OOJSRelinquishContext(context);
100}
101
102- (void)runCallback:(HPVector)location
103{
104 OOJavaScriptEngine* engine = [OOJavaScriptEngine sharedEngine];
105 JSContext* context = OOJSAcquireContext();
106 jsval loc, rval = JSVAL_VOID;
107
108 VectorToJSValue(context, HPVectorToVector(location), &loc);
109
110 OOJSScript* owner = [_owningScript retain]; // local copy needed
111 [OOJSScript pushScript:owner];
112
113 [engine callJSFunction:_callback
114 forObject:_callbackThis
115 argc:1
116 argv:&loc
117 result:&rval];
118
119 [OOJSScript popScript:owner];
120 [owner release];
121
122 OOJSRelinquishContext(context);
123}
124
125@end
OOINLINE Vector HPVectorToVector(HPVector v) INLINE_CONST_FUNC
Definition OOHPVector.h:175
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:264
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: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()