From 66e9451e4aaa5f0e790a3f89efd9cc0640f0bbd4 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Sat, 28 Dec 2024 15:20:08 +0300 Subject: [PATCH] Migrating to more robust bhl_custom_rw semantics. Struct methods can affect the caller now, same applies to bhl_blob now --- src/bind.inc.php | 1 + tpl/bhl_bind_macro.twig | 57 +++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/bind.inc.php b/src/bind.inc.php index 47f31f9..c89d4d5 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -22,6 +22,7 @@ function supported_tokens() 'bhl_ref_arg', 'bhl_bin_op', 'bhl_blob', + 'bhl_refc', ]; } diff --git a/tpl/bhl_bind_macro.twig b/tpl/bhl_bind_macro.twig index 3345a9a..ace2d6d 100644 --- a/tpl/bhl_bind_macro.twig +++ b/tpl/bhl_bind_macro.twig @@ -14,13 +14,27 @@ #if !BHL_FRONT static bhl.Coroutine {{local_fn_name}}(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { - {{ _self.read_args2natives(o, '__', this_type) }} + {{ _self.read_args2natives(o, '__') }} + + {%- if this_type and not has_token(o, 'bhl_static') ~%} + + var dv_self = stack.Pop(); + {%if has_token(this_type, 'bhl_blob')%}ref{%endif%} var {{ _self.val2native(this_type, 'dv_self', '__self', true, has_token(this_type, 'bhl_blob')) }}; + + {%- endif ~%} {%~ if o.returntype %} var return_val = {%- endif ~%} {{this_prefix}}{{ _self.call_native_prefix_func(o, '__') }} + {%- if this_type and not has_token(o, 'bhl_static') ~%} + {%if has_token(this_type, 'bhl_custom_rw')%} + dv_self.Encode(__self); + {%- endif ~%} + dv_self.Release(); + {%- endif ~%} + {# pass back and release ref args #} {%- for arg in o.args -%} {%- if has_token(arg, 'bhl_ref_arg') ~%} @@ -307,7 +321,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- for idx, type_item in ret_types -%} {% set ridx = ret_types|length - idx - 1 ~%} { - var dv{{ridx}} = bhl.Val.New(frm.vm); + var dv{{ridx}} = bhl.Val.NewNoReset(frm.vm); {{ _self.native2val(ret_types[ridx], 'v'~ridx, 'dv'~ridx) }}; stack.Push(dv{{ridx}}); } @@ -320,7 +334,6 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- if this_type ~%} {{this_type|native_type}} {{prefix}}self = default; {%- endif -%} - {%- for v in vars -%} {%- if has_token(v, 'bhl_ref_arg') -%} bhl.Val {{prefix}}{{v.name}} = default; @@ -350,6 +363,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- set default_counter = default_counter + 1 -%} if(!args_info.IsDefaultArgUsed({{o.args|length - count_required_args(o) - default_counter}})) {%- endif ~%} + { {%- if has_token(arg, 'bhl_ref_arg') ~%} var dv = stack.Pop(); @@ -363,6 +377,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack dv.Release(); {%- endif ~%} } + {#-default arg value-#} {%- if has_token(arg, 'default') ~%} else @@ -440,7 +455,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- endfor -%} {%- endmacro -%} -{%- macro val2native(type, value, native, is_arg = false) -%} +{%- macro val2native(type, value, native, is_arg = false, is_ref = false) -%} {# any special case #} {%- if type == 'any' -%} {{native}} = {{value}}; @@ -492,8 +507,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack for(int i=0;i<_src_arr_count;++i) { var _arr_item = _src_arr_type.ArrGetAt({{value}}, i); - {#TODO: get rid of replace hack below#} - {{('var ' ~ _self.val2native(type.value, '_arr_item', 'tmp'))|replace({'tmp = tmp' : '', 'var var' : 'var'})}}; + var {{_self.val2native(type.value, '_arr_item', 'tmp')}}; if(i < _dst_before_count) {{native}}[i] = tmp; else @@ -532,9 +546,9 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {{native}} = ({{type|native_class_name}})((int){{value}}._num) {%- else -%} {%- if has_token(type, 'bhl_custom_rw') -%} - var tmp = new {{type|native_class_name}}(); {{value}}.Decode(ref tmp); {{native}} = tmp + {{native}} = {{value}}.Decode(new {{type|native_class_name}}()) {%- elseif has_token(type, 'bhl_blob') -%} - {{native}} = {{value}}.GetBlob<{{type|native_class_name}}>() + {{native}} = {%if is_ref%}ref{%endif%} {{value}}.GetBlob<{{type|native_class_name}}>() {%- else -%} {{native}} = ({{type|native_class_name}}){{value}}._obj {%- endif -%} @@ -593,8 +607,10 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {{value}}.Encode({{native}}) {%- elseif has_token(type, 'bhl_blob') -%} {{value}}.SetBlob({{native}}, Types_{{type.name|norm_name}}.Value) - {%- else -%} + {%- elseif has_token(type, 'bhl_refc') -%} {{value}}.SetObj({{native}}, Types_{{type.name|norm_name}}.Value) + {%- else -%} + {{value}}.SetObjNoRefc({{native}}, Types_{{type.name|norm_name}}.Value) {%- endif -%} {%- endif -%} {%- endif -%} @@ -616,8 +632,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack "{{o.name}}", (v) => { #if !BHL_FRONT - {#TODO: get rid of replace hack below#} - {{('var ' ~ _self.val2native(token(o, 'bhl_native_arr_proxy'), 'v', 'tmp'))|replace({'tmp = tmp' : '', 'var var' : 'var'})}}; + var {{_self.val2native(token(o, 'bhl_native_arr_proxy'), 'v', 'tmp')}}; return tmp; #else return null; @@ -625,7 +640,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack }, (_vm, itype, n) => { #if !BHL_FRONT - var v = Val.New(_vm); + var v = Val.NewNoReset(_vm); {{ _self.native2val(token(o, 'bhl_native_arr_proxy'), 'n', 'v')}}; return v; #else @@ -686,8 +701,10 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack v.Encode(o); {% elseif has_token(o, 'bhl_blob') %} v.SetBlob(ref o, type); - {% else %} + {% elseif has_token(o, 'bhl_refc') %} v.SetObj(o, type); + {% else %} + v.SetObjNoRefc(o, type); {% endif %} #endif } @@ -718,9 +735,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack , native_type: typeof({{o|native_class_name}}) {% if has_token(o, 'bhl_custom_rw') %} , native_object_getter: (v) => { - var tmp = new {{o|native_class_name}}(); - v.Decode(ref tmp); - return tmp; + return v.Decode(new {{o|native_class_name}}()); } {% elseif has_token(o, 'bhl_blob') %} , native_object_getter: (v) => { @@ -767,8 +782,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack static void get_{{o.name|norm_name}}_{{f.name}}(VM.Frame frm, Val ctx, ref Val v, FieldSymbol fld) { #if !BHL_FRONT {%~ if has_token(o, 'bhl_custom_rw') ~%} - {{class}} f = new {{class}}(); - ctx.Decode(ref f); + {{class}} f = ctx.Decode(new {{class}}()); {%- elseif has_token(o, 'bhl_blob') ~%} ref var f = ref ctx.GetBlob<{{class}}>(); {%- else ~%} @@ -793,8 +807,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack static void set_{{o.name|norm_name}}_{{f.name}}(VM.Frame frm, ref Val ctx, Val v, FieldSymbol fld) { #if !BHL_FRONT {%~ if has_token(o, 'bhl_custom_rw') ~%} - {{class}} f = new {{class}}(); - ctx.Decode(ref f); + {{class}} f = ctx.Decode(new {{class}}()); {%- elseif has_token(o, 'bhl_blob') ~%} ref var f = ref ctx.GetBlob<{{class}}>(); {%- else ~%} @@ -802,7 +815,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- endif ~%} {% if token(f, 'bhl_set') == 2 %} - {{_self.val2native(f.type, 'v', 'var nv')}}; + var {{_self.val2native(f.type, 'v', 'nv')}}; Script_{{o.name|norm_name}}.Set_{{f.name}}(ref f, nv); {% else %} {%- if has_token(f, 'bhl_native_prefix') -%} @@ -877,7 +890,7 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack {%- endif ~%} { - var dv = bhl.Val.New(frm.vm); + var dv = bhl.Val.NewNoReset(frm.vm); {{ _self.native2val(f.returntype, return_var, 'dv') }}; stack.Push(dv); }