// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Calls the underlying native function to create and return a new instance, which will be wrapped in the specified managed object type. /// </summary> /// <typeparam name="T">A managed object type to wrap the new native object handle.</typeparam> /// <param name="args">Arguments to pass to the function to construct the new native instance.</param> /// <returns>A new instance of 'T'.</returns> public V8ManagedObject CreateInstance <T>(params InternalHandle[] args) // TODO: Parameter passing needs testing. where T : V8ManagedObject, new() { HandleProxy **_args = null; if (args.Length > 0) { _args = (HandleProxy **)Utilities.AllocPointerArray(args.Length); for (var i = 0; i < args.Length; i++) { _args[i] = args[i]; } } // (note: the special case here is that the native function object will use its own template to create instances) T obj = _Engine._CreateManagedObject <T>(this, null); obj.Template = InstanceTemplate; try { obj._Handle.Set(V8NetProxy.CreateInstanceFromFunctionTemplate(_NativeFunctionTemplateProxy, obj.ID, args.Length, _args)); // (note: setting '_NativeObject' also updates it's '_ManagedObject' field if necessary. obj.Initialize(true, args); } catch (Exception ex) { // ... something went wrong, so remove the new managed object ... _Engine._RemoveObjectWeakReference(obj.ID); throw ex; } finally { Utilities.FreeNativeMemory((IntPtr)_args); } return(obj); }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Calls the underlying native function to create a new native object and return its handle. /// Use this method if you only need the native object and not a managed wrapper. /// </summary> /// <param name="args">Arguments to pass to the function to construct the new native instance.</param> /// <returns>A handle to the new object.</returns> public InternalHandle CreateNativeInstance(params InternalHandle[] args) // TODO: Parameter passing needs testing. { HandleProxy **_args = null; if (args.Length > 0) { _args = (HandleProxy **)Utilities.AllocPointerArray(args.Length); for (var i = 0; i < args.Length; i++) { _args[i] = args[i]; } } try { return((InternalHandle)V8NetProxy.CreateInstanceFromFunctionTemplate(_NativeFunctionTemplateProxy, -1, args.Length, _args)); } finally { Utilities.FreeNativeMemory((IntPtr)_args); } }