pml-lang / pml-companion

Java source code of the 'PML Companion (PMLC)'
https://www.pml-lang.dev
GNU General Public License v2.0
22 stars 1 forks source link

JavaScript Date function fails in user-defined node #71

Closed coemgenuslcp closed 2 years ago

coemgenuslcp commented 2 years ago

The following user-defined node

[node
    [name timestamp]
    [writer
        [script
            """
            context.write(new Date())
            """
        ]
    ]
]

yields the following error in PML 2.2.0 2021-12-14.

Running PMLC on the file "myfolder/Main.pml" ...
Error      The following error occurred when a script was executed for node 'timestamp':                              
           java.lang.ExceptionInInitializerError                                                                       
Error id   SCRIPTING_ERROR                                                                                             
-----------------------------------------------------------------------------------------------------------------------

THE FOLLOWING APPLICATION ERROR OCCURRED (2022-02-01T09:41:45.650332221):
Java throwable: org.graalvm.polyglot.PolyglotException: java.lang.ExceptionInInitializerError
See file 'myfolder/errors.log' for more information.

errors.log

pml-lang commented 2 years ago

I could reproduce the error.

It is caused by the GraalVM Javascript implementation used in PML.

The root error is:

java.util.MissingResourceException: Could not find the bundle com/ibm/icu/impl/data/icudt70b/supplementalData

I checked the relevant jar file and saw a file named supplementalData.res, but there is no file named supplementalData. So it seems to be a bug in the Javascript implementation. I couldn't find a workaround for this. Hope the bug will be solved in a future version.

Anyway, in the next PML version (that includes 'embedded scripting') there will be a dedicated timeUtils object with functions like currentLocalDate() that will return a string in the ISO8601 format YYYY-MM-DD. So you will be able to write:

context.write ( timeUtils.currentLocalDate() );
pml-lang commented 2 years ago

UPDATE:

I made a few more tests. The error is not caused by new Date(). It is caused by the implicit conversion to string when context.write() is called.

Hence, you could use a workaround like this:

const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1; // 0-based
const day = date.getDate();
const dateAsString = `${year}-${month}-${day}`;
context.write ( dateAsString );

I've closed the issue because it is not related to PML.