Project

Profile

Help

Feature #5057

Constructing JS objects

Added by Martynas Jusevicius 3 months ago. Updated 2 months ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2021-08-07
Due date:
% Done:

0%

Estimated time:
Applies to JS Branch:
2
Fix Committed on JS Branch:
Fixed in JS Release:
SEF Generated with:
Platforms:
Company:
-
Contact person:
-
Additional contact persons:
-

Description

Hi. I cannot seem to find a native way to construct JS objects, which makes porting JS code to XSLT complicated. It think it was possible with Saxon-CE, but I might be wrong.

I know there is ixsl:eval(), but since it accepts string as parameter, how does one pass a JS object from a $var for example? It does not seem possible.

So how would I port some simple JS code like the following?

  var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([37.41, 8.82]),
      zoom: 4
    })
  });

https://openlayers.org/en/latest/doc/quickstart.html

History

#1 Updated by Martynas Jusevicius 2 months ago

As always Martin Honnen has a solution:

  <xsl:function name="mf:construct" as="item()">
    <xsl:param name="constructor" as="xs:string"/>
    <xsl:param name="arguments" as="array(*)"/>
    <xsl:sequence
      select="ixsl:window()
              => ixsl:call(
                   'Reflect.construct', 
                    [ixsl:window() => ixsl:get($constructor), $arguments]
                 )"/>
  </xsl:function>

I just think it feels like that should be a built-in function under the ixsl namespace.

#2 Updated by Norm Tovey-Walsh 2 months ago

Hi Martynas,

I'm having a little trouble understanding exactly what you're trying to do. I think you're probably right that an ixsl function makes sense, but I'd like to be able to construct some more test cases.

In the initial report where you've got the var map = ... example. Are you saying you'd like to be able to construct that whole map as a JavaScript object from the Saxon-JS side?

It seems to me you could construct it as an XDM map pretty easily. Is there a context where you want to use it that doesn't work if it's an XDM map?

Sorry if I'm being slow on the uptake.

#3 Updated by Martynas Jusevicius 2 months ago

Basically I'm looking for a way to invoke the new keyword. Yes I'm looking to construct the object from the Saxon-JS side, or rather multiple objects I could store in separate variables and then compose as necessary. I'd like to be able to port JS code like that to Saxon-JS.

Maybe it can be solved using XDM maps -- could I please see a small example of that? I was looking at the XDM to JavaScript Conversion and my impression was that there is not a direct way to construct a JS object as an XDM map? How do you specify the class name such as ol.Map or ol.source.OSM?

#4 Updated by Norm Tovey-Walsh 2 months ago

Right. I was fixated on the "object" part not the "new" part. I think the workaround that Martynas proposed might be the current workaround. Another possibility, if there are only a small number of objects like this, would be to create global JavaScript functions that do the construction.

#5 Updated by Martynas Jusevicius 2 months ago

Yes the workaround works. I would like to propose an ixsl:new extension function though. For example: ixsl:new('Class', [ $arg ]).

Please register to edit this issue

Also available in: Atom PDF Tracking page