Project

Profile

Help

Feature #5057

open

Constructing JS objects

Added by Martynas Jusevicius over 2 years ago. Updated over 1 year ago.

Status:
New
Priority:
High
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


Related issues

Related to SaxonJS - Feature #5530: For Saxon-JS under Node, expose global as ixsl:window() or ixsl:global()In Progress2022-05-21

Actions
Actions #1

Updated by Martynas Jusevicius over 2 years 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.

Actions #2

Updated by Norm Tovey-Walsh over 2 years 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.

Actions #3

Updated by Martynas Jusevicius over 2 years 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?

Actions #4

Updated by Norm Tovey-Walsh over 2 years 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.

Actions #5

Updated by Martynas Jusevicius over 2 years ago

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

Actions #8

Updated by Norm Tovey-Walsh almost 2 years ago

  • Related to Feature #5530: For Saxon-JS under Node, expose global as ixsl:window() or ixsl:global() added
Actions #9

Updated by Norm Tovey-Walsh almost 2 years ago

  • Priority changed from Normal to High
  • Sprint/Milestone set to SaxonJS 3.0
Actions #10

Updated by Martynas Jusevicius over 1 year ago

ixsl:new() without arguments could be used to create an empty JSON object ({ }).

Please register to edit this issue

Also available in: Atom PDF Tracking page