From 7c7671a15a5ab44cb0441d092ba279b0aacb7d92 Mon Sep 17 00:00:00 2001 From: lordlogo2002 Date: Thu, 1 Jan 2026 17:55:46 +0100 Subject: [PATCH] Enhance oop.install function: add support for dynamic class creation via __newindex --- oop/oop.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/oop/oop.lua b/oop/oop.lua index 0a723bc..f28a6a3 100644 --- a/oop/oop.lua +++ b/oop/oop.lua @@ -307,6 +307,25 @@ function oop.install(env) target.isinstance = oop.isinstance target.issubclass = oop.issubclass target.Visibility = oop.Visibility + + local mt = getmetatable(target) + if not mt then + mt = {} + setmetatable(target, mt) + end + local prev_newindex = mt.__newindex + mt.__newindex = function(t, k, v) + local val = v + if type(k) == "string" and type(v) == "function" and k:match("^[A-Z]") then + val = oop.class(v) + val.__name = k + function_def_cache[v] = val + end + if prev_newindex then + return prev_newindex(t, k, val) + end + rawset(t, k, val) + end end oop.install()