Oolite
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 JSValue TimerGetProperty (OOJS_GETTER_ARGS)
static JSValue TimerSetProperty (OOJS_SETTER_ARGS)
static void TimerFinalize (JSRuntime *runtime, JSValueConst this)
static JSValue TimerConstruct (OOJS_NATIVE_ARGS)
static JSValue TimerStart (OOJS_NATIVE_ARGS)
static JSValue TimerStop (OOJS_NATIVE_ARGS)
 DEFINE_JS_OBJECT_GETTER (JSTimerGetTimer, sTimerClassID, OOJSTimer)
void InitOOJSTimer (JSContext *context, JSValueConst global)

Variables

static JSClassID sTimerClassID
static JSClassDef sTimerClass
static const JSCFunctionListEntry sTimerProperties []
static const JSCFunctionListEntry sTimerMethods []

Macro Definition Documentation

◆ kMinInterval

#define kMinInterval   0.25

Definition at line 31 of file OOJSTimer.m.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kTimer_nextTime 
kTimer_interval 
kTimer_isRunning 

Definition at line 287 of file OOJSTimer.m.

287 {
288 // Property IDs
289 kTimer_nextTime, // next fire time, double, read/write
290 kTimer_interval, // interval, double, read/write
291 kTimer_isRunning // is scheduled, boolean, read-only
292};
@ kTimer_isRunning
Definition OOJSTimer.m:206
@ kTimer_nextTime
Definition OOJSTimer.m:204
@ kTimer_interval
Definition OOJSTimer.m:205

Function Documentation

◆ DEFINE_JS_OBJECT_GETTER()

DEFINE_JS_OBJECT_GETTER ( JSTimerGetTimer ,
sTimerClassID ,
OOJSTimer  )

References sTimerClassID.

◆ InitOOJSTimer()

void InitOOJSTimer ( JSContext * context,
JSValueConst global )

Definition at line 310 of file OOJSTimer.m.

311{
313 OOJSInitClass(context, global, sTimerClassID, JS_UNDEFINED, TimerConstruct, 0,
316}
static JSPropertySpec sTimerProperties[]
Definition OOJSTimer.m:209
static JSClass sTimerClass
Definition OOJSTimer.m:34
static JSBool TimerConstruct(JSContext *context, uintN argc, jsval *vp)
Definition OOJSTimer.m:324
static JSFunctionSpec sTimerMethods[]
Definition OOJSTimer.m:217
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
static JSClassID sTimerClassID
Definition OOJSTimer.m:33
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
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_COUNT(array)

References OOJS_COUNT, OOJSBasicPrivateObjectConverter(), OOJSInitClass(), OOJSRegisterClass(), OOJSRegisterObjectConverter(), sTimerClass, sTimerClassID, sTimerMethods, sTimerProperties, and TimerConstruct().

Here is the call graph for this function:

◆ TimerConstruct()

JSValue TimerConstruct ( OOJS_NATIVE_ARGS )
static

Definition at line 405 of file OOJSTimer.m.

406{
407 OOJS_NATIVE_ENTER(context)
408
409 JSValueConst function = JS_UNDEFINED;
410 double delay;
411 double interval = -1.0;
412 OOJSTimer* timer = nil;
413 JSValue callbackThis = JS_NULL;
414
416 OOJSReportError(context, @"Timer() cannot be called as a function, it must be used as a constructor (as in new Timer(...)).");
417 return JS_EXCEPTION;
418 }
419
420 if (argc < 3) {
421 OOJSReportBadArguments(context, nil, @"Timer", argc, OOJS_ARGV, @"Invalid arguments in constructor", @"(object, function, number [, number])");
422 return JS_EXCEPTION;
423 }
424
425 if (!JS_IsNull(OOJS_ARGV[0]) && !JS_IsUndefined(OOJS_ARGV[0])) {
426 callbackThis = JS_ToObject(context, OOJS_ARGV[0]);
427 if (JS_IsException(callbackThis)) {
428 JS_FreeValue(context, JS_GetException(context));
429 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV, @"Invalid argument in constructor", @"object");
430 return JS_EXCEPTION;
431 }
432 }
433
434 function = OOJS_ARGV[1];
435 if (!JS_IsFunction(context, function)) {
436 JS_FreeValue(context, callbackThis);
437 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV + 1, @"Invalid argument in constructor", @"function");
438 return JS_EXCEPTION;
439 }
440
441 if (JS_ToFloat64(context, &delay, OOJS_ARGV[2]) < 0 || isnan(delay)) {
442 if (JS_HasException(context))
443 JS_FreeValue(context, JS_GetException(context));
444 JS_FreeValue(context, callbackThis);
445 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV + 2, @"Invalid argument in constructor", @"number");
446 return JS_EXCEPTION;
447 }
448
449 // Fourth argument is optional.
450 if (3 < argc && JS_ToFloat64(context, &interval, OOJS_ARGV[3]) < 0) {
451 JS_FreeValue(context, JS_GetException(context));
452 interval = -1;
453 }
454
455 // Ensure interval is not too small.
456 if (0.0 < interval && interval < kMinInterval)
457 interval = kMinInterval;
458
459 timer = [[OOJSTimer alloc] initWithDelay:delay
460 interval:interval
461 context:context
462 function:function
463 this:callbackThis];
464 JS_FreeValue(context, callbackThis);
465 if (EXPECT_NOT(!timer))
466 return OOJSNativeFailure(context);
467
468 if (delay >= 0) // Leave in stopped state if delay is negative
469 {
470 [timer scheduleTimer];
471 }
472 JSValue result = [timer transferJSSelf];
473 [timer autorelease];
474 return result;
475
477}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define kMinInterval
Definition OOJSTimer.m:31
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
#define OOJS_IS_CONSTRUCTING
JSValue OOJSNativeFailure(JSContext *context)

References EXPECT_NOT, kMinInterval, nil, OOJS_ARGV, OOJS_IS_CONSTRUCTING, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSNativeFailure(), OOJSReportBadArguments(), OOJSReportError(), OOScriptTimer::scheduleTimer, and OOJSTimer::transferJSSelf.

Here is the call graph for this function:

◆ TimerFinalize()

void TimerFinalize ( JSRuntime * runtime,
JSValueConst this )
static

Definition at line 385 of file OOJSTimer.m.

386{
388
389 // Can't use JSTimerGetTimer() here - potential chicken-and-egg problem manifesting as a crash.
390 OOJSTimer* timer = (OOJSTimer*)JS_GetOpaque(this, sTimerClassID);
391
392 if (timer != nil) {
393 if ([timer isScheduled] && ![timer willNotFireAgain]) {
394 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);
395 }
396 [timer clearJSSelf];
397 [timer release];
398 JS_SetOpaque(this, NULL);
399 }
400
402}
#define OOJS_PROFILE_EXIT_VOID
#define OOJS_PROFILE_ENTER
#define OOLogWARN(class, format,...)
Definition OOLogging.h:126

References nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT_VOID, OOLogWARN, sTimerClassID, and OOJSTimer(Private)::willNotFireAgain.

Here is the call graph for this function:

◆ TimerGetProperty()

JSValue TimerGetProperty ( OOJS_GETTER_ARGS )
static

Definition at line 318 of file OOJSTimer.m.

319{
320 OOJS_NATIVE_ENTER(context)
321
322 OOJSTimer* timer = nil;
323
324 if (EXPECT_NOT(!JSTimerGetTimer(context, this, &timer)))
325 return OOJSNativeFailure(context);
326
327 switch (magic) {
328 case kTimer_nextTime:
329 return JS_NewNumber(context, [timer nextTime]);
330
331 case kTimer_interval:
332 return JS_NewNumber(context, [timer interval]);
333
334 case kTimer_isRunning:
335 return OOJSValueFromBOOL([timer isScheduled]);
336
337 default:
339 return JS_EXCEPTION;
340 }
341
343}
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_COUNT, OOJS_GETTER_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSNativeFailure(), OOJSReportBadPropertySelector(), OOJSValueFromBOOL(), and sTimerProperties.

Here is the call graph for this function:

◆ TimerSetProperty()

JSValue TimerSetProperty ( OOJS_SETTER_ARGS )
static

Definition at line 345 of file OOJSTimer.m.

346{
347 OOJS_NATIVE_ENTER(context)
348
349 OOJSTimer* timer = nil;
350 double fValue;
351
352 if (EXPECT_NOT(!JSTimerGetTimer(context, this, &timer)))
353 return OOJSNativeFailure(context);
354
355 switch (magic) {
356 case kTimer_nextTime:
357 if (JS_ToFloat64(context, &fValue, value) == 0) {
358 if (![timer setNextTime:fValue]) {
359 OOJSReportWarning(context, @"Ignoring attempt to change next fire time for running timer %@.", timer);
360 }
361 return JS_UNDEFINED;
362 }
363 JS_FreeValue(context, JS_GetException(context));
364 break;
365
366 case kTimer_interval:
367 if (JS_ToFloat64(context, &fValue, value) == 0) {
368 [timer setInterval:fValue];
369 return JS_UNDEFINED;
370 }
371 JS_FreeValue(context, JS_GetException(context));
372 break;
373
374 default:
376 return JS_EXCEPTION;
377 }
378
380 return JS_EXCEPTION;
381
383}
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_COUNT, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_SETTER_ARGS, OOJSNativeFailure(), OOJSReportBadPropertySelector(), OOJSReportBadPropertyValue(), OOJSReportWarning(), OOScriptTimer::setInterval:, and sTimerProperties.

Here is the call graph for this function:

◆ TimerStart()

JSValue TimerStart ( OOJS_NATIVE_ARGS )
static

Definition at line 482 of file OOJSTimer.m.

483{
484 OOJS_NATIVE_ENTER(context)
485
486 OOJSTimer* thisTimer = nil;
487
488 if (EXPECT_NOT(!JSTimerGetTimer(context, OOJS_THIS, &thisTimer)))
489 return OOJSNativeFailure(context);
490
491 OOJS_RETURN_BOOL([thisTimer scheduleTimer]);
492
494}
#define OOJS_THIS
#define OOJS_RETURN_BOOL(v)

References EXPECT_NOT, nil, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJS_THIS, and OOJSNativeFailure().

Here is the call graph for this function:

◆ TimerStop()

JSValue TimerStop ( OOJS_NATIVE_ARGS )
static

Definition at line 497 of file OOJSTimer.m.

498{
499 OOJS_NATIVE_ENTER(context)
500
501 OOJSTimer* thisTimer = nil;
502
503 if (EXPECT_NOT(!JSTimerGetTimer(context, OOJS_THIS, &thisTimer)))
504 return OOJSNativeFailure(context);
505
506 [thisTimer unscheduleTimer];
508
510}
#define OOJS_RETURN_VOID

References EXPECT_NOT, nil, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_VOID, OOJS_THIS, OOJSNativeFailure(), and OOScriptTimer::unscheduleTimer.

Here is the call graph for this function:

Variable Documentation

◆ sTimerClass

JSClassDef sTimerClass
static
Initial value:
= {
.class_name = "Timer",
.finalizer = TimerFinalize,
}
static void TimerFinalize(JSContext *context, JSObject *this)
Definition OOJSTimer.m:305

Definition at line 34 of file OOJSTimer.m.

Referenced by OOJSTimer(Private)::initWithDelay:interval:context:function:this:.

◆ sTimerClassID

JSClassID sTimerClassID
static

◆ sTimerMethods

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

Definition at line 301 of file OOJSTimer.m.

301 {
302 // JS name Function min args
303 JS_CFUNC_DEF("toString", 0, OOJSObjectWrapperToString),
304 JS_CFUNC_DEF("start", 0, TimerStart),
305 JS_CFUNC_DEF("stop", 0, TimerStop),
306};

◆ sTimerProperties

const JSCFunctionListEntry sTimerProperties[]
static
Initial value:
= {
}
static JSBool TimerSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSTimer.m:264
static JSBool TimerGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSTimer.m:233
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
#define OOJS_PROPERTY_RO(name, getter, magic)

Definition at line 294 of file OOJSTimer.m.

294 {
295 // JS name Getter Setter ID
299};