/// <summary> /// Allocates native memory to marshal an array of proxy handles. /// Uses 'Utilities.AllocPointerArray()', so be sure to call 'Utilities.FreeNativeMemory()' when done. /// </summary> public static HandleProxy **MakeHandleProxyArray(InternalHandle[] items) { HandleProxy **nativeArrayMem = (HandleProxy **)Utilities.AllocPointerArray(items.Length); for (var i = 0; i < items.Length; i++) { nativeArrayMem[i] = (HandleProxy *)items[i]; } return(nativeArrayMem); }
/// <summary> /// Allocates native memory to marshal an array of proxy handles. /// Uses 'Utilities.AllocPointerArray()', so be sure to call 'Utilities.FreeNativeMemory()' when done. /// </summary> public static HandleProxy **MakeHandleProxyArray(InternalHandle[] items) { // (Note: items may be NULL, which is equivalent to "undefined") HandleProxy **nativeArrayMem = (HandleProxy **)Utilities.AllocPointerArray(items.Length); for (var i = 0; i < items.Length; i++) { nativeArrayMem[i] = (HandleProxy *)items[i]; } return(nativeArrayMem); }
/// <summary> /// Allocates native memory to marshal an array of proxy handles. /// Uses 'Utilities.AllocPointerArray()', so be sure to call 'Utilities.FreeNativeMemory()' when done. /// </summary> public static HandleProxy **MakeHandleProxyArray <T>(T[] items) where T : INativeHandleBased { // (Note: items may be NULL, which is equivalent to "undefined") HandleProxy **nativeArrayMem = (HandleProxy **)Utilities.AllocPointerArray(items.Length); for (var i = 0; i < items.Length; i++) { nativeArrayMem[i] = items[i].GetNativeHandleProxy(); } return(nativeArrayMem); }
// -------------------------------------------------------------------------------------------------------------------- /// <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); } }