-
Notifications
You must be signed in to change notification settings - Fork 8
Rulesets for table-based function params #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,3 +30,4 @@ __pycache__ | |
| /fuzz/corpus/json/* | ||
| !/fuzz/corpus/json/*.json | ||
| *.code-workspace | ||
| *.tar.bz2 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #pragma once | ||
| #include <cstddef> | ||
|
|
||
| struct lua_State; | ||
| typedef int (*lua_CFunction)(lua_State* L); | ||
|
|
||
| // Default link target: apply to the prim running the script. | ||
| static const int SLUA_LINK_THIS = -4; | ||
|
|
||
| // A note about C++20 porting. | ||
| // The const char* name fields in the descriptors must have static storage duration | ||
| // (e.g. string literals). | ||
| // This is not enforced at compile time, | ||
| // but when moving to C++20 we can do something like the following: | ||
| // | ||
| // struct LiteralString | ||
| // { | ||
| // consteval LiteralString(const char* p) noexcept : ptr(p) {} | ||
| // operator const char*() const noexcept { return ptr; } | ||
| // const char* ptr; | ||
| // }; | ||
| // The consteval constructor ensures that only string literals can be used | ||
| // to construct a LiteralString. | ||
|
|
||
| struct FluentParamDescriptor | ||
| { | ||
| const char* name; // effective property name (pretty alias or strict fallback) | ||
| char semantic; // scalar: 'i'=integer 'f'=float 's'=string 'v'=vector 'r'=rotation 'b'=boolean 'a'=asset 'k'=key | ||
| // collection: 'C'=string-csv ({string} array → escaped comma-joined string, one tag/value pair) | ||
| // 'M'=string-map ({[string]:string} table → one tag/key/value triple per entry) | ||
|
Rider-Linden marked this conversation as resolved.
|
||
| // 'N'=string-multi ({string} array → one tag/value pair per element, preserving order) | ||
| int tag; // PSYS_* constant integer value | ||
| }; | ||
|
|
||
| struct FluentFlagDescriptor | ||
| { | ||
| const char* name; // boolean property name, e.g. "color_interp" | ||
| int mask; // bitmask, e.g. 0x01 | ||
| int field_tag; // tag of the integer field holding the bits, e.g. 0 for "flags" | ||
| }; | ||
|
|
||
| struct FluentBuilderDef; | ||
|
|
||
| // Build a FluentBuilderDef from an array of descriptors. | ||
| // The .name pointers in descs must have static storage duration (e.g. string literals). | ||
| // Caller owns the returned pointer (process lifetime expected). | ||
| // Descriptors are sorted by tag internally; caller order does not matter. | ||
| FluentBuilderDef* fluent_builder_def_build( | ||
| const FluentParamDescriptor* descs, | ||
| size_t count | ||
| ); | ||
|
|
||
| // Attach flag-bit boolean properties to an existing def. | ||
| // Each descriptor maps a property name to a bitmask within the integer field at field_tag. | ||
| // The .name pointers in descs must have static storage duration (e.g. string literals). | ||
| // Call after fluent_builder_def_build(). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔕 I think monty brought this up as well, but the wrappers aren't fluent. Maybe add a todo to rename them, I don't think we need to block on renaming.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am open to suggestions.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really anything other than fluent, since it's explicitly not that. Rulesets, rulebuilders, rulemapper, probably something with rule in it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Otherwise 'rulesets' sounds good. |
||
| void fluent_builder_def_add_flags( | ||
| FluentBuilderDef* def, | ||
| const FluentFlagDescriptor* descs, | ||
| size_t count | ||
| ); | ||
|
|
||
| // Serialize a params table into a flat tag/value rules list and push it onto the stack. | ||
| // params_idx is the stack index of the params table (may be nil — emits an empty list). | ||
| // Flag boolean properties are merged into their backing integer field before emission. | ||
| void slua_fluent_serialize(lua_State* L, int params_idx, const FluentBuilderDef* def); | ||
|
|
||
| // Register fn as module_name.fn_name in L's globals, with def stored as upvalue 1. | ||
| // Creates the module table if it does not yet exist; adds to it if it does. | ||
| // Sets the module table readonly after each call. | ||
| void slua_register_fluent_fn( | ||
| lua_State* L, | ||
| const char* module_name, | ||
| const char* fn_name, | ||
| lua_CFunction fn, | ||
| const FluentBuilderDef* def | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not owned storage, correct? Owner of FPD needs to guarantee safety. Normally, would consider this and the other occurrences a hazard. This might be short-lived temporary state which allows you to handwave it off, but still. Wondering if 'constexpr'ing these could actually be used to give safety.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generator constructs these as
and they are owned by the imbedding process, in our case the simulator.. There is never a case where one would be created dynamically.
I've updated the comment for
fluent_builder_def_buildto call for static storage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this done in one or two other locations in VM/include. It's generally poor programming. But can be okay when it's used to point to const, read-only memory. I.e. compile-time text constants or constexprs. That wasn't the case here. It was really just duplicating c_str() in another form and requiring more memory.