Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Adding items to a JS array using CallFunction doesn't add #70

Open
danpetitt opened this issue Mar 4, 2017 · 1 comment
Open

Adding items to a JS array using CallFunction doesn't add #70

danpetitt opened this issue Mar 4, 2017 · 1 comment

Comments

@danpetitt
Copy link

danpetitt commented Mar 4, 2017

Trying to add items to a Javascript Array fails to add the item for some reason:

var jsTextToInsert = JavaScriptValue.FromString( "String to add to array" );

var arrayObject = JavaScriptValue.GlobalObject.GetProperty( JavaScriptPropertyId.FromString( "myArray") );
var pushFunctionObject = arrayObjectGetProperty( JavaScriptPropertyId.FromString( "push" ) );
var result = pushFunctionObject.CallFunction( JavaScriptValue.GlobalObject, jsTextToInsert );

var length = (int)arrayObject.GetProperty( JavaScriptPropertyId.FromString( "length" ) ).ToDouble();
Debug.Assert( length == 1 );

I can add items to the array using an alternative indexed property approach but it would be nice the 'push' function worked:

JavaScriptValue indexValue = JavaScriptValue.FromInt32( length );
arrayObject.SetIndexedProperty( indexValue, jsFileName );

Any ideas what I am doing wrong, or is this an issue; calling other functions works fine.

@liminzhu
Copy link
Member

liminzhu commented Mar 11, 2017

Hi @coderangerdan . Speaking in terms of JS, your code can be roughly translated to,

let arr = [];
let p = arr.push;
p.call(global, 'new string');
arr.length; // eval to 0 in JS

In this case you want arr to be the thisArg for call, or bind p to arr in the first place.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants