Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOJSTimer.m File Reference
import "OOJSTimer.h"
import "OOJavaScriptEngine.h"
import "Universe.h"
+ Include dependency graph for OOJSTimer.m:

Go to the source code of this file.

Classes

category  OOJSTimer(Private)
 

Macros

#define kMinInterval   0.25
 

Enumerations

enum  { kTimer_nextTime , kTimer_interval , kTimer_isRunning }
 

Functions

static JSBool TimerGetProperty (JSContext *context, JSObject *this, jsid propID, jsval *value)
 
static JSBool TimerSetProperty (JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
 
static void TimerFinalize (JSContext *context, JSObject *this)
 
static JSBool TimerConstruct (JSContext *context, uintN argc, jsval *vp)
 
static JSBool TimerStart (JSContext *context, uintN argc, jsval *vp)
 
static JSBool TimerStop (JSContext *context, uintN argc, jsval *vp)
 
 DEFINE_JS_OBJECT_GETTER (JSTimerGetTimer, &sTimerClass, sTimerPrototype, OOJSTimer)
 
void InitOOJSTimer (JSContext *context, JSObject *global)
 

Variables

static JSObject * sTimerPrototype
 
static JSClass sTimerClass
 
static JSPropertySpec sTimerProperties []
 
static JSFunctionSpec sTimerMethods []
 

Macro Definition Documentation

◆ kMinInterval

#define kMinInterval   0.25

Definition at line 32 of file OOJSTimer.m.

Referenced by TimerConstruct().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kTimer_nextTime 
kTimer_interval 
kTimer_isRunning 

Definition at line 222 of file OOJSTimer.m.

223{
224 // Property IDs
225 kTimer_nextTime, // next fire time, double, read/write
226 kTimer_interval, // interval, double, read/write
227 kTimer_isRunning // is scheduled, boolean, read-only
228};
@ kTimer_isRunning
Definition OOJSTimer.m:227
@ kTimer_nextTime
Definition OOJSTimer.m:225
@ kTimer_interval
Definition OOJSTimer.m:226

Function Documentation

◆ DEFINE_JS_OBJECT_GETTER()

DEFINE_JS_OBJECT_GETTER ( JSTimerGetTimer ,
& sTimerClass,
sTimerPrototype ,
OOJSTimer  )

◆ InitOOJSTimer()

void InitOOJSTimer ( JSContext * context,
JSObject * global )

Definition at line 254 of file OOJSTimer.m.

255{
256 sTimerPrototype = JS_InitClass(context, global, NULL, &sTimerClass, TimerConstruct, 0, sTimerProperties, sTimerMethods, NULL, NULL);
258}
static JSObject * sTimerPrototype
Definition OOJSTimer.m:35
static JSPropertySpec sTimerProperties[]
Definition OOJSTimer.m:231
static JSClass sTimerClass
Definition OOJSTimer.m:36
static JSBool TimerConstruct(JSContext *context, uintN argc, jsval *vp)
Definition OOJSTimer.m:358
static JSFunctionSpec sTimerMethods[]
Definition OOJSTimer.m:241
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)

References OOJSBasicPrivateObjectConverter(), OOJSRegisterObjectConverter(), sTimerClass, sTimerMethods, sTimerProperties, sTimerPrototype, and TimerConstruct().

+ Here is the call graph for this function:

◆ TimerConstruct()

static JSBool TimerConstruct ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 358 of file OOJSTimer.m.

359{
360 OOJS_NATIVE_ENTER(context)
361
362 jsval function = JSVAL_VOID;
363 double delay;
364 double interval = -1.0;
365 OOJSTimer *timer = nil;
366 JSObject *callbackThis = NULL;
367
368 if (EXPECT_NOT(!JS_IsConstructing(context, vp)))
369 {
370 OOJSReportError(context, @"Timer() cannot be called as a function, it must be used as a constructor (as in new Timer(...)).");
371 return NO;
372 }
373
374 if (argc < 3)
375 {
376 OOJSReportBadArguments(context, nil, @"Timer", argc, OOJS_ARGV, @"Invalid arguments in constructor", @"(object, function, number [, number])");
377 return NO;
378 }
379
380 if (!JSVAL_IS_NULL(OOJS_ARGV[0]) && !JSVAL_IS_VOID(OOJS_ARGV[0]))
381 {
382 if (!JS_ValueToObject(context, OOJS_ARGV[0], &callbackThis))
383 {
384 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV, @"Invalid argument in constructor", @"object");
385 return NO;
386 }
387 }
388
389 function = OOJS_ARGV[1];
390 if (JS_ValueToFunction(context, function) == NULL)
391 {
392 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV + 1, @"Invalid argument in constructor", @"function");
393 return NO;
394 }
395
396 if (!JS_ValueToNumber(context, OOJS_ARGV[2], &delay) || isnan(delay))
397 {
398 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV + 2, @"Invalid argument in constructor", @"number");
399 return NO;
400 }
401
402 // Fourth argument is optional.
403 if (3 < argc && !JS_ValueToNumber(context, OOJS_ARGV[3], &interval)) interval = -1;
404
405 // Ensure interval is not too small.
406 if (0.0 < interval && interval < kMinInterval) interval = kMinInterval;
407
408 timer = [[OOJSTimer alloc] initWithDelay:delay
409 interval:interval
410 context:context
411 function:function
412 this:callbackThis];
413 if (EXPECT_NOT(!timer)) return NO;
414
415 if (delay >= 0) // Leave in stopped state if delay is negative
416 {
417 [timer scheduleTimer];
418 }
419 [timer autorelease];
420 OOJS_RETURN_OBJECT(timer);
421
423}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define kMinInterval
Definition OOJSTimer.m:32
#define OOJS_RETURN_OBJECT(o)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
return nil

References EXPECT_NOT, kMinInterval, nil, OOJS_ARGV, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_OBJECT, OOJSReportBadArguments(), OOJSReportError(), and OOScriptTimer::scheduleTimer.

Referenced by InitOOJSTimer().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ TimerFinalize()

static void TimerFinalize ( JSContext * context,
JSObject * this )
static

Definition at line 336 of file OOJSTimer.m.

337{
339
340 // Can't use JSTimerGetTimer() here - potential chicken-and-egg problem manifesting as a crash.
341 OOJSTimer *timer = (OOJSTimer *)JS_GetPrivate(context, this);
342
343 if (timer != nil)
344 {
345 if ([timer isScheduled])
346 {
347 OOLogWARN(@"script.javaScript.unrootedTimer", @"Timer %@ is being garbage-collected while still running. You must keep a reference to all running timers, or they will stop unpredictably!", timer);
348 }
349 [timer release];
350 JS_SetPrivate(context, this, NULL);
351 }
352
354}
#define OOJS_PROFILE_EXIT_VOID
#define OOJS_PROFILE_ENTER
#define OOLogWARN(class, format,...)
Definition OOLogging.h:113

References nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT_VOID, and OOLogWARN.

◆ TimerGetProperty()

static JSBool TimerGetProperty ( JSContext * context,
JSObject * this,
jsid propID,
jsval * value )
static

Definition at line 261 of file OOJSTimer.m.

262{
263 if (!JSID_IS_INT(propID)) return YES;
264
265 OOJS_NATIVE_ENTER(context)
266
267 OOJSTimer *timer = nil;
268
269 if (EXPECT_NOT(!JSTimerGetTimer(context, this, &timer))) return NO;
270
271 switch (JSID_TO_INT(propID))
272 {
273 case kTimer_nextTime:
274 return JS_NewNumberValue(context, [timer nextTime], value);
275
276 case kTimer_interval:
277 return JS_NewNumberValue(context, [timer interval], value);
278
279 case kTimer_isRunning:
280 *value = OOJSValueFromBOOL([timer isScheduled]);
281 return YES;
282
283 default:
284 OOJSReportBadPropertySelector(context, this, propID, sTimerProperties);
285 return NO;
286 }
287
289}
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC

References EXPECT_NOT, kTimer_interval, kTimer_isRunning, kTimer_nextTime, nil, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportBadPropertySelector(), OOJSValueFromBOOL(), and sTimerProperties.

+ Here is the call graph for this function:

◆ TimerSetProperty()

static JSBool TimerSetProperty ( JSContext * context,
JSObject * this,
jsid propID,
JSBool strict,
jsval * value )
static

Definition at line 292 of file OOJSTimer.m.

293{
294 if (!JSID_IS_INT(propID)) return YES;
295
296 OOJS_NATIVE_ENTER(context)
297
298 OOJSTimer *timer = nil;
299 double fValue;
300
301 if (EXPECT_NOT(!JSTimerGetTimer(context, this, &timer))) return NO;
302
303 switch (JSID_TO_INT(propID))
304 {
305 case kTimer_nextTime:
306 if (JS_ValueToNumber(context, *value, &fValue))
307 {
308 if (![timer setNextTime:fValue])
309 {
310 OOJSReportWarning(context, @"Ignoring attempt to change next fire time for running timer %@.", timer);
311 }
312 return YES;
313 }
314 break;
315
316 case kTimer_interval:
317 if (JS_ValueToNumber(context, *value, &fValue))
318 {
319 [timer setInterval:fValue];
320 return YES;
321 }
322 break;
323
324 default:
325 OOJSReportBadPropertySelector(context, this, propID, sTimerProperties);
326 return NO;
327 }
328
329 OOJSReportBadPropertyValue(context, this, propID, sTimerProperties, *value);
330 return NO;
331
333}
void OOJSReportWarning(JSContext *context, NSString *format,...)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)

References EXPECT_NOT, kTimer_interval, kTimer_nextTime, nil, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportBadPropertySelector(), OOJSReportBadPropertyValue(), OOJSReportWarning(), OOScriptTimer::setInterval:, and sTimerProperties.

+ Here is the call graph for this function:

◆ TimerStart()

static JSBool TimerStart ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 429 of file OOJSTimer.m.

430{
431 OOJS_NATIVE_ENTER(context)
432
433 OOJSTimer *thisTimer = nil;
434
435 if (EXPECT_NOT(!JSTimerGetTimer(context, OOJS_THIS, &thisTimer))) return NO;
436
437 OOJS_RETURN_BOOL([thisTimer scheduleTimer]);
438
440}
#define OOJS_THIS
#define OOJS_RETURN_BOOL(v)

References EXPECT_NOT, nil, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, and OOJS_THIS.

◆ TimerStop()

static JSBool TimerStop ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 444 of file OOJSTimer.m.

445{
446 OOJS_NATIVE_ENTER(context)
447
448 OOJSTimer *thisTimer = nil;
449
450 if (EXPECT_NOT(!JSTimerGetTimer(context, OOJS_THIS, &thisTimer))) return NO;
451
452 [thisTimer unscheduleTimer];
454
456}
#define OOJS_RETURN_VOID

Variable Documentation

◆ sTimerClass

static JSClass sTimerClass
static
Initial value:
=
{
"Timer",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
JSCLASS_NO_OPTIONAL_MEMBERS
}
static JSBool TimerSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSTimer.m:292
static JSBool TimerGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSTimer.m:261
static void TimerFinalize(JSContext *context, JSObject *this)
Definition OOJSTimer.m:336

Definition at line 36 of file OOJSTimer.m.

Referenced by InitOOJSTimer().

◆ sTimerMethods

JSFunctionSpec sTimerMethods[]
static
Initial value:
=
{
{ "toString", OOJSObjectWrapperToString, 0 },
{ "start", TimerStart, 0 },
{ "stop", TimerStop, 0 },
{ 0 }
}
static JSBool TimerStart(JSContext *context, uintN argc, jsval *vp)
Definition OOJSTimer.m:429
static JSBool TimerStop(JSContext *context, uintN argc, jsval *vp)
Definition OOJSTimer.m:444
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)

Definition at line 241 of file OOJSTimer.m.

242{
243 // JS name Function min args
244 { "toString", OOJSObjectWrapperToString, 0 },
245 { "start", TimerStart, 0 },
246 { "stop", TimerStop, 0 },
247 { 0 }
248};

Referenced by InitOOJSTimer().

◆ sTimerProperties

JSPropertySpec sTimerProperties[]
static
Initial value:
=
{
{ 0 }
}
#define OOJS_PROP_READWRITE_CB
#define OOJS_PROP_READONLY_CB

Definition at line 231 of file OOJSTimer.m.

232{
233 // JS name ID flags
235 { "isRunning", kTimer_isRunning, OOJS_PROP_READONLY_CB },
237 { 0 }
238};

Referenced by InitOOJSTimer(), TimerGetProperty(), and TimerSetProperty().

◆ sTimerPrototype

JSObject* sTimerPrototype
static

Definition at line 35 of file OOJSTimer.m.

Referenced by InitOOJSTimer().