Oolite
Loading...
Searching...
No Matches
OOJSTimer Class Reference

#include <OOJSTimer.h>

Inheritance diagram for OOJSTimer:
Collaboration diagram for OOJSTimer:

Instance Methods

(id) - initWithDelay:interval:context:function:this: [implementation]
(void) - deleteJSPointers [implementation]
(void) - dealloc [implementation]
(NSString *) - descriptionComponents [implementation]
(NSString *) - oo_jsClassName [implementation]
(void) - timerFired [implementation]
(jsval) - oo_jsValueInContext: [implementation]
(id) - initWithDelay:interval:context:function:this: [implementation]
(void) - releaseJSValues [implementation]
(void) - clearJSSelf [implementation]
(BOOL) - willNotFireAgain [implementation]
(JSValue) - transferJSSelf [implementation]
Instance Methods inherited from OOScriptTimer
(id) - initWithNextTime:interval:
(id) - initOneShotTimerWithDelay:
(OOTimeAbsolute) - nextTime
(BOOL) - setNextTime:
(OOTimeDelta) - interval
(void) - setInterval:
(BOOL) - scheduleTimer
(void) - unscheduleTimer
(BOOL) - isScheduled
(BOOL) - isValidForScheduling
(NSComparisonResult) - compareByNextFireTime:

Private Attributes

jsval _function
JSObject * _jsThis
OOJSScript * _owningScript
JSObject * _jsSelf
JSValue _function
JSValue _jsThis
NSString * _functionName
JSValue _jsSelf
BOOL _firing

Additional Inherited Members

Class Methods inherited from OOScriptTimer
(void) + updateTimers
(void) + noteGameReset

Detailed Description

Definition at line 32 of file OOJSTimer.h.

Method Documentation

◆ clearJSSelf

- (void) clearJSSelf
implementation

Provided by category OOJSTimer(Private).

Definition at line 246 of file OOJSTimer.m.

247{
248 _jsSelf = JS_UNDEFINED;
249}
JSObject * _jsSelf
Definition OOJSTimer.h:39

References OOJSTimer(Private)::clearJSSelf.

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

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

◆ dealloc

- (void) dealloc
implementation

Reimplemented from OOScriptTimer.

Definition at line 104 of file OOJSTimer.m.

105{
106 [_owningScript release];
107
108 [self deleteJSPointers];
109
110 [super dealloc];
111}

References dealloc, OOScriptTimer::dealloc, and deleteJSPointers.

Referenced by dealloc.

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

◆ deleteJSPointers

- (void) deleteJSPointers
implementation

Definition at line 85 of file OOJSTimer.m.

86{
87 [self unscheduleTimer];
88
89 if (_jsThis != NULL) {
90 _jsThis = NULL;
91 _function = JSVAL_VOID;
92
93 JSContext* context = OOJSAcquireContext();
94 JS_RemoveObjectRoot(context, &_jsThis);
95 JS_RemoveValueRoot(context, &_function);
96 OOJSRelinquishContext(context);
97
98 [[NSNotificationCenter defaultCenter] removeObserver:self
99 name:kOOJavaScriptEngineWillResetNotification
101 }
102}
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
jsval _function
Definition OOJSTimer.h:34
JSObject * _jsThis
Definition OOJSTimer.h:35
OOJavaScriptEngine * sharedEngine()

References _function, _jsThis, deleteJSPointers, OOJSAcquireContext(), OOJSRelinquishContext(), OOJavaScriptEngine::sharedEngine, and OOScriptTimer::unscheduleTimer.

Referenced by dealloc, and deleteJSPointers.

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

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Reimplemented from OOScriptTimer.

Definition at line 113 of file OOJSTimer.m.

114{
115 NSString* funcName = nil;
116 JSContext* context = NULL;
117
118 if (JSVAL_IS_VOID(_function) || JSVAL_IS_NULL(_function)) {
119 return @"invalid";
120 }
121
122 context = OOJSAcquireContext();
123 funcName = OOStringFromJSString(context, JS_GetFunctionId(JS_ValueToFunction(context, _function)));
124 OOJSRelinquishContext(context);
125
126 if (funcName == nil) {
127 funcName = @"anonymous";
128 }
129
130 return [NSString stringWithFormat:@"%@, function: %@", [super descriptionComponents], funcName];
131}
NSString * OOStringFromJSString(JSContext *context, JSString *string)
return nil

References _function, descriptionComponents, OOScriptTimer::descriptionComponents, nil, OOJSAcquireContext(), OOJSRelinquishContext(), and OOStringFromJSString().

Referenced by descriptionComponents.

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

◆ initWithDelay:interval:context:function:this: [1/2]

- (id) initWithDelay: (OOTimeAbsolute) delay
interval: (OOTimeDelta) interval
context: (JSContext*) context
function: (jsval) function
this: (JSObject*) jsThis 
implementation

Provided by category OOJSTimer(Private).

Definition at line 48 of file OOJSTimer.m.

48 :(OOTimeAbsolute)delay
50 context:(JSContext*)context
51 function:(jsval)function
52 this:(JSObject*)jsThis
53{
54 self = [super initWithNextTime:[UNIVERSE getTime] + delay interval:interval];
55 if (self != nil) {
56 NSAssert(OOJSValueIsFunction(context, function), @"Attempt to init OOJSTimer with a function that isn't.");
57
58 _jsThis = jsThis;
59 OOJSAddGCObjectRoot(context, &_jsThis, "OOJSTimer this");
60
61 _function = function;
62 OOJSAddGCValueRoot(context, &_function, "OOJSTimer function");
63
64 _jsSelf = JS_NewObject(context, &sTimerClass, sTimerPrototype, NULL);
65 if (_jsSelf != NULL) {
66 if (!JS_SetPrivate(context, _jsSelf, [self retain]))
67 _jsSelf = NULL;
68 }
69 if (_jsSelf == NULL) {
70 [self release];
71 return nil;
72 }
73
75
76 [[NSNotificationCenter defaultCenter] addObserver:self
77 selector:@selector(deleteJSPointers)
78 name:kOOJavaScriptEngineWillResetNotification
80 }
81
82 return self;
83}
static JSObject * sTimerPrototype
Definition OOJSTimer.m:33
static JSClass sTimerClass
Definition OOJSTimer.m:34
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
#define OOJSAddGCObjectRoot(context, root, name)
#define OOJSAddGCValueRoot(context, root, name)
double OOTimeDelta
Definition OOTypes.h:193
double OOTimeAbsolute
Definition OOTypes.h:192
OOJSScript * currentlyRunningScript()
Definition OOJSScript.m:101
OOJSScript * _owningScript
Definition OOJSTimer.h:37
OOTimeDelta interval()

References OOJSScript::currentlyRunningScript, OOScriptTimer::initWithNextTime:interval:, nil, OOJSAddGCObjectRoot, OOJSAddGCValueRoot, OOJSValueIsFunction(), OOJavaScriptEngine::sharedEngine, sTimerClass, sTimerPrototype, and OOJSScript::weakRetain.

Here is the call graph for this function:

◆ initWithDelay:interval:context:function:this: [2/2]

- (id) initWithDelay: (OOTimeAbsolute) delay
interval: (OOTimeDelta) interval
context: (JSContext*) context
function: (JSValueConst) function
this: (JSValueConst) jsThis 
implementation

Definition at line 69 of file OOJSTimer.m.

69 :(OOTimeAbsolute)delay
71 context:(JSContext*)context
72 function:(JSValueConst)function
73 this:(JSValueConst)jsThis
74{
75 self = [super initWithNextTime:[UNIVERSE getTime] + delay interval:interval];
76 if (self != nil) {
77 NSAssert(OOJSValueIsFunction(context, function), @"Attempt to init OOJSTimer with a function that isn't.");
78
79 _jsThis = JS_DupValue(context, jsThis);
80 _function = JS_DupValue(context, function);
81
82 /* QuickJS has no JS_GetFunctionId(); use the function's name property.
83 It's cached because -descriptionComponents is used by the
84 finalizer, which must not run JS code (the property could be a
85 getter, or the function a Proxy).
86 */
87 JSValue nameValue = JS_GetPropertyStr(context, function, "name");
88 if (JS_IsException(nameValue)) {
89 JS_FreeValue(context, JS_GetException(context));
90 } else {
91 NSString* name = OOStringFromJSValue(context, nameValue);
92 if ([name length] != 0)
93 _functionName = [name copy];
94 }
95 JS_FreeValue(context, nameValue);
96
97 _jsSelf = JS_NewObjectClass(context, sTimerClassID);
98 if (!JS_IsException(_jsSelf)) {
99 if (JS_SetOpaque(_jsSelf, [self retain]) < 0) {
100 [self release];
101 JS_FreeValue(context, _jsSelf);
102 _jsSelf = JS_EXCEPTION;
103 }
104 }
105 if (JS_IsException(_jsSelf)) {
106 _jsSelf = JS_UNDEFINED;
107 [self releaseJSValues];
108 [self release];
109 return nil;
110 }
111
113
114 [[NSNotificationCenter defaultCenter] addObserver:self
115 selector:@selector(deleteJSPointers)
116 name:kOOJavaScriptEngineWillResetNotification
118 }
119
120 return self;
121}
NSString * OOStringFromJSValue(JSContext *context, jsval value)
static JSClassID sTimerClassID
Definition OOJSTimer.m:33
NSString * _functionName
Definition OOJSTimer.h:38

References _function, _functionName, _jsSelf, _jsThis, _owningScript, OOJSScript::currentlyRunningScript, OOScriptTimer::initWithNextTime:interval:, nil, OOJSValueIsFunction(), OOStringFromJSValue(), releaseJSValues, OOJavaScriptEngine::sharedEngine, sTimerClassID, and OOJSScript::weakRetain.

Here is the call graph for this function:

◆ oo_jsClassName

- (NSString *) oo_jsClassName
implementation

Definition at line 133 of file OOJSTimer.m.

134{
135 return @"Timer";
136}

References oo_jsClassName.

Referenced by oo_jsClassName.

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

◆ oo_jsValueInContext:

- (JSValue) oo_jsValueInContext: (JSContext*) context
implementation

Definition at line 171 of file OOJSTimer.m.

171 :(JSContext*)context
172{
173 return OBJECT_TO_JSVAL(_jsSelf);
174}

References _jsSelf.

◆ releaseJSValues

- (void) releaseJSValues
implementation

Provided by category OOJSTimer(Private).

Definition at line 133 of file OOJSTimer.m.

134{
135 [self unscheduleTimer];
136
137 // Ivars are zero-filled (not objects) before they are set.
138 if (JS_IsObject(_function)) {
139 // Don't touch the ivars after freeing (see -deleteJSPointers).
140 JSValue function = _function;
141 JSValue jsThis = _jsThis;
142 _function = JS_UNDEFINED;
143 _jsThis = JS_UNDEFINED;
144
145 [[NSNotificationCenter defaultCenter] removeObserver:self
146 name:kOOJavaScriptEngineWillResetNotification
148
149 JSContext* context = OOJSAcquireContext();
150 JS_FreeValue(context, function);
151 JS_FreeValue(context, jsThis);
152 OOJSRelinquishContext(context);
153 }
154}

References OOJSAcquireContext(), OOJSRelinquishContext(), OOJSTimer(Private)::releaseJSValues, OOJavaScriptEngine::sharedEngine, and OOScriptTimer::unscheduleTimer.

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

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

◆ timerFired

- (void) timerFired
implementation

Reimplemented from OOScriptTimer.

Definition at line 138 of file OOJSTimer.m.

139{
140 jsval rval = JSVAL_VOID;
141 NSString* description = nil;
142
143 OOJavaScriptEngine* engine = [OOJavaScriptEngine sharedEngine];
144 JSContext* context = OOJSAcquireContext();
145
146 // stop and remove the timer if _jsThis (the first parameter in the constructor) dies.
147 id object = OOJSNativeObjectFromJSObject(context, _jsThis);
148 if (object != nil) {
149 description = [object oo_jsDescription];
150 if (description == nil)
151 description = [object description];
152 }
153
154 if (description == nil) {
155 [self unscheduleTimer];
156 OOJSRelinquishContext(context);
157 return;
158 }
159
160 [OOJSScript pushScript:_owningScript];
161 [engine callJSFunction:_function
162 forObject:_jsThis
163 argc:0
164 argv:NULL
165 result:&rval];
166 [OOJSScript popScript:_owningScript];
167
168 OOJSRelinquishContext(context);
169}
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
void pushScript:(OOJSScript *script)
Definition OOJSScript.m:480
void popScript:(OOJSScript *script)
Definition OOJSScript.m:493
BOOL callJSFunction:forObject:argc:argv:result:(jsval function,[forObject] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *outResult)

References _jsThis, OOJavaScriptEngine::callJSFunction:forObject:argc:argv:result:, nil, OOJSAcquireContext(), OOJSNativeObjectFromJSObject(), OOJSRelinquishContext(), OOJSScript::popScript:, OOJSScript::pushScript:, OOJavaScriptEngine::sharedEngine, timerFired, and OOScriptTimer::unscheduleTimer.

Referenced by timerFired.

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

◆ transferJSSelf

- (JSValue) transferJSSelf
implementation

Provided by category OOJSTimer(Private).

Definition at line 266 of file OOJSTimer.m.

267{
268 return _jsSelf;
269}

References OOJSTimer(Private)::transferJSSelf.

Referenced by OOJSTimer(Private)::initWithDelay:interval:context:function:this:, TimerConstruct(), and OOJSTimer(Private)::transferJSSelf.

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

◆ willNotFireAgain

- (BOOL) willNotFireAgain
implementation

Provided by category OOJSTimer(Private).

Definition at line 256 of file OOJSTimer.m.

257{
258 return _firing && [self interval] <= 0.0;
259}
BOOL _firing
Definition OOJSTimer.h:42

References OOScriptTimer::interval, and OOJSTimer(Private)::willNotFireAgain.

Referenced by OOJSTimer(Private)::initWithDelay:interval:context:function:this:, TimerFinalize(), and OOJSTimer(Private)::willNotFireAgain.

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

Member Data Documentation

◆ _firing

- (BOOL) _firing
private

Definition at line 42 of file OOJSTimer.h.

◆ _function [1/2]

- (jsval) _function
private

◆ _function [2/2]

- (JSValue) _function
private

Definition at line 34 of file OOJSTimer.h.

◆ _functionName

- (NSString*) _functionName
private

Definition at line 38 of file OOJSTimer.h.

Referenced by initWithDelay:interval:context:function:this:.

◆ _jsSelf [1/2]

- (JSObject*) _jsSelf
private

Definition at line 39 of file OOJSTimer.h.

Referenced by initWithDelay:interval:context:function:this:, and oo_jsValueInContext:.

◆ _jsSelf [2/2]

- (JSValue) _jsSelf
private

Definition at line 40 of file OOJSTimer.h.

◆ _jsThis [1/2]

- (JSObject*) _jsThis
private

◆ _jsThis [2/2]

- (JSValue) _jsThis
private

Definition at line 35 of file OOJSTimer.h.

◆ _owningScript

- (OOJSScript *) _owningScript
private

Definition at line 37 of file OOJSTimer.h.

Referenced by initWithDelay:interval:context:function:this:.


The documentation for this class was generated from the following files: