1

I want to process an array from java script. (i.e., from html page to plugin code. I want to get the values from html code to variables in plugin so that I can modify them within code and process for some other purpose.).

But I am facing a problem in this. I know that internally an array will be processed as NPObject. I am trying to retrieve the length of the array and the elements of the array from this NPObject but the array length returned is zero even thought the array is of non-zero length.

Any suggestions are Welcome....

I have a html page which has an array The HTML page is as follows:

<html>
<script type="text/javascript">
var arrayData = [9,1,2,3,4,5,6,7,8,9];
function handleEvent(e) {
    if (e.keyCode == 55) {
        document.getElementById('arrayint_ele_disp').innerHTML = arrayData;
        process_intarray(arrayData);
    }
}
function  process_intarray(arrayData){
    obj = document.getElementById("Obj");
    if(obj){
        obj.process_array_intval(arrayData); 
    }
}
</script>

<body onload="init()" onkeydown="handleEvent(event)">
<div id ="arrayint_ele_disp" style="position:absolute;left:100px;top:250px"> 
ARRAY_INT VAL </div>
</body>

</html>

My plugin code for processing array elements is as folows:

bool ScriptableObject::process_intarray(const NPVariant* args, uint32_t 
     argCount, NPVariant* result)
{

    //Get the Length of the array
    //NPObject *inobject = args[0].value.objectValue;
    NPObject *inobject = args->value.objectValue;

    bool bRetval = false;
    NPVariant npvlength;
    //NPIdentifier id;

    bRetval = 
   NPN_GetProperty(m_npp,inobject,NPN_GetStringIdentifier("length"),&npvlength);
    printf("\n NPN_GetProperty length type %d value : %d\n"
  ,npvlength.type,npvlength.value.intValue);

    //Get the array elements
    int i = 0;
    for(i = 0; i < npvlength.value.intValue; i++)
    {
        NPVariant CurVal;
        NPN_GetProperty(m_npp,inobject,NPN_GetIntIdentifier(i),&CurVal);
        m_prop_array_intval[i] = CurVal.value.intValue;
    }
    return true;

}

Here in this function NPN_GetProperty(m_npp,inobject,NPN_GetStringIdentifier("length"),&npvlength); is returning a true.

But on checking the length of npvlength its giving 0 and its type as NPVariantType_Double. I am unable to understand why..

Any suggestions are welcome...

1 Answer 1

1

You shouldn't rely on numbers being one of Int32 or Double. If the numbers type is double use value.doubleValue to get the length, otherwise you will get unexpected values (due to value being a union and hence doubleValue and intValue referencing the same memory).
Ideally write yourself helper functions for the conversions so you don't have to worry about the specific NPVariantType everywhere.

Also you should cache the NPIdentifiers: In the worst case NPN_Get*Identifier() could result in a full interprocess-roundtrip from the plugin process into the browser process and back, in addition to the expense of string comparisons/hashing/...

Sign up to request clarification or add additional context in comments.

2 Comments

Hi George, Thanks for suggestion... I tried npvlength.value.doubleValue also but it is still zero. and the array elements are integers, so I was using npvlength.value.intValue... Any other suggestions...
@user1523492: Just to be sure, did you also adjust your printf() statements to use %f instead of %d?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.