Oolite
Loading...
Searching...
No Matches
OOJSEngineDebuggerHelpers.m
Go to the documentation of this file.
1/*
2
3OOJSEngineDebuggerHelpers.m
4
5JavaScript support for Oolite
6Copyright (C) 2007-2013 David Taylor and Jens Ayton.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25/*
26 These functions exist to help debugging JavaScript code. They can be called
27 directly from gdb, for example:
28
29 call (char *)JSValueToStrDbg(someValue)
30
31 The functions are:
32
33 const char *JSValueToStrDbg(jsval)
34 const char *JSObjectToStrDbg(JSObject *)
35 const char *JSStringToStrDbg(JSString *)
36 Converts any JS value/object/JSString to a string, using the complete
37 process and potentially calling into SpiderMonkey with a secondory
38 context and invoking JS toString() methods. This might mess up
39 SpiderMonkey internal state in some cases.
40
41 const char *JSValueToStrSafeDbg(jsval)
42 const char *JSObjectToStrSafeDbg(JSObject *)
43 const char *JSStringToStrSafeDbg(JSString *)
44 As above, but without calling into SpiderMonkey functions that require
45 a context. In particular, as of the FF4b9 version of SpiderMonkey only
46 interned strings can be converted, and for objects only the class name
47 is provided.
48
49 const char *JSIDToStrSafeDbg(jsid)
50 Like JSValueToStrSafeDbg() for jsids. (String jsids must always be
51 interned, so this is generally sufficient.)
52
53 const char *JSValueTypeDbg(jsval)
54 Returns the type of the jsval, or the class name if it's an object.
55
56 All dynamic strings are autoreleased.
57
58 Another useful function is OOJSDumpStack (results are found in the log):
59
60 call OOJSDumpStack(context)
61
62 A set of macros can be found in tools/gdb-macros.txt.
63 In almost all Oolite functions that deal with JavaScript, there is a single
64 JSContext called "context". In SpiderMonkey functions, it's called "cx".
65
66
67 In addition to calling them from the debug console, Xcode users might want
68 to use them in data formatters (by double-clicking the "Summary" field for
69 a variable of the appropriate type). I recommend the following:
70
71 jsval: {JSValueToStrSafeDbg($VAR)}:s
72 jsval*: {JSValueToStrSafeDbg(*$VAR)}:s
73 jsid: {JSIDToStrSafeDbg($VAR)}:s
74 JSObject*: {JSObjectToStrSafeDbg($VAR)}:s
75 JSString*: {JSStringToStrSafeDbg($VAR)}:s
76
77 These, and a variety of Oolite type formatters, can be set up using
78 Mac-specific/DataFormatters.
79*/
80
81#ifdef OO_DEBUG_MONITOR_ENABLED
82
84
85const char* JSValueToStrDbg(jsval val)
86{
87 JSContext* context = OOJSAcquireContext();
88 const char* result = [OOStringFromJSValueEvenIfNull(context, val) UTF8String];
89 OOJSRelinquishContext(context);
90
91 return result;
92}
93
94const char* JSObjectToStrDbg(JSObject* obj)
95{
96 if (obj == NULL)
97 return "null";
98 return JSValueToStrDbg(OBJECT_TO_JSVAL(obj));
99}
100
101const char* JSStringToStrDbg(JSString* str)
102{
103 if (str == NULL)
104 return "null";
105 return JSValueToStrDbg(STRING_TO_JSVAL(str));
106}
107
108const char* JSValueTypeDbg(jsval val)
109{
110 if (JSVAL_IS_INT(val))
111 return "integer";
112 if (JSVAL_IS_DOUBLE(val))
113 return "double";
114 if (JSVAL_IS_STRING(val))
115 return "string";
116 if (JSVAL_IS_BOOLEAN(val))
117 return "boolean";
118 if (JSVAL_IS_NULL(val))
119 return "null";
120 if (JSVAL_IS_VOID(val))
121 return "void";
122#ifdef JS_USE_JSVAL_JSID_STRUCT_TYPES
123 if (JSVAL_IS_MAGIC_IMPL(val)) {
124 switch (val.s.payload.why) {
125 case JS_ARRAY_HOLE:
126 return "magic (array hole)";
127 case JS_ARGS_HOLE:
128 return "magic (args hole)";
129 case JS_NATIVE_ENUMERATE:
130 return "magic (native enumerate)";
131 case JS_NO_ITER_VALUE:
132 return "magic (no iter value)";
133 case JS_GENERATOR_CLOSING:
134 return "magic (generator closing)";
135 case JS_NO_CONSTANT:
136 return "magic (no constant)";
137 case JS_THIS_POISON:
138 return "magic (this poison)";
139 case JS_ARG_POISON:
140 return "magic (arg poison)";
141 case JS_SERIALIZE_NO_NODE:
142 return "magic (serialize no node)";
143 case JS_GENERIC_MAGIC:
144 return "magic (generic)";
145 };
146 return "magic";
147 }
148#endif
149 if (JSVAL_IS_OBJECT(val))
150 return OOJSGetClass(NULL, JSVAL_TO_OBJECT(val))->name; // Fun fact: although a context is required if JS_THREADSAFE is defined, it isn't actually used.
151 return "unknown";
152}
153
154// Doesn't follow pointers, mess with requests or otherwise poke the SpiderMonkey.
155const char* JSValueToStrSafeDbg(jsval val)
156{
157 NSString* formatted = nil;
158
159 if (JSVAL_IS_INT(val))
160 formatted = [NSString stringWithFormat:@"%i", JSVAL_TO_INT(val)];
161 else if (JSVAL_IS_DOUBLE(val))
162 formatted = [NSString stringWithFormat:@"%g", JSVAL_TO_DOUBLE(val)];
163 else if (JSVAL_IS_BOOLEAN(val))
164 formatted = (JSVAL_TO_BOOLEAN(val)) ? @"true" : @"false";
165 else if (JSVAL_IS_STRING(val)) {
166 JSString* string = JSVAL_TO_STRING(val);
167 const jschar* chars = NULL;
168 size_t length = JS_GetStringLength(string);
169
170 if (JS_StringHasBeenInterned(string)) {
171 chars = JS_GetInternedStringChars(string);
172 }
173 // Flat strings can be extracted without a context, but cannot be detected.
174
175 if (chars == NULL)
176 formatted = [NSString stringWithFormat:@"string [%zu chars]", length];
177 else
178 formatted = [NSString stringWithCharacters:chars length:length];
179 } else if (JSVAL_IS_VOID(val))
180 return "undefined";
181 else
182 return JSValueTypeDbg(val);
183
184 return [formatted UTF8String];
185}
186
187const char* JSObjectToStrSafeDbg(JSObject* obj)
188{
189 if (obj == NULL)
190 return "null";
191 return JSValueToStrSafeDbg(OBJECT_TO_JSVAL(obj));
192}
193
194const char* JSStringToStrSafeDbg(JSString* str)
195{
196 if (str == NULL)
197 return "null";
198 return JSValueToStrSafeDbg(STRING_TO_JSVAL(str));
199}
200
201const char* JSIDToStrSafeDbg(jsid anID)
202{
203 NSString* formatted = nil;
204
205 if (JSID_IS_INT(anID))
206 formatted = [NSString stringWithFormat:@"%i", JSID_TO_INT(anID)];
207 else if (JSID_IS_VOID(anID))
208 return "void";
209 else if (JSID_IS_EMPTY(anID))
210 return "empty";
211 else if (JSID_IS_ZERO(anID))
212 return "0";
213 else if (JSID_IS_OBJECT(anID))
214 return OOJSGetClass(NULL, JSID_TO_OBJECT(anID))->name;
215 else if (JSID_IS_DEFAULT_XML_NAMESPACE(anID))
216 return "default XML namespace";
217 else if (JSID_IS_STRING(anID)) {
218 JSString* string = JSID_TO_STRING(anID);
219 const jschar* chars = NULL;
220 size_t length = JS_GetStringLength(string);
221
222 if (JS_StringHasBeenInterned(string)) {
223 chars = JS_GetInternedStringChars(string);
224 } else {
225 // Bug; jsid strings must be interned.
226 return "*** uninterned string in jsid! ***";
227 }
228 formatted = [NSString stringWithCharacters:chars length:length];
229 } else {
230 formatted = [NSString stringWithFormat:@"unknown <0x%llX>", (long long)JSID_BITS(anID)];
231 }
232
233 return [formatted UTF8String];
234}
235#endif
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
OOINLINE JSClass * OOJSGetClass(JSContext *cx, JSObject *obj) ALWAYS_INLINE_FUNC
return nil