Book a Demo

Author Topic: Javascript engine broken... or am I missing something here?  (Read 4802 times)

Benno Dielmann

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-1
    • View Profile
Javascript engine broken... or am I missing something here?
« on: April 04, 2018, 12:42:48 am »
Hi everybody,

I have written a bunch of JScript scripts which I want to port over to Javascript now to benefit from stuff available only there (e.g. the filter() method of Array objects).

I'm getting a couple of very strange problems with this, for example with Element.HasStereotype(). Its return value is boolean both in JScript and Javascript. In Javascript there seems to be something wrong, though. If the function call returns "true", it evaluates to "false"...

Let me demonstrate:

1. Create a diagram (e.g. a "Custom Diagram" from EA's Extended category) with a test case element on it (because we need an element with a stereotype ("testcase")).
2. Create two new scripts, one JScript and one Javascript in a Diagram Scripts folder. Replace the automatically generated code with this identical code in both scripts:

Code: [Select]
!INC Local Scripts.EAConstants-JScript

function main()
{
Repository.EnsureOutputVisible("Script");
        Repository.ClearOutput("Script");
var currentDiagram = Repository.GetCurrentDiagram();

var el as EA.Element;
el = Repository.GetElementByID(currentDiagram.SelectedObjects.GetAt(0).ElementID);

Session.Output("Stereotype: " + el.Stereotype);
Session.Output("HasStereotype('testcase'): " + el.HasStereotype('testcase'));
Session.Output("HasStereotype('testcase') == true: " + (el.HasStereotype('testcase') == true));
Session.Output(typeof(el.HasStereotype('testcase')));
}

main();

3. Execute both scripts by right-clicking on the testcase element and selecting the script from the Scripts submenu.

Here is what I get with the JScript:

Code: [Select]
Stereotype: testcase
HasStereotype('testcase'): true
HasStereotype('testcase') == true: true
boolean

So far, so good.

Now here's the output of the identical code executed as Javascript:

Code: [Select]
Stereotype: testcase
HasStereotype('testcase'): true
HasStereotype('testcase') == true: false
boolean

As you can see, HasStereotype returns true in both cases and typeof() says the return value's type is boolean. Only difference: HasStereotype('testcase') == true evaluates to false in Javascript.

What is going on here???

Any clarification or help is highly appreciated.

Benno
All models are wrong. Some are useful. - George E. P. Box

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Javascript engine broken... or am I missing something here?
« Reply #1 on: April 04, 2018, 01:17:56 pm »
I'd say it has to do with the way COM handles booleans, and that JScript has a closer integration with COM than Javascript.

COM is unusual as a framework that the integer value of true (VARIANT_TRUE) is not 1. It's -1. Apparently JScript handles that, but the way COM is being called from Javascript isn't wrapping that value into a 'normal' true. It doesn't make any difference until you try to compare it. Then again, I remember being told very early on that you shouldn't try to compare two different true values.

Code: [Select]
Stereotype: testcase
HasStereotype('testcase'): true
HasStereotype('testcase') == true: false
boolean

Benno Dielmann

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-1
    • View Profile
Re: Javascript engine broken... or am I missing something here?
« Reply #2 on: April 04, 2018, 06:07:34 pm »
Thank you, Simon, for your quick answer.

Unfortunately, I don't know much about COM and have never been told not to compare true values...

What I was actually trying to do in my code is find out if an element has got at least one of two stereotypes:

Code: [Select]
if (el.HasStereotype('testcase') || el.HasStereotype('something')) {
Session.Output("alright1");
}

if (el.HasStereotype('something') || el.HasStereotype('testcase')) {
Session.Output("alright2");
}

This code, appended to the code in my first post, prints

Code: [Select]
alright1
alright2

when run as JScript, but prints only

Code: [Select]
alright2

in Javascript. I don't understand if this is caused by the same issue... As I don't understand what is happening here.

What do you suggest to do in Javascript in this case?

By the way,

Code: [Select]
Session.Output(el.HasStereotype('testcase') == -1);

prints "False" in both scripting engines. Comparing to 1 instead of -1 prints True in both...
All models are wrong. Some are useful. - George E. P. Box

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Javascript engine broken... or am I missing something here?
« Reply #3 on: April 06, 2018, 08:45:31 am »
That extra information makes a difference. It seems like the logical or operator is ignoring the right side. I certainly can't explain why.