C# Class V8.Net.V8NativeObject

Represents a basic JavaScript object. This class wraps V8 functionality for operations required on any native V8 object (including managed ones).

This class implements 'DynamicObject' to make setting properties a bit easier.

Inheritance: IHandleBased, IV8Object, IV8NativeObject, IDynamicMetaObjectProvider, IFinalizable
Show file Open project: rjamesnw/v8dotnet Class Usage Examples

Private Properties

Property Type Description
IFinalizable void
_MakeWeak void
_OnNativeGCRequested bool
_TryDisposeNativeHandle void

Public Methods

Method Description
AsHandle ( ) : Handle
Call ( InternalHandle _this ) : InternalHandle

Calls the underlying object as a function. The '_this' parameter is the "this" reference within the function when called.

Call ( string functionName, InternalHandle _this ) : InternalHandle

Calls an object property with a given name on a specified object as a function and returns the result. The '_this' property is the "this" object within the function when called.

DeleteProperty ( Int32 index ) : bool

Calls the V8 'Delete()' function on the underlying native object. Returns true if the property was deleted.

DeleteProperty ( string name ) : bool

Calls the V8 'Delete()' function on the underlying native object. Returns true if the property was deleted.

Dispose ( ) : void

Called when there are no more references (on either the managed or native side) and the object is ready to be deleted from the V8.NET system. You should never call this from code directly unless you need to force the release of native resources associated with a custom implementation (and if so, a custom internal flag should be kept indicating whether or not the resources have been disposed). You should always override/implement this if you need to dispose of any native resources in custom implementations. DO NOT rely on the destructor (finalizer) - the object can still survive it.

Note: This can be triggered either by the worker thread, or on the call-back from the V8 garbage collector. In either case, tread it as if it was called from the GC finalizer (not on the main thread).

*** If overriding, DON'T call back to this method, otherwise it will call back and end up in a cyclical call (and a stack overflow!). ***

GetMetaObject ( Expression parameter ) : DynamicMetaObject
GetOwnPropertyNames ( ) : string[]

Returns a list of all property names for this object (excluding the prototype chain).

GetProperty ( Int32 index ) : InternalHandle

Calls the V8 'Get()' function on the underlying native object. If the property doesn't exist, the 'IsUndefined' property will be true.

GetProperty ( string name ) : InternalHandle

Calls the V8 'Get()' function on the underlying native object. If the property doesn't exist, the 'IsUndefined' property will be true.

GetPropertyAttributes ( string name ) : V8PropertyAttributes

Get the attribute flags for a property of this object. If a property doesn't exist, then 'V8PropertyAttributes.None' is returned (Note: only V8 returns 'None'. The value 'Undefined' has an internal proxy meaning for property interception).

GetPropertyNames ( ) : string[]

Returns a list of all property names for this object (including all objects in the prototype chain).

Initialize ( bool isConstructCall ) : ObjectHandle

Called immediately after creating an object instance and setting the V8Engine property. Derived objects should override this for construction instead of using the constructor, and be sure to call back to this base method just before exiting (not at the beginning). In the constructor, the object only exists as an empty shell. It's ok to setup non-v8 values in constructors, but be careful not to trigger any calls into the V8Engine itself.

Note: Because this method is virtual, it does not guarantee that 'IsInitialized' will be considered. Implementations should check against the 'IsInitilized' property.

Initialize ( V8NativeObject owner, bool isConstructCall ) : void

(Exists only to support the 'IV8NativeInterface' interface and should not be called directly - call 'Initialize(isConstructCall, args)' instead.)

SetAccessor ( string name, V8NativeObjectPropertyGetter getter, V8NativeObjectPropertySetter setter, V8PropertyAttributes attributes = V8PropertyAttributes.None, V8AccessControl access = V8AccessControl.Default ) : void

Calls the V8 'SetAccessor()' function on the underlying native object to create a property that is controlled by "getter" and "setter" callbacks.

SetProperty ( Int32 index, InternalHandle value ) : bool

Calls the V8 'Set()' function on the underlying native object. Returns true if successful.

SetProperty ( Type type, V8PropertyAttributes propertyAttributes = V8PropertyAttributes.None, string className = null, bool recursive = null, ScriptMemberSecurity memberSecurity = null ) : bool

Binds a 'V8Function' object to the specified type and associates the type name (or custom script name) with the underlying object. Returns true if successful.

SetProperty ( string name, InternalHandle value, V8PropertyAttributes attributes = V8PropertyAttributes.None ) : bool

Calls the V8 'Set()' function on the underlying native object. Returns true if successful.

SetProperty ( string name, object obj, string className = null, bool recursive = null, ScriptMemberSecurity memberSecurity = null ) : bool

Sets a property to a given object. If the object is not V8.NET related, then the system will attempt to bind the instance and all public members to the specified property name. Returns true if successful.

StaticCall ( ) : InternalHandle

Calls the underlying object as a function. The 'this' property will not be specified, which will default to the global scope as expected.

StaticCall ( string functionName ) : InternalHandle

Calls an object property with a given name on a specified object as a function and returns the result.

ToString ( ) : string
V8NativeObject ( ) : System.Dynamic
V8NativeObject ( IV8NativeObject proxy ) : System.Dynamic
this ( int index ) : InternalHandle
this ( string propertyName ) : InternalHandle

Private Methods

Method Description
IFinalizable ( ) : void
_MakeWeak ( ) : void

Called by the worker thread to make the native handle weak. Once the native GC attempts to collect the underlying native object, then '_OnNativeGCRequested()' will get called to finalize the disposal of the managed object.

_OnNativeGCRequested ( ) : bool
_TryDisposeNativeHandle ( ) : void

This is called automatically when both the handle AND reference for the managed object are weak [no longer in use], in which case this object info instance is ready to be removed.

Method Details

AsHandle() public method

public AsHandle ( ) : Handle
return Handle

Call() public method

Calls the underlying object as a function. The '_this' parameter is the "this" reference within the function when called.
public Call ( InternalHandle _this ) : InternalHandle
_this InternalHandle
return InternalHandle

Call() public method

Calls an object property with a given name on a specified object as a function and returns the result. The '_this' property is the "this" object within the function when called.
public Call ( string functionName, InternalHandle _this ) : InternalHandle
functionName string
_this InternalHandle
return InternalHandle

DeleteProperty() public method

Calls the V8 'Delete()' function on the underlying native object. Returns true if the property was deleted.
public DeleteProperty ( Int32 index ) : bool
index System.Int32
return bool

DeleteProperty() public method

Calls the V8 'Delete()' function on the underlying native object. Returns true if the property was deleted.
public DeleteProperty ( string name ) : bool
name string
return bool

Dispose() public method

Called when there are no more references (on either the managed or native side) and the object is ready to be deleted from the V8.NET system. You should never call this from code directly unless you need to force the release of native resources associated with a custom implementation (and if so, a custom internal flag should be kept indicating whether or not the resources have been disposed). You should always override/implement this if you need to dispose of any native resources in custom implementations. DO NOT rely on the destructor (finalizer) - the object can still survive it.

Note: This can be triggered either by the worker thread, or on the call-back from the V8 garbage collector. In either case, tread it as if it was called from the GC finalizer (not on the main thread).

*** If overriding, DON'T call back to this method, otherwise it will call back and end up in a cyclical call (and a stack overflow!). ***
public Dispose ( ) : void
return void

GetMetaObject() public method

public GetMetaObject ( Expression parameter ) : DynamicMetaObject
parameter System.Linq.Expressions.Expression
return System.Dynamic.DynamicMetaObject

GetOwnPropertyNames() public method

Returns a list of all property names for this object (excluding the prototype chain).
public GetOwnPropertyNames ( ) : string[]
return string[]

GetProperty() public method

Calls the V8 'Get()' function on the underlying native object. If the property doesn't exist, the 'IsUndefined' property will be true.
public GetProperty ( Int32 index ) : InternalHandle
index System.Int32
return InternalHandle

GetProperty() public method

Calls the V8 'Get()' function on the underlying native object. If the property doesn't exist, the 'IsUndefined' property will be true.
public GetProperty ( string name ) : InternalHandle
name string
return InternalHandle

GetPropertyAttributes() public method

Get the attribute flags for a property of this object. If a property doesn't exist, then 'V8PropertyAttributes.None' is returned (Note: only V8 returns 'None'. The value 'Undefined' has an internal proxy meaning for property interception).

public GetPropertyAttributes ( string name ) : V8PropertyAttributes
name string
return V8PropertyAttributes

GetPropertyNames() public method

Returns a list of all property names for this object (including all objects in the prototype chain).
public GetPropertyNames ( ) : string[]
return string[]

Initialize() public method

Called immediately after creating an object instance and setting the V8Engine property. Derived objects should override this for construction instead of using the constructor, and be sure to call back to this base method just before exiting (not at the beginning). In the constructor, the object only exists as an empty shell. It's ok to setup non-v8 values in constructors, but be careful not to trigger any calls into the V8Engine itself.

Note: Because this method is virtual, it does not guarantee that 'IsInitialized' will be considered. Implementations should check against the 'IsInitilized' property.

public Initialize ( bool isConstructCall ) : ObjectHandle
isConstructCall bool
return ObjectHandle

Initialize() public method

(Exists only to support the 'IV8NativeInterface' interface and should not be called directly - call 'Initialize(isConstructCall, args)' instead.)
public Initialize ( V8NativeObject owner, bool isConstructCall ) : void
owner V8NativeObject
isConstructCall bool
return void

SetAccessor() public method

Calls the V8 'SetAccessor()' function on the underlying native object to create a property that is controlled by "getter" and "setter" callbacks.
public SetAccessor ( string name, V8NativeObjectPropertyGetter getter, V8NativeObjectPropertySetter setter, V8PropertyAttributes attributes = V8PropertyAttributes.None, V8AccessControl access = V8AccessControl.Default ) : void
name string
getter V8NativeObjectPropertyGetter
setter V8NativeObjectPropertySetter
attributes V8PropertyAttributes
access V8AccessControl
return void

SetProperty() public method

Calls the V8 'Set()' function on the underlying native object. Returns true if successful.
public SetProperty ( Int32 index, InternalHandle value ) : bool
index System.Int32
value InternalHandle
return bool

SetProperty() public method

Binds a 'V8Function' object to the specified type and associates the type name (or custom script name) with the underlying object. Returns true if successful.
public SetProperty ( Type type, V8PropertyAttributes propertyAttributes = V8PropertyAttributes.None, string className = null, bool recursive = null, ScriptMemberSecurity memberSecurity = null ) : bool
type System.Type The type to wrap.
propertyAttributes V8PropertyAttributes Flags that describe the property behavior. They must be 'OR'd together as needed.
className string A custom in-script function name for the specified type, or 'null' to use either the type name as is (the default) or any existing 'ScriptObject' attribute name.
recursive bool For object types, if true, then object reference members are included, otherwise only the object itself is bound and returned. /// For security reasons, public members that point to object instances will be ignored. This must be true to included those as well, effectively allowing /// in-script traversal of the object reference tree (so make sure this doesn't expose sensitive methods/properties/fields).
memberSecurity ScriptMemberSecurity For object instances, these are default flags that describe JavaScript properties for all object instance members that /// don't have any 'ScriptMember' attribute. The flags should be 'OR'd together as needed.
return bool

SetProperty() public method

Calls the V8 'Set()' function on the underlying native object. Returns true if successful.
public SetProperty ( string name, InternalHandle value, V8PropertyAttributes attributes = V8PropertyAttributes.None ) : bool
name string
value InternalHandle
attributes V8PropertyAttributes Flags that describe the property behavior. They must be 'OR'd together as needed.
return bool

SetProperty() public method

Sets a property to a given object. If the object is not V8.NET related, then the system will attempt to bind the instance and all public members to the specified property name. Returns true if successful.
public SetProperty ( string name, object obj, string className = null, bool recursive = null, ScriptMemberSecurity memberSecurity = null ) : bool
name string The property name.
obj object Some value or object instance. 'Engine.CreateValue()' will be used to convert value types.
className string A custom in-script function name for the specified object type, or 'null' to use either the type name as is (the default) or any existing 'ScriptObject' attribute name.
recursive bool For object instances, if true, then object reference members are included, otherwise only the object itself is bound and returned. /// For security reasons, public members that point to object instances will be ignored. This must be true to included those as well, effectively allowing /// in-script traversal of the object reference tree (so make sure this doesn't expose sensitive methods/properties/fields).
memberSecurity ScriptMemberSecurity For object instances, these are default flags that describe JavaScript properties for all object instance members that /// don't have any 'ScriptMember' attribute. The flags should be 'OR'd together as needed.
return bool

StaticCall() public method

Calls the underlying object as a function. The 'this' property will not be specified, which will default to the global scope as expected.
public StaticCall ( ) : InternalHandle
return InternalHandle

StaticCall() public method

Calls an object property with a given name on a specified object as a function and returns the result.
public StaticCall ( string functionName ) : InternalHandle
functionName string
return InternalHandle

ToString() public method

public ToString ( ) : string
return string

V8NativeObject() public method

public V8NativeObject ( ) : System.Dynamic
return System.Dynamic

V8NativeObject() public method

public V8NativeObject ( IV8NativeObject proxy ) : System.Dynamic
proxy IV8NativeObject
return System.Dynamic

this() public method

public this ( int index ) : InternalHandle
index int
return InternalHandle

this() public method

public this ( string propertyName ) : InternalHandle
propertyName string
return InternalHandle