diff --git a/src/bind.inc.php b/src/bind.inc.php index c51c10e..d1394a6 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -43,10 +43,58 @@ function supported_tokens() ]; } +function bhl_prepare_meta(\mtgMetaInfo $orig) +{ + //NOTE: making a deep clone + $meta = unserialize(serialize($orig)); + + $arr_proxies = array(); + foreach($meta->getUnits() as $u) + { + if($u->object instanceof \mtgMetaStruct) + { + $need_to_replace = false; + $fields = $u->object->getFields(); + //NOTE: let's replace arrays with array proxies + foreach($fields as $name => $fld) + { + if($fld->getType() instanceof \mtgArrType) + { + $proxy_name = "List_{$fld->getType()->getValue()->getName()}"; + if(!isset($arr_proxies[$proxy_name])) + { + $proxy = new \mtgMetaStruct($proxy_name); + $proxy->setToken('bhl_native_arr_proxy', $fld->getType()->getValue()); + $proxy->setToken('bhl_no_itype', true); + $meta->addUnit(new \mtgMetaInfoUnit('', $proxy)); + $arr_proxies[$proxy_name] = $proxy; + } + else + $proxy = $arr_proxies[$proxy_name]; + + $new_fld = new \mtgMetaField($name, new \mtgTypeRef($proxy)); + //forcing no setter + $new_fld->setToken('bhl_set', 0); + $fields[$name] = $new_fld; + + $need_to_replace = true; + } + } + + if($need_to_replace) + { + foreach($u->object->getFields() as $fld) + $u->object->delField($fld); + $u->object->setFields($fields); + } + } + } + + return $meta; +} + function bhl_add_twig_support(\Twig\Environment $twig) { - $twig->addGlobal('native_array_types', array()); - $twig->addTest(new \Twig\TwigTest('instanceof', function($obj, $class) { @@ -83,22 +131,6 @@ function bhl_add_twig_support(\Twig\Environment $twig) return bhl_gen_native_type($type); } )); - $twig->addFilter(new \Twig\TwigFilter('native_arr_type', - function($type) use($twig) - { - if($type instanceof \mtgArrType) - { - //let's remember all native array proxies - $types = $twig->getGlobals()['native_array_types']; - $proxy_name = "List_{$type->getValue()->getName()}"; - $types[$proxy_name] = $type; - $twig->addGlobal('native_array_types', $types); - return $proxy_name; - } - else - return $type; - } - )); $twig->addFilter(new \Twig\TwigFilter('norm_name', function($name) { @@ -189,12 +221,6 @@ function bhl_add_twig_support(\Twig\Environment $twig) return $arr; } )); - $twig->addFunction(new \Twig\TwigFunction('get_native_arr_types', - function() use($twig) - { - return array_values($twig->getGlobals()['native_array_types']); - } - )); } function bhl_get_last_name($name) diff --git a/tpl/codegen_register.twig b/tpl/codegen_autobind.twig similarity index 82% rename from tpl/codegen_register.twig rename to tpl/codegen_autobind.twig index e8ee6dc..faf61cf 100644 --- a/tpl/codegen_register.twig +++ b/tpl/codegen_autobind.twig @@ -16,7 +16,7 @@ namespace bhl { {% block declare %} #if !BHL_FRONT -{{ macro.decl_units(units) }} +{{ macro.decl_units(meta.units) }} #endif {% endblock %} @@ -26,9 +26,9 @@ public static void Register(Types types) { BHL_Types.Type_GenericArray = new GenericArrayTypeSymbol(new Proxy()); - {% block register %} - {{ macro.reg_units(units) }} - {% endblock %} +{% block register %} + {{ macro.reg_units(meta.units) }} +{% endblock %} } } diff --git a/tpl/codegen_types.twig b/tpl/codegen_types.twig index d5ea95f..eee4b88 100644 --- a/tpl/codegen_types.twig +++ b/tpl/codegen_types.twig @@ -4,12 +4,13 @@ static public class BHL_Types { static public IType Type_GenericArray; -{%- for u in units ~%} +{%- for u in meta.units ~%} {%- if u.object is instanceof('\\mtgMetaStruct') ~%} static public IType Type_{{u.object.name|norm_name}}; {%- endif ~%} -{%- endfor -%} +{%- endfor ~%} } + } //namespace bhl diff --git a/tpl/macro.twig b/tpl/macro.twig index 9405050..1d93caf 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -495,15 +495,15 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- endif -%} {%- endmacro -%} -{%- macro reg_native_arr_type(o, scope = 'types.ns') ~%} +{%- macro reg_native_arr_proxy(o, scope = 'types.ns') ~%} { - var cl = new ClassSymbolNative("List_{{o.value.name}}", + var cl = new ClassSymbolNative("{{o.name}}", //constructor is not allowed null ); { var vs = new FieldSymbol("Count", Types.Int, - delegate(VM.Frame frame, Val ctx, ref Val v, FieldSymbol fld) + delegate(VM.Frame frm, Val ctx, ref Val v, FieldSymbol fld) { #if !BHL_FRONT v.SetNum((ctx.obj as IList).Count); @@ -513,14 +513,18 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack cl.Define(vs); } { - var fn = new FuncSymbolNative("At", types.T("{{o.value.name}}"), - delegate(VM.Frame frame, ValStack stack, FuncArgsInfo args_info, ref BHS status) + var fn = new FuncSymbolNative("At", types.T("{{o.name}}"), + delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { #if !BHL_FRONT var val_idx = stack.Pop(); var val_lst = stack.Pop(); - stack.Push(Val.NewObj(frm.vm, ((List<{{o.value.name}}>)val_lst.obj)[(int)val_idx._num], BHL_Types.Type_{{o.value.name}})); + var v = ((List<{{token(o, 'bhl_native_arr_proxy').name}}>)val_lst.obj)[(int)val_idx._num]; + var dv = Val.New(frm.vm); + + {{ _self.native2val(token(o, 'bhl_native_arr_proxy'), 'v', 'dv') }}; + stack.Push(dv); val_idx.Release(); val_lst.Release(); @@ -611,7 +615,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {% set class = token_or(o, 'bhl_native_class', o.name) %} { - cl.Define(new FieldSymbol("{{f.name}}", {{f.type|native_arr_type|bhl_type_ref}}, + cl.Define(new FieldSymbol("{{f.name}}", {{f.type|bhl_type_ref}}, {% if token_or(f, 'bhl_get', 1) != 0 ~%} //getter delegate(VM.Frame frm, Val ctx, ref Val v, FieldSymbol fld) { @@ -986,6 +990,8 @@ public partial class Script_{{u.object.name}} { {%- if has_token(u.object, 'bhl_ecs_component') -%} {{ _self.reg_ecs_component(u.object) }} + {%- elseif has_token(u.object, 'bhl_native_arr_proxy') -%} + {{ _self.reg_native_arr_proxy(u.object) }} {%- else -%} {{ _self.reg_class(u.object) }} {%- endif -%} @@ -1002,10 +1008,6 @@ public partial class Script_{{u.object.name}} { {{Error('Not supported type: ' ~ u.object)}} {%- endif -%} - {%- endfor -%} - - {%- for type in get_native_arr_types() -%} - {{ _self.reg_native_arr_type(type) }} {%- endfor ~%} //assign global static types