From df8ba2db00295989e074791a7a85b9876519668c Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 21 Nov 2024 11:48:21 +0300 Subject: [PATCH] Using local static functions instead of lambdas --- src/bind.inc.php | 18 ++++++ tpl/bind_ecslite.twig | 134 +++++++++++++++++++----------------------- 2 files changed, 79 insertions(+), 73 deletions(-) diff --git a/src/bind.inc.php b/src/bind.inc.php index 7fd6939..9649001 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -54,6 +54,24 @@ class BindEclLitePlugin implements \bhl_bind\BindPlugin $code = ''; + $code .= "#if !BHL_FRONT\n"; + $code .= "public static bool TryUnpackEcsEntity(ValStack stack, out EcsWorld world, out int id)\n"; + $code .= "{\n"; + $code .= " EcsPackedEntityWithWorld e = default;\n"; + $code .= " stack.PopRelease().Decode(ref e);\n\n"; + $code .= " return e.Unpack(out world, out id);\n"; + $code .= "}\n\n"; + + $code .= "public static void UnpackEcsEntity(ValStack stack, out EcsWorld world, out int id)\n"; + $code .= "{\n"; + $code .= " EcsPackedEntityWithWorld e = default;\n"; + $code .= " stack.PopRelease().Decode(ref e);\n\n"; + $code .= " if(e.Unpack(out world, out id) == false)\n"; + $code .= " throw new Exception(\"No entity found\");\n"; + $code .= "}\n\n"; + + $code .= "#endif\n"; + foreach($sliced as $idx => $units) { $code .= "public static void Register_Ecslite_$idx(Types types)\n"; diff --git a/tpl/bind_ecslite.twig b/tpl/bind_ecslite.twig index c61abd7..a253999 100644 --- a/tpl/bind_ecslite.twig +++ b/tpl/bind_ecslite.twig @@ -2,22 +2,20 @@ {%- macro reg_ecslite_component(o) ~%} { + #if !BHL_FRONT + static bhl.Coroutine {{o.name}}_Ensure(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + UnpackEcsEntity(stack, out EcsWorld world, out int id); + world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>().Ensure(id); + return null; + } + #endif + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_Ensure", Types.Void, #if !BHL_FRONT - delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { - EcsPackedEntityWithWorld e = default; - stack.PopRelease().Decode(ref e); - - if(e.Unpack(out EcsWorld world, out int id) == false) - throw new Exception("No entity found"); - - world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>().Ensure(id); - - return null; - } - #else - null - #endif + {{o.name}}_Ensure + #else + null + #endif , new FuncArgSymbol("__self", types.T("{{token(o, 'bhl_ecslite_component_entity')}}")) ); @@ -25,22 +23,22 @@ } { + #if !BHL_FRONT + static bhl.Coroutine {{o.name}}_Del(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + if(TryUnpackEcsEntity(stack, out EcsWorld world, out int id) == false) + return null; + + world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>().Del(id); + + return null; + } + #endif var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_Del", Types.Void, #if !BHL_FRONT - delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { - EcsPackedEntityWithWorld e = default; - stack.PopRelease().Decode(ref e); - - if(e.Unpack(out EcsWorld world, out int id) == false) - return null; - - world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>().Del(id); - - return null; - } - #else + {{o.name}}_Del + #else null - #endif + #endif , new FuncArgSymbol("__self", types.T("{{token(o, 'bhl_ecslite_component_entity')}}")) ); @@ -48,25 +46,23 @@ } { + #if !BHL_FRONT + static bhl.Coroutine {{o.name}}_Exists(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + var dv = bhl.Val.New(frm.vm); + if(TryUnpackEcsEntity(stack, out EcsWorld world, out int id) == false) + dv.SetBool(false); + else + dv.SetBool(world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>().Has(id)); + stack.Push(dv); + return null; + } + #endif var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_Exists", Types.Bool, #if !BHL_FRONT - delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { - EcsPackedEntityWithWorld e = default; - stack.PopRelease().Decode(ref e); - - var dv = bhl.Val.New(frm.vm); - - if(e.Unpack(out EcsWorld world, out int id) == false) - dv.SetBool(false); - else - dv.SetBool(world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>().Has(id)); - stack.Push(dv); - - return null; - } - #else + {{o.name}}_Exists + #else null - #endif + #endif , new FuncArgSymbol("__self", types.T("{{token(o, 'bhl_ecslite_component_entity')}}")) ); @@ -85,11 +81,7 @@ {%- macro ecslite_component_field(o, f) ~%} { - var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_{{f.name}}", {{f.type|bhl_type_ref}}, - {%- if token_or(f, 'bhl_set', 1) != 0 ~%} - 1, - {%- endif ~%} - delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + static bhl.Coroutine {{o.name}}_{{f.name}}(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { #if !BHL_FRONT Val dv = null; @@ -98,12 +90,7 @@ dv = stack.Pop(); {%- endif ~%} - EcsPackedEntityWithWorld e = default; - stack.PopRelease().Decode(ref e); - - if(e.Unpack(out EcsWorld world, out int id) == false) - throw new Exception("No entity found"); - + UnpackEcsEntity(stack, out EcsWorld world, out int id); {%- if token_or(f, 'bhl_set', 1) != 0 ~%} if(dv != null) @@ -131,9 +118,15 @@ throw new Exception("No such component '{{token_or(o, 'bhl_native_class', o.name)}}' on entity: " + id); } stack.Push(dv); - #endif - return null; - }, + #endif + return null; + } + + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_{{f.name}}", {{f.type|bhl_type_ref}}, + {%- if token_or(f, 'bhl_set', 1) != 0 ~%} + 1, + {%- endif ~%} + {{o.name}}_{{f.name}}, new FuncArgSymbol("e", types.T("{{token(o, 'bhl_ecslite_component_entity')}}")) {%- if token_or(f, 'bhl_set', 1) != 0 ~%} , new FuncArgSymbol("v", {{f.type|bhl_type_ref}}) @@ -145,19 +138,11 @@ {%- macro ecslite_component_func(o, m) ~%} { - var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_{{m.name}}", - {{m.returntype|bhl_type_ref}}, - {{ count_default_args(m) }}, + static bhl.Coroutine {{o.name}}_{{m.name}}(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { #if !BHL_FRONT - delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { - EcsPackedEntityWithWorld e = default; - {{ bhl_bind.read_args2natives(m, '__') }} - stack.PopRelease().Decode(ref e); - - if(e.Unpack(out EcsWorld world, out int id) == false) - throw new Exception("No entity found"); + UnpackEcsEntity(stack, out EcsWorld world, out int id); var pool = world.GetPool<{{token_or(o, 'bhl_native_class', o.name)}}>(); @@ -177,12 +162,15 @@ else throw new Exception("No such component '{{token_or(o, 'bhl_native_class', o.name)}}' on entity: " + id); - return null; - } - #else - null - #endif - , new FuncArgSymbol("__self", types.T("{{token(o, 'bhl_ecslite_component_entity')}}")) + #endif + return null; + } + + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_{{m.name}}", + {{m.returntype|bhl_type_ref}}, + {{ count_default_args(m) }}, + {{o.name}}_{{m.name}}, + new FuncArgSymbol("__self", types.T("{{token(o, 'bhl_ecslite_component_entity')}}")) {%- if m.args %},{% endif ~%} {{ bhl_bind.func_decl_args(m) }} );