Oolite
Loading...
Searching...
No Matches
OOJSEngineNativeWrappers.h
Go to the documentation of this file.
1/*
2
3OOJSEngineNativeWrappers.h
4(Included by OOJavaScriptEngine.h)
5
6Exception safety and profiling macros.
7
8Every JavaScript native callback that could concievably cause an
9Objective-C exception should begin with OOJS_NATIVE_ENTER() and end with
10OOJS_NATIVE_EXIT. Callbacks which have been carefully audited for potential
11exceptions, and support functions called from JavaScript native callbacks,
12may start with OOJS_PROFILE_ENTER and end with OOJS_PROFILE_EXIT to be
13included in profiling reports.
14
15Functions using either of these pairs _must_ return before
16OOJS_NATIVE_EXIT/OOJS_PROFILE_EXIT, or they will crash.
17
18For functions with a non-scalar return type, OOJS_PROFILE_EXIT should be
19replaced with OOJS_PROFILE_EXIT_VAL(returnValue). The returnValue is never
20used (and should be a constant expression), but is required to placate the
21compiler.
22
23For values with void return, use OOJS_PROFILE_EXIT_VOID. It is not
24necessary to insert a return statement before OOJS_PROFILE_EXIT_VOID.
25
26
27JavaScript support for Oolite
28Copyright (C) 2007-2013 David Taylor and Jens Ayton.
29
30This program is free software; you can redistribute it and/or
31modify it under the terms of the GNU General Public License
32as published by the Free Software Foundation; either version 2
33of the License, or (at your option) any later version.
34
35This program is distributed in the hope that it will be useful,
36but WITHOUT ANY WARRANTY; without even the implied warranty of
37MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38GNU General Public License for more details.
39
40You should have received a copy of the GNU General Public License
41along with this program; if not, write to the Free Software
42Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
43MA 02110-1301, USA.
44
45*/
46
47#import "OOFoundation.h"
48
49#define OOJS_PROFILE OOLITE_DEBUG
50
51#if OOJS_PROFILE
52
53#define OOJS_PROFILE_ENTER_NAMED(NAME) \
54 { \
55 OOJS_DECLARE_PROFILE_STACK_FRAME(oojsProfilerStackFrame) \
56 @try { \
57 OOJSProfileEnter(&oojsProfilerStackFrame, NAME);
58
59#define OOJS_PROFILE_ENTER \
60 OOJS_PROFILE_ENTER_NAMED(__FUNCTION__)
61
62#define OOJS_PROFILE_EXIT_VAL(rval) \
63 } \
64 @finally \
65 { \
66 OOJSProfileExit(&oojsProfilerStackFrame); \
67 } \
68 OOJSUnreachable(__FUNCTION__, __FILE__, __LINE__); \
69 return rval; \
70 }
71#define OOJS_PROFILE_EXIT_VOID \
72 return; \
73 OOJS_PROFILE_EXIT_VAL()
74
75#define OOJS_PROFILE_ENTER_FOR_NATIVE OOJS_PROFILE_ENTER
76
77#else
78
79#define OOJS_PROFILE_ENTER {
80#define OOJS_PROFILE_EXIT_VAL(rval) \
81 } \
82 OOJSUnreachable(__FUNCTION__, __FILE__, __LINE__); \
83 return (rval);
84#define OOJS_PROFILE_EXIT_VOID \
85 } \
86 return;
87#define OOJS_PROFILE_ENTER_FOR_NATIVE @try {
88
89#endif // OOJS_PROFILE
90
91#define OOJS_NATIVE_ENTER(cx) \
92 { \
93 JSContext* oojsNativeContext = (cx); \
94 OOJS_PROFILE_ENTER_FOR_NATIVE
95
96#define OOJS_NATIVE_EXIT \
97 } \
98 @catch (id exception) \
99 { \
100 OOJSReportWrappedException(oojsNativeContext, exception); \
101 return NO; \
102 OOJS_PROFILE_EXIT_VAL(NO) \
103 }
104
105void OOJSReportWrappedException(JSContext* context, id exception);
106
107#ifndef NDEBUG
108void OOJSUnreachable(const char* function, const char* file, unsigned line) NO_RETURN_FUNC;
109#else
110#define OOJSUnreachable(function, file, line) OO_UNREACHABLE()
111#endif
112
113#define OOJS_PROFILE_EXIT OOJS_PROFILE_EXIT_VAL(0)
114#define OOJS_PROFILE_EXIT_JSVAL OOJS_PROFILE_EXIT_VAL(JSVAL_VOID)
115
116/*
117 OOJS_BEGIN_FULL_NATIVE() and OOJS_END_FULL_NATIVE
118 These macros are used to bracket sections of native Oolite code within JS
119 callbacks which may take a long time. Thet do two things: pause the
120 time limiter, and (in JS_THREADSAFE builds) suspend the current JS context
121 request.
122
123 These macros must be used in balanced pairs. They introduce a scope.
124
125 JSAPI functions may not be used, directly or indirectily, between these
126 macros unless explicitly opening a request first.
127*/
128#if JS_THREADSAFE
129#define OOJS_BEGIN_FULL_NATIVE(context) \
130 { \
131 OOJSPauseTimeLimiter(); \
132 JSContext* oojsRequestContext = (context); \
133 jsrefcount oojsRequestRefCount = JS_SuspendRequest(oojsRequestContext); \
134 @try {
135
136#define OOJS_END_FULL_NATIVE \
137 } \
138 @finally \
139 { \
140 JS_ResumeRequest(oojsRequestContext, oojsRequestRefCount); \
141 OOJSResumeTimeLimiter(); \
142 } \
143 }
144#else
145#define OOJS_BEGIN_FULL_NATIVE(context) \
146 { \
147 (void)(context); \
148 OOJSPauseTimeLimiter(); \
149 @try {
150
151#define OOJS_END_FULL_NATIVE \
152 } \
153 @finally \
154 { \
155 OOJSResumeTimeLimiter(); \
156 } \
157 }
158#endif
159
160#if OOJS_PROFILE
161
163
164/*
165 Profiler implementation details. This should be internal to
166 OOJSTimeManagement.m, but needs to be declared on the stack by the macros
167 above when profiling is enabled.
168*/
169
170typedef struct OOJSProfileStackFrame OOJSProfileStackFrame;
171struct OOJSProfileStackFrame {
172 OOJSProfileStackFrame* back; // Stack link
173 const void* key; // Key to look up profile entries. May be any pointer; currently const char * for native frames and JSFunction * for JS frames.
174 const char* function; // Name of function, for native frames.
175 OOHighResTimeValue startTime; // Time frame was entered.
176 OOTimeDelta subTime; // Time spent in subroutine calls.
177 OOTimeDelta* total; // Pointer to accumulator for this type of frame.
178 void (*cleanup)(OOJSProfileStackFrame*); // Cleanup function if needed (used for JS frames).
179};
180
181#define OOJS_DECLARE_PROFILE_STACK_FRAME(name) OOJSProfileStackFrame name;
182void OOJSProfileEnter(OOJSProfileStackFrame* frame, const char* function);
183void OOJSProfileExit(OOJSProfileStackFrame* frame);
184
185#else
186
187#define OOJS_DECLARE_PROFILE_STACK_FRAME(name)
188#define OOJSProfileEnter(frame, function) \
189 do { \
190 } while (0)
191#define OOJSProfileExit(frame) \
192 do { \
193 } while (0)
194
195#endif
#define NO_RETURN_FUNC
void OOJSReportWrappedException(JSContext *context, id exception)
#define OOJSProfileExit(frame)
#define OOJSProfileEnter(frame, function)
void OOJSUnreachable(const char *function, const char *file, unsigned line) NO_RETURN_FUNC
uint64_t OOHighResTimeValue
double OOTimeDelta
Definition OOTypes.h:193