Line data Source code
1 0 : /*
2 :
3 : OOJSWorldScripts.m
4 :
5 :
6 : Oolite
7 : Copyright (C) 2004-2013 Giles C Williams and contributors
8 :
9 : This program is free software; you can redistribute it and/or
10 : modify it under the terms of the GNU General Public License
11 : as published by the Free Software Foundation; either version 2
12 : of the License, or (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 : MA 02110-1301, USA.
23 :
24 : */
25 :
26 : #import "OOJSWorldScripts.h"
27 : #import "OOJavaScriptEngine.h"
28 : #import "PlayerEntity.h"
29 : #import "OOJSPlayer.h"
30 :
31 :
32 : static JSBool WorldScriptsGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
33 : static JSBool WorldScriptsEnumerate(JSContext *cx, JSObject *obj);
34 :
35 :
36 0 : static JSClass sWorldScriptsClass =
37 : {
38 : "WorldScripts",
39 : 0,
40 :
41 : JS_PropertyStub,
42 : JS_PropertyStub,
43 : WorldScriptsGetProperty,
44 : JS_StrictPropertyStub,
45 : WorldScriptsEnumerate,
46 : JS_ResolveStub,
47 : JS_ConvertStub,
48 : JS_FinalizeStub
49 : };
50 :
51 :
52 0 : void InitOOJSWorldScripts(JSContext *context, JSObject *global)
53 : {
54 : JS_DefineObject(context, global, "worldScripts", &sWorldScriptsClass, NULL, OOJS_PROP_READONLY);
55 : }
56 :
57 :
58 0 : static JSBool WorldScriptsGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
59 : {
60 : OOJS_NATIVE_ENTER(context)
61 :
62 : PlayerEntity *player = OOPlayerForScripting();
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 :
90 : OOJS_NATIVE_EXIT
91 : }
92 :
93 :
94 0 : static 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 :
120 : OOJS_NATIVE_EXIT
121 : }
|