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 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 31 of file OOJSTimer.m.

Referenced by TimerConstruct(), and TimerConstruct().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kTimer_nextTime 
kTimer_interval 
kTimer_isRunning 

Definition at line 202 of file OOJSTimer.m.

202 {
203 // Property IDs
204 kTimer_nextTime, // next fire time, double, read/write
205 kTimer_interval, // interval, double, read/write
206 kTimer_isRunning // is scheduled, boolean, read-only
207};
@ 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 ,
& sTimerClass,
sTimerPrototype ,
OOJSTimer  )

References sTimerClass, and sTimerPrototype.

◆ InitOOJSTimer()

void InitOOJSTimer ( JSContext * context,
JSObject * global )

Definition at line 227 of file OOJSTimer.m.

228{
229 sTimerPrototype = JS_InitClass(context, global, NULL, &sTimerClass, TimerConstruct, 0, sTimerProperties, sTimerMethods, NULL, NULL);
231}
static JSObject * sTimerPrototype
Definition OOJSTimer.m:33
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)

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

Referenced by OOJavaScriptEngine(Private)::createMainThreadContext.

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

◆ TimerConstruct()

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

Definition at line 324 of file OOJSTimer.m.

325{
326 OOJS_NATIVE_ENTER(context)
327
328 jsval function = JSVAL_VOID;
329 double delay;
330 double interval = -1.0;
331 OOJSTimer* timer = nil;
332 JSObject* callbackThis = NULL;
333
334 if (EXPECT_NOT(!JS_IsConstructing(context, vp))) {
335 OOJSReportError(context, @"Timer() cannot be called as a function, it must be used as a constructor (as in new Timer(...)).");
336 return NO;
337 }
338
339 if (argc < 3) {
340 OOJSReportBadArguments(context, nil, @"Timer", argc, OOJS_ARGV, @"Invalid arguments in constructor", @"(object, function, number [, number])");
341 return NO;
342 }
343
344 if (!JSVAL_IS_NULL(OOJS_ARGV[0]) && !JSVAL_IS_VOID(OOJS_ARGV[0])) {
345 if (!JS_ValueToObject(context, OOJS_ARGV[0], &callbackThis)) {
346 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV, @"Invalid argument in constructor", @"object");
347 return NO;
348 }
349 }
350
351 function = OOJS_ARGV[1];
352 if (JS_ValueToFunction(context, function) == NULL) {
353 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV + 1, @"Invalid argument in constructor", @"function");
354 return NO;
355 }
356
357 if (!JS_ValueToNumber(context, OOJS_ARGV[2], &delay) || isnan(delay)) {
358 OOJSReportBadArguments(context, nil, @"Timer", 1, OOJS_ARGV + 2, @"Invalid argument in constructor", @"number");
359 return NO;
360 }
361
362 // Fourth argument is optional.
363 if (3 < argc && !JS_ValueToNumber(context, OOJS_ARGV[3], &interval))
364 interval = -1;
365
366 // Ensure interval is not too small.
367 if (0.0 < interval && interval < kMinInterval)
368 interval = kMinInterval;
369
370 timer = [[OOJSTimer alloc] initWithDelay:delay
371 interval:interval
372 context:context
373 function:function
374 this:callbackThis];
375 if (EXPECT_NOT(!timer))
376 return NO;
377
378 if (delay >= 0) // Leave in stopped state if delay is negative
379 {
380 [timer scheduleTimer];
381 }
382 [timer autorelease];
383 OOJS_RETURN_OBJECT(timer);
384
386}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define kMinInterval
Definition OOJSTimer.m:31
#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(), and InitOOJSTimer().

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

◆ TimerFinalize()

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

Definition at line 305 of file OOJSTimer.m.

306{
308
309 // Can't use JSTimerGetTimer() here - potential chicken-and-egg problem manifesting as a crash.
310 OOJSTimer* timer = (OOJSTimer*)JS_GetPrivate(context, this);
311
312 if (timer != nil) {
313 if ([timer isScheduled]) {
314 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);
315 }
316 [timer release];
317 JS_SetPrivate(context, this, NULL);
318 }
319
321}
#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, and OOLogWARN.

◆ TimerGetProperty()

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

Definition at line 233 of file OOJSTimer.m.

234{
235 if (!JSID_IS_INT(propID))
236 return YES;
237
238 OOJS_NATIVE_ENTER(context)
239
240 OOJSTimer* timer = nil;
241
242 if (EXPECT_NOT(!JSTimerGetTimer(context, this, &timer)))
243 return NO;
244
245 switch (JSID_TO_INT(propID)) {
246 case kTimer_nextTime:
247 return JS_NewNumberValue(context, [timer nextTime], value);
248
249 case kTimer_interval:
250 return JS_NewNumberValue(context, [timer interval], value);
251
252 case kTimer_isRunning:
253 *value = OOJSValueFromBOOL([timer isScheduled]);
254 return YES;
255
256 default:
257 OOJSReportBadPropertySelector(context, this, propID, sTimerProperties);
258 return NO;
259 }
260
262}
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()

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

Definition at line 264 of file OOJSTimer.m.

265{
266 if (!JSID_IS_INT(propID))
267 return YES;
268
269 OOJS_NATIVE_ENTER(context)
270
271 OOJSTimer* timer = nil;
272 double fValue;
273
274 if (EXPECT_NOT(!JSTimerGetTimer(context, this, &timer)))
275 return NO;
276
277 switch (JSID_TO_INT(propID)) {
278 case kTimer_nextTime:
279 if (JS_ValueToNumber(context, *value, &fValue)) {
280 if (![timer setNextTime:fValue]) {
281 OOJSReportWarning(context, @"Ignoring attempt to change next fire time for running timer %@.", timer);
282 }
283 return YES;
284 }
285 break;
286
287 case kTimer_interval:
288 if (JS_ValueToNumber(context, *value, &fValue)) {
289 [timer setInterval:fValue];
290 return YES;
291 }
292 break;
293
294 default:
295 OOJSReportBadPropertySelector(context, this, propID, sTimerProperties);
296 return NO;
297 }
298
299 OOJSReportBadPropertyValue(context, this, propID, sTimerProperties, *value);
300 return NO;
301
303}
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()

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

Definition at line 391 of file OOJSTimer.m.

392{
393 OOJS_NATIVE_ENTER(context)
394
395 OOJSTimer* thisTimer = nil;
396
397 if (EXPECT_NOT(!JSTimerGetTimer(context, OOJS_THIS, &thisTimer)))
398 return NO;
399
400 OOJS_RETURN_BOOL([thisTimer scheduleTimer]);
401
403}
#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()

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

Definition at line 406 of file OOJSTimer.m.

407{
408 OOJS_NATIVE_ENTER(context)
409
410 OOJSTimer* thisTimer = nil;
411
412 if (EXPECT_NOT(!JSTimerGetTimer(context, OOJS_THIS, &thisTimer)))
413 return NO;
414
415 [thisTimer unscheduleTimer];
417
419}
#define OOJS_RETURN_VOID

References EXPECT_NOT, nil, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_VOID, OOJS_THIS, and OOScriptTimer::unscheduleTimer.

Here is the call graph for this function:

Variable Documentation

◆ sTimerClass

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:264
static JSBool TimerGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSTimer.m:233
static void TimerFinalize(JSContext *context, JSObject *this)
Definition OOJSTimer.m:305

Definition at line 34 of file OOJSTimer.m.

Referenced by DEFINE_JS_OBJECT_GETTER(), InitOOJSTimer(), and 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: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 217 of file OOJSTimer.m.

217 {
218 // JS name Function min args
219 { "toString", OOJSObjectWrapperToString, 0 },
220 { "start", TimerStart, 0 },
221 { "stop", TimerStop, 0 },
222 { 0 }
223};

Referenced by InitOOJSTimer(), and InitOOJSTimer().

◆ sTimerProperties

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

Definition at line 209 of file OOJSTimer.m.

209 {
210 // JS name ID flags
212 { "isRunning", kTimer_isRunning, OOJS_PROP_READONLY_CB },
214 { 0 }
215};

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

◆ sTimerPrototype

JSObject* sTimerPrototype
static