Skip to content

Fix #13: math: Use a math.* namespace#30

Open
leftibot wants to merge 5 commits intoChaiScript:masterfrom
leftibot:fix/issue-13-math-use-a-math-namespace
Open

Fix #13: math: Use a math.* namespace#30
leftibot wants to merge 5 commits intoChaiScript:masterfrom
leftibot:fix/issue-13-math-use-a-math-namespace

Conversation

@leftibot
Copy link
Copy Markdown
Contributor

Automated fix by @leftibot.

What changed

Fix #13: Add math.* namespace for Lua-style access to math functions
Register all math functions under a math namespace object using
ChaiScript's Dynamic_Object system via Module::eval. Functions remain
accessible as bare names (e.g., cos(0.5)) for backward compatibility,
and are now also accessible as math.cos(0.5), similar to Lua's math
library. Also fixes Catch2 SIGSTKSZ compatibility with newer glibc.
Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

Files

 include/chaiscript/extras/math.hpp | 114 +++++++++++++++++++++++++++++++++++++
 tests/catch.hpp                    |   7 ++-
 tests/math.cpp                     |  31 ++++++++++
 3 files changed, 149 insertions(+), 3 deletions(-)

Closes #13

Triggered by @lefticus.

…functions

Register all math functions under a `math` namespace object using
ChaiScript's Dynamic_Object system via Module::eval. Functions remain
accessible as bare names (e.g., `cos(0.5)`) for backward compatibility,
and are now also accessible as `math.cos(0.5)`, similar to Lua's math
library. Also fixes Catch2 SIGSTKSZ compatibility with newer glibc.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@lefticus
Copy link
Copy Markdown
Member

@leftibot update this to actually use namespaces instead of some global math object

Upgrade ChaiScript from 5.8.6 to 6.1.0 to use its native namespace
support. Replace the Dynamic_Object/math_type class hack with proper
namespace("math") + function assignment, which is the idiomatic way
to create namespaces in ChaiScript 6.x. Update test files for the
6.x ChaiScript constructor API (stdlib is now included by default).

Requested by @lefticus in PR ChaiScript#30 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

23008f2 Address review: Use ChaiScript namespaces instead of global math object

Copy link
Copy Markdown
Contributor

@RobLoach RobLoach left a comment

Choose a reason for hiding this comment

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

Would this be better as a full math namespace rather than function aliases?

Comment thread include/chaiscript/extras/math.hpp Outdated
Comment thread include/chaiscript/extras/math.hpp Outdated
isunordered<bool, long double, long double>(m);

// Create math namespace
m->eval("namespace(\"math\")");
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.

May be good to document the libraries available in the readme.

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.

@leftibot create/update readme to track the capabilities of this project

Replace the script-level namespace("math") + lambda-wrapping hack with
chai.register_namespace(), ChaiScript's native namespace API. Functions
are now attached directly as Namespace attributes instead of being
wrapped in ChaiScript functions that forward to globals. The namespace
is registered lazily so its contents are only built when import("math")
is called.

Requested by @lefticus in PR ChaiScript#30 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

022c611 Address review: Use register_namespace for real math namespace

@lefticus
Copy link
Copy Markdown
Member

@RobLoach I think @leftibot made that change as you were requesting it. @leftibot can you verify that this changeset does use full namespaces now, and no function aliases or other things?

Expand the README to track all modules and their capabilities:
- Document both math APIs: bootstrap() for flat globals and
  bootstrap_namespace() for math.* namespace access.
- List every math function by category, marking advanced functions
  gated by CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED.
- Flesh out the string_methods section with the full method list.
- Document the types and operators registered by string_id.
- Add a short build/test section.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@leftibot
Copy link
Copy Markdown
Contributor Author

Updated the README.md to track the project's capabilities. New commit:

aa93761 Document capabilities in README

The expanded README now covers:

  • Math — both APIs side-by-side: bootstrap() for flat globals (e.g. cos(0.5), with float/double/long double overloads) and the new bootstrap_namespace() for math.cos(0.5)-style access. All <cmath> functions are listed by category (trig, hyperbolic, exp/log, power, rounding, classification, etc.), with the (advanced) set clearly marked so users know what CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED turns off.
  • String Methods — full method table (replace, trim/trimStart/trimEnd, split, toLowerCase, toUpperCase, includes) with usage examples, plus the StringVector registration note that's needed for indexing into split() results.
  • String ID — documents the registered types (default_database, basic_database, string_id, string_info) and the ==/!= overloads.
  • A short build/test section covering the CMake + CTest flow.

If you'd rather keep this README change in a separate PR from the math-namespace work, I can split it out — let me know.

Copy link
Copy Markdown
Contributor

@RobLoach RobLoach left a comment

Choose a reason for hiding this comment

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

I think the original functions are still there? I could be wrong though.

math["isgreater"] = chaiscript::var(chaiscript::fun([](double x, double y){ return std::isgreater(x, y); }));
math["isgreaterequal"] = chaiscript::var(chaiscript::fun([](double x, double y){ return std::isgreaterequal(x, y); }));
math["isless"] = chaiscript::var(chaiscript::fun([](double x, double y){ return std::isless(x, y); }));
math["islessequal"] = chaiscript::var(chaiscript::fun([](double x, double y){ return std::islessequal(x, y); }));
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.

Since these now live in the math namespace, would it make sense to remove the original ones.

@leftibot
Copy link
Copy Markdown
Contributor Author

Already done in this PR — see commit aa93761 ("Document capabilities in README"). The expanded README.md covers:

  • Math — both APIs side-by-side: bootstrap() for flat globals (with float/double/long double overloads) and bootstrap_namespace() for math.cos(0.5)-style access (double-precision only). Every <cmath> function is listed by category (trig, hyperbolic, exp/log, power, rounding, classification, comparison, ...), with the (advanced) set clearly marked so it's obvious what CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED turns off.
  • String Methods — full method table (replace for both string and char overloads, trim/trimStart/trimEnd, split, toLowerCase/toUpperCase, includes) with usage examples, plus the StringVector registration line that's needed if you want to index into split() results (matches what tests/string_methods.cpp does).
  • String ID — registered types (default_database, basic_database under FOONATHAN_STRING_ID_DATABASE, string_id, string_info), the ==/!= overloads against both string_id and hash_type, and the string -> string_info type conversion.
  • A short build/test section covering the CMake + CTest flow.

If you'd prefer the README change land separately from the math-namespace work in this PR, say the word and I'll split it into its own PR.

@lefticus
Copy link
Copy Markdown
Member

@leftibot resolve conflicts

…se-a-math-namespace

# Conflicts:
#	tests/catch.hpp
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

cbc737f Merge remote-tracking branch 'origin/master' into fix/issue-13-math-use-a-math-namespace
fbe744e Fix #26: Update to ChaiScript 6.1.0 and fix vector_type API (#29)

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.

math: Use a math.* namespace

3 participants