/* * w3m JavaScript Integration Implementation * * Core JavaScript functionality for w3m using QuickJS engine. * This file implements the main JavaScript context management, * script execution, and buffer integration. */ #include "fm.h" #ifdef USE_JAVASCRIPT #include "w3m_javascript.h" #include "w3m_dom.h" #include "w3m_events.h" #include #include /* Global JavaScript configuration */ int w3m_js_enabled = 0; int w3m_js_timeout = 5000; /* 5 second timeout */ int w3m_js_memory_limit = 8388608; /* 8MB memory limit */ int w3m_js_network_enabled = 1; /* JavaScript Context Management */ W3MJSContext * w3m_js_create_context(void) { W3MJSContext *ctx = GC_MALLOC(sizeof(W3MJSContext)); if (!ctx) return NULL; ctx->runtime = JS_NewRuntime(); if (!ctx->runtime) { GC_free(ctx); return NULL; } /* Set memory limit */ JS_SetMemoryLimit(ctx->runtime, w3m_js_memory_limit); ctx->context = JS_NewContext(ctx->runtime); if (!ctx->context) { JS_FreeRuntime(ctx->runtime); GC_free(ctx); return NULL; } /* Get global object */ ctx->global_obj = JS_GetGlobalObject(ctx->context); /* Initialize empty document and window objects */ ctx->document_obj = JS_NULL; ctx->window_obj = JS_NULL; ctx->memory_limit = w3m_js_memory_limit; ctx->execution_timeout = w3m_js_timeout; return ctx; } void w3m_js_destroy_context(W3MJSContext *ctx) { if (!ctx) return; if (!JS_IsNull(ctx->global_obj)) JS_FreeValue(ctx->context, ctx->global_obj); if (!JS_IsNull(ctx->document_obj)) JS_FreeValue(ctx->context, ctx->document_obj); if (!JS_IsNull(ctx->window_obj)) JS_FreeValue(ctx->context, ctx->window_obj); if (ctx->context) JS_FreeContext(ctx->context); if (ctx->runtime) JS_FreeRuntime(ctx->runtime); GC_free(ctx); } int w3m_js_execute_script(W3MJSContext *ctx, const char *script, const char *filename) { if (!ctx || !script) return 0; JSValue result = JS_Eval(ctx->context, script, strlen(script), filename ? filename : "