Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOJSWorldScripts.m
Go to the documentation of this file.
1/*
2
3OOJSWorldScripts.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
26#import "OOJSWorldScripts.h"
28#import "PlayerEntity.h"
29#import "OOJSPlayer.h"
30
31
32static JSBool WorldScriptsGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
33static JSBool WorldScriptsEnumerate(JSContext *cx, JSObject *obj);
34
35
36static JSClass sWorldScriptsClass =
37{
38 "WorldScripts",
39 0,
40
41 JS_PropertyStub,
42 JS_PropertyStub,
44 JS_StrictPropertyStub,
46 JS_ResolveStub,
47 JS_ConvertStub,
48 JS_FinalizeStub
49};
50
51
52void InitOOJSWorldScripts(JSContext *context, JSObject *global)
53{
54 JS_DefineObject(context, global, "worldScripts", &sWorldScriptsClass, NULL, OOJS_PROP_READONLY);
55}
56
57
58static JSBool WorldScriptsGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
59{
60 OOJS_NATIVE_ENTER(context)
61
63 NSString *scriptName = nil;
64 id script = nil;
65
66 if (!JSID_IS_STRING(propID)) return YES;
67 scriptName = OOStringFromJSString(context, JSID_TO_STRING(propID));
68
69 if (scriptName != nil)
70 {
71 script = [[player worldScriptsByName] objectForKey:scriptName];
72 if (script != nil)
73 {
74 /* If script is an OOJSScript, this should return a JS Script
75 object. For other OOScript subclasses, it will return
76 JSVAL_NULL. If no script exists, the value will be
77 JSVAL_VOID.
78 */
79 *value = [script oo_jsValueInContext:context];
80 }
81 else
82 {
83 *value = JSVAL_VOID;
84 }
85
86 }
87
88 return YES;
89
91}
92
93
94static JSBool WorldScriptsEnumerate(JSContext *context, JSObject *object)
95{
96 OOJS_NATIVE_ENTER(context)
97
98 /* In order to support enumeration of world scripts (e.g.,
99 for (name in worldScripts) { ... }), define each property on demand.
100 Since world scripts cannot be deleted, we don't need to worry about
101 that case (as in OOJSMissionVariables).
102
103 Since WorldScriptsGetProperty() will be called for each access anyway,
104 we define the value as null here.
105 */
106
107 NSArray *names = nil;
108 NSEnumerator *nameEnum = nil;
109 NSString *name = nil;
110
111 names = [OOPlayerForScripting() worldScriptNames];
112
113 for (nameEnum = [names objectEnumerator]; (name = [nameEnum nextObject]); )
114 {
115 if (!JS_DefineProperty(context, object, [name UTF8String], JSVAL_NULL, WorldScriptsGetProperty, NULL, OOJS_PROP_READONLY_CB)) return NO;
116 }
117
118 return YES;
119
121}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:191
static JSBool WorldScriptsEnumerate(JSContext *cx, JSObject *obj)
void InitOOJSWorldScripts(JSContext *context, JSObject *global)
static JSClass sWorldScriptsClass
static JSBool WorldScriptsGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
#define OOJS_PROP_READONLY
#define OOJS_PROP_READONLY_CB
NSString * OOStringFromJSString(JSContext *context, JSString *string)
return nil
NSDictionary * worldScriptsByName()