https://saxonica.plan.io/https://saxonica.plan.io/favicon.ico2022-08-22T22:53:14ZSaxonica Developer CommunitySaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=216072022-08-22T22:53:14ZMichael Kaymike@saxonica.com
<ul></ul><p>The specification of <code>ixsl:call</code> at <a href="https://www.saxonica.com/saxon-js/documentation2/index.html#!ixsl-extension/functions/call" class="external">https://www.saxonica.com/saxon-js/documentation2/index.html#!ixsl-extension/functions/call</a> says</p>
<p>See <em>Type Conversion</em> for details on how the supplied parameters in $arguments are converted from XDM to JavaScript, and how any returned JavaScript objects are converted to XDM.</p>
<p>I believe that no conversion is applied to the <code>$object</code> and <code>$method</code> arguments of <code>ixsl:call</code>, only to the values supplied in <code>$arguments</code>.</p> SaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=216082022-08-23T06:54:25ZMartynas Jusevicius
<ul></ul><p>Hmm, okay. Any workarounds for this then?</p> SaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=216092022-08-23T08:18:03ZMichael Kaymike@saxonica.com
<ul></ul><p>It rather depends on what you are trying to achieve, which isn't clear. What's wrong with <code>array:append()</code>?</p>
<p>I would discourage any attempt to modify an XDM array in-situ. Saxon is going to assume that the contents of a variable remain unchanged once bound, and any attempt to subvert that is going to have very unpredictable consequences. I think that the conversion rules mean you'll normally get a copy of the array anyway.</p> SaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=216102022-08-23T12:38:24ZMartynas Jusevicius
<ul></ul><p>There is still something weird going on, if not with array conversion then with <code>ixsl:set-property</code> in combination with arrays.</p>
<pre><code><xsl:variable name="js-array" select="ixsl:eval('Array(1, 2)')"/>
<xsl:value-of select="ixsl:call(ixsl:get(ixsl:window(), 'Array'), 'isArray', [ $js-array ])"/> <!-- true -->
<xsl:value-of select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'stringify', [ $js-array ])"/> <!-- [1, 2] -->
<ixsl:set-property name="prop" select="$js-array"/>
<xsl:value-of select="ixsl:call(ixsl:get(ixsl:window(), 'Array'), 'isArray', [ ixsl:get(ixsl:window(), 'prop') ])"/> <!-- false -->
<xsl:value-of select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'stringify', [ ixsl:get(ixsl:window(), 'prop') ])"/> <!-- 1 -->
</code></pre>
<p>This test case shows that <code>$js-array</code> is an array as expected, but after it's been set as the value of <code>window.prop</code>, it's not an array anymore.</p> SaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=216772022-08-31T15:37:39ZNorm Tovey-Walsh
<ul><li><strong>Sprint/Milestone</strong> set to <i>SaxonJS 2.5</i></li></ul> SaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=217402022-09-05T13:00:48ZNorm Tovey-Walsh
<ul></ul><p>I think Mike is right about the attempt to call <code>push</code> on an XDM array. The first argument to <code>ixsl:call</code> is expected to be a (native) JavaScript object.</p>
<p>The <code>ixsl:set-property</code> problem is trickier. It feels like that should work, and I have managed to make it work, but I'm not sure I like my workaround.</p> SaxonJS - Bug #5656: XDM array is not converted to a true JS array?https://saxonica.plan.io/issues/5656?journal_id=218472022-09-26T09:10:20ZNorm Tovey-Walsh
<ul><li><strong>Sprint/Milestone</strong> changed from <i>SaxonJS 2.5</i> to <i>SaxonJS 3.0</i></li><li><strong>Applies to JS Branch</strong> deleted (<del><i>2</i></del>)</li></ul><p>The "problem" here is that the type conversion rules apply to the return value of <code>ixsl:get</code>. So when you get the JavaScript array, it's converted into an XDM array, and then it isn't a JSON array anymore.</p>
<p>I don't think we can fix this in 2.x. What we need is a new function, <code>ixsl:get-javascript</code> or <code>ixsl:get-raw</code> or <code>ixsl:get-unconverted</code> or something (or an additional, optional argument on <code>ixsl:get</code> perhaps) that returns the value without type conversion. Then what you'd get back is a JavaScript object and it would still be an array.</p>