Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ __pycache__
/fuzz/corpus/json/*
!/fuzz/corpus/json/*.json
*.code-workspace
*.tar.bz2
2 changes: 2 additions & 0 deletions Sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ target_sources(Luau.VM PRIVATE
VM/include/luaconf.h
VM/include/lualib.h
VM/include/llsl.h
VM/include/llruleset_builder.h

VM/src/ares.cpp
VM/src/lapi.cpp
Expand Down Expand Up @@ -429,6 +430,7 @@ target_sources(Luau.VM PRIVATE
VM/src/llprim.cpp
VM/src/llprim.h
VM/src/llprim_set_primitive_params.inl
VM/src/llruleset_builder.cpp
VM/src/lyieldable.cpp
VM/src/lstrbuf.cpp
VM/src/lyieldstrlib.h
Expand Down
77 changes: 77 additions & 0 deletions VM/include/llruleset_builder.h
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)

Copy link
Copy Markdown

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.

@Rider-Linden Rider-Linden Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

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

static const FluentParamDescriptor [] = { }

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_build to call for static storage.

Copy link
Copy Markdown

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.

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
);
Loading
Loading