cheerp-meta icon indicating copy to clipboard operation
cheerp-meta copied to clipboard

[[cheerp::jsexport]] ERROR (name conflict)

Open uMaxmaxmaximus opened this issue 8 years ago • 2 comments

i set methid name as "i0"

class [[cheerp::jsexport]] Cat {
public:
    int age;

    Cat(int age) : age(age) {}

    void i0() {
        console.log("RUN!!", age++);
    }

};

but "i0" is a internal C++ field to storage int age class member.

in js outout i get:

"use strict";
/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/
function Cat(a0) {
    this.i0 = 0;
    this.d = [this];
    t(this, a0);
}
Cat.prototype.i0 = function () {
    return s(this);
};

It is a name conflict. Just automatic rename internal member names

uMaxmaxmaximus avatar Feb 10 '17 20:02 uMaxmaxmaximus

We use consistent names to simplify optimization of code for the JS engines, so changing them could not be a good idea. It's possible that the since we have a custom prototype for this object we could change the names without any side effects but we need to study the internals of JS engines to make sure about this. Anyway, an error should be at least reported at compile time when such collisions happen.

alexp-sssup avatar Feb 13 '17 09:02 alexp-sssup

Just automatic rename internal member names, WITH WARNING IN CONSOLE! =) like "You used i1 ad method in export class, we rename internal prop blah blah blah, but is slow performance blah blah blah"

uMaxmaxmaximus avatar Feb 14 '17 18:02 uMaxmaxmaximus

Hi @uMaxmaxmaximus!

We comprehensively solved the issue you identified (and other connected problems) by wrapping JSExported objects a level deeper.

The code generated in your case will be like:

/opt/cheerp/bin/clang++ -target cheerp cat.cpp -o cat.js -cheerp-pretty-code
"use strict";
/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/
var __imul=Math.imul;
var __fround=Math.fround;
var oSlot=0;var nullArray=[null];var nullObj={d:nullArray,o:0};
function __ZN3CatC1Ei(Lthis,Lage){
        Lthis.i0=Lage;
}
function __ZN3Cat2i0Ev(Lthis){
        var LretConstructor=null,tmp1=0,Lref$ptmp2=null;
        LretConstructor="RUN!!";
        tmp1=Lthis.i0|0;
        Lthis.i0=tmp1+1|0;
        Lref$ptmp2=[0];
        Lref$ptmp2[0]=tmp1;
        __ZN6client7Console3logIJiEEEvRKNS_6StringEDpOT_(LretConstructor,Lref$ptmp2,0);
}
function __ZN6client7Console3logIJiEEEvRKNS_6StringEDpOT_(Lmessage,LoptionalParams,MoptionalParams){
        console.log(Lmessage,String(LoptionalParams[MoptionalParams]|0));
}
function Cat(a0){
        this.this={i0:0};
        __ZN3CatC1Ei(this.this,a0);
};
Cat.prototype.i0=function(){
        return __ZN3Cat2i0Ev(this.this);
};
Cat.prototype.delete=function(){
};
Cat.promise=
Promise.resolve();
__start();

As you can see member this will hold an inner object, in this case with a i0 member that will not clash with the i0 member function.

Thanks for reporting the bug, it's now solved in the master branch of cheerp-compiler, with a test added on the basis of this issue: https://github.com/leaningtech/cheerp-utils/commit/ca7819169e5b1566d305b95c1ecc529729c98543.

carlopi avatar Oct 11 '22 13:10 carlopi