From 7a46f12c02701ba539a54d09f03c07c1444365cd Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 20 Apr 2024 23:38:06 +0200 Subject: [PATCH] Fix type metamethod and test case for regex --- src/lune/builtins/regex/captures.rs | 5 ++++- src/lune/builtins/regex/matches.rs | 3 ++- src/lune/builtins/regex/regex.rs | 5 ++++- tests/regex/metamethods.luau | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/lune/builtins/regex/captures.rs b/src/lune/builtins/regex/captures.rs index 412370d..5dbea74 100644 --- a/src/lune/builtins/regex/captures.rs +++ b/src/lune/builtins/regex/captures.rs @@ -79,10 +79,13 @@ impl LuaUserData for LuaCaptures { Ok(new) }); - methods.add_meta_method(LuaMetaMethod::Type, |_, _, ()| Ok("RegexCaptures")); methods.add_meta_method(LuaMetaMethod::Len, |_, this, ()| Ok(this.num_captures())); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { Ok(format!("RegexCaptures({})", this.num_captures())) }); } + + fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { + fields.add_meta_field(LuaMetaMethod::Type, "RegexCaptures"); + } } diff --git a/src/lune/builtins/regex/matches.rs b/src/lune/builtins/regex/matches.rs index 42984a5..bc109f8 100644 --- a/src/lune/builtins/regex/matches.rs +++ b/src/lune/builtins/regex/matches.rs @@ -40,10 +40,11 @@ impl LuaUserData for LuaMatch { fields.add_field_method_get("finish", |_, this| Ok(this.end)); fields.add_field_method_get("len", |_, this| Ok(this.range().len())); fields.add_field_method_get("text", |_, this| Ok(this.slice().to_string())); + + fields.add_meta_field(LuaMetaMethod::Type, "RegexMatch"); } fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { - methods.add_meta_method(LuaMetaMethod::Type, |_, _, ()| Ok("RegexMatch")); methods.add_meta_method(LuaMetaMethod::Len, |_, this, ()| Ok(this.range().len())); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { Ok(format!("RegexMatch({})", this.slice())) diff --git a/src/lune/builtins/regex/regex.rs b/src/lune/builtins/regex/regex.rs index b40331d..3325e5d 100644 --- a/src/lune/builtins/regex/regex.rs +++ b/src/lune/builtins/regex/regex.rs @@ -65,9 +65,12 @@ impl LuaUserData for LuaRegex { }, ); - methods.add_meta_method(LuaMetaMethod::Type, |_, _, ()| Ok("Regex")); methods.add_meta_method(LuaMetaMethod::ToString, |_, this, ()| { Ok(format!("Regex({})", this.inner.as_str())) }); } + + fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { + fields.add_meta_field(LuaMetaMethod::Type, "Regex"); + } } diff --git a/tests/regex/metamethods.luau b/tests/regex/metamethods.luau index 66741d8..f14231c 100644 --- a/tests/regex/metamethods.luau +++ b/tests/regex/metamethods.luau @@ -10,6 +10,7 @@ local mtch = re:find("1337 wow") assert(tostring(mtch) == "RegexMatch(1337)") assert(typeof(mtch) == "RegexMatch") -local captures = re:captures("1337 125600 wow! 1984 0") -assert(tostring(captures) == "RegexCaptures(3)") +local re2 = regex.new("([0-9]+) ([0-9]+) wow! ([0-9]+) ([0-9]+)") +local captures = re2:captures("1337 125600 wow! 1984 0") +assert(tostring(captures) == "RegexCaptures(4)") assert(typeof(captures) == "RegexCaptures")