cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.16k stars 7.05k forks source link

[cocos2d-js] Scene from compiled jsc file can't be entered #18608

Open kosmosR2 opened 6 years ago

kosmosR2 commented 6 years ago

Steps to Reproduce:

  1. cocos jscompile -s path/to/src -d path/to/src
  2. cocos run -p linux/android
  3. Even if you work on the project create by cocos command line tool just now(and sure it works fine before we compile its js code),main.js can't take us to HelloWorldScene.And no error will be thrown.I can get a instance of HelloWorldScene class and access its attributes correctly while cc.director can't make use of the instance.
kosmosR2 commented 6 years ago

I think i catch the error. When scene.onEnter() called,this._super() will be called.TypeError: this._super is not a function will be thrown in compiled scene file.

kosmosR2 commented 6 years ago

It works fine if i use cc.Scene.prototype.onEnter.call(this) to replace this._super().

kosmosR2 commented 6 years ago

From line 248 of cocos2d-x/cocos/scripting/js-bindings/script/jsb_prepare.js:

    fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

    // Copy the properties over onto the new prototype
    for (name in prop) {
        // Check if we're overwriting an existing function
        prototype[name] = typeof prop[name] == "function" &&
            typeof _super[name] == "function" && fnTest.test(prop[name]) ?
            (function (name, fn) {
                return function () {
                    var tmp = this._super;

                    // Add a new ._super() method that is the same method
                    // but on the super-class
                    this._super = _super[name];

                    // The method only need to be bound temporarily, so we
                    // remove it when we're done executing
                    var ret = fn.apply(this, arguments);
                    this._super = tmp;

                    return ret;
                };
            })(name, prop[name]) :
            prop[name];

You guys may want use fnTest.test to check if the method going to call this._super.Maybe this issue will not happen if we compile all jsb code... But my suggestion is to remove fnTest.test(prop[name]).I don't think it is necessary.

kopos commented 5 years ago

Can confirm that this issue does not happen if the frameworks/cocos2d-x/cocos/scripting/js-bindings/script/ folder is also jscompile-d