#include "cell.h" #define TRACY_ENABLE #include "TracyC.h" #define MAX_ZONE_DEPTH 256 typedef struct { TracyCZoneCtx zone_stack[MAX_ZONE_DEPTH]; int zone_depth; } tracy_context; static void tracy_hook(JSContext *js, int type, struct js_debug *dbg, void *user) { tracy_context *ctx = (tracy_context *)user; if (!ctx) return; if (type == JS_HOOK_CALL) { if (ctx->zone_depth >= MAX_ZONE_DEPTH) return; uint64_t srcloc = ___tracy_alloc_srcloc( dbg->line, dbg->filename ? dbg->filename : "", dbg->filename ? strlen(dbg->filename) : 9, dbg->name ? dbg->name : "", dbg->name ? strlen(dbg->name) : 11, 0 ); ctx->zone_stack[ctx->zone_depth] = ___tracy_emit_zone_begin_alloc(srcloc, 1); ___tracy_emit_zone_color(ctx->zone_stack[ctx->zone_depth], dbg->unique); ctx->zone_depth++; } else if (type == JS_HOOK_RET) { if (ctx->zone_depth <= 0) return; ctx->zone_depth--; ___tracy_emit_zone_end(ctx->zone_stack[ctx->zone_depth]); } } static void tracy_cell_hook(const char *name, int type) { printf("%s\n", name); #ifdef TRACY_FIBERS if (type == CELL_HOOK_ENTER) ___tracy_fiber_enter(name); else if (type == CELL_HOOK_EXIT) ___tracy_fiber_leave(); #endif } static JSValue js_tracy_init(JSContext *js, JSValue self, int argc, JSValue *argv) { tracy_context *ctx = malloc(sizeof(tracy_context)); if (!ctx) return JS_EXCEPTION; ctx->zone_depth = 0; memset(ctx->zone_stack, 0, sizeof(ctx->zone_stack)); js_debug_sethook(js, tracy_hook, JS_HOOK_CALL | JS_HOOK_RET, ctx); cell_trace_sethook(tracy_cell_hook); return JS_NULL; } static JSValue js_tracy_frame(JSContext *js, JSValue self, int argc, JSValue *argv) { ___tracy_emit_frame_mark(NULL); return JS_NULL; } static JSValue js_tracy_message(JSContext *js, JSValue self, int argc, JSValue *argv) { if (argc < 1) return JS_NULL; const char *msg = JS_ToCString(js, argv[0]); if (!msg) return JS_EXCEPTION; ___tracy_emit_messageL(msg, 0); JS_FreeCString(js, msg); return JS_NULL; } static JSValue js_tracy_image(JSContext *js, JSValue self, int argc, JSValue *argv) { if (argc < 3) return JS_NULL; size_t size; void *data = js_get_blob_data(js, &size, argv[0]); if (!data) return JS_EXCEPTION; uint16_t w = (uint16_t)js2number(js, argv[1]); uint16_t h = (uint16_t)js2number(js, argv[2]); uint8_t offset = argc > 3 ? (uint8_t)js2number(js, argv[3]) : 0; int32_t flip = argc > 4 ? js2bool(js, argv[4]) : 0; ___tracy_emit_frame_image(data, w, h, offset, flip); return JS_NULL; } static JSValue js_tracy_appinfo(JSContext *js, JSValue self, int argc, JSValue *argv) { if (argc < 1) return JS_NULL; const char *txt = JS_ToCString(js, argv[0]); if (!txt) return JS_EXCEPTION; ___tracy_emit_message_appinfo(txt, strlen(txt)); JS_FreeCString(js, txt); return JS_NULL; } static JSValue js_tracy_begin_sampling(JSContext *js, JSValue self, int argc, JSValue *argv) { // int result = ___tracy_begin_sampling_profiler(); // return JS_NewInt32(js, result); return JS_NULL; } static JSValue js_tracy_end_sampling(JSContext *js, JSValue self, int argc, JSValue *argv) { // ___tracy_end_sampling_profiler(); return JS_NULL; } static const JSCFunctionListEntry js_tracy_funcs[] = { JS_CFUNC_DEF("init", 0, js_tracy_init), JS_CFUNC_DEF("frame", 0, js_tracy_frame), JS_CFUNC_DEF("message", 1, js_tracy_message), JS_CFUNC_DEF("image", 1, js_tracy_image), JS_CFUNC_DEF("appinfo", 1, js_tracy_appinfo), JS_CFUNC_DEF("beginSampling", 0, js_tracy_begin_sampling), JS_CFUNC_DEF("endSampling", 0, js_tracy_end_sampling), }; CELL_USE_FUNCS(js_tracy_funcs)