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

Required. The programmatic identifier of the object instance. The object is exposed to JavaScript through this global identifier, also accessible as named property of the window object.

Required. An IDispatch interface pointer of the COM object instance you wish to expose to JavaScript.

Reserved.

In This Topic
    AddObject Method
    In This Topic
    Description

    Exports a native COM Object to JavaScript.

    Syntax
    Visual Basic
    Public Sub AddObject( _
       ByVal Name As String, _
       ByVal Object As Object, _
       Optional ByVal AddMembers As Boolean = False _
    ) 
    Parameters
    Name

    Required. The programmatic identifier of the object instance. The object is exposed to JavaScript through this global identifier, also accessible as named property of the window object.

    Object

    Required. An IDispatch interface pointer of the COM object instance you wish to expose to JavaScript.

    AddMembers

    Reserved.

    Remarks

    This technology allows you to use headless COM objects (non-controls) from JavaScript.

    Any simple in-process or out-of-process apartment threaded COM object with scalar properties and/or methods with scalar arguments and scalar return value can be wrapped into an V8 object and exposed through a global identifier to the V8 JavaScript context. The method will not work for COM objects that have non-scalar properties or method arguments or method return values.

    Visual Basic 6.0 developers, please note that if you wish to expose a local VB6 Class to JavaScript, you must compile your executable as ActiveX EXE. Standard executables will only work while in Visual Basic 6.0 IDE but not if you compile them.

    JavaScriptX in-process ActiveX wraps the IDispatch interface pointer you provide, inside an in-process Proxy object and generates a Stub V8 object, which is exported to the Global object of the V8 context. Thus, in JavaScript you can call your COM object directly from any scope. The communication between the Proxy and Stub counterparts is synchronous.

    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.AddObject "MyClass", MyClass
        Result = SC.Eval("MyClass.C")
            
    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