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