-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| 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 | ||
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
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
| 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 RulesetParamDescriptor | ||
| { | ||
| 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) | ||
| // 'N'=string-multi ({string} array -> one tag/value pair per element, preserving order) | ||
| int tag; // PSYS_* constant integer value | ||
| }; | ||
|
|
||
| struct RulesetFlagDescriptor | ||
| { | ||
| 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 RulesetBuilderDef; | ||
|
|
||
| // Build a RulesetBuilderDef 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. | ||
| RulesetBuilderDef* ruleset_builder_def_build( | ||
| const RulesetParamDescriptor* 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 ruleset_builder_def_build(). | ||
| void ruleset_builder_def_add_flags( | ||
| RulesetBuilderDef* def, | ||
| const RulesetFlagDescriptor* 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_ruleset_serialize(lua_State* L, int params_idx, const RulesetBuilderDef* 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_ruleset_fn( | ||
| lua_State* L, | ||
| const char* module_name, | ||
| const char* fn_name, | ||
| lua_CFunction fn, | ||
| const RulesetBuilderDef* def | ||
| ); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.