Oolite
Loading...
Searching...
No Matches
EntityOOJavaScriptExtensions.m
Go to the documentation of this file.
1/*
2
3EntityOOJavaScriptExtensions.m
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*/
24
26#import "DockEntity.h"
27#import "OOJSDock.h"
28#import "OOJSEntity.h"
29#import "OOJSShip.h"
30#import "OOJSStation.h"
31#import "OOJSVisualEffect.h"
32#import "OOJSWormhole.h"
33#import "OOPlanetEntity.h"
34#import "StationEntity.h"
35#import "WormholeEntity.h"
36
37@implementation Entity (OOJavaScriptExtensions)
38
40{
41 return NO;
42}
43
44- (NSString*)oo_jsClassName
45{
46 return @"Entity";
47}
48
49- (jsval)oo_jsValueInContext:(JSContext*)context
50{
51 JSClass* class = NULL;
52 JSObject* prototype = NULL;
53 jsval result = JSVAL_NULL;
54
55 if (_jsSelf == NULL && [self isVisibleToScripts]) {
56 // Create JS object
57 [self getJSClass:&class andPrototype:&prototype];
58
59 _jsSelf = JS_NewObject(context, class, prototype, NULL);
60 if (_jsSelf != NULL) {
61 if (!JS_SetPrivate(context, _jsSelf, OOConsumeReference([self weakRetain])))
62 _jsSelf = NULL;
63 }
64
65 if (_jsSelf != NULL) {
66 OOJSAddGCObjectRoot(context, &_jsSelf, "Entity jsSelf");
67 [[NSNotificationCenter defaultCenter] addObserver:self
68 selector:@selector(deleteJSSelf)
69 name:kOOJavaScriptEngineWillResetNotification
71 }
72 }
73
74 if (_jsSelf != NULL)
75 result = OBJECT_TO_JSVAL(_jsSelf);
76
77 return result;
78 // Analyzer: object leaked. [Expected, object is retained by JS object.]
79}
80
81- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
82{
83 *outClass = JSEntityClass();
84 *outPrototype = JSEntityPrototype();
85}
86
87- (void)deleteJSSelf
88{
89 if (_jsSelf != NULL) {
90 _jsSelf = NULL;
91 JSContext* context = OOJSAcquireContext();
92 JS_RemoveObjectRoot(context, &_jsSelf);
93 OOJSRelinquishContext(context);
94
95 [[NSNotificationCenter defaultCenter] removeObserver:self
96 name:kOOJavaScriptEngineWillResetNotification
98 }
99}
100
101@end
102
103@implementation ShipEntity (OOJavaScriptExtensions)
104
106{
107 return YES;
108}
109
110- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
111{
112 *outClass = JSShipClass();
113 *outPrototype = JSShipPrototype();
114}
115
116- (NSString*)oo_jsClassName
117{
118 return @"Ship";
119}
120
121- (NSArray*)subEntitiesForScript
122{
123 return [[self shipSubEntityEnumerator] allObjects];
124}
125
126- (void)setTargetForScript:(ShipEntity*)target
127{
128 ShipEntity* me = self;
129
130 // Ensure coherence by not fiddling with subentities.
131 while ([me isSubEntity]) {
132 if (me == [me owner] || [me owner] == nil)
133 break;
134 me = (ShipEntity*)[me owner];
135 }
136 while ([target isSubEntity]) {
137 if (target == [target owner] || [target owner] == nil)
138 break;
139 target = (ShipEntity*)[target owner];
140 }
141 if (![me isKindOfClass:[ShipEntity class]])
142 return;
143 if (target != nil) {
144 [me addTarget:target];
145 } else
146 [me removeTarget:[me primaryTarget]];
147}
148
149@end
id OOConsumeReference(id OO_NS_CONSUMED value)
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
JSObject * JSShipPrototype(void)
Definition OOJSShip.m:587
JSClass * JSShipClass(void)
Definition OOJSShip.m:582
OOINLINE JSContext * OOJSAcquireContext(void)
#define OOJSAddGCObjectRoot(context, root, name)
OOINLINE void OOJSRelinquishContext(JSContext *context)
return nil
void getJSClass:andPrototype:(JSClass **outClass,[andPrototype] JSObject **outPrototype)
OOJavaScriptEngine * sharedEngine()
void addTarget:(Entity *targetEntity)
void removeTarget:(Entity *targetEntity)