This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

xdc config String

I'm attempting to pass a string as a config parameter to a custom XDC object, and am having difficulties in the xs script in copying that string to my instance struct. Below is a snippet of what I am attempting.

xdc module:

instance:

config String MessageQName;

internal:

struct Instance_State

{

char MessageQName[20];

}

In the xs script I have attempted:

function instance$static$init(obj, params)
{
obj.MessageQName = new String(params.MessageQName);

or

obj.MessageQName = params.MessageQName;

}

 

There is something fundamental I am missing here.

Thanks

  • Can you try adding some print statements into that init function?

    e.g.:

    function instance$static$init(obj, params)
    {

        print("************* inside instance$static$init()*************")

        for(var x in params) {

            print("\t params contains: " + x); // this will print out the members that params contains

        }

        print(" message Q name = " + params.MessageQName);

    }

     

    Steve

  • Were you planning to allow changing the instance state at runtime? If not, the easiest way to have a string in the instance state is to declare it as String, the same type as the config parameter:

    struct Instance_State
    {
        String MessageQName;
    }

    and then limit its size with

    function instance$static$init(obj, params)
    {
        obj.MessageQName = params.MessageQName.substr(0, 20);
    }

    The advantage of that is that your instance will use only the number of characters actually needed, up to 20.
    If you have to have exactly 20 chars, things are just a little bit more complicated, and you can have your original declarations. However, an instance state must be completely defined, which means all 20 characters must be assigned to in instance$static$init. Since there is no char type in JavaString, you have to use numbers.

    Also, now you need to extract characters from a JavaScript String and assign them to the array's elements one by one. Again, no chars in JavaScript, so you have to use charCodeAt(), a standard JavaScript String function.

    function instance$static$init(obj, params)
    {
        for (var i = 0; i < obj.MessageQName.length; i++) {
            obj.MessageQName[i] = 0;
        }

        for (var i = 0; i < params.MessageQName.length; i++) {
            obj.MessageQName[i] = params.MessageQName.charCodeAt(i);
        }
    }

  • I was not intending to change the String at runtime in this case. If I do declare it as a String in the instance state, can I just pass obj.MessageQName in to the MessageQ_open function? I guess I'm a little fuzzy on the details of how a javascript String can be used in C land. I may very well be thinking about it too hard.

     

    Thanks

  • JavaScript String will become char* in C. I don't know much about MessageQ_open function, but you should be able to access MessageQName in the same way you access any other member of the Instance_State structure, using an object handle:
    obj->MessageQName