31#define kMinInterval 0.25
52@interface OOJSTimer (Private)
56 context:(JSContext*)context
57 function:(JSValueConst)function
58 this:(JSValueConst)jsThis;
71 context:(JSContext*)context
72 function:(JSValueConst)function
73 this:(JSValueConst)jsThis
77 NSAssert(
OOJSValueIsFunction(context,
function),
@"Attempt to init OOJSTimer with a function that isn't.");
79 _jsThis = JS_DupValue(context, jsThis);
80 _function = JS_DupValue(context,
function);
87 JSValue nameValue = JS_GetPropertyStr(context,
function,
"name");
88 if (JS_IsException(nameValue)) {
89 JS_FreeValue(context, JS_GetException(context));
92 if ([name length] != 0)
95 JS_FreeValue(context, nameValue);
99 if (JS_SetOpaque(
_jsSelf, [
self retain]) < 0) {
101 JS_FreeValue(context,
_jsSelf);
115 selector:@selector(deleteJSPointers)
116 name:kOOJavaScriptEngineWillResetNotification
124- (void)deleteJSPointers
129 [[
self retain] autorelease];
130 [
self releaseJSValues];
138 if (JS_IsObject(_function)) {
140 JSValue
function = _function;
141 JSValue jsThis = _jsThis;
142 _function = JS_UNDEFINED;
143 _jsThis = JS_UNDEFINED;
146 name:kOOJavaScriptEngineWillResetNotification
150 JS_FreeValue(context,
function);
151 JS_FreeValue(context, jsThis);
158 [_owningScript release];
159 [_functionName release];
165 [
self releaseJSValues];
170- (NSString*)descriptionComponents
172 NSString* funcName =
nil;
174 if (!JS_IsObject(_function)) {
178 funcName = _functionName;
179 if (funcName ==
nil) {
180 funcName =
@"anonymous";
183 return [NSString stringWithFormat:@"%@, function: %@", [
super descriptionComponents], funcName];
186- (NSString*)oo_jsClassName
193 JSValue rval = JS_UNDEFINED;
194 NSString* description =
nil;
202 description = [object oo_jsDescription];
203 if (description ==
nil)
204 description = [object description];
207 if (description ==
nil || !JS_IsObject(_function)) {
208 [
self unscheduleTimer];
214 [[
self retain] autorelease];
215 JSValue
function = JS_DupValue(context, _function);
216 JSValue jsThis = JS_DupValue(context, _jsThis);
228 JS_FreeValue(context, rval);
229 JS_FreeValue(context,
function);
230 JS_FreeValue(context, jsThis);
235- (JSValue)oo_jsValueInContext:(JSContext*)context
241 if (JS_IsObject(_jsSelf))
242 return JS_DupValue(context, _jsSelf);
248 _jsSelf = JS_UNDEFINED;
258 return _firing && [
self interval] <= 0.0;
275static void TimerFinalize(JSRuntime* runtime, JSValueConst
this);
283 .class_name =
"Timer",
324 if (
EXPECT_NOT(!JSTimerGetTimer(context,
this, &timer)))
329 return JS_NewNumber(context, [timer nextTime]);
332 return JS_NewNumber(context, [timer interval]);
352 if (
EXPECT_NOT(!JSTimerGetTimer(context,
this, &timer)))
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);
363 JS_FreeValue(context, JS_GetException(context));
367 if (JS_ToFloat64(context, &fValue, value) == 0) {
371 JS_FreeValue(context, JS_GetException(context));
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);
398 JS_SetOpaque(
this, NULL);
409 JSValueConst
function = JS_UNDEFINED;
411 double interval = -1.0;
413 JSValue callbackThis = JS_NULL;
416 OOJSReportError(context,
@"Timer() cannot be called as a function, it must be used as a constructor (as in new Timer(...)).");
426 callbackThis = JS_ToObject(context,
OOJS_ARGV[0]);
427 if (JS_IsException(callbackThis)) {
428 JS_FreeValue(context, JS_GetException(context));
435 if (!JS_IsFunction(context,
function)) {
436 JS_FreeValue(context, callbackThis);
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);
450 if (3 < argc && JS_ToFloat64(context, &interval,
OOJS_ARGV[3]) < 0) {
451 JS_FreeValue(context, JS_GetException(context));
459 timer = [[
OOJSTimer alloc] initWithDelay:delay
464 JS_FreeValue(context, callbackThis);
#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 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)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
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
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 OOLogWARN(class, format,...)
static JSValue TimerStart(OOJS_NATIVE_ARGS)
static JSValue TimerGetProperty(OOJS_GETTER_ARGS)
void InitOOJSTimer(JSContext *context, JSValueConst global)
static JSValue TimerSetProperty(OOJS_SETTER_ARGS)
static JSClassID sTimerClassID
static JSValue TimerStop(OOJS_NATIVE_ARGS)
static void TimerFinalize(JSRuntime *runtime, JSValueConst this)
static JSValue TimerConstruct(OOJS_NATIVE_ARGS)
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
#define OOJS_IS_CONSTRUCTING
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
#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_COUNT(array)
void pushScript:(OOJSScript *script)
OOJSScript * currentlyRunningScript()
void popScript:(OOJSScript *script)
OOJSScript * _owningScript
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)