Oolite
Loading...
Searching...
No Matches
EntityOOJavaScriptExtensions.m
Go to the documentation of this file.
1/*
2
3EntityOOJavaScriptExtensions.m (QuickJS-ng)
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22*/
23
25#import "DockEntity.h"
26#import "OOJSDock.h"
27#import "OOJSEntity.h"
28#import "OOJSShip.h"
29#import "OOJSStation.h"
30#import "OOJSVisualEffect.h"
31#import "OOJSWormhole.h"
32#import "OOPlanetEntity.h"
33#import "StationEntity.h"
34#import "WormholeEntity.h"
35
36@implementation Entity (OOJavaScriptExtensions)
37
39{
40 return NO;
41}
42
43- (NSString*)oo_jsClassName
44{
45 return @"Entity";
46}
47
48- (JSValue)oo_jsValueInContext:(JSContext*)context
49{
50 if (!JS_IsObject(_jsSelf) && [self isVisibleToScripts]) {
51 // Create JS object. The entity keeps a reference to it for as long as the entity exists.
52 _jsSelf = JS_NewObjectClass(context, (int)[self oo_jsClassID]);
53 if (JS_IsException(_jsSelf)) {
54 JS_FreeValue(context, JS_GetException(context));
55 _jsSelf = JS_UNDEFINED;
56 } else {
57 JS_SetOpaque(_jsSelf, OOConsumeReference([self weakRetain]));
58
59 [[NSNotificationCenter defaultCenter] addObserver:self
60 selector:@selector(deleteJSSelf)
61 name:kOOJavaScriptEngineWillResetNotification
63 }
64 }
65
66 if (JS_IsObject(_jsSelf))
67 return JS_DupValue(context, _jsSelf);
68
69 return JS_NULL;
70 // Analyzer: object leaked. [Expected, object is retained by JS object.]
71}
72
73- (JSClassID)oo_jsClassID
74{
75 return JSEntityClassID();
76}
77
79{
80 if (JS_IsObject(_jsSelf)) {
81 /* Scripts may still hold the JS object. Its private data is a weak
82 reference, so it becomes stale (isValid false) when the entity is
83 deallocated.
84 */
85 JS_FreeValue(OOJSAcquireContext(), _jsSelf);
86 _jsSelf = JS_UNDEFINED;
87
88 [[NSNotificationCenter defaultCenter] removeObserver:self
89 name:kOOJavaScriptEngineWillResetNotification
91 }
92}
93
94@end
95
96@implementation ShipEntity (OOJavaScriptExtensions)
97
98- (BOOL)isVisibleToScripts
99{
100 return YES;
101}
102
103- (JSClassID)oo_jsClassID
104{
105 return JSShipClassID();
106}
107
108- (NSString*)oo_jsClassName
109{
110 return @"Ship";
111}
112
114{
115 return [[self shipSubEntityEnumerator] allObjects];
116}
117
118- (void)setTargetForScript:(ShipEntity*)target
119{
120 ShipEntity* me = self;
121
122 // Ensure coherence by not fiddling with subentities.
123 while ([me isSubEntity]) {
124 if (me == [me owner] || [me owner] == nil)
125 break;
126 me = (ShipEntity*)[me owner];
127 }
128 while ([target isSubEntity]) {
129 if (target == [target owner] || [target owner] == nil)
130 break;
131 target = (ShipEntity*)[target owner];
132 }
133 if (![me isKindOfClass:[ShipEntity class]])
134 return;
135 if (target != nil) {
136 [me addTarget:target];
137 } else
139}
140
141@end
id OOConsumeReference(id OO_NS_CONSUMED value)
OOINLINE JSContext * OOJSAcquireContext(void)
return nil
JSClassID JSEntityClassID(void)
Definition OOJSEntity.m:115
JSClassID JSShipClassID(void)
Definition OOJSShip.m:576
OOJavaScriptEngine * sharedEngine()
void addTarget:(Entity *targetEntity)
void removeTarget:(Entity *targetEntity)
NSEnumerator * shipSubEntityEnumerator()