mgusmano / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

Invalid ref call code generation for class members #217

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I find critical error that prevent my application work.
I just try to simplify it here, the code snipped here look useless, but I just 
try to demonstrate the situation.

the following code should not generate for members, the member may be use in 
calling methods.

this.myVar = {Value:this.myVar};
method1(this.myVar); //the method1 may use this.myVar, but it already destroyed
this.myVar = this.myVar.Value

I think you should always copy it to temporary variable:
var $$vv = {Value:this.myVar};
method1(this.myVar);
this.myVar = $$vv.Value

C#:
    [JsType(JsMode.Prototype)]
    public class Foo
    {
        public JsNumber myVar = 1;
        public JsNumber changeVar(ref JsNumber val)
        {
            val = myVar + 1;
            return val;
        }

        public void foo2()
        {
            changeVar(ref myVar);
        }

    }
------------------------
JS:
shetab.common.Foo = function()
{
    this.myVar = 1;
};
shetab.common.Foo.prototype.changeVar = function(val)
{
    val.Value = this.myVar + 1;
    return val.Value;
};
shetab.common.Foo.prototype.foo2 = function()
{
    (function()
    {
        this.myVar = {Value:this.myVar}; //** ERROR: this destroy memeber and make it invalid in changeVar method **//
        var $res=this.changeVar(this.myVar);
        this.myVar = this.myVar.Value;
        return $res;
    }).call(this);
}; 

Wish to get fix it soon.
Regards

Original issue reported on code.google.com by Madnik7@gmail.com on 18 Sep 2012 at 12:19

GoogleCodeExporter commented 8 years ago
Better solution:
As I go deeper in the issue i find even using temp variable is not appreciated 
solution, because when the value changed in method, the actual field is not 
modified till function return,

ALL code is Pseudo-code

see:
Foo.prototype.changeVar = function(val, propName) //where var is reference
{
    val.Value = "something"; 
    this.firePropertyChanged(propName); //ERROR: function should return till the actual reference is modified.
    return val.Value;
};

so I think you should do different approach something like this

JS:

var $$this = this;
var $$v = {Setter:function ($$value) {
    $$this._test = $$value;
}, Value:this._test};

Foo.prototype.changeVar = function(val)
{
   val.Setter(1);
};

It is just Idea and Pseudo-code, not working code.

Original comment by Madnik7@gmail.com on 18 Sep 2012 at 2:12

GoogleCodeExporter commented 8 years ago

Original comment by DanelK...@gmail.com on 28 Jan 2013 at 4:56