Skip to content

Add addStaticIndexMetaMethod and addStaticNewIndexMetaMethod to Class#210

Merged
kunitoki merged 5 commits intomasterfrom
copilot/add-support-for-static-index-methods
Mar 28, 2026
Merged

Add addStaticIndexMetaMethod and addStaticNewIndexMetaMethod to Class#210
kunitoki merged 5 commits intomasterfrom
copilot/add-support-for-static-index-methods

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

Extends the __index/__newindex metamethod fallback system to the static class table (MyClass.key), which previously had no interception mechanism. Instance-level addIndexMetaMethod/addNewIndexMetaMethod were unaffected.

New API

  • addStaticIndexMetaMethod(fn) — intercepts unknown key reads on the static class table; callback signature (const LuaRef& key, lua_State*) -> LuaRef (no self)
  • addStaticNewIndexMetaMethod(fn) — intercepts unknown key writes; callback signature (const LuaRef& key, const LuaRef& value, lua_State*) -> LuaRef

Both accept a raw function pointer or any callable (lambda with captures, functor).

std::unordered_map<std::string, int> store;

luabridge::getGlobalNamespace(L)
    .beginClass<MyClass>("MyClass")
        .addStaticIndexMetaMethod([&store](const luabridge::LuaRef& key, lua_State* L) -> luabridge::LuaRef {
            auto it = store.find(key.tostring());
            return it != store.end() ? luabridge::LuaRef(L, it->second) : luabridge::LuaRef(L);
        })
        .addStaticNewIndexMetaMethod([&store](const luabridge::LuaRef& key, const luabridge::LuaRef& value, lua_State*) -> luabridge::LuaRef {
            if (value.isNumber()) store[key.tostring()] = value.unsafe_cast<int>();
            return value;
        })
    .endClass();
// Lua: MyClass.x = 42  →  print(MyClass.x)  --> 42

Implementation

  • ClassInfo.hgetStaticIndexFallbackKey() / getStaticNewIndexFallbackKey() registry keys, separate from instance fallback keys
  • CFunctions.htry_call_static_index_fallback() / try_call_static_newindex_fallback(); wired into index_metamethod<false> and newindex_metamethod<false>
  • Namespace.haddStaticIndexMetaMethod / addStaticNewIndexMetaMethod on Class<T>, each with functor and function-pointer overloads
  • Distribution/LuaBridge.h — amalgamated header updated

Fallback priority

The fallback is consulted first. If it returns nil, normal lookup continues and registered static properties/functions are found as usual. A non-nil return overrides any same-named registered property.

Tests & docs

  • 10 tests in ClassExtensibleTests covering both callable forms, nil fall-through to real properties/functions, priority shadowing, standalone newindex, and combined usage
  • Manual.md §2.7.3 added with usage examples and priority semantics

⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Agent-Logs-Url: https://github.com/kunitoki/LuaBridge3/sessions/0b371b0f-d95d-4e8a-9513-8514e6daa460

Co-authored-by: kunitoki <707032+kunitoki@users.noreply.github.com>
Copy link
Copy Markdown
Owner

@kunitoki kunitoki left a comment

Choose a reason for hiding this comment

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

these changes are unrelated to the requested change

Copilot AI changed the title [WIP] Add support for addStaticIndexMetamethod and addStaticNewIndexMetamethod Add addStaticIndexMetaMethod and addStaticNewIndexMetaMethod to Class<T> Mar 27, 2026
Copilot AI requested a review from kunitoki March 27, 2026 21:54
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 27, 2026

these changes are unrelated to the requested change

The accidentally committed Build/ directory artifacts have been removed in commit 6126934. The PR now only contains the relevant source changes.

Copy link
Copy Markdown
Owner

@kunitoki kunitoki left a comment

Choose a reason for hiding this comment

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

@copilot Improvements to do:

  • make sure test coverage is high on newly added features
  • update Manual.md with usage and examples

Copilot AI changed the title Add addStaticIndexMetaMethod and addStaticNewIndexMetaMethod to Class<T> Add addStaticIndexMetaMethod and addStaticNewIndexMetaMethod to Class Mar 28, 2026
Copilot AI requested a review from kunitoki March 28, 2026 08:33
Copy link
Copy Markdown
Owner

@kunitoki kunitoki left a comment

Choose a reason for hiding this comment

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

Looks great !

@kunitoki kunitoki marked this pull request as ready for review March 28, 2026 13:02
@kunitoki kunitoki merged commit 7c25e3f into master Mar 28, 2026
33 checks passed
@kunitoki kunitoki deleted the copilot/add-support-for-static-index-methods branch March 28, 2026 13:11
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.

Add support for addStaticIndexMetamethod and addStaticNewIndexMetamethod

2 participants