Oolite
Loading...
Searching...
No Matches
OOJSPopulatorDefinition.m
Go to the documentation of this file.
1/*
2
3OOJSPopulatorDefinition.m (QuickJS-ng)
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 = JS_UNDEFINED;
37 _callbackThis = JS_UNDEFINED;
38
40
41 // Stays registered until dealloc, so values set after a reset are also released on the next one.
42 [[NSNotificationCenter defaultCenter] addObserver:self
43 selector:@selector(deleteJSPointers)
44 name:kOOJavaScriptEngineWillResetNotification
46
47 return self;
48}
49
50- (void)deleteJSPointers
51{
52 JSContext* context = OOJSAcquireContext();
53 JS_FreeValue(context, _callback);
54 _callback = JS_UNDEFINED;
55 JS_FreeValue(context, _callbackThis);
56 _callbackThis = JS_UNDEFINED;
57 OOJSRelinquishContext(context);
58}
59
60- (void)dealloc
61{
62 [[NSNotificationCenter defaultCenter] removeObserver:self
63 name:kOOJavaScriptEngineWillResetNotification
65
66 [_owningScript release];
67
68 [self deleteJSPointers];
69
70 [super dealloc];
71}
72
73- (JSValue)callback
74{
75 return JS_DupValue(OOJSAcquireContext(), _callback);
76}
77
78- (void)setCallback:(JSValueConst)callback
79{
80 JSContext* context = OOJSAcquireContext();
81 JSValue old = _callback;
82 _callback = JS_DupValue(context, callback);
83 JS_FreeValue(context, old);
84 OOJSRelinquishContext(context);
85}
86
87- (JSValue)callbackThis
88{
89 return JS_DupValue(OOJSAcquireContext(), _callbackThis);
90}
91
92- (void)setCallbackThis:(JSValueConst)callbackThis
93{
94 JSContext* context = OOJSAcquireContext();
95 JSValue old = _callbackThis;
96 _callbackThis = JS_DupValue(context, callbackThis);
97 JS_FreeValue(context, old);
98 OOJSRelinquishContext(context);
99}
100
101- (void)runCallback:(HPVector)location
102{
104 JSContext* context = OOJSAcquireContext();
105 JSValue rval = JS_UNDEFINED;
106
107 if (!JS_IsFunction(context, _callback)) {
108 // The callback was released by an engine reset.
109 OOJSRelinquishContext(context);
110 return;
111 }
112
113 JSValue loc = JS_UNDEFINED;
114 VectorToJSValue(context, HPVectorToVector(location), &loc);
115
116 OOJSScript* owner = [_owningScript retain]; // local copy needed
117 [OOJSScript pushScript:owner];
118
119 // Hold our own references in case the callback replaces them.
120 JSValue callback = JS_DupValue(context, _callback);
121 JSValue callbackThis = JS_IsObject(_callbackThis) ? JS_DupValue(context, _callbackThis) : JS_UNDEFINED;
122 [engine callJSFunction:callback
123 forObject:callbackThis
124 argc:1
125 argv:&loc
126 result:&rval];
127 JS_FreeValue(context, callback);
128 JS_FreeValue(context, callbackThis);
129
130 [OOJSScript popScript:owner];
131 [owner release];
132
133 JS_FreeValue(context, rval);
134 JS_FreeValue(context, loc);
135 OOJSRelinquishContext(context);
136}
137
138@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)
OOINLINE void OOJSRelinquishContext(JSContext *context)
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()