32#define kMinInterval 0.25
39@interface OOJSTimer (Private)
43 context:(JSContext *)context
44 function:(jsval)function
45 this:(JSObject *)jsThis;
54 context:(JSContext *)context
55 function:(jsval)function
56 this:(JSObject *)jsThis
61 NSAssert(
OOJSValueIsFunction(context,
function),
@"Attempt to init OOJSTimer with a function that isn't.");
72 if (!JS_SetPrivate(context, _jsSelf, [
self retain])) _jsSelf = NULL;
82 [[NSNotificationCenter defaultCenter] addObserver:self
83 selector:@selector(deleteJSPointers)
84 name:kOOJavaScriptEngineWillResetNotification
92- (void) deleteJSPointers
94 [
self unscheduleTimer];
99 _function = JSVAL_VOID;
102 JS_RemoveObjectRoot(context, &_jsThis);
103 JS_RemoveValueRoot(context, &_function);
106 [[NSNotificationCenter defaultCenter] removeObserver:self
107 name:kOOJavaScriptEngineWillResetNotification
115 [_owningScript release];
117 [
self deleteJSPointers];
123- (NSString *) descriptionComponents
125 NSString *funcName =
nil;
126 JSContext *context = NULL;
128 if (JSVAL_IS_VOID(_function) || JSVAL_IS_NULL(_function))
134 funcName =
OOStringFromJSString(context, JS_GetFunctionId(JS_ValueToFunction(context, _function)));
139 funcName =
@"anonymous";
142 return [NSString stringWithFormat:@"%@, function: %@", [
super descriptionComponents], funcName];
146- (NSString *) oo_jsClassName
154 jsval rval = JSVAL_VOID;
155 NSString *description =
nil;
164 description = [object oo_jsDescription];
165 if (description ==
nil) description = [object description];
168 if (description ==
nil)
170 [
self unscheduleTimer];
187- (jsval) oo_jsValueInContext:(JSContext *)context
189 return OBJECT_TO_JSVAL(_jsSelf);
195static JSBool
TimerGetProperty(JSContext *context, JSObject *
this, jsid propID, jsval *value);
196static JSBool
TimerSetProperty(JSContext *context, JSObject *
this, jsid propID, JSBool strict, jsval *value);
197static void TimerFinalize(JSContext *context, JSObject *
this);
198static JSBool
TimerConstruct(JSContext *context, uintN argc, jsval *vp);
201static JSBool
TimerStart(JSContext *context, uintN argc, jsval *vp);
202static JSBool
TimerStop(JSContext *context, uintN argc, jsval *vp);
218 JSCLASS_NO_OPTIONAL_MEMBERS
261static JSBool
TimerGetProperty(JSContext *context, JSObject *
this, jsid propID, jsval *value)
263 if (!JSID_IS_INT(propID))
return YES;
269 if (
EXPECT_NOT(!JSTimerGetTimer(context,
this, &timer)))
return NO;
271 switch (JSID_TO_INT(propID))
274 return JS_NewNumberValue(context, [timer nextTime], value);
277 return JS_NewNumberValue(context, [timer interval], value);
292static JSBool
TimerSetProperty(JSContext *context, JSObject *
this, jsid propID, JSBool strict, jsval *value)
294 if (!JSID_IS_INT(propID))
return YES;
301 if (
EXPECT_NOT(!JSTimerGetTimer(context,
this, &timer)))
return NO;
303 switch (JSID_TO_INT(propID))
306 if (JS_ValueToNumber(context, *value, &fValue))
308 if (![timer setNextTime:fValue])
310 OOJSReportWarning(context,
@"Ignoring attempt to change next fire time for running timer %@.", timer);
317 if (JS_ValueToNumber(context, *value, &fValue))
345 if ([timer isScheduled])
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);
350 JS_SetPrivate(context,
this, NULL);
362 jsval
function = JSVAL_VOID;
364 double interval = -1.0;
366 JSObject *callbackThis = NULL;
368 if (
EXPECT_NOT(!JS_IsConstructing(context, vp)))
370 OOJSReportError(context,
@"Timer() cannot be called as a function, it must be used as a constructor (as in new Timer(...)).");
382 if (!JS_ValueToObject(context,
OOJS_ARGV[0], &callbackThis))
390 if (JS_ValueToFunction(context,
function) == NULL)
396 if (!JS_ValueToNumber(context,
OOJS_ARGV[2], &delay) || isnan(delay))
403 if (3 < argc && !JS_ValueToNumber(context,
OOJS_ARGV[3], &interval)) interval = -1;
408 timer = [[
OOJSTimer alloc] initWithDelay:delay
429static JSBool
TimerStart(JSContext *context, uintN argc, jsval *vp)
444static JSBool
TimerStop(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROFILE_EXIT_VOID
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_PROFILE_ENTER
static JSBool TimerSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSObject * sTimerPrototype
void InitOOJSTimer(JSContext *context, JSObject *global)
static JSBool TimerGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSPropertySpec sTimerProperties[]
static JSClass sTimerClass
static JSBool TimerConstruct(JSContext *context, uintN argc, jsval *vp)
static JSBool TimerStart(JSContext *context, uintN argc, jsval *vp)
static void TimerFinalize(JSContext *context, JSObject *this)
static JSFunctionSpec sTimerMethods[]
static JSBool TimerStop(JSContext *context, uintN argc, jsval *vp)
void OOJSReportWarning(JSContext *context, NSString *format,...)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
OOINLINE JSContext * OOJSAcquireContext(void)
void OOJSReportError(JSContext *context, NSString *format,...)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
#define OOJSAddGCObjectRoot(context, root, name)
OOINLINE void OOJSRelinquishContext(JSContext *context)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
#define OOJSAddGCValueRoot(context, root, name)
NSString * OOStringFromJSString(JSContext *context, JSString *string)
#define OOLogWARN(class, format,...)
void pushScript:(OOJSScript *script)
OOJSScript * currentlyRunningScript()
void popScript:(OOJSScript *script)
BOOL callJSFunction:forObject:argc:argv:result:(jsval function,[forObject] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *outResult)
OOJavaScriptEngine * sharedEngine()
void setInterval:(OOTimeDelta interval)
id initWithNextTime:interval:(OOTimeAbsolute nextTime,[interval] OOTimeDelta interval)