29#ifdef OO_DEBUG_MONITOR_ENABLED
39#import "OOJSEngineTimeManagement.h"
48@interface Entity (OODebugInspector)
57static JSObject* sConsolePrototype = NULL;
58static JSObject* sConsoleSettingsPrototype = NULL;
60static JSBool ConsoleGetProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value);
61static JSBool ConsoleSetProperty(JSContext* context, JSObject*
this, jsid propID, JSBool strict, jsval* value);
62static void ConsoleFinalize(JSContext* context, JSObject*
this);
65static JSBool ConsoleConsoleMessage(JSContext* context, uintN argc, jsval* vp);
66static JSBool ConsoleClearConsole(JSContext* context, uintN argc, jsval* vp);
67static JSBool ConsoleScriptStack(JSContext* context, uintN argc, jsval* vp);
68static JSBool ConsoleInspectEntity(JSContext* context, uintN argc, jsval* vp);
70static JSBool ConsoleCallObjCMethod(JSContext* context, uintN argc, jsval* vp);
71static JSBool ConsoleSetUpCallObjC(JSContext* context, uintN argc, jsval* vp);
73static JSBool ConsoleIsExecutableJavaScript(JSContext* context, uintN argc, jsval* vp);
74static JSBool ConsoleDisplayMessagesInClass(JSContext* context, uintN argc, jsval* vp);
75static JSBool ConsoleSetDisplayMessagesInClass(JSContext* context, uintN argc, jsval* vp);
76static JSBool ConsoleWriteLogMarker(JSContext* context, uintN argc, jsval* vp);
77static JSBool ConsoleWriteMemoryStats(JSContext* context, uintN argc, jsval* vp);
78static JSBool ConsoleWriteJSMemoryStats(JSContext* context, uintN argc, jsval* vp);
79static JSBool ConsoleGarbageCollect(JSContext* context, uintN argc, jsval* vp);
81static JSBool ConsoleDumpNamedRoots(JSContext* context, uintN argc, jsval* vp);
82static JSBool ConsoleDumpHeap(JSContext* context, uintN argc, jsval* vp);
85static JSBool ConsoleProfile(JSContext* context, uintN argc, jsval* vp);
86static JSBool ConsoleGetProfile(JSContext* context, uintN argc, jsval* vp);
87static JSBool ConsoleTrace(JSContext* context, uintN argc, jsval* vp);
90static JSBool ConsoleSettingsDeleteProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value);
91static JSBool ConsoleSettingsGetProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value);
92static JSBool ConsoleSettingsSetProperty(JSContext* context, JSObject*
this, jsid propID, JSBool strict, jsval* value);
95static JSBool PerformProfiling(JSContext* context, NSString* nominalFunction, uintN argc, jsval* argv, jsval* rval, BOOL trace, OOTimeProfile** profile);
98static JSClass sConsoleClass = {
110 JSCLASS_NO_OPTIONAL_MEMBERS
116 kConsole_detailLevel,
117 kConsole_maximumDetailLevel,
119 kConsole_platformDescription,
120 kConsole_ignoreDroppedPackets,
121 kConsole_pedanticMode,
122 kConsole_showErrorLocations,
123 kConsole_dumpStackForErrors,
124 kConsole_dumpStackForWarnings,
126 kConsole_glVendorString,
127 kConsole_glRendererString,
128 kConsole_glFixedFunctionTextureUnitCount,
129 kConsole_glFragmentShaderTextureUnitCount,
132 kConsole_DEBUG_LINKED_LISTS,
133 kConsole_DEBUG_COLLISIONS,
134 kConsole_DEBUG_DOCKING,
135 kConsole_DEBUG_OCTREE_LOGGING,
136 kConsole_DEBUG_BOUNDING_BOXES,
137 kConsole_DEBUG_OCTREE_DRAW,
138 kConsole_DEBUG_DRAW_NORMALS,
139 kConsole_DEBUG_NO_DUST,
140 kConsole_DEBUG_NO_SHADER_FALLBACK,
141 kConsole_DEBUG_SHADER_VALIDATION,
146static JSPropertySpec sConsoleProperties[] = {
160 {
"glFixedFunctionTextureUnitCount", kConsole_glFixedFunctionTextureUnitCount,
OOJS_PROP_READONLY_CB },
161 {
"glFragmentShaderTextureUnitCount", kConsole_glFragmentShaderTextureUnitCount,
OOJS_PROP_READONLY_CB },
163#define DEBUG_FLAG_DECL(x) { #x, kConsole_##x, OOJS_PROP_READONLY_CB }
176#undef DEBUG_FLAG_DECL
181static JSFunctionSpec sConsoleMethods[] = {
183 {
"consoleMessage", ConsoleConsoleMessage, 2 },
184 {
"clearConsole", ConsoleClearConsole, 0 },
185 {
"scriptStack", ConsoleScriptStack, 0 },
186 {
"inspectEntity", ConsoleInspectEntity, 1 },
188 {
"__setUpCallObjC", ConsoleSetUpCallObjC, 1 },
190 {
"isExecutableJavaScript", ConsoleIsExecutableJavaScript, 2 },
191 {
"displayMessagesInClass", ConsoleDisplayMessagesInClass, 1 },
192 {
"setDisplayMessagesInClass", ConsoleSetDisplayMessagesInClass, 2 },
193 {
"writeLogMarker", ConsoleWriteLogMarker, 0 },
194 {
"writeMemoryStats", ConsoleWriteMemoryStats, 0 },
195 {
"writeJSMemoryStats", ConsoleWriteJSMemoryStats, 0 },
196 {
"garbageCollect", ConsoleGarbageCollect, 0 },
198 {
"dumpNamedRoots", ConsoleDumpNamedRoots, 0 },
199 {
"dumpHeap", ConsoleDumpHeap, 0 },
202 {
"profile", ConsoleProfile, 1 },
203 {
"getProfile", ConsoleGetProfile, 1 },
204 {
"trace", ConsoleTrace, 1 },
209static JSClass sConsoleSettingsClass = {
214 ConsoleSettingsDeleteProperty,
215 ConsoleSettingsGetProperty,
216 ConsoleSettingsSetProperty,
221 JSCLASS_NO_OPTIONAL_MEMBERS
224static void InitOOJSConsole(JSContext* context, JSObject* global)
226 sConsolePrototype = JS_InitClass(context, global, NULL, &sConsoleClass,
OOJSUnconstructableConstruct, 0, sConsoleProperties, sConsoleMethods, NULL, NULL);
229 sConsoleSettingsPrototype = JS_InitClass(context, global, NULL, &sConsoleSettingsClass,
OOJSUnconstructableConstruct, 0, NULL, NULL, NULL, NULL);
235 sConsolePrototype = NULL;
242 OOJavaScriptEngine* engine =
nil;
243 JSObject*
object = NULL;
244 JSObject* settingsObject = NULL;
247 NSCAssert(JS_EnterLocalRootScope(context),
@"Failed to create JS GC root scope");
250 if (sConsolePrototype == NULL) {
251 InitOOJSConsole(context, [engine globalObject]);
255 object = JS_NewObject(context, &sConsoleClass, sConsolePrototype, NULL);
256 if (
object != NULL) {
257 if (!JS_SetPrivate(context,
object, [monitor weakRetain]))
261 if (
object != NULL) {
263 settingsObject = JS_NewObject(context, &sConsoleSettingsClass, sConsoleSettingsPrototype, NULL);
264 if (settingsObject != NULL) {
265 if (!JS_SetPrivate(context, settingsObject, [monitor weakRetain]))
266 settingsObject = NULL;
268 if (settingsObject != NULL) {
269 value = OBJECT_TO_JSVAL(settingsObject);
270 if (!JS_SetProperty(context,
object,
"settings", &value)) {
271 settingsObject = NULL;
275 if (settingsObject == NULL)
279 JS_LeaveLocalRootScope(context);
287static JSBool ConsoleGetProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value)
289 if (!JSID_IS_INT(propID))
294 switch (JSID_TO_INT(propID)) {
295#ifdef OO_DEBUG_MONITOR_ENABLED
296 case kConsole_debugFlags:
301 case kConsole_detailLevel:
302 *value = [OOStringFromGraphicsDetail([UNIVERSE detailLevel]) oo_jsValueInContext:context];
305 case kConsole_maximumDetailLevel:
309 case kConsole_displayFPS:
313 case kConsole_platformDescription:
317 case kConsole_pedanticMode: {
318 uint32_t options = JS_GetOptions(context);
322 case kConsole_ignoreDroppedPackets:
323 *value =
OOJSValueFromBOOL([[OODebugMonitor sharedDebugMonitor] TCPIgnoresDroppedPackets]);
326 case kConsole_showErrorLocations:
327 *value =
OOJSValueFromBOOL([[OOJavaScriptEngine sharedEngine] showErrorLocations]);
330 case kConsole_dumpStackForErrors:
331 *value =
OOJSValueFromBOOL([[OOJavaScriptEngine sharedEngine] dumpStackForErrors]);
334 case kConsole_dumpStackForWarnings:
335 *value =
OOJSValueFromBOOL([[OOJavaScriptEngine sharedEngine] dumpStackForWarnings]);
338 case kConsole_glVendorString:
342 case kConsole_glRendererString:
346 case kConsole_glFixedFunctionTextureUnitCount:
347 *value = INT_TO_JSVAL([[OOOpenGLExtensionManager sharedManager] textureUnitCount]);
350 case kConsole_glFragmentShaderTextureUnitCount:
351 *value = INT_TO_JSVAL([[OOOpenGLExtensionManager sharedManager] textureImageUnitCount]);
354#define DEBUG_FLAG_CASE(x) \
356 *value = INT_TO_JSVAL(x); \
370#undef DEBUG_FLAG_CASE
382static JSBool ConsoleSetProperty(JSContext* context, JSObject*
this, jsid propID, JSBool strict, jsval* value)
384 if (!JSID_IS_INT(propID))
393 switch (JSID_TO_INT(propID)) {
394#ifdef OO_DEBUG_MONITOR_ENABLED
395 case kConsole_debugFlags:
396 if (JS_ValueToInt32(context, *value, &iValue)) {
401 case kConsole_detailLevel:
404 [UNIVERSE setDetailLevel:OOGraphicsDetailFromString(sValue)];
408 case kConsole_displayFPS:
409 if (JS_ValueToBoolean(context, *value, &bValue)) {
410 [UNIVERSE setDisplayFPS:bValue];
414 case kConsole_pedanticMode:
415 if (JS_ValueToBoolean(context, *value, &bValue)) {
416 uint32_t options = JS_GetOptions(context);
418 options |= JSOPTION_STRICT;
420 options &= ~JSOPTION_STRICT;
422 JS_SetOptions(context, options);
426 case kConsole_ignoreDroppedPackets:
427 if (JS_ValueToBoolean(context, *value, &bValue)) {
432 case kConsole_showErrorLocations:
433 if (JS_ValueToBoolean(context, *value, &bValue)) {
438 case kConsole_dumpStackForErrors:
439 if (JS_ValueToBoolean(context, *value, &bValue)) {
444 case kConsole_dumpStackForWarnings:
445 if (JS_ValueToBoolean(context, *value, &bValue)) {
461static BOOL DoWeDefineAllDebugFlags(
enum OODebugFlags flags)
489static void ConsoleFinalize(JSContext* context, JSObject*
this)
493 [(id)JS_GetPrivate(context, this) release];
494 JS_SetPrivate(context,
this,
nil);
499static JSBool ConsoleSettingsDeleteProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value)
506 if (!JSID_IS_STRING(propID))
511 if (![monitor isKindOfClass:[OODebugMonitor
class]]) {
512 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class], __PRETTY_FUNCTION__,
@"This is an internal error, please report it.");
516 [monitor setConfigurationValue:nil forKey:key];
523static JSBool ConsoleSettingsGetProperty(JSContext* context, JSObject*
this, jsid propID, jsval* value)
525 if (!JSID_IS_STRING(propID))
531 id settingValue =
nil;
537 if (![monitor isKindOfClass:[OODebugMonitor
class]]) {
538 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class], __PRETTY_FUNCTION__,
@"This is an internal error, please report it.");
542 settingValue = [monitor configurationValueForKey:key];
543 if (settingValue != NULL)
544 *value = [settingValue oo_jsValueInContext:context];
553static JSBool ConsoleSettingsSetProperty(JSContext* context, JSObject*
this, jsid propID, JSBool strict, jsval* value)
555 if (!JSID_IS_STRING(propID))
561 id settingValue =
nil;
567 if (![monitor isKindOfClass:[OODebugMonitor
class]]) {
568 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class], __PRETTY_FUNCTION__,
@"This is an internal error, please report it.");
574 if (JSVAL_IS_NULL(*value) || JSVAL_IS_VOID(*value)) {
575 [monitor setConfigurationValue:nil forKey:key];
578 if (settingValue !=
nil) {
579 [monitor setConfigurationValue:settingValue forKey:key];
594static JSBool ConsoleConsoleMessage(JSContext* context, uintN argc, jsval* vp)
596 NSRange emphasisRange = { 0, 0 };
601 NSString *colorKey =
nil,
603 jsdouble location, length;
608 if (monitor ==
nil) {
609 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class], __PRETTY_FUNCTION__,
@"This is an internal error, please report it.");
621 if (JS_ValueToNumber(context,
OOJS_ARGV[2], &location) && JS_ValueToNumber(context,
OOJS_ARGV[3], &length)) {
622 emphasisRange = (NSRange) { location, length };
626 if (message ==
nil) {
627 if (colorKey ==
nil) {
628 OOJSReportWarning(context,
@"Console.consoleMessage() called with no parameters.");
631 colorKey =
@"command-result";
635 if (message !=
nil) {
636 [monitor appendJSConsoleLine:message
638 emphasisRange:emphasisRange];
648static JSBool ConsoleClearConsole(JSContext* context, uintN argc, jsval* vp)
655 if (![monitor isKindOfClass:[OODebugMonitor
class]]) {
656 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class], __PRETTY_FUNCTION__,
@"This is an internal error, please report it.");
660 [monitor clearJSConsole];
667static JSBool ConsoleScriptStack(JSContext* context, uintN argc, jsval* vp)
677static JSBool ConsoleInspectEntity(JSContext* context, uintN argc, jsval* vp)
681 Entity* entity =
nil;
685 if ([entity respondsToSelector:
@selector(inspect)]) {
698static JSBool ConsoleCallObjCMethod(JSContext* context, uintN argc, jsval* vp)
714 OK = OOJSCallObjCObjectMethod(context,
object, [
object oo_jsClassName], argc,
OOJS_ARGV, &result);
724static JSBool ConsoleSetUpCallObjC(JSContext* context, uintN argc, jsval* vp)
733 JSObject* obj = JSVAL_TO_OBJECT(
OOJS_ARGV[0]);
742static JSBool ConsoleIsExecutableJavaScript(JSContext* context, uintN argc, jsval* vp)
747 JSObject* target = NULL;
749 if (argc < 2 || !JS_ValueToObject(context,
OOJS_ARGV[0], &target) || !JSVAL_IS_STRING(
OOJS_ARGV[1])) {
758 NSData* stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
759 result = JS_BufferIsCompilableUnit(context, target, [stringData bytes], [stringData length]);
769static JSBool ConsoleDisplayMessagesInClass(JSContext* context, uintN argc, jsval* vp)
773 NSString* messageClass =
nil;
782static JSBool ConsoleSetDisplayMessagesInClass(JSContext* context, uintN argc, jsval* vp)
786 NSString* messageClass =
nil;
790 if (messageClass !=
nil && JS_ValueToBoolean(context,
OOJS_ARGV[1], &flag)) {
799static JSBool ConsoleWriteLogMarker(JSContext* context, uintN argc, jsval* vp)
810static JSBool ConsoleWriteMemoryStats(JSContext* context, uintN argc, jsval* vp)
824static JSBool ConsoleWriteJSMemoryStats(JSContext* context, uintN argc, jsval* vp)
838static JSBool ConsoleGarbageCollect(JSContext* context, uintN argc, jsval* vp)
842 uint32_t bytesBefore = JS_GetGCParameter(JS_GetRuntime(context), JSGC_BYTES);
844 uint32_t bytesAfter = JS_GetGCParameter(JS_GetRuntime(context), JSGC_BYTES);
846 OOJS_RETURN_OBJECT(([NSString stringWithFormat:
@"Bytes before: %u Bytes after: %u", bytesBefore, bytesAfter]));
858static void DumpCallback(
const char* name,
void* rp, JSGCRootType type,
void* datap)
860 assert(type == JS_GC_ROOT_VALUE_PTR || type == JS_GC_ROOT_GCTHING_PTR);
862 DumpCallbackData* data = datap;
864 const char* typeString =
"unknown type";
867 case JS_GC_ROOT_VALUE_PTR:
868 typeString =
"value";
872 case JS_GC_ROOT_GCTHING_PTR:
873 typeString =
"gc-thing";
874 value = OBJECT_TO_JSVAL(*(JSObject**)rp);
877 fprintf(data->file,
"%s @ %p (%s): %s\n", name, rp, typeString, [
OOJSDescribeValue(data->context, value, NO) UTF8String]);
880static JSBool ConsoleDumpNamedRoots(JSContext* context, uintN argc, jsval* vp)
884 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
888 FILE* file = fopen([path UTF8String],
"w");
890 DumpCallbackData data = {
894 JS_DumpNamedRoots(JS_GetRuntime(context), DumpCallback, &data);
905static JSBool ConsoleDumpHeap(JSContext* context, uintN argc, jsval* vp)
911 FILE* file = fopen([path UTF8String],
"w");
913 OK = JS_DumpHeap(context, file, NULL, 0, NULL, SIZE_MAX, NULL);
926static JSBool ConsoleProfile(JSContext* context, uintN argc, jsval* vp)
931 OOJSReportError(context,
@"Profiling functions may not be called while already profiling.");
935 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
936 OOTimeProfile* profile =
nil;
938 JSBool result = PerformProfiling(context,
@"profile", argc,
OOJS_ARGV, NULL, NO, &profile);
950static JSBool ConsoleGetProfile(JSContext* context, uintN argc, jsval* vp)
955 OOJSReportError(context,
@"Profiling functions may not be called while already profiling.");
959 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
960 OOTimeProfile* profile =
nil;
962 JSBool result = PerformProfiling(context,
@"getProfile", argc,
OOJS_ARGV, NULL, NO, &profile);
974static JSBool ConsoleTrace(JSContext* context, uintN argc, jsval* vp)
979 OOJSReportError(context,
@"Profiling functions may not be called while already profiling.");
983 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
986 JSBool result = PerformProfiling(context,
@"trace", argc,
OOJS_ARGV, &rval, YES, NULL);
997static JSBool PerformProfiling(JSContext* context, NSString* nominalFunction, uintN argc, jsval* argv, jsval* outRval, BOOL trace, OOTimeProfile** outProfile)
1000 jsval
function = argv[0];
1012 assert(JSVAL_IS_OBJECT(debugConsole) && !JSVAL_IS_NULL(debugConsole));
1013 JS_GetProperty(context, JSVAL_TO_OBJECT(debugConsole),
"script", &
this);
1017 if (!JS_ValueToObject(context,
this, &thisObj))
1021 if (outRval == NULL)
1026#define LONG_TIME (1e7)
1032 OOJSBeginProfiling(trace);
1035 BOOL result = JS_CallFunctionValue(context, thisObj,
function, 0, NULL, outRval);
1038 OOTimeProfile* profile = OOJSEndProfiling();
1039 if (outProfile != NULL)
1040 *outProfile = profile;
1046 JS_ReportPendingException(context);
@ DEBUG_SHADER_VALIDATION
@ DEBUG_NO_SHADER_FALLBACK
JSObject * DebugMonitorToJSConsole(JSContext *context, OODebugMonitor *monitor)
void OOJSConsoleDestroy(void)
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_EXIT_VOID
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_PROFILE_ENTER
void OOJSResetTimeLimiter(void)
OOTimeDelta OOJSGetTimeLimiterLimit(void)
void OOJSSetTimeLimiterLimit(OOTimeDelta limit)
BOOL JSValueToEntity(JSContext *context, jsval value, Entity **outEntity)
void OOJSPauseTimeLimiter(void)
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
void OOJSReportWarning(JSContext *context, NSString *format,...)
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
NSString * OOJSDescribeValue(JSContext *context, jsval value, BOOL abbreviateObjects)
void OOJSReportError(JSContext *context, NSString *format,...)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
NSString * OOStringFromJSValueEvenIfNull(JSContext *context, 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 OOJS_PROP_READONLY_CB
void OOJSResumeTimeLimiter(void)
#define OOJS_PROP_HIDDEN_READWRITE_CB
NSString * OOStringFromJSString(JSContext *context, JSString *string)
#define OOJS_METHOD_READONLY
id OOJSNativeObjectOfClassFromJSObject(JSContext *context, JSObject *object, Class requiredClass)
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
void OOLogSetDisplayMessagesInClass(NSString *inClass, BOOL inFlag)
void OOLogInsertMarker(void)
void dumpMemoryStatistics()
void setTCPIgnoresDroppedPackets:(BOOL flag)
size_t dumpJSMemoryStatistics()
OODebugMonitor * sharedDebugMonitor()
void setShowErrorLocations:(BOOL value)
OOJavaScriptEngine * sharedEngine()
OOGraphicsDetail maximumDetailLevel()
OOOpenGLExtensionManager * sharedManager()
NSString * diagnosticFileLocation()