Look Ma, I can override global variables, even if Option Explicit is active, but why?

Here is one wonderful crazy discovery:

Option Explicit ExecuteGlobal "Option Explicit: Dim TestVar: TestVar=41" ExecuteGlobal "Option Explicit: TestVar=42" MsgBox "TestVar=" & CStr (TestVar) 

works as expected - displays 42.

and

 Option Explicit ExecuteGlobal "Option Explicit: TestVar: TestVar=41" MsgBox "TestVar=" & CStr (TestVar) 

returns an "Undefined variable" in an ExecuteGlobal call because TestVar is undefined. OK

I do not understand what it is:

 Option Explicit ExecuteGlobal "Option Explicit: Dim TestVar: TestVar=41" ExecuteGlobal "Option Explicit: Dim TestVar: TestVar=42" MsgBox "TestVar=" & CStr (TestVar) 

it does not return "Identifier ExecuteGlobal " in the second call to ExecuteGlobal , but displays 42 - as if Dim not present in the second call to ExecuteGlobal .

If you do the same with Class declarations, everything works fine, i.e. you cannot override a class under any circumstances.

What the heck?

My question is: Why ExecuteGlobal allow me to override a global variable, but a) ExecuteGlobal forbids access to undeclared variables and b) class definitions are processed differently?

I have a usecase that leads to this (generating the source code during testing and executing it through ExecuteGlobal for some reason not-as-weird as you might expect), but the points I just made are valid enough without a description real selenario real world i think.

I came across this using QTP (HP QuickTest Professional), which uses the VisualBasic script host engine to play the script, but this is exactly the same situation only on the VB script node.

+4
source share
1 answer

Following code

 Option Explicit ExecuteGlobal "WSCript.Echo b " 

won't let you down. The executeglobal context is unaware of an explicitly declared option. But

 Option Explicit ExecuteGlobal "Option Explicit : WSCript.Echo b " 

does not work with runtime error. Everyting works, but in a separate environment. AND

 Option explicit Dim b ExecuteGlobal "Option Explicit : WScript.Echo b " 

It works as expected.

In the following code

 Option Explicit ExecuteGlobal "Option Explicit: Dim a : a = 1 : Dim a : a = 2" 

you will get an overridden name error. And this is a compiler error, not a runtime error.

If, as indicated, you are doing the same with classes

 Option Explicit Class thisThing End Class ExecuteGlobal "class thisThing : End Class" 

you get a runtime error, redefined name.

So, from your tests and these tests (and a few more), ExecuteGlobal โ€œseemsโ€ to generate a new context, work inside it during execution of the transmitted code, and when exiting, the context is combined with the original call context.

So, to answer your questions:

a) Variables can be "redefined" if they are executed in different contexts. The values โ€‹โ€‹of the variables are combined.

b) ExecuteGlobal does not allow access to undefined variables if the ExplicitGlobal option is explicitly used.

c) A variable is a variable. The value can be changed to ExecuteGlobal, and it merges on exit. But class redefinition changes that something is, and not that contains somenthing.

I did not decompile the VBScript engine, but this seems like the observed behavior.

+5
source

Source: https://habr.com/ru/post/1488241/


All Articles