Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Variable shadowing bug in generated JavaScript code #485

Closed
jeromesimeon opened this issue Dec 13, 2018 · 4 comments
Closed

Variable shadowing bug in generated JavaScript code #485

jeromesimeon opened this issue Dec 13, 2018 · 4 comments

Comments

@jeromesimeon
Copy link
Member

For the JavaScript target, when using operators, the compiler produces JavaScript code with erroneous variable shadowing.

This can be seen on the following unit test.
inlinetest.zip

To test:

bash-3.2$ ergorun ./model.cto ./logic.ergo --contractname org.accordproject.inlinetest.InlineTest --contract ./contract.json --request ./request1.json --state ./state.json 
12:35:12 - info:
{
  "response": {
    "x": 10,
    "y": "2018-11-01T04:00:00.000Z",
    "$class": "org.accordproject.inlinetest.Response"
  },
  "state": {
    "$class": "org.accordproject.cicero.contract.AccordContractState",
    "stateId": "1"
  },
  "emit": []
}

Note that on test2, using a let binding you get the correct result:

bash-3.2$ ergorun ./model.cto ./logic.ergo --contractname org.accordproject.inlinetest.InlineTest --contract ./contract.json --request ./request2.json --state ./state.json 
12:35:17 - info:
{
  "response": {
    "x": 0,
    "y": "2018-11-01T04:00:00.000Z",
    "$class": "org.accordproject.inlinetest.Response"
  },
  "state": {
    "$class": "org.accordproject.cicero.contract.AccordContractState",
    "stateId": "1"
  },
  "emit": []
}
@jeromesimeon
Copy link
Member Author

jeromesimeon commented Dec 13, 2018

The issue can be seen on the generated JavaScript code:

bash-3.2$ ergoc ./model.cto ./logic.ergo 
Compiling Ergo './logic.ergo' -- creating './logic.js'

lines 121-122 of the compiled logic.js:

    var vp1 = 0.0;
    var vp1 = "2018-01-01";

so the first constant 0.0 in vp1 is over-ridden.

@jeromesimeon
Copy link
Member Author

jeromesimeon commented Dec 14, 2018

Here is a simpler inlining test which illustrate the problem on a simpler example using Ergo functions:
inlinetest.zip

Again, running it:

bash-3.2$ ergorun ./model.cto ./logic.ergo --contractname org.accordproject.inlinetest.InlineTest --contract ./contract.json --request ./request1.json --state ./state.json 
23:47:03 - info:
{
  "response": {
    "x": 10,
    "y": 22,
    "$class": "org.accordproject.inlinetest.Response"
  },
  "state": {
    "$class": "org.accordproject.cicero.contract.AccordContractState",
    "stateId": "1"
  },
  "emit": []
}

Again, compiling it with:

bash-3.2$ ergoc ./model.cto ./logic.ergo 
Compiling Ergo 'logic.ergo' -- creating 'logic.js'

lines 17-18 shows the problem:

  var vp1 = 0.0;
  var vp1 = 10.0;

@jeromesimeon
Copy link
Member Author

After adding some instrumentation to print out the NNRC module during compilation, one can see that the code looks correct:

NNRC Module: 
FUNCTION: org.accordproject.inlinetest.h
#brand[org.accordproject.inlinetest.Response]([ x :
                                                let $vp1 := 0.000000 in $vp1 ]
                                              [+]
                                              [ y :
                                                let $vp1 := 10.000000
                                                in
                                                  let $vp2 := 12.000000
                                                  in $vp1 F+ $vp2 ])

Which means the issue is in the Qcert JavaScript code generation.

A couple of options to address the issue:

  1. Switch to the more recent JavaScript code-generation from Qcert (i.e., implement Switch to improved JavaScript backend #191)
  2. Add a global variable renaming phase before code-generation to avoid name collisions

@jeromesimeon
Copy link
Member Author

Besides the fix that was implemented for the shadowing bug a few months ago, we are now using a completely new JavaScript backend which produces properly JavaScript blocks with a proof that it handles scoping properly. (See PR #732)

I'm considering this issue finally resolved and closing the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant