execute method

  1. @override
Future execute(
  1. String script
)
override

Execute JavaScript code and return the result

Implementation

@override
Future<dynamic> execute(String script) async {
  var processedScript = script;
  try {
    // Handle large scripts
    if (processedScript.length > maxScriptSize) {
      if (truncateLargeScripts) {
        _log.warning(
            'JavaScript script truncated from ${processedScript.length} to $maxScriptSize bytes');
        processedScript = processedScript.substring(0, maxScriptSize);
      } else {
        _log.warning(
            'JavaScript script too large: ${processedScript.length} bytes (max: $maxScriptSize)');
        return null;
      }
    }

    // Use existing runtime (reset should be called once per QueryString.execute())
    final runtime = _getRuntime();

    JsEvalResult result;
    try {
      result = runtime.evaluate(processedScript);
    } catch (e) {
      _log.warning('Runtime evaluation crashed: $e');
      // Reset runtime on crash
      _resetRuntime();
      return null;
    }

    if (result.isError) {
      _log.warning('JavaScript execution error: ${result.stringResult}');
      return null;
    }
    return result.stringResult;
  } catch (e) {
    _log.warning('Failed to execute JavaScript: $e');
    // Reset runtime on any error
    _resetRuntime();
    return null;
  }
}