Skip to content

Refactor JNI lifecycle and log formatting into reusable helpers#176

Merged
bernardladenthin merged 4 commits into
mainfrom
claude/nice-ptolemy-5kVv8
May 22, 2026
Merged

Refactor JNI lifecycle and log formatting into reusable helpers#176
bernardladenthin merged 4 commits into
mainfrom
claude/nice-ptolemy-5kVv8

Conversation

@bernardladenthin
Copy link
Copy Markdown
Owner

Summary

  • Extract log formatting logic (format_log_as_json, log_level_name) into a new log_helpers.hpp header for reusability and testability
  • Consolidate JNI global reference lifecycle management using lookup tables in JNI_OnLoad and JNI_OnUnload, eliminating repetitive boilerplate
  • Add helper functions erase_reader() and require_embedding_support() to jni_helpers.hpp to reduce code duplication across JNI entry points
  • Update log_callback_trampoline to accept explicit timestamp parameter for deterministic testing

Test plan

  • Added comprehensive unit tests for log_helpers.hpp (test_log_helpers.cpp) covering all log levels, null handling, and JSON structure
  • Added unit tests for new erase_reader() and require_embedding_support() helpers in test_jni_helpers.cpp
  • Existing JNI tests continue to pass with refactored code paths
  • CI validates all changes

Related issues / PRs

None

Checklist

  • I have read CONTRIBUTING.md and CODE_OF_CONDUCT.md
  • My commits follow Conventional Commits
  • No security-sensitive changes

https://claude.ai/code/session_01TBELeBieKqvMwp1Ez8HgSk

claude added 4 commits May 22, 2026 06:27
Pulls log_level_name() and a deterministic format_log_as_json(level, text, ts)
out of jllama.cpp into a new pure header with zero JNI/llama dependencies.
Adds 13 GoogleTest cases covering level mapping, JSON shape, null text, and
explicit timestamp handling.

https://claude.ai/code/session_01TBELeBieKqvMwp1Ez8HgSk
Replaces four identical lock_guard + readers.erase() pairs in jllama.cpp
(releaseTask, receiveCompletionJson x2, cancelCompletion) with a single
inline helper in jni_helpers.hpp. Adds 3 unit tests covering present,
missing, and selective-id removal.

https://claude.ai/code/session_01TBELeBieKqvMwp1Ez8HgSk
Replaces two identical 'if (!jctx->params.embedding) ThrowNew(...) return'
blocks in embed() and handleEmbeddings() with a single helper in
jni_helpers.hpp. Adds 2 unit tests using the existing mock JNI fixture.

https://claude.ai/code/session_01TBELeBieKqvMwp1Ez8HgSk
Replaces 14 hand-written NewGlobalRef + 7 GetStaticObjectField + 7
NewGlobalRef + 21 DeleteGlobalRef calls with iteration over three small
tables: g_global_class_refs, g_global_object_refs, g_static_object_bindings.
Adding a new global ref is now a single-line table edit instead of
touching four separate places.

https://claude.ai/code/session_01TBELeBieKqvMwp1Ez8HgSk
@bernardladenthin bernardladenthin merged commit fc55802 into main May 22, 2026
6 of 9 checks passed
@bernardladenthin bernardladenthin deleted the claude/nice-ptolemy-5kVv8 branch May 22, 2026 07:22
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants