Repository navigation
Codegen fixes for geoml - #299
Merged
Merged
Conversation
…l2 and prperly expand headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Codegen correctness fixes + dynamic-dispatch safety hardening
Summary
This batch of 11 commits came out of using real downstream plugin development
(
grunk-occt, thengrunk-geoml) as a deliberate stress test of grunk core —every fix here was found because a real plugin's real generated code hit it,
not from writing speculative new tests. They range from codegen correctness
issues (silently-rejected
std::string/std::vector/array parameters) todynamic-dispatch memory-safety gaps (
Handle(Derived)→Handle(Base)conversion, mutating calls silently no-op'ing, a dangling-pointer segfault in
Feature::as(), and sol2's unchecked-by-default argument extraction).Changes
Codegen (
grunk.codegen.generate)expand_headers()broke on nested header layouts (125299f,834eb97)— a glob spanning subdirectories (e.g.
geoml/<module>/*.h) collapsed eachmatch to its bare filename instead of its path relative to
include_dir,silently reconstructing the wrong path for any non-flat layout (OCCT's own
flat layout had hidden this). Now returns each match's
relative_to(include_dir)spelling, rendered via
.as_posix()so it doesn't vary with the hostplatform's path separator (confirmed broken on Windows).
std::string/std::vector<T>were silently rejected (125299f,b836a65)—
Param.kind()'s "object" fallback rejects any parameter whose canonical(typedef-resolved) spelling isn't a bare identifier.
std::string's owncanonical spelling (a full
std::__cxx11::basic_string<...>instantiation)and
std::vector<T>'s own surface spelling (angle brackets) both failthat check outright — found wrapping geoml (
has_tag,nurbs_curve,interpolate_curves, every filename parameter, ...). Both are nowrecognized as
"passthrough"unconditionally (sol2 already converts themnatively) before the canonical-spelling check runs. Also narrowed that
check itself: it used to reject any class whose canonical spelling isn't a
bare identifier, when the only real hazard is a pointer spelling.
classify_param()'sNonevs. "rejected" ambiguity (2742afb) —returning
Nonemeant both "not my concern, use the generic fallback" and"I explicitly reject this," indistinguishable to the caller. Added a
generate.REJECTEDsentinel, distinct fromNone, thatParam.kind()nowrespects. This uncovered a real, previously-silent correctness bug in
grunk-occt's own generated bindings: several OCCT methods with a mutableNCollection_Array1/2out-parameter (Geom_BSplineCurve::Poles,::Weights,::Knots, ...) were being silently accepted as a plain"object"parameterthat sol2 can never correctly fill in place.
Handle(Derived)→Handle(Base)smart-pointer conversionsol2 never actually performed the upcast (
d9b0799) —add_bases<...>()alone isn't enough for a unique usertype like
opencascade::handle<T>;sol2's
inheritance<T>::type_unique_castonly walks a base list whenunique_usertype_traits<T>::rebind_actual_type<Base>exists (a plugin's ownconcern) and two compile-time macros per class exist (
SOL_BASE_CLASSES/SOL_DERIVED_CLASSES), which the generator never emitted. Addedrender_sol_base_classes_header(), producing a new, run-widesol_base_classes.hpp. Without this, any function accepting a base handletype (e.g.
BRepBuilderAPI_MakeFace(Handle(Geom_Surface))) silentlyrejected every derived handle regardless of what
add_basesdeclared.with_std_vector<VecElement>()excluded by-referenceHandle(X)elements(
13ab623) — a fourth manifestation of CI: Add builds and tests for Clang and MSVC #4's own underlying sol2 behavior:cast_to_elementtook its elementVecElement const&, and sol2'sbase-aware unique-usertype checker excludes any by-reference parameter from
a Derived→Base conversion attempt outright. Fixed by taking the element by
value.
Dynamic dispatch / usability
DynamicFeaturehad no__callmetamethod (c0cc16d) — aFeaturewrapping a type with its own bound
operator()(e.g. geoml'sTransform)couldn't be called as
obj(args...)from inside a recipe's decoratedenvironment (
"attempt to call a sol.grunk::Feature<...> value"), eventhough the same call worked undecorated. Registered
sol::meta_function::call, forwarded through the samemake_dynamic_actionmachinery the arithmetic operators already use.
Mutating calls inside a decorated/parametric environment silently no-op
(
40d8a1e) — grunk's evaluation is lazy and pull-based: a node'seval()only runs when its own output feature is explicitly read. A bare
obj:Mutate(...)statement (the common setter pattern) never reads thatresult, so the mutation inside it silently never runs — a real trap, not a
hypothetical one (it's what made an early
wing_loft.grr.ymlattempt atbuilding three
gp_GTrsftransforms via separateSetValue(...)callssilently produce three identical, untransformed results). Added
function_meta::receiver_is_mutating()(non-const-reference receiver andvoidreturn — the narrowest signal a call's only purpose is the mutation)and a check in
make_dynamic_actionthat now throws immediately instead ofsilently doing nothing.
modules:script bodies (undecorated dispatch) areunaffected.
DynamicFeature::as(Type)captured a dangling raw pointer (1566d74)— the proxy table
:as()returns capturedthisinstead of a copy. Forthe common case of calling
:as()on the unnamed result of amodule-function call, nothing else keeps that temporary's own Lua userdata
reachable, so once Lua's GC collected it, the captured pointer dangled; the
next method call through the proxy then read the wrapped type's internal
state through garbage memory (found via
grunk-geoml'sairplane_seats.grr.yml,a real, reproducible segfault). Fixed by capturing a copy of
*thisinstead(
Feature<object>is a cheap,shared_ptr-backed handle), while stillpassing the Feature itself — not its already-evaluated value — into the
wrapped call, so laziness/dependency-tracking is unaffected.
sol2's unchecked-by-default argument extraction (
de18b58) — sol2defaults to unchecked userdata extraction (
SOL_ALL_SAFETIES_ONoffoutside a debug build): a Lua value of one registered usertype handed to a
function expecting a different, unrelated usertype gets blindly
reinterpreted as the target type instead of rejected, corrupting memory and
segfaulting in unrelated code. Found via
grunk-geoml's own generated code(a
geoml::Shapepassed where aTopoDS_Shapewas expected).grunk::dynamicnow definesSOL_ALL_SAFETIES_ON=1as anINTERFACEcompile definition, so every consumer — grunk's own code and any downstream
plugin linking against
grunk::dynamic— gets the checked getterconsistently (this can't be a per-project opt-in: sol2 template
instantiations for the same types must agree on this define across every
translation unit that touches them). The same mismatch now throws a clear,
catchable
sol::errorinstead of crashing.Docs
docs/usage.rst's subrecipe example was missing a call (cdab46b) —multiplication = recipes.multiplication(a bare reference to theSubRecipeobject) should have beenrecipes.multiplication()(invokingit to get a
RecipeCaller). Fixed in both code examples in the doc.Test plan
pixi run test_cpp: 198/198 passing — every fix above landed with itsown new regression test(s) (
dynamic_operators.cpp,dynamic_serialization.cpp,dynamic_state.cpp,dynamic_std_vector.cpp).pixi run test_python: 97/97 passing (test_codegen.pygained coveragefor the
expand_headerspath fix, thestd::string/std::vectorfix, andthe
REJECTEDsentinel).synthetic tests:
grunk-occt(52/52 C++ tests, 21/21 Python tests, all4 example recipes) and
grunk-geoml(23/23 C++ tests, 18/18 Pythontests, all 4 example recipes — including a full, line-by-line re-port of a
real, multi-sub-recipe
airplane_seatsmodel) both rebuilt against thisbranch's tip and re-verified end to end.
Known follow-ups (not blocking, tracked separately)
SOL_ALL_SAFETIES_ON's checked getter for unique usertypes (Handle(X))doesn't consult
add_bases/rebind_actual_typeat all (a real, narrow,pre-existing sol2 limitation) — only bites a
Handle(Derived)value bound toa
Handle(Base) const&by-reference parameter. Every generatedHandle(X)parameter already uses pass-by-value (see CI: Add builds and tests for Clang and MSVC #4/Error handling #5's ownconvention), so generated code is unaffected; only hand-written escape
hatches that drift from that convention can hit it (one found and fixed
downstream, in
grunk-occt).docs/usage.rst's dot-notation subrecipe form (multiplication.x = a)still doesn't actually work against this branch — only
recipes.name():get("output")/caller["input"] = valuedo. This fix onlycorrected the missing
()call; the deeper doc/API mismatch is still open.