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(JSValue)
34 const char *JSObjectToStrDbg(JSValue)
35 const char *JSStringToStrDbg(JSValue)
36 Converts any JS value to a string, using the complete process and
37 potentially invoking JS toString() methods. This might mess up
38 QuickJS internal state in some cases (for instance, if called while
39 an exception is pending).
40
41 const char *JSValueToStrSafeDbg(JSValue)
42 const char *JSObjectToStrSafeDbg(JSValue)
43 const char *JSStringToStrSafeDbg(JSValue)
44 As above, but without calling any JavaScript code. Primitives
45 (including strings) are converted; for objects only the class name
46 is provided.
47
48 const char *JSIDToStrSafeDbg(JSAtom)
49 Like JSValueToStrSafeDbg() for property IDs (atoms).
50
51 const char *JSValueTypeDbg(JSValue)
52 Returns the type of the value, or the class name if it's an object.
53
54 All dynamic strings are autoreleased.
55
56 Changes from the SpiderMonkey version: QuickJS has no JSObject* or
57 JSString* handle types, so the Object and String variants take a JSValue
58 and are equivalent to the Value variants. Strings can always be read
59 without running JS code, so the Safe variants show string contents
60 rather than "string [n chars]". JS_ARRAY_HOLE and the other SpiderMonkey
61 "magic" values do not exist; QuickJS's internal tags are reported instead.
62
63 Another useful function is OOJSDumpStack (results are found in the log):
64
65 call OOJSDumpStack(context)
66
67 QuickJS has no stack frame API, so the dump is the stack property of a
68 new Error: function names and locations only.
69
70 A set of macros can be found in tools/gdb-macros.txt. (They were written
71 for SpiderMonkey types and have not been updated.)
72 In almost all Oolite functions that deal with JavaScript, there is a single
73 JSContext called "context". In QuickJS functions, it's called "ctx".
74*/
75
76#ifdef OO_DEBUG_MONITOR_ENABLED
77
79
80// Exported for use from a debugger; not declared in a header.
81const char* JSValueToStrDbg(JSValue val);
82const char* JSObjectToStrDbg(JSValue obj);
83const char* JSStringToStrDbg(JSValue str);
84const char* JSValueTypeDbg(JSValue val);
85const char* JSValueToStrSafeDbg(JSValue val);
86const char* JSObjectToStrSafeDbg(JSValue obj);
87const char* JSStringToStrSafeDbg(JSValue str);
88const char* JSIDToStrSafeDbg(JSAtom anID);
89
90const char* JSValueToStrDbg(JSValue val)
91{
92 JSContext* context = OOJSAcquireContext();
93 const char* result = [OOStringFromJSValueEvenIfNull(context, val) UTF8String];
94 OOJSRelinquishContext(context);
95
96 return result;
97}
98
99const char* JSObjectToStrDbg(JSValue obj)
100{
101 return JSValueToStrDbg(obj);
102}
103
104const char* JSStringToStrDbg(JSValue str)
105{
106 return JSValueToStrDbg(str);
107}
108
109const char* JSValueTypeDbg(JSValue val)
110{
111 switch (JS_VALUE_GET_TAG(val)) {
112 case JS_TAG_INT:
113 return "integer";
114 case JS_TAG_FLOAT64:
115 return "double";
116 case JS_TAG_STRING:
117 case JS_TAG_STRING_ROPE:
118 return "string";
119 case JS_TAG_BOOL:
120 return "boolean";
121 case JS_TAG_NULL:
122 return "null";
123 case JS_TAG_UNDEFINED:
124 return "void";
125 case JS_TAG_SYMBOL:
126 return "symbol";
127 case JS_TAG_BIG_INT:
128 case JS_TAG_SHORT_BIG_INT:
129 return "bigint";
130 case JS_TAG_UNINITIALIZED:
131 return "magic (uninitialized)";
132 case JS_TAG_CATCH_OFFSET:
133 return "magic (catch offset)";
134 case JS_TAG_EXCEPTION:
135 return "magic (exception)";
136 case JS_TAG_MODULE:
137 return "magic (module)";
138 case JS_TAG_FUNCTION_BYTECODE:
139 return "magic (function bytecode)";
140 case JS_TAG_OBJECT: {
141 const char* className = OOJSClassName(JS_GetClassID(val));
142 return (className != NULL) ? className : "object";
143 }
144 }
145 return "unknown";
146}
147
148// Doesn't run JavaScript code.
149const char* JSValueToStrSafeDbg(JSValue val)
150{
151 NSString* formatted = nil;
152
153 switch (JS_VALUE_GET_TAG(val)) {
154 case JS_TAG_INT:
155 formatted = [NSString stringWithFormat:@"%i", JS_VALUE_GET_INT(val)];
156 break;
157
158 case JS_TAG_FLOAT64:
159 formatted = [NSString stringWithFormat:@"%g", JS_VALUE_GET_FLOAT64(val)];
160 break;
161
162 case JS_TAG_BOOL:
163 formatted = JS_VALUE_GET_BOOL(val) ? @"true" : @"false";
164 break;
165
166 case JS_TAG_STRING:
167 case JS_TAG_STRING_ROPE: {
168 // Converting a string primitive doesn't call into JS code.
169 JSContext* context = OOJSAcquireContext();
170 size_t length;
171 const char* chars = JS_ToCStringLen(context, &length, val);
172 if (chars != NULL) {
173 formatted = [[[NSString alloc] initWithBytes:chars length:length encoding:NSUTF8StringEncoding] autorelease];
174 JS_FreeCString(context, chars);
175 } else {
176 JS_FreeValue(context, JS_GetException(context));
177 formatted = @"string [unreadable]";
178 }
179 break;
180 }
181
182 case JS_TAG_UNDEFINED:
183 return "undefined";
184
185 default:
186 return JSValueTypeDbg(val);
187 }
188
189 return [formatted UTF8String];
190}
191
192const char* JSObjectToStrSafeDbg(JSValue obj)
193{
194 return JSValueToStrSafeDbg(obj);
195}
196
197const char* JSStringToStrSafeDbg(JSValue str)
198{
199 return JSValueToStrSafeDbg(str);
200}
201
202const char* JSIDToStrSafeDbg(JSAtom anID)
203{
204 if (anID == JS_ATOM_NULL)
205 return "void";
206
207 JSContext* context = OOJSAcquireContext();
208 NSString* formatted = nil;
209 const char* chars = JS_AtomToCString(context, anID);
210 if (chars != NULL) {
211 formatted = [NSString stringWithUTF8String:chars];
212 JS_FreeCString(context, chars);
213 } else {
214 JS_FreeValue(context, JS_GetException(context));
215 formatted = [NSString stringWithFormat:@"unknown <0x%X>", (unsigned)anID];
216 }
217
218 return [formatted UTF8String];
219}
220#endif
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
return nil
const char * OOJSClassName(JSClassID classID)
Definition OOJSClasses.m:89