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

Required. String containing the expression being evaluated.

Eval Method
Description

Evaluates a JavaScript expression and returns the result.

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

Required. String containing the expression being evaluated.

Return Type

Variant containing the result of Eval.

Remarks

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.

Example
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
See Also