Forum Stats

  • 3,758,923 Users
  • 2,251,475 Discussions
  • 7,870,436 Comments

Discussions

Variables from js scripts live on in SQLcl past execution of the script?

ptfl
ptfl Member Posts: 33
edited May 16, 2018 1:29AM in SQLcl

I just noticed, thanks to a typo, that variables I created in one js script stay available in SQLcl after execution and can be (in my case accidentally) referenced by subsequent scripts.

If I exit SQLcl and restart it again, it looks like variables are cleared -- is there a way to 'clear' all previous variables etc at the end of a script, or start of the next script, without having to exit and restart?

SQLcl version is 18.1.1, Oracle Server is 11g

My simple test of this is:

create "test_1.js" with single line of:

test_variable = "this is a test";

create "test_2.js" with single line of:

ctx.write(test_variable + "\n");

run the two scripts:

script "test_1.js"

script "test_2.js"

Output should be "this is a test"

ptfl

Best Answer

  • Gaz in Oz
    Gaz in Oz Member Posts: 3,782 Bronze Crown
    edited May 11, 2018 2:51AM Accepted Answer

    In answer to your questions:

    |  Variables from js scripts live on in SQLcl past execution of the script?

    Yes. Of course.

    is there a way to 'clear' all previous variables etc at the end of a script, or start of the next script, without having to exit and restart?

    Yes.

    Use javascript delete operator to remove the variable.

    For example:

    SQL> $type test_1.jstest_variable = "this is a test";delete test_variable;SQL> $type test_2.jsctx.write(test_variable + "\n");SQL> script test_1.jsSQL> script test_2.jsjavax.script.ScriptException: ReferenceError: "test_variable" is not defined in <eval> at line number 1        at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)...Caused by: <eval>:1 ReferenceError: "test_variable" is not defined        at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)...etc.
    ptflptfl

Answers

  • Gaz in Oz
    Gaz in Oz Member Posts: 3,782 Bronze Crown
    edited May 11, 2018 2:51AM Accepted Answer

    In answer to your questions:

    |  Variables from js scripts live on in SQLcl past execution of the script?

    Yes. Of course.

    is there a way to 'clear' all previous variables etc at the end of a script, or start of the next script, without having to exit and restart?

    Yes.

    Use javascript delete operator to remove the variable.

    For example:

    SQL> $type test_1.jstest_variable = "this is a test";delete test_variable;SQL> $type test_2.jsctx.write(test_variable + "\n");SQL> script test_1.jsSQL> script test_2.jsjavax.script.ScriptException: ReferenceError: "test_variable" is not defined in <eval> at line number 1        at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)...Caused by: <eval>:1 ReferenceError: "test_variable" is not defined        at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)...etc.
    ptflptfl
  • ptfl
    ptfl Member Posts: 33
    edited May 11, 2018 5:37AM

    Interesting, this is standard behaviour?

    Is there no way to reset the entire environment, rather than variable by variable?

    I'm curious about situations where you might be running scripts from different developers, any of which might clash on a variable name in an unexpected way.

    ptfl

  • Gaz in Oz
    Gaz in Oz Member Posts: 3,782 Bronze Crown
    edited May 11, 2018 7:14AM
    Interesting, this is standard behaviour?

    Yes. A UI session that gets a variable defined, I would expect it to stay defined until it is explicitly undefined or the UI session is terminated/ended.

    Is there no way to reset the entire environment, rather than variable by variable?

    Yes, there is a way, which is also standard, terminate and restart the UI session.

    Instead of keeping your single session "running" and worrying about how the "developers" have defined javscript variables as "local" or "global", review the script/s before running, approve them, then run  them up in their own sqlcl instances:

    I'm curious about situations where you might be running scripts from different developers, any of which might clash on a variable name in an unexpected way.

    If a developer "uses" a variable before defining it, then the script is run in a UI session that already had that variable defined and NOT undefined, then sure, it will pick up the value that is already defined. That is a bug in the developers code that used a variable before defining it (and thus not setting/resetting/changing its value). Not the UI's fault, the developers.

    To make sure that sort of bug is picked up, run all scripts in their own sqlcl instance. No big deal.

    If there were two different UI sessions running at the same time, then those variables would be exclusive to the individual UI session.

    For example, if you had 5 scripts to run, from different developers, who's code you haven't reviewed and so can not trust, you could loop through each, in a shell script and run them one by one.

    ptfl