An example to share vb6 script control vs clearscript

Jul 23, 2013 at 9:09 PM
Edited Jul 23, 2013 at 9:10 PM
In my current VB6 app I use scripting to plug in classes that the scripting can maniplate and also call functions within my plugged in classes to effect change and act as events in my main app. This is a bit of test code that shows the same capacity in clearscript.

VB6 EXAMPLE

SIMPLE CLASS VBStest

public A as Long
publc B as Long


Set SC = New ScriptControl
SC.AllowUI = False
SC.Language = "VBScript"
SC.Reset

Set TEST1 = New VBStest
TEST1.A = 1000
TEST1.B = 2000
SC.AddObject "TEST1", TEST1, True

newCode = "Public Sub addtoTEST1A(inval)" & _
        "    TEST1.A = TEST1.A + inval" & _
        "End Sub"
SC.AddCode newCode

T1 = new VBStest;
T1.A = 3000
T1.B = 4000
TEST1 = T1;

SC.RUN "addtoTEST1A(" & 5000 & ")"

' TEST1.A is now 8000

CLEARSCRIPT

public class V8test
{

  public int A = 0;
  public int B = 0; 

}

public void testV8()
{

    V8ScriptEngine V8JS = new V8ScriptEngine();
string newCode = @"
var TEST1;

function setTEST1(inval)
{
    TEST1 = inval;
}

function addtoTEST1A(inval)
{
   TEST1.A += inval;
}
";
    V8JS.Execute(newCode);

            V8test T1 = new V8test();
            T1.A = 3000;
    T1.B = 4000;

    // V8JS.Script.TEST1 = T1; did not work at setting to new object
            V8JS.Script.setTEST1(T1);

            V8JS.Script.TEST1.A += 5000;

          // T1.A and V8JS.Script.TEST1.A are same object, value 8000

          V8test T2 = new V8test();
          T2.A = 1000;
      T2.B = 2000;

          V8JS.Script.setTEST1(T2);

          V8JS.Script.addtoTEST1A(3000);

          // T2.A and V8JS.Script.TEST1.A are same object, value 4000

}
Coordinator
Jul 24, 2013 at 6:35 PM
Thanks for sharing this sample, notahack! We're concerned about the comment indicating that the Script property did not allow you to assign a global property. We're going to see if we can reproduce that.
Jul 25, 2013 at 12:44 PM
Edited Jul 25, 2013 at 12:56 PM
It sure would be easier and more elegant if assigning a new object to a variable was as easy as V8JS.Script.TEST1 = T1;

This also works to assign a new object to a variable but it didn't 'look' right.

T1 = new V8test();
V8JS.AddHostObject("TEST1", T1);

T2 = new V8test();
V8JS.AddHostObject("TEST1", T2);
Coordinator
Jul 25, 2013 at 1:17 PM
Edited Aug 3, 2013 at 3:16 AM
It sure would be easier and more elegant if assigning a new object to a variable as as easy as V8JS.Script.TEST1 = T1;

ClearScript's test code uses this kind of assignment in many places; see the V8ScriptEngine_Script_Property test for an example. We've just tested it with your V8test class and setup code exactly as above and could not reproduce any issue. Does it still fail for you? If so, what are you seeing? Is there an exception?
Jul 26, 2013 at 3:22 AM
nope assigning an object to a createdobject or assigning a new object to the object passed in does not work for me.
        V8ScriptEngine V8JS = new V8ScriptEngine("FRED");

        string newCode = @" 

var TEST1;

function setTEST1(inval)
{
    TEST1 = inval;
}

function addtoTEST1A(inval)
{
   TEST1.A += inval;
}
";
        V8JS.Execute(newCode);

        V8test T1 = new V8test();
        T1.A = 3000;
        T1.B = 4000;
        V8JS.AddHostObject("TEST1", T1);

        V8JS.Script.TEST1.A += 5000;

        // T1.A and V8JS.Script.TEST1.A are same object, value 8000

        V8test T2 = new V8test();
        T2.A = 1000;
        T2.B = 2000;
        // does not work
        V8JS.Script.TEST1 = T2;
        // does not work either
        T1 = T2;

        V8JS.Script.addtoTEST1A(2000);

        // V8JS.Script.TEST1.A = 10000
        // T1.A = 10000
        // T2.A = 1000
        // ergo V8JS.Script.TEST1 = T2 does not work
        int a = 0;
    }

    public class V8test
    {

        public int A = 0;
        public int B = 0;

    }
Coordinator
Jul 27, 2013 at 10:41 AM
Edited Aug 3, 2013 at 3:17 AM
The code above initially calls AddHostObject() to create the "TEST1" global property. What you may not realize - most likely because of insufficient documentation on our part - is that AddHostobject() creates a read-only global property. Subsequent reassignment is quietly ignored. This is by design - to prevent script code from clobbering the host's API, and to achieve compatibility with ClearScript's original JScript/VBScript behavior.

On the other hand, properties created via Script can be reassigned normally. Instead of:
V8JS.AddHostObject("TEST1", T1);
please try:
V8JS.Script.TEST1 = T1;
Cheers!
Jul 27, 2013 at 1:24 PM
SHAZAM!! A little knowledge goes a long way.

I love this project and intend to wring this code out. In my production software I use scripting to process 1000s of documents a day and affect workflow decisions throughout the application.