Skip to content

[JULES] Refactor: Streamline standard library native function registration - #400

Closed
logbie wants to merge 1 commit into
mainfrom
refactor/define-native-api-14845541140646196026
Closed

[JULES] Refactor: Streamline standard library native function registration#400
logbie wants to merge 1 commit into
mainfrom
refactor/define-native-api-14845541140646196026

Conversation

@logbie

@logbie logbie commented Mar 11, 2026

Copy link
Copy Markdown
Collaborator

Summary of Changes

  • The Issue: The codebase contained significant redundancy during standard library initialization across src/stdlib/*.rs modules. Registering native functions required passing the name twice through a cumbersome wrapper: env.define("name", Value::NativeFunction("name", native_func)).
  • The Rational: Improved maintainability and DRY principle adherence. By consolidating this pattern into a single helper method, the codebase is cleaner, less prone to copy-paste typos where the string names might inadvertently mismatch, and easier to read.
  • The Solution: Implemented a new helper method define_native directly on the Environment struct in src/interpreter/environment.rs. Refactored all standard library registration functions (math, json, string, array, etc.) to use env.define_native("name", native_func).

Verification Checklist

  • cargo fmt executed and passed.
  • cargo clippy returned no warnings or errors.
  • All cargo test suites passed (100% success rate).

PR created automatically by Jules for task 14845541140646196026 started by @logbie


Open with Devin

Summary by CodeRabbit

  • Refactor
    • Simplified internal function registration mechanism across standard library modules for improved code maintainability.

Introduced a `define_native` helper method to the `Environment` struct to remove the repetitive `Value::NativeFunction(name, func)` boilerplate required when registering standard library functions. Updated all `src/stdlib/*.rs` files to utilize this new API.

Co-authored-by: logbie <1138960+logbie@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings March 11, 2026 09:24
@coderabbitai

coderabbitai Bot commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bda24045-8a76-4385-9036-e75bf6f4aaca

📥 Commits

Reviewing files that changed from the base of the PR and between 6a37448 and 3b36182.

📒 Files selected for processing (11)
  • src/interpreter/environment.rs
  • src/stdlib/core.rs
  • src/stdlib/crypto.rs
  • src/stdlib/filesystem.rs
  • src/stdlib/json.rs
  • src/stdlib/list.rs
  • src/stdlib/math.rs
  • src/stdlib/pattern.rs
  • src/stdlib/random.rs
  • src/stdlib/text.rs
  • src/stdlib/time.rs

📝 Walkthrough

Walkthrough

Introduces a new convenience method define_native to the Environment struct for registering native functions, eliminating manual wrapping with Value::NativeFunction. Updates all stdlib modules (core, crypto, filesystem, json, list, math, pattern, random, text, time) to use this simplified registration API.

Changes

Cohort / File(s) Summary
Environment API
src/interpreter/environment.rs
Adds public define_native method as a convenience wrapper that automatically wraps native functions in Value::NativeFunction and delegates to existing define method.
Core Stdlib
src/stdlib/core.rs
Updates 5 core native functions (print, typeof, isnothing, type_of, is_nothing) to register via define_native instead of explicit Value::NativeFunction wrapping.
Crypto Stdlib
src/stdlib/crypto.rs
Replaces manual Value::NativeFunction wrappers with define_native calls for 5 crypto functions (wflhash256, wflhash512, wflhash256_with_salt, wflmac256, generate_csrf_token).
Filesystem Stdlib
src/stdlib/filesystem.rs
Updates 19 filesystem functions (list_dir, glob, rglob, path_join, path_basename, path_dirname, makedirs, file_mtime, path_exists, is_file, is_dir, count_lines, path_extension, path_stem, file_size, copy_file, move_file, remove_file, remove_dir) to use define_native.
JSON Stdlib
src/stdlib/json.rs
Switches 3 JSON functions (parse_json, stringify_json, stringify_json_pretty) to define_native registration.
List Stdlib
src/stdlib/list.rs
Converts all list function registrations (length, push, pop, contains, indexof, shift, unshift, remove_at, insert_at, clear, slice, concat, join, unique, count, size, fill, sort, reverse_list, find, find_index, every, some) from manual wrapping to define_native.
Math Stdlib
src/stdlib/math.rs
Updates all math native functions to use define_native instead of explicit Value::NativeFunction construction.
Pattern Stdlib
src/stdlib/pattern.rs
Replaces Value::NativeFunction wrapping with define_native for 3 pattern functions (pattern_matches, pattern_find, pattern_find_all).
Random Stdlib
src/stdlib/random.rs
Converts 7 random functions (random, random_between, random_int, random_boolean, random_from, random_seed, generate_uuid) to define_native registration.
Text Stdlib
src/stdlib/text.rs
Updates 23 text functions (touppercase, tolowercase, substring, string_split, to_uppercase, to_lowercase, trim, starts_with, ends_with, split, startswith, endswith, replace, last_index_of, lastindexof, padleft, padright, capitalize, reverse, reverse_text, parse_query_string, parse_cookies, parse_form_urlencoded) to use define_native.
Time Stdlib
src/stdlib/time.rs
Replaces manual wrapping with define_native calls for 13 time functions (today, now, datetime_now, format_date, format_time, format_datetime, parse_date, parse_time, create_time, create_date, add_days, days_between, current_date).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 Thump thump! Our stdlib now hops with grace,
With define_native brightening the place—
No more wrapping, just names and their kin,
Seventeen files sing cleaner within!
Simpler paths for the functions so keen,
The sweetest refactor we've ever seen! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 53.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: refactoring the standard library to use a new streamlined native function registration method.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/define-native-api-14845541140646196026

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors standard library initialization by introducing a dedicated Environment::define_native helper to register native functions without repeating the function name string, reducing boilerplate and avoiding name mismatch typos.

Changes:

  • Added Environment::define_native(name, func) as a wrapper around define(name, Value::NativeFunction(name, func)).
  • Refactored multiple src/stdlib/*.rs registration functions to use define_native instead of manually constructing Value::NativeFunction.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/interpreter/environment.rs Adds define_native helper on Environment to centralize native function registration.
src/stdlib/core.rs Updates core stdlib function registration to use define_native.
src/stdlib/crypto.rs Updates crypto stdlib function registration to use define_native.
src/stdlib/filesystem.rs Updates filesystem stdlib function registration to use define_native.
src/stdlib/json.rs Updates JSON stdlib function registration to use define_native.
src/stdlib/list.rs Updates list stdlib function registration to use define_native.
src/stdlib/math.rs Updates math stdlib function registration to use define_native.
src/stdlib/pattern.rs Updates pattern stdlib function registration to use define_native.
src/stdlib/random.rs Updates random stdlib function registration to use define_native.
src/stdlib/text.rs Updates text stdlib function registration to use define_native.
src/stdlib/time.rs Updates time stdlib function registration to use define_native.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@logbie

logbie commented Mar 27, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: bot-generated PR, cleaning up duplicates.

@logbie logbie closed this Mar 27, 2026
@logbie
logbie deleted the refactor/define-native-api-14845541140646196026 branch June 19, 2026 04:08
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