mobileFX JavaScriptX - Google V8 JavaScript Embedded Framework
JavaScriptXLib ActiveX DLL / ScriptControl Object / Eval Method

Required. String containing the expression being evaluated.

In This Topic
    Eval Method
    In This Topic

    Evaluates a JavaScript expression and returns the result.

    Visual Basic
    Public Function Eval( _
       ByVal Expression As String _
    ) As Variant

    Required. String containing the expression being evaluated.

    Return Type

    Variant containing the result of Eval.


    Typically this should be ECMA JavaScript code but you can also pass ES6 JavaScript code to the control.

    There are four different ways to execute a program using JavaScriptX. The simplest way is with the Eval method. This method returns the value of the specified expression. For instance x = JavaScriptX.Eval(“1+2”) will assign a value of 3 to the variable x. The Eval method can also reference functions and variables that are defined in either the global module or the local module, if the method was invoked from a local module. It also can access any resource declared as public in any module.

    You can also execute a single statement by using the ExecuteStatement method, as in: JavaScriptX.ExecuteStatement(“alert('Hello');"). This method works just like the Eval method and can access resources in the module it was declared, in public variables declared in any module, and in the global module.

    Another way to execute script code is to use the Run method. This method allows you to execute any subroutine declared in the JavaScriptX. The subroutine may call any other subroutine or access any objects according to the rules that are used to create modules. You also can specify an array containing the parameters to be passed to the subroutine.

    The AddCode method adds a block of code of code to JavaScriptX. During this process, the syntax of the code is checked, and the first error found will trigger the Error event.

    NOTE: When you run a script using the JavaScriptX, you can’t change most of the properties or use any of the methods until the script has finished.

    WARNING: Be sure to keep a separate copy of the code to which you added the JavaScriptX. There is no way to retrieve code from the control once it has been added.

    OLE/COM Variants to V8 Values Conversion

    JavaScriptX has a powerful OLE/COM Variant to Google V8 Value bi-directional converter that works both for Scalar and Array values, including nested array values (arrays of arrays of variants). JavaScriptX can serialize variant scalars VT_BSTR, VT_UINT, VT_INT, VT_NULL, VT_BOOL, VT_R4, VT_R8, VT_CY, VT_DATE as well as variant safearray of variants VT_ARRAY|VT_VARIANT and even typed arrays VT_ARRAY|VT_BSTR, VT_ARRAY|VT_UINT, VT_ARRAY|VT_INT, VT_ARRAY|VT_BOOL, VT_ARRAY|VT_R4, VT_ARRAY|VT_R8, VT_ARRAY|VT_CY, VT_ARRAY|VT_DATE to Google V8 value objects. Currently serialization of VT_DISPATCH is not supported and therefore you cannot pass OLE/COM Objects to JavaScript with IDispatch members or arguments.

    Private Sub Form_Load()
        Set SC = New ScriptControl
        SC.Language = "JavaScript"
        SC.AllowUI = True
        SC.SitehWnd = Me.hWnd
        SC.UseSafeSubset = False
        SC.Timeout = 60
        Dim Result As Variant
        SC.AddCode "function foo(){ return 'Hello!' }"
        Result = SC.Eval("foo()")
    End Sub
    Option Explicit
    Private SC As ScriptControl
    Private MyClass As Class1
    Private Sub Form_Load()
        Set MyClass = New Class1
        Dim i As Long
        Dim Result As Variant
        For i = 1 To 50
            ' Destroy previous Script Control
            Set SC = Nothing
            ' Create new Script Control
            Set SC = New ScriptControl
            Debug.Print SC.DeveloperUUID
            SC.Language = "JavaScript"
            SC.AllowUI = True
            SC.SitehWnd = Me.hWnd
            SC.UseSafeSubset = False
            SC.Timeout = 60
            SC.AddCode "function foo(){ alert('test') }"
            ' Add a COM object
            SC.AddObject "MyClass", MyClass
            ' Set COM object member field from JavaScript
            SC.Eval "MyClass.C=" & i
            ' Get COM object member property from JavaScript
            Result = SC.Eval("MyClass.C")
            Debug.Print i, "eval MyClass.C = " & Result
            ' Set COM object member property from JavaScript
            SC.Eval "MyClass.D=" & i
            ' Invoke COM object member function from JavaScript
            Result = SC.Eval("" & i & "," & i & ")")
            Debug.Print i, "eval = " & Result
            ' Use Run method
            Result = SC.Run("eval", "1+2")
            Debug.Print i, "eval(1+2) = " & Result
            ' Use Run method
            SC.ExecuteStatement "function foo(){return 4;}"
            SC.ExecuteStatement "eval(1+2)"
            ' Exception
            'Result = SC.Run("eval", "1/c")
            'Debug.Print i, "eval(1/0) = " & Result
    End Sub
    Option Explicit
    Public C As Long
    Private m_D As Long
    Private m_I(100) As Long
    Public Property Get D() As Long
        D = m_D
    End Property
    Public Property Let D(ByRef v As Long)
        m_D = v
    End Property
    Public Function foo(ByVal a As Long, ByRef b As Long) As Long
        foo = a + b + C + D
    End Function
    See Also