Skip to content

add keyword arg support to callMethod directly - #9403

Merged
headius merged 13 commits into
jruby:masterfrom
drzaiusx11:add_keyword_arg_support_to_call_method
Aug 21, 2026
Merged

headius merged 13 commits into
jruby:masterfrom
drzaiusx11:add_keyword_arg_support_to_call_method

Conversation

@drzaiusx11

Copy link
Copy Markdown
Contributor

started a discussion here: #9402, copying verbatim here for reference:

I've been using JRuby to wrap some pre-existing Gems to be called by Java using the ScriptingContainer interface provided by JRuby 10.x, however, whenever a Gem uses keyword arguments there doesn't seem to be a clean way to force a Map to be treated as the keyword arguments for that Ruby API. I see theres a ThreadContext.CALL_KEYWORD flag I can use, but it seems more an internal implementation detail than something that should be exposed to scripting users. Afaict this works:

  // Manually build a RubyHash with symbol keys
  RubyHash kwargs = RubyHash.newHash(runtime);
  kwargs.fastASet(runtime.newSymbol("name"), JavaUtil.convertJavaToUsableRubyObject(runtime, "World"));

  // Set the flag right before callMethod
  context.callInfo = ThreadContext.CALL_KEYWORD;
  container.callMethod(receiver, "greet", new Object[]{kwargs});

What I'm proposing is adding further callMethod method overloads to include a last argument of Map<String, Object> or similar that will become those keyword arguments (convert strings to symbols automatically, call convertJavaToUsableRubyObject() on values, etc. My guess on why this hasn't come up before is that any scripting use cases predated Ruby's 3.2 order argument changes around auto-conversion of the last hash into keyword args. Since 3.2+ that behavior is disallowed so some other mechanism needs to exist going forward. I think what I propose makes sense here, but i could be missing something important.

This is my attempt at implementing it

@enebo

enebo commented May 2, 2026

Copy link
Copy Markdown
Member

I like this and I also like the PR only pushes this into embedding API itself. We have a long running task to remove callInfo and to make kwargs more first class and not based on some out-of-band field set per thread.

My only thing I wonder about is that Map<String, Object> would somehow be nicer if it could be String or RubySymbol for people who already know they are directly interacting with Ruby from an embedded context. Not sure I am recommending another set of overloads here or whether that case is solved by our larger effort to rewrite kwargs but it seems like a reasonable use case (also it probably could be added as a later PR).

@headius

headius commented May 6, 2026

Copy link
Copy Markdown
Member

Yes, this seems like a reasonable addition.

@enebo To your point, we could use Map<CharSequence, Object> since RubySymbol and RubyString also implement CharSequence. Dunno if it's worth it or not.

I'll review.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally on the right track. I think it's worth discussing whether it's safer/cleaner to just add new method names for the kwargs calls rather than more complicated overloads that might be ambiguous in some cases.

Comment thread core/src/main/java/org/jruby/embed/internal/EmbedRubyObjectAdapterImpl.java Outdated
Jeff Rousseau added 3 commits June 8, 2026 22:06
…ub.com/drzaiusx11/jruby into add_keyword_arg_support_to_call_method

# Conflicts:
#	core/src/main/java/org/jruby/embed/EmbedRubyObjectAdapter.java
#	core/src/main/java/org/jruby/embed/ScriptingContainer.java
#	core/src/main/java/org/jruby/embed/internal/EmbedRubyObjectAdapterImpl.java
#	core/src/test/java/org/jruby/embed/ScriptingContainerTest.java
@drzaiusx11

drzaiusx11 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

ok pushed the updates

  • renamed keyword-specific callMethod(...) overloads to a new API: callMethodWIthKeywordArgs(...)
  • restores context.callInfo to original value in finally block

I'm also purposefully avoiding using 'Object...' for now in the new APIs, opting for arrays to avoid the inevitable disambiguation issues discussed.

We can add N-arg positional overloads for N...4 or something sane if thats more ergonomic but I think arrays isn't a terrible first pass

Comment thread core/src/main/java/org/jruby/embed/internal/EmbedRubyObjectAdapterImpl.java Outdated
Comment thread core/src/main/java/org/jruby/embed/EmbedRubyObjectAdapter.java
@drzaiusx11

Copy link
Copy Markdown
Contributor Author

Ready for re-review @enebo @headius

@headius

headius commented Aug 13, 2026

Copy link
Copy Markdown
Member

Hey I missed the re-review but I'll take a look tonight.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one small change (same change in several places) needed and this can merge for 10.1.2.0.

Comment thread core/src/main/java/org/jruby/embed/EmbedRubyObjectAdapter.java Outdated
@drzaiusx11
drzaiusx11 requested a review from headius August 20, 2026 22:48
@headius
headius enabled auto-merge August 21, 2026 01:20
@headius
headius merged commit fb650c1 into jruby:master Aug 21, 2026
100 of 109 checks passed
@headius

headius commented Aug 21, 2026

Copy link
Copy Markdown
Member

Thank you for your help on this!

I am not sure if we've ever chatted about your use of JRuby, but I wanted to mention this in case it works for your use of JRuby:

My work on JRuby is now 100% funded by support contracts from Headius Enterprises. We offer customizable packages for JRuby users, with the middle-tier Expert Support being the most popular. Expert Support gives you a couple of dedicated hours of my time per month, prioritized bug fixes, security SLAs, and a whole lot of peace-of-mind that your application will keep running and getting better. We also offer a baseline Pro Support level on a per-seat basis and higher levels of support for codebases under NDA, performance profiling, debugging help, and more.

If you have a mission-critical use of JRuby, I'd recommend the Expert Support level, and I'd love to jump on a call to talk about how we can support you. Please consider partnering with us to keep your app running well and the JRuby community moving forward!

JRuby Support Services by Headius Enterprises

@headius headius added this to the JRuby 10.1.2.0 milestone Aug 21, 2026
@drzaiusx11
drzaiusx11 deleted the add_keyword_arg_support_to_call_method branch August 25, 2026 02:44
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.

3 participants