Skip to content

Add RTTI-powered dynamic natives - #2598

Closed
adriansmares wants to merge 2 commits into
alliedmodders:masterfrom
adriansmares:users/adriansmares/NOTICKET/feature/syntax-exploration-trimmed
Closed

adriansmares wants to merge 2 commits into
alliedmodders:masterfrom
adriansmares:users/adriansmares/NOTICKET/feature/syntax-exploration-trimmed

Conversation

@adriansmares

Copy link
Copy Markdown
Contributor

Dynamic natives currently have to be registered by hand with CreateNative in AskPluginLoad2, and implemented as an untyped NativeCall(Handle plugin, int numParams) callback that unpacks its arguments with GetNativeCell, GetNativeString and GetNativeArray. With int64 and 64-bit Address, callers pass 64-bit arguments by reference and pass a hidden first argument for 64-bit return values, which shifts the remaining parameter indices.

Allow plugins to declare a dynamic native directly as a typed public function prefixed with __native_ (or __native_<Methodmap>__<Method> for methodmap natives):

public int64 __native_MyPlugin_AddGold(int client, const char[] reason, int64 amount, int &newTotal)

Before calling AskPluginLoad2, scan the plugin's .publics table for __native_ functions, decode their signature from .rtti.methods and .rtti.data, and register the stripped native name (MyPlugin_AddGold, or Methodmap.Method when matching a classdef or enum in RTTI). When called, marshal the caller's stack frame onto the target IPluginFunction according to the RTTI signature—handling cells, by-reference parameters, const and mutable strings, arrays, enum structs, int64 values, references and hidden return slots, and variadic arguments—while keeping s_curcaller and s_curparams set so FormatNativeString and IsNativeParamNull* still work inside the callee. A __native_ function with a (Handle, int) signature falls back to FakeNativeRouter.

Because Address is declared as typedef Address = int64 in SourceMod 1.13+ and spcomp resolves typedefs to their underlying type before emitting .rtti.data, Address and int64 both encode as cb::kInt64 in the provider's .rtti.methods. When called by a plugin compiled before SourceMod 1.13 (!UsesInt64Address), inspect the caller's rtti.natives entry for the native: if the caller compiled that parameter or return slot as enum Address (cb::kEnum pointing to "Address" in .rtti.enums), widen or narrow the 32-bit cell (throwing an error if a 64-bit address does not fit in 32 bits), and otherwise report a signature mismatch error rather than silently truncating an int64 to a cell.

Also add multi-plugin suite support to the hl2sdk-mock CI workflow so provider and consumer test plugins under plugins/testsuite/mock/multi/<suite>/ are compiled and loaded together on x86 and x86_64.

@adriansmares
adriansmares force-pushed the users/adriansmares/NOTICKET/feature/syntax-exploration-trimmed branch from e8574ee to 5acf7e8 Compare October 28, 2026 00:54
@adriansmares
adriansmares force-pushed the users/adriansmares/NOTICKET/feature/syntax-exploration-trimmed branch from 5acf7e8 to 854d0e8 Compare October 28, 2026 01:06
@dvander

dvander commented Oct 28, 2026

Copy link
Copy Markdown
Member

This is not how this should be done. You are hardcoding all sorts of implementation details about the bytecode format. It's not type-safe and does not properly use the runtime mechanisms in the VM. Please wait for actual language support.

@dvander dvander closed this Oct 28, 2026
@adriansmares

Copy link
Copy Markdown
Contributor Author

I shouldn't have opened the PR at all, as it was not meant to be merged, in retrospective.

I wanted to explore how feasible it is to implement the simpler native syntax with the current RTTI, in order to understand if the proposal in #2595 should be dropped and for 'native' natives to take over. I realize that the guts of the implementation here are beyond redemption in the current form.

Sorry for the noise.

@adriansmares
adriansmares deleted the users/adriansmares/NOTICKET/feature/syntax-exploration-trimmed branch October 28, 2026 11:21
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.

2 participants