29#ifdef OO_DEBUG_MONITOR_ENABLED
38#import "OOJSEngineTimeManagement.h"
46@interface Entity (OODebugInspector)
66static JSClassID sConsoleClassID;
67static JSClassID sConsoleSettingsClassID;
68static BOOL sPedanticMode = YES;
97static BOOL PerformProfiling(JSContext* context, NSString* nominalFunction,
int argc, JSValueConst* argv, JSValue* outRval, BOOL trace, OOTimeProfile** outProfile);
100static int ConsoleSettingsGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst
object, JSAtom propID);
101static int ConsoleSettingsDeleteProperty(JSContext* context, JSValueConst
object, JSAtom propID);
102static int ConsoleSettingsDefineOwnProperty(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter,
int flags);
103static JSValue ConsoleSettingsGetProperty(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst receiver);
104static int ConsoleSettingsSetProperty(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst value, JSValueConst receiver,
int flags);
106static JSClassDef sConsoleClass = {
107 .class_name =
"Console",
114 kConsole_detailLevel,
115 kConsole_maximumDetailLevel,
117 kConsole_platformDescription,
118 kConsole_ignoreDroppedPackets,
119 kConsole_pedanticMode,
120 kConsole_showErrorLocations,
121 kConsole_dumpStackForErrors,
122 kConsole_dumpStackForWarnings,
124 kConsole_glVendorString,
125 kConsole_glRendererString,
126 kConsole_glFixedFunctionTextureUnitCount,
127 kConsole_glFragmentShaderTextureUnitCount,
130 kConsole_DEBUG_LINKED_LISTS,
131 kConsole_DEBUG_COLLISIONS,
132 kConsole_DEBUG_DOCKING,
133 kConsole_DEBUG_OCTREE_LOGGING,
134 kConsole_DEBUG_BOUNDING_BOXES,
135 kConsole_DEBUG_OCTREE_DRAW,
136 kConsole_DEBUG_DRAW_NORMALS,
137 kConsole_DEBUG_NO_DUST,
138 kConsole_DEBUG_NO_SHADER_FALLBACK,
139 kConsole_DEBUG_SHADER_VALIDATION,
144#define OOJS_PROPERTY_HIDDEN_RW(name, getter, setter, magic) JS_CGETSET_MAGIC_DEF2(name, getter, setter, magic, OOJS_PROP_HIDDEN_READWRITE_CB)
146static const JSCFunctionListEntry sConsoleProperties[] = {
148 OOJS_PROPERTY_RW(
"debugFlags", ConsoleGetProperty, ConsoleSetProperty, kConsole_debugFlags),
149 OOJS_PROPERTY_RW(
"detailLevel", ConsoleGetProperty, ConsoleSetProperty, kConsole_detailLevel),
150 OOJS_PROPERTY_RO(
"maximumDetailLevel", ConsoleGetProperty, kConsole_maximumDetailLevel),
151 OOJS_PROPERTY_RW(
"displayFPS", ConsoleGetProperty, ConsoleSetProperty, kConsole_displayFPS),
152 OOJS_PROPERTY_RO(
"platformDescription", ConsoleGetProperty, kConsole_platformDescription),
153 OOJS_PROPERTY_RW(
"pedanticMode", ConsoleGetProperty, ConsoleSetProperty, kConsole_pedanticMode),
154 OOJS_PROPERTY_RW(
"ignoreDroppedPackets", ConsoleGetProperty, ConsoleSetProperty, kConsole_ignoreDroppedPackets),
155 OOJS_PROPERTY_HIDDEN_RW(
"__showErrorLocations", ConsoleGetProperty, ConsoleSetProperty, kConsole_showErrorLocations),
156 OOJS_PROPERTY_HIDDEN_RW(
"__dumpStackForErrors", ConsoleGetProperty, ConsoleSetProperty, kConsole_dumpStackForErrors),
157 OOJS_PROPERTY_HIDDEN_RW(
"__dumpStackForWarnings", ConsoleGetProperty, ConsoleSetProperty, kConsole_dumpStackForWarnings),
158 OOJS_PROPERTY_RO(
"glVendorString", ConsoleGetProperty, kConsole_glVendorString),
159 OOJS_PROPERTY_RO(
"glRendererString", ConsoleGetProperty, kConsole_glRendererString),
160 OOJS_PROPERTY_RO(
"glFixedFunctionTextureUnitCount", ConsoleGetProperty, kConsole_glFixedFunctionTextureUnitCount),
161 OOJS_PROPERTY_RO(
"glFragmentShaderTextureUnitCount", ConsoleGetProperty, kConsole_glFragmentShaderTextureUnitCount),
163#define DEBUG_FLAG_DECL(x) OOJS_PROPERTY_RO(#x, ConsoleGetProperty, kConsole_##x)
176#undef DEBUG_FLAG_DECL
179static const JSCFunctionListEntry sConsoleMethods[] = {
181 JS_CFUNC_DEF(
"consoleMessage", 2, ConsoleConsoleMessage),
182 JS_CFUNC_DEF(
"clearConsole", 0, ConsoleClearConsole),
183 JS_CFUNC_DEF(
"scriptStack", 0, ConsoleScriptStack),
184 JS_CFUNC_DEF(
"inspectEntity", 1, ConsoleInspectEntity),
186 JS_CFUNC_DEF(
"__setUpCallObjC", 1, ConsoleSetUpCallObjC),
188 JS_CFUNC_DEF(
"isExecutableJavaScript", 2, ConsoleIsExecutableJavaScript),
189 JS_CFUNC_DEF(
"displayMessagesInClass", 1, ConsoleDisplayMessagesInClass),
190 JS_CFUNC_DEF(
"setDisplayMessagesInClass", 2, ConsoleSetDisplayMessagesInClass),
191 JS_CFUNC_DEF(
"writeLogMarker", 0, ConsoleWriteLogMarker),
192 JS_CFUNC_DEF(
"writeMemoryStats", 0, ConsoleWriteMemoryStats),
193 JS_CFUNC_DEF(
"writeJSMemoryStats", 0, ConsoleWriteJSMemoryStats),
194 JS_CFUNC_DEF(
"garbageCollect", 0, ConsoleGarbageCollect),
196 JS_CFUNC_DEF(
"dumpHeap", 0, ConsoleDumpHeap),
199 JS_CFUNC_DEF(
"profile", 1, ConsoleProfile),
200 JS_CFUNC_DEF(
"getProfile", 1, ConsoleGetProfile),
201 JS_CFUNC_DEF(
"trace", 1, ConsoleTrace),
205static JSClassExoticMethods sConsoleSettingsExoticMethods = {
206 .get_own_property = ConsoleSettingsGetOwnProperty,
207 .delete_property = ConsoleSettingsDeleteProperty,
208 .define_own_property = ConsoleSettingsDefineOwnProperty,
209 .get_property = ConsoleSettingsGetProperty,
210 .set_property = ConsoleSettingsSetProperty,
213static JSClassDef sConsoleSettingsClass = {
214 .class_name =
"ConsoleSettings",
216 .exotic = &sConsoleSettingsExoticMethods,
219static void InitOOJSConsole(JSContext* context, JSValueConst global)
223 sConsoleProperties,
OOJS_COUNT(sConsoleProperties), sConsoleMethods,
OOJS_COUNT(sConsoleMethods), NULL, 0);
228 NULL, 0, NULL, 0, NULL, 0);
237 JSValue consolePrototype = (sConsoleClassID != 0) ? JS_GetClassProto(context, sConsoleClassID) : JS_NULL;
238 if (!JS_IsObject(consolePrototype)) {
239 JS_FreeValue(context, consolePrototype);
240 InitOOJSConsole(context, [[OOJavaScriptEngine sharedEngine] globalObject]);
241 consolePrototype = JS_GetClassProto(context, sConsoleClassID);
245 JSValue
object = JS_NewObjectProtoClass(context, consolePrototype, sConsoleClassID);
246 JS_FreeValue(context, consolePrototype);
247 if (JS_IsException(
object)) {
251 JS_SetOpaque(
object, [monitor weakRetain]);
254 JSValue settingsPrototype = JS_GetClassProto(context, sConsoleSettingsClassID);
255 JSValue settingsObject = JS_NewObjectProtoClass(context, settingsPrototype, sConsoleSettingsClassID);
256 JS_FreeValue(context, settingsPrototype);
257 if (JS_IsException(settingsObject)) {
259 JS_FreeValue(context,
object);
262 JS_SetOpaque(settingsObject, [monitor weakRetain]);
264 if (JS_SetPropertyStr(context,
object,
"settings", settingsObject) < 0) {
266 JS_FreeValue(context,
object);
275static OODebugMonitor* MonitorForConsoleObject(JSContext* context, JSValueConst
object,
const char*
function)
278 if (![monitor isKindOfClass:[OODebugMonitor
class]]) {
279 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class],
function,
@"This is an internal error, please report it.");
290#ifdef OO_DEBUG_MONITOR_ENABLED
291 case kConsole_debugFlags:
292 return JS_NewUint32(context, (uint32_t)
gDebugFlags);
295 case kConsole_detailLevel:
296 return [OOStringFromGraphicsDetail([UNIVERSE detailLevel]) oo_jsValueInContext:context];
298 case kConsole_maximumDetailLevel:
301 case kConsole_displayFPS:
304 case kConsole_platformDescription:
307 case kConsole_pedanticMode:
310 case kConsole_ignoreDroppedPackets:
311 return OOJSValueFromBOOL([[OODebugMonitor sharedDebugMonitor] TCPIgnoresDroppedPackets]);
313 case kConsole_showErrorLocations:
314 return OOJSValueFromBOOL([[OOJavaScriptEngine sharedEngine] showErrorLocations]);
316 case kConsole_dumpStackForErrors:
317 return OOJSValueFromBOOL([[OOJavaScriptEngine sharedEngine] dumpStackForErrors]);
319 case kConsole_dumpStackForWarnings:
320 return OOJSValueFromBOOL([[OOJavaScriptEngine sharedEngine] dumpStackForWarnings]);
322 case kConsole_glVendorString:
325 case kConsole_glRendererString:
328 case kConsole_glFixedFunctionTextureUnitCount:
329 return JS_NewInt32(context, [[OOOpenGLExtensionManager sharedManager] textureUnitCount]);
331 case kConsole_glFragmentShaderTextureUnitCount:
332 return JS_NewInt32(context, [[OOOpenGLExtensionManager sharedManager] textureImageUnitCount]);
334#define DEBUG_FLAG_CASE(x) \
336 return JS_NewInt32(context, x);
349#undef DEBUG_FLAG_CASE
367#ifdef OO_DEBUG_MONITOR_ENABLED
368 case kConsole_debugFlags:
374 case kConsole_detailLevel:
377 [UNIVERSE setDetailLevel:OOGraphicsDetailFromString(sValue)];
381 case kConsole_displayFPS:
382 [UNIVERSE setDisplayFPS:OOJSValueToBOOL(context, value)];
385 case kConsole_pedanticMode:
389 case kConsole_ignoreDroppedPackets:
393 case kConsole_showErrorLocations:
397 case kConsole_dumpStackForErrors:
401 case kConsole_dumpStackForWarnings:
416static BOOL DoWeDefineAllDebugFlags(
enum OODebugFlags flags)
451static BOOL IsSettingsPrototype(JSValueConst
object)
453 return JS_GetOpaque(
object, sConsoleSettingsClassID) == NULL;
457static NSString* SettingKeyForPropertyID(JSContext* context, JSAtom propID)
459 NSString* result =
nil;
460 JSValue idValue = JS_AtomToValue(context, propID);
461 if (JS_IsString(idValue)) {
464 JS_FreeValue(context, idValue);
472static int ConsoleSettingsGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst
object, JSAtom propID)
477static JSValue ConsoleSettingsGetProperty(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst receiver)
481 NSString* key = SettingKeyForPropertyID(context, propID);
482 if (key ==
nil || IsSettingsPrototype(
object)) {
484 JSValue prototype = JS_GetPrototype(context,
object);
485 JSValue result = JS_GetProperty(context, prototype, propID);
486 JS_FreeValue(context, prototype);
490 OODebugMonitor* monitor = MonitorForConsoleObject(context,
object, __PRETTY_FUNCTION__);
495 if (settingValue !=
nil)
496 return [settingValue oo_jsValueInContext:context];
502static int SetSetting(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst value)
504 NSString* key = SettingKeyForPropertyID(context, propID);
505 if (key ==
nil || IsSettingsPrototype(
object))
508 OODebugMonitor* monitor = MonitorForConsoleObject(context,
object, __PRETTY_FUNCTION__);
514 if (JS_IsNull(value) || JS_IsUndefined(value)) {
518 if (settingValue !=
nil) {
529static int ConsoleSettingsSetProperty(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst value, JSValueConst receiver,
int flags)
532 return SetSetting(context,
object, propID, value);
534 @catch (
id exception) {
541static int ConsoleSettingsDefineOwnProperty(JSContext* context, JSValueConst
object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter,
int flags)
544 if (!(flags & JS_PROP_HAS_VALUE))
546 return SetSetting(context,
object, propID, value);
548 @catch (
id exception) {
554static int ConsoleSettingsDeleteProperty(JSContext* context, JSValueConst
object, JSAtom propID)
557 NSString* key = SettingKeyForPropertyID(context, propID);
558 if (key ==
nil || IsSettingsPrototype(
object))
561 OODebugMonitor* monitor = MonitorForConsoleObject(context,
object, __PRETTY_FUNCTION__);
568 @catch (
id exception) {
579 NSRange emphasisRange = { 0, 0 };
584 NSString *colorKey =
nil,
586 double location, length;
591 if (monitor ==
nil) {
592 OOJSReportError(context,
@"Expected OODebugMonitor, got %@ in %s. %@", [monitor
class], __PRETTY_FUNCTION__,
@"This is an internal error, please report it.");
604 if (JS_ToFloat64(context, &location,
OOJS_ARGV[2]) == 0 && JS_ToFloat64(context, &length,
OOJS_ARGV[3]) == 0) {
605 emphasisRange = (NSRange) { location, length };
609 if (message ==
nil) {
610 if (colorKey ==
nil) {
611 OOJSReportWarning(context,
@"Console.consoleMessage() called with no parameters.");
614 colorKey =
@"command-result";
618 if (message !=
nil) {
619 [monitor appendJSConsoleLine:message
621 emphasisRange:emphasisRange];
635 OODebugMonitor* monitor = MonitorForConsoleObject(context,
OOJS_THIS, __PRETTY_FUNCTION__);
660 Entity* entity =
nil;
664 if ([entity respondsToSelector:
@selector(inspect)]) {
692 result = JS_UNDEFINED;
693 OK = OOJSCallObjCObjectMethod(context,
object, [
object oo_jsClassName], argc,
OOJS_ARGV, &result);
697 JS_FreeValue(context, result);
716 JSValue
function = JS_NewCFunction(context, ConsoleCallObjCMethod,
"callObjC", 1);
717 JS_DefinePropertyValueStr(context,
OOJS_ARGV[0],
"callObjC",
function, 0);
732static BOOL IsIncompleteSyntaxError(JSContext* context, JSValueConst exception, NSString* code)
734 if (!JS_IsError(exception))
737 JSValue nameVal = JS_GetPropertyStr(context, exception,
"name");
739 JS_FreeValue(context, nameVal);
740 if (![name isEqualToString:
@"SyntaxError"])
743 JSValue messageVal = JS_GetPropertyStr(context, exception,
"message");
745 JS_FreeValue(context, messageVal);
747 if ([message rangeOfString:
@"end of " options:NSCaseInsensitiveSearch].location != NSNotFound)
749 if ([message hasSuffix:
@"''"])
753 JSValue stackVal = JS_GetPropertyStr(context, exception,
"stack");
755 JS_FreeValue(context, stackVal);
757 NSArray* lines = [code componentsSeparatedByString:@"\n"];
758 NSString* endLocation = [NSString stringWithFormat:@"<console input>:%lu:%lu", (unsigned long)[lines count], (unsigned long)[[lines lastObject] length] + 1];
759 NSString* firstFrame = [[stack componentsSeparatedByString:@"\n"] firstObject];
760 return [firstFrame rangeOfString:endLocation].location != NSNotFound;
778 NSData* stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
779 NSMutableData* source = [NSMutableData dataWithData:stringData];
780 [source appendBytes:"" length:1];
782 JSValue compiled = JS_Eval(context, [source bytes], [stringData length],
"<console input>", JS_EVAL_TYPE_GLOBAL | JS_EVAL_FLAG_COMPILE_ONLY);
783 if (JS_IsException(compiled)) {
784 JSValue exception = JS_GetException(context);
785 result = !IsIncompleteSyntaxError(context, exception,
string);
786 JS_FreeValue(context, exception);
788 JS_FreeValue(context, compiled);
803 NSString* messageClass =
nil;
817 NSString* messageClass =
nil;
821 if (messageClass !=
nil && argc > 1) {
868static size_t JSHeapSize(JSRuntime* runtime)
871 JS_ComputeMemoryUsage(runtime, &usage);
872 return (
size_t)usage.malloc_size;
880 JSRuntime* runtime = JS_GetRuntime(context);
881 size_t bytesBefore = JSHeapSize(runtime);
883 size_t bytesAfter = JSHeapSize(runtime);
885 OOJS_RETURN_OBJECT(([NSString stringWithFormat:
@"Bytes before: %zu Bytes after: %zu", bytesBefore, bytesAfter]));
897 FILE* file = fopen([path UTF8String],
"w");
899 JSRuntime* runtime = JS_GetRuntime(context);
901 JS_ComputeMemoryUsage(runtime, &usage);
902 JS_DumpMemoryUsage(file, &usage, runtime);
921 OOJSReportError(context,
@"Profiling functions may not be called while already profiling.");
925 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
926 OOTimeProfile* profile =
nil;
927 JSValue result = JS_UNDEFINED;
929 if (PerformProfiling(context,
@"profile", argc,
OOJS_ARGV, NULL, NO, &profile)) {
947 OOJSReportError(context,
@"Profiling functions may not be called while already profiling.");
951 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
952 OOTimeProfile* profile =
nil;
953 JSValue result = JS_UNDEFINED;
955 if (PerformProfiling(context,
@"getProfile", argc,
OOJS_ARGV, NULL, NO, &profile)) {
973 OOJSReportError(context,
@"Profiling functions may not be called while already profiling.");
977 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
978 JSValue rval = JS_UNDEFINED;
981 if (PerformProfiling(context,
@"trace", argc,
OOJS_ARGV, &rval, YES, NULL)) {
997static BOOL PerformProfiling(JSContext* context, NSString* nominalFunction,
int argc, JSValueConst* argv, JSValue* outRval, BOOL trace, OOTimeProfile** outProfile)
1004 JSValueConst
function = argv[0];
1009 this = JS_DupValue(context, argv[1]);
1012 this = JS_GetPropertyStr(context, debugConsole,
"script");
1013 JS_FreeValue(context, debugConsole);
1018#define LONG_TIME (1e7)
1024 OOJSBeginProfiling(trace);
1027 JSValue rval = JS_Call(context,
function,
this, 0, NULL);
1030 OOTimeProfile* profile = OOJSEndProfiling();
1031 if (outProfile != NULL)
1032 *outProfile = profile;
1038 JS_FreeValue(context,
this);
1040 if (JS_IsException(rval)) {
1045 if (outRval != NULL)
1048 JS_FreeValue(context, rval);
@ DEBUG_SHADER_VALIDATION
@ DEBUG_NO_SHADER_FALLBACK
JSObject * DebugMonitorToJSConsole(JSContext *context, OODebugMonitor *monitor)
#define OOJS_PROFILE_EXIT
#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,...)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
OOINLINE BOOL OOJSValueToBOOL(JSContext *context, jsval value)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
OOINLINE BOOL OOJSValueToInt32(JSContext *context, jsval value, int32_t *outValue)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
OOINLINE void OOJSReportPendingException(JSContext *context)
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)
void OOJSResumeTimeLimiter(void)
id OOJSNativeObjectOfClassFromJSObject(JSContext *context, JSObject *object, Class requiredClass)
void OOJSReportWrappedException(JSContext *context, id exception)
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
void OOLogSetDisplayMessagesInClass(NSString *inClass, BOOL inFlag)
void OOLogInsertMarker(void)
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
#define OOJS_PROPERTY_RO(name, getter, magic)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_COUNT(array)
void dumpMemoryStatistics()
void setTCPIgnoresDroppedPackets:(BOOL flag)
size_t dumpJSMemoryStatistics()
OODebugMonitor * sharedDebugMonitor()
void setShowErrorLocations:(BOOL value)
OOJavaScriptEngine * sharedEngine()
OOGraphicsDetail maximumDetailLevel()
OOOpenGLExtensionManager * sharedManager()
NSString * diagnosticFileLocation()
id configurationValueForKey:(in NSString *key)
void setConfigurationValue:forKey:(in id value,[forKey] in NSString *key)