Skip to content

Support loading compiled LUA modules - #262

Merged
joergbrech merged 5 commits into
mainfrom
plugin-loading-primitives
Jul 24, 2026
Merged

joergbrech merged 5 commits into
mainfrom
plugin-loading-primitives

Conversation

@joergbrech

Copy link
Copy Markdown
Collaborator

Summary

Fixes #261.

Adds primitives to grunk::state for loading a compiled Lua plugin (e.g. a SWIG-generated module) and integrating it into grunk's dynamic type system, plus a core fix these primitives depend on. This is the first (compiled-Lua) backend of a planned plugin contract that will also cover pure-C++ and pure-Lua-script plugins (see #235) — those aren't implemented here.

Also included: a small pre-existing, previously-unmerged register_type/modify_type change needed by geoml/OCCT-style types.

Changes

Core fix: recursive decoration

create_decorated_environment's __index only ever decorated one level below original_env: a type registered flat (e.g. gp_Pnt) got dependency tracking, but the same type nested one level deeper (e.g. inside a namespace/plugin table) silently fell back to raw, untracked calls. Replaced the duplicated one-level logic with decorate_value/decorate_table, a small mutually-recursive
pair that applies the same decoration rule at any depth. Top-level caching and the globals fallback are preserved exactly as before.

Compiled-plugin loading

  • state::load_module(name, open_fn) — wraps luaL_requiref to load a compiled Lua C extension into the state's interpreter.
  • state::decorate_module_functions (now public — it already existed internally for run_module_script) — turns any plain Lua function found in a table into a grunk-tracked function_meta.
  • external_type_proxy + state::register_external_type — bridges a class whose constructor/methods live behind foreign metatables (as SWIG-Lua's binding does) by probing one instance and capturing each named method as an unbound callable, generically, with no compile-time C++ type or hand-written binding code required. Operators need no separate bridging: once a value is wrapped in a DynamicFeature, its existing generic operator overloads dispatch to the raw value's native metamethods with full dependency tracking.
  • PluginInfo{name, version} + state::load_compiled_plugin(info, open_fn) — wraps load_module + decorate_module_functions, registers the module's table under original_env[name](so its free functions are actually reachable from a recipe — nothing did this before), and records the plugin's identity viastate::plugins()`.
  • register_external_type's name is now documented/handled as the type's fully-qualified display path (e.g. "occt.gp_Pnt"), not just its local table key — needed once a type is nested, since the serialized form of an action embeds that name verbatim and a saved recipe must still resolve it on read-back.

register_type / modify_type (pre-existing, bundled in here)

  • register_type<T, Flags = sol::automagic_flags::all> lets a caller opt a usertype out of sol2's automagic enrollments (comparison operators, index metamethods, …) — needed for types wrapped via unique_usertype_traits where the underlying type doesn't support those operators.
  • modify_type extends an already-registered usertype without re-running sol2's automagic registration, for the same reason.

Not included

Pure-C++ and pure-Lua-script plugin backends. PluginInfo/plugins() are shaped so a future state::begin_plugin (handing back a namespace table for register_type/register_function or run_module_script to populate directly) can slot in without changing what's here.

Tests

  • module.nested_type_keeps_dependency_tracking (new) — locks in the recursive-decoration fix.
  • plugin.* (new, tests/cpp/dynamic_plugin.cpp) — loading a compiled plugin, tracking its free functions, and the full construct + nested-method + serialize + deserialize round trip for a bridged class.
  • All existing tests pass unchanged.

This PR is co-authored with Claude and thoroughly reviewed by me.

joergbrech and others added 5 commits July 24, 2026 09:26
register_type<T, Flags> lets a caller opt a usertype out of sol2's automagic
enrollments (comparison operators, index metamethods, ...), which matters for
types wrapped via unique_usertype_traits where the underlying type doesn't
support those operators. modify_type extends an already-registered usertype
(e.g. one a plugin registered before) without re-running sol2's automagic
registration, for the same reason.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TS5yFa1xBMh7dhCLj5yNi3
Towards a future plugin contract (C++ plugins registering directly, pure-Lua
script plugins, and compiled Lua plugins like adtl.so) that all report a
name+version and register under a namespace table keyed by that name, this
lands the prerequisite and the first (compiled-plugin) backend:

- create_decorated_environment's __index only ever decorated one level below
  original_env: a type registered flat got tracked, but the same type nested
  one level deeper (e.g. inside a namespace/plugin table) silently fell back
  to raw, untracked calls - confirmed by a standalone repro against sol2
  before touching anything. Replaced the hand-duplicated one-level logic
  with decorate_value/decorate_table, a small mutually-recursive pair that
  applies the same rule at any depth. Top-level caching and the globals
  fallback are preserved exactly; a new test (module.nested_type_keeps_
  dependency_tracking) locks in the previously-broken case, and all prior
  tests still pass unchanged.

- state::load_module wraps luaL_requiref to load a compiled Lua C extension
  (e.g. a SWIG-generated module) into the state's Lua interpreter.

- state::decorate_module_functions (made public - it already existed for
  run_module_script) turns any plain Lua function found in a table into a
  grunk-tracked function_meta, covering a module's free functions with one
  call.

- external_type_proxy + state::register_external_type bridge a class whose
  constructor/methods live behind foreign metatables (as SWIG-Lua's binding
  does) by probing one instance and capturing each named method as an
  unbound callable, generically, with no compile-time C++ type or hand-
  written binding code involved. Operators need no separate bridging: once
  a value is wrapped in a DynamicFeature, its existing generic operator
  overloads already dispatch to the raw value's native metamethods with
  full dependency tracking.

- PluginInfo{name, version} + state::load_compiled_plugin(info, open_fn)
  wraps load_module+decorate_module_functions and registers the module's
  table under original_env[name] (so its free functions are actually
  reachable from a recipe, which nothing did before), recording the
  plugin's identity in state::plugins().

- register_external_type's `name` is documented and handled as the type's
  fully-qualified display path (e.g. "adtl.adouble"), not just its local
  table key - needed once a type is nested, since ActionDynamic's
  serialized form embeds that name verbatim and a saved recipe must still
  resolve it correctly on read-back. A new test (dynamic_plugin.cpp)
  exercises loading a fake compiled plugin, bridging a nested class, and
  the construct + nested-method-call + serialize + deserialize round trip.

C++ and pure-Lua-script plugin backends are intentionally not implemented
yet, but PluginInfo/plugins() are shaped so a future state::begin_plugin
(handing back a namespace table for register_type/register_function or
run_module_script to populate directly) slots in without changing this.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TS5yFa1xBMh7dhCLj5yNi3
@joergbrech
joergbrech merged commit b10856a into main Jul 24, 2026
10 checks passed
joergbrech added a commit that referenced this pull request Jul 24, 2026
register_external_type now auto-discovers member functions by probing an
instance the first time each is looked up (merged in
#262), so the explicit setADValue/getADValue/getValue
listing is no longer needed to bridge adtl.adouble.
joergbrech added a commit that referenced this pull request Jul 27, 2026
joergbrech added a commit that referenced this pull request Sep 17, 2026
register_external_type now auto-discovers member functions by probing an
instance the first time each is looked up (merged in
#262), so the explicit setADValue/getADValue/getValue
listing is no longer needed to bridge adtl.adouble.
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.

Support loading compiled LUA code

1 participant