[Cocos2d-x from c ++ to js] 10: JS and C ++ interaction 2-JS and C ++ "function overload" Problem

Source: Internet
Author: User

For C ++, function overloading exists, for example:

void CCNode::setScale(float scale)void CCNode::setScale(float scaleX,float scaleY)

The two functions have the same name, but different parameter tables. Finally, the function signature after compilation is different.

However, this mechanism does not exist in JavaScript. How can this problem be solved? There are two situations:

First, JS needs to call the overloaded C ++ function interface

Let's take the above functions as an example to see how function Overloading is handled in the automatically generated code of cxx-generator. Open jsb_cocos2dx_auto.cpp and find the following code:

JSBool js_cocos2dx_Node_setScale(JSContext *cx, uint32_t argc, jsval *vp){    jsval *argv = JS_ARGV(cx, vp);    JSBool ok = JS_TRUE;    JSObject *obj = NULL;    cocos2d::Node* cobj = NULL;    obj = JS_THIS_OBJECT(cx, vp);    js_proxy_t *proxy = jsb_get_js_proxy(obj);    cobj = (cocos2d::Node *)(proxy ? proxy->ptr : NULL);    JSB_PRECONDITION2( cobj, cx, JS_FALSE, "js_cocos2dx_Node_setScale : Invalid Native Object");    do {        if (argc == 2) {            double arg0;            ok &= JS_ValueToNumber(cx, argv[0], &arg0);            if (!ok) { ok = JS_TRUE; break; }            double arg1;            ok &= JS_ValueToNumber(cx, argv[1], &arg1);            if (!ok) { ok = JS_TRUE; break; }            cobj->setScale(arg0, arg1);            JS_SET_RVAL(cx, vp, JSVAL_VOID);            return JS_TRUE;        }    } while(0);    do {        if (argc == 1) {            double arg0;            ok &= JS_ValueToNumber(cx, argv[0], &arg0);            if (!ok) { ok = JS_TRUE; break; }            cobj->setScale(arg0);            JS_SET_RVAL(cx, vp, JSVAL_VOID);            return JS_TRUE;        }    } while(0);    JS_ReportError(cx, "js_cocos2dx_Node_setScale : wrong number of arguments");    return JS_FALSE;}

The number of parameters is simply determined by the argc parameter, and then the corresponding branch code is executed. But what if the number of parameters is the same and the type is different? It is unknown.

Second, you do not need to call the C ++ function interface and directly simulate function overloading in the JS-Layer Code. This requires the use of some features of the JS language. Let's look at the corresponding code in the Cocos2d-html5. Oh, no, because the CCNode: setScale function in html5 has written some acrobatics code. So let's change it to the setPosition function. The same is true.

setPosition:function (newPosOrxValue, yValue) {    var locPosition = this._position;    if (arguments.length == 2) {        locPosition._x = newPosOrxValue;        locPosition._y = yValue;    } else if (arguments.length == 1) {        locPosition._x = newPosOrxValue.x;        locPosition._y = newPosOrxValue.y;    }    this.setNodeDirty();},

As you can see, this Code uses the arguments of JS to determine the number of parameters, and then runs the corresponding branch code.

Okay. Let's just reload it. Next, let's continue ~

This article is from the "Old G hut" blog, please be sure to keep this source http://4137613.blog.51cto.com/4127613/1354185

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.