Oolite 1.91.0.7604-240417-a536cbe
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
28#import "OOMaths.h"
29#import "OOJSVector.h"
30
31@implementation OOJSPopulatorDefinition
32
33- (id) init {
34 self = [super init];
35 _callback = JSVAL_VOID;
36 _callbackThis = NULL;
37
39
40 [[NSNotificationCenter defaultCenter] addObserver:self
41 selector:@selector(deleteJSPointers)
42 name:kOOJavaScriptEngineWillResetNotification
44
45 return self;
46}
47
48- (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
62
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
80- (void)setCallback:(jsval)callback
81{
82 JSContext *context = OOJSAcquireContext();
83 JS_RemoveValueRoot(context, &_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);
101 OOJSAddGCObjectRoot(context, &_callbackThis, "OOJSPopulatorDefinition callback this");
102 OOJSRelinquishContext(context);
103}
104
105
106- (void)runCallback:(HPVector)location
107{
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
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:185
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()