Oolite
Loading...
Searching...
No Matches
OOJSSound.m
Go to the documentation of this file.
1/*
2
3OOJSSound.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*/
24
25#import "OOJSSound.h"
27#import "OOMusicController.h"
28#import "ResourceManager.h"
29#import "Universe.h"
30
31static JSClassID sSoundClassID;
32
33static OOSound* GetNamedSound(NSString* name);
34
36
37// Static methods
38static JSValue SoundStaticLoad(OOJS_NATIVE_ARGS);
42
43static JSClassDef sSoundClass = {
44 .class_name = "Sound",
45 .finalizer = OOJSObjectWrapperFinalize,
46};
47
48enum {
49 // Property IDs
51};
52
53static const JSCFunctionListEntry sSoundProperties[] = {
54 // JS name, getter, ID
56};
57
58static const JSCFunctionListEntry sSoundMethods[] = {
59 // JS name, min args, function
60 JS_CFUNC_DEF("toString", 0, OOJSObjectWrapperToString),
61};
62
63static const JSCFunctionListEntry sSoundStaticMethods[] = {
64 // JS name, min args, function
65 JS_CFUNC_DEF("load", 1, SoundStaticLoad),
66 JS_CFUNC_DEF("musicSoundSource", 0, SoundStaticMusicSoundSource),
67 JS_CFUNC_DEF("playMusic", 1, SoundStaticPlayMusic),
68 JS_CFUNC_DEF("stopMusic", 0, SoundStaticStopMusic),
69};
70
72
73// *** Public ***
74
83
84OOSound* SoundFromJSValue(JSContext* context, JSValueConst value)
85{
87
88 OOSound* result = nil;
89
90 /* The SpiderMonkey version returned before resuming the time limiter,
91 leaving it paused; the pause is balanced here.
92 */
94 if ([PLAYER status] != STATUS_START_GAME && JS_IsString(value)) {
95 result = GetNamedSound(OOStringFromJSValue(context, value));
96 } else {
97 result = OOJSNativeObjectOfClassFromJSValue(context, value, [OOSound class]);
98 }
100
101 return result;
102
104}
105
106// *** Implementation stuff ***
107
109{
110 OOJS_NATIVE_ENTER(context)
111
112 OOSound* sound = nil;
113
114 if (EXPECT_NOT(!JSSoundGetSound(context, this, &sound)))
115 return OOJSNativeFailure(context);
116
117 switch (magic) {
118 case kSound_name:
119 return OOJSValueFromNativeObject(context, [sound name]);
120
121 default:
123 return OOJSNativeFailure(context);
124 }
125
127}
128
129static OOSound* GetNamedSound(NSString* name)
130{
131 OOSound* sound = nil;
132
133 if ([name hasPrefix:@"["] && [name hasSuffix:@"]"]) {
134 sound = [OOSound soundWithCustomSoundKey:name];
135 } else {
136 sound = [ResourceManager ooSoundNamed:name inFolder:@"Sounds"];
137 }
138
139 return sound;
140}
141
142// *** Static methods ***
143
144// load(name : String) : Sound
146{
147 OOJS_NATIVE_ENTER(context)
148
149 NSString* name = nil;
150 OOSound* sound = nil;
151
152 if (argc > 0)
153 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
154 if (name == nil) {
155 OOJSReportBadArguments(context, @"Sound", @"load", MIN(argc, 1), OOJS_ARGV, nil, @"string");
156 return OOJSNativeFailure(context);
157 }
158
160 sound = GetNamedSound(name);
162
163 OOJS_RETURN_OBJECT(sound);
164
166}
167
178
179// playMusic(name : String [, loop : Boolean] [, gain : float])
181{
182 OOJS_NATIVE_ENTER(context)
183
184 NSString* name = nil;
185 BOOL loop = NO;
186 double gain = OO_DEFAULT_SOUNDSOURCE_GAIN;
187
188 if (argc > 0)
189 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
190 if (name == nil) {
191 OOJSReportBadArguments(context, @"Sound", @"playMusic", MIN(argc, 1), OOJS_ARGV, nil, @"string");
192 return OOJSNativeFailure(context);
193 }
194 if (argc > 1) {
195 int b = JS_ToBool(context, OOJS_ARGV[1]);
196 if (b < 0) {
197 OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV + 1, nil, @"boolean");
198 return OOJSNativeFailure(context);
199 }
200 loop = b;
201 }
202
203 if (argc > 2) {
204 if (!OOJSArgumentListGetNumber(context, @"Sound", @"playMusic", 2, OOJS_ARGV + 2, &gain, NULL)) {
205 OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV + 2, nil, @"float");
206 return OOJSNativeFailure(context);
207 }
208 }
209
213
215
217}
218
219// Sound.stopMusic([name : String])
221{
222 OOJS_NATIVE_ENTER(context)
223
224 NSString* name = nil;
225
226 if (argc > 0) {
227 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
228 if (EXPECT_NOT(name == nil)) {
229 OOJSReportBadArguments(context, @"Sound", @"stopMusic", argc, OOJS_ARGV, nil, @"string or no argument");
230 return OOJSNativeFailure(context);
231 }
232 }
233
236 if (name == nil || [name isEqualToString:[controller playingMusic]]) {
238 }
240
242
244}
245
246@implementation OOSound (OOJavaScriptExtentions)
247
248- (JSValue)oo_jsValueInContext:(JSContext*)context
249{
250 JSValue result = JS_NewObjectClass(context, (int)sSoundClassID);
251 if (JS_IsException(result)) {
252 JS_FreeValue(context, JS_GetException(context));
253 return JS_NULL;
254 }
255 JS_SetOpaque(result, [self retain]);
256
257 return result;
258}
259
260- (NSString*)oo_jsDescription
261{
262 return [NSString stringWithFormat:@"[Sound \"%@\"]", [self name]];
263}
264
265- (NSString*)oo_jsClassName
266{
267 return @"Sound";
268}
269
270@end
#define EXPECT_NOT(x)
#define OOJS_PROFILE_EXIT
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
void InitOOJSSound(JSContext *context, JSObject *global)
Definition OOJSSound.m:108
static JSBool SoundGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSSound.m:131
@ kSound_name
Definition OOJSSound.m:60
static JSPropertySpec sSoundProperties[]
Definition OOJSSound.m:63
static JSBool SoundStaticStopMusic(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:245
static JSBool SoundStaticPlayMusic(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:207
static JSBool SoundStaticLoad(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:172
static JSFunctionSpec sSoundMethods[]
Definition OOJSSound.m:69
static JSFunctionSpec sSoundStaticMethods[]
Definition OOJSSound.m:79
OOSound * SoundFromJSValue(JSContext *context, jsval value)
Definition OOJSSound.m:114
static JSClass sSoundClass
Definition OOJSSound.m:43
static JSBool SoundStaticMusicSoundSource(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:195
static OOSound * GetNamedSound(NSString *name)
Definition OOJSSound.m:156
void OOJSPauseTimeLimiter(void)
BOOL OOJSArgumentListGetNumber(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, double *outNumber, uintN *outConsumed)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
id OOJSNativeObjectOfClassFromJSValue(JSContext *context, jsval value, Class requiredClass)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_ARGV
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
void OOJSResumeTimeLimiter(void)
#define OOJS_RETURN_VOID
#define MIN(A, B)
Definition OOMaths.h:91
return nil
#define PLAYER
static JSClassID sSoundClassID
Definition OOJSSound.m:31
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_PROPERTY_RO(name, getter, magic)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_GETTER_ARGS
#define OOJS_NATIVE_ARGS
#define OOJS_COUNT(array)
OOMusicController * sharedController()
void playMusicNamed:loop:gain:(NSString *name,[loop] BOOL loop,[gain] float gain)
OOSoundSource * soundSource()
id soundWithCustomSoundKey:(NSString *key)
Definition Universe.m:9950
OOSound * ooSoundNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)