MicrosoftEdge / MicrosoftEdge/WebView2Feedback
Indexing into collection exposed through AddHostObjectToScript passes numerical index as string
Nobody has claimed this yet.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
Description
When a COM collection is exposed through AddHostObjectToScript, indexing into the collection calls the Item method passing the index argument as a string instead of the numerical value specified in Javascript.
The collection is defined as:
interface IDictionary : IDispatch {
...
[propget, id(DISPID_VALUE), helpstring("Given an index, returns the element in the dictionary. If the index is an integer, then method returns the element in the dictionary at the given position. If the index is a string, then the method returns the entry with the specified name.") ]
HRESULT Item([in] VARIANT IndexOrName, [out, retval] IDictionaryEntry **ppRetVal);
...
};
This dictionary is exposed to Javascript using the ICoreWebView2::AddHostObjectToScript method, and later on accessed from Javascript using:
var dict = window.chrome.webview.hostObjects.sync.dictionary;
return dict[1].Value;
The call to dict[1] generates an IDispatch call where the IndexOrName parameter is "1" passed as a string instead of the expected numerical value. Similarly, when looping over the dictionary from Javascript using:
var dict = window.chrome.webview.hostObjects.sync.dictionary;
var result = '';
for (var i=0; i<dict.Count; ++i) {
result = result + dict[i].Value;
}
return result;
The loop index (i) gets passed as a string too.
As a workaround I tried to invoke the Item method explicitly:
var dict = window.chrome.webview.hostObjects.sync.dictionary;
return dict.Item(1).Value;
but that does not work either and returns an error:
Member not found. (0x80020003)
Version
SDK: 1.0.664.37
Runtime: GA 87.0.664.41
Framework: Win32
OS: Win10 version 1909
Repro Steps
To reproduce:
- unzip the project attached (WebView2HostObjectTests.zip)
- build and run the solution
- the unit tests that rely on indexing into the dictionary using a numerical index fail:
- DictionaryTests.IndexUsingSquareBrackets
- DictionaryTests.IndexUsingItemMethodExplicitly
- DictionaryTests.IterateUsingNumericalIndex
- to debug the issue run the DictionaryTests.IndexUsingSquareBrackets test in the debugger with a breakpoint on CDictionary::get_Item, and you will see that the index is passed as a string.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the attached WebView2HostObjectTests project and run DictionaryTests.IndexUsingSquareBrackets, IndexUsingItemMethodExplicitly, and IterateUsingNumericalIndex. Debug CDictionary::get_Item to compare the VARIANT type received from bracket indexing and explicit Item calls. Done means numeric JavaScript indexes reach Item as numerical values and all three tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100