CreateExternalObject() 공개 정적인 메소드

Creates a new Object that stores some external data
Requires an active script context.
public static CreateExternalObject ( IntPtr data, JsObjectFinalizeCallback finalizer ) : JsValue
data System.IntPtr External data that the object will represent. May be null
finalizer JsObjectFinalizeCallback The callback for when the object is finalized. May be null.
리턴 JsValue
        private EmbeddedObject CreateEmbeddedFunction(Delegate del)
        {
            JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
#if NET40
                MethodInfo method = del.Method;
#else
                MethodInfo method = del.GetMethodInfo();
#endif
                ParameterInfo[] parameters    = method.GetParameters();
                object[]        processedArgs = GetHostItemMemberArguments(args, parameters.Length);

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters);

                object result;

                try
                {
                    result = del.DynamicInvoke(processedArgs);
                }
                catch (Exception e)
                {
                    JsValue   undefinedValue   = JsValue.Undefined;
                    Exception exception        = UnwrapException(e);
                    var       wrapperException = exception as WrapperException;
                    JsValue   errorValue       = wrapperException != null?
                                                 CreateErrorFromWrapperException(wrapperException)
                                                     :
                                                     JsErrorHelpers.CreateError(string.Format(
                                                                                    Strings.Runtime_HostDelegateInvocationFailed, exception.Message))
                    ;

                    JsContext.SetException(errorValue);

                    return(undefinedValue);
                }

                JsValue resultValue = MapToScriptType(result);

                return(resultValue);
            };

            GCHandle delHandle = GCHandle.Alloc(del);
            IntPtr   delPtr    = GCHandle.ToIntPtr(delHandle);
            JsValue  objValue  = JsValue.CreateExternalObject(delPtr, _embeddedObjectFinalizeCallback);

            JsValue functionValue = JsValue.CreateFunction(nativeFunction);

            SetNonEnumerableProperty(functionValue, ExternalObjectPropertyName, objValue);

            var embeddedObject = new EmbeddedObject(del, functionValue,
                                                    new List <JsNativeFunction> {
                nativeFunction
            });

            return(embeddedObject);
        }
예제 #2
0
        private EmbeddedObject CreateEmbeddedObject(object obj)
        {
            GCHandle objHandle = GCHandle.Alloc(obj);
            IntPtr   objPtr    = GCHandle.ToIntPtr(objHandle);
            JsValue  objValue  = JsValue.CreateExternalObject(objPtr, _embeddedObjectFinalizeCallback);

            var embeddedObject = new EmbeddedObject(obj, objValue);

            ProjectFields(embeddedObject);
            ProjectProperties(embeddedObject);
            ProjectMethods(embeddedObject);
            FreezeObject(objValue);

            return(embeddedObject);
        }
예제 #3
0
        private EmbeddedType CreateEmbeddedType(Type type)
        {
#if NET40
            Type typeInfo = type;
#else
            TypeInfo typeInfo = type.GetTypeInfo();
#endif
            string            typeName            = type.FullName;
            BindingFlags      defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true);
            ConstructorInfo[] constructors        = type.GetConstructors(defaultBindingFlags);

            JsNativeFunction nativeConstructorFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                object   result;
                JsValue  resultValue;
                object[] processedArgs = GetHostItemMemberArguments(args);

                if (processedArgs.Length == 0 && typeInfo.IsValueType)
                {
                    result      = Activator.CreateInstance(type);
                    resultValue = MapToScriptType(result);

                    return(resultValue);
                }

                if (constructors.Length == 0)
                {
                    CreateAndSetError(string.Format(Strings.Runtime_HostTypeConstructorNotFound, typeName));
                    return(JsValue.Undefined);
                }

                var bestFitConstructor = (ConstructorInfo)ReflectionHelpers.GetBestFitMethod(
                    constructors, processedArgs);
                if (bestFitConstructor == null)
                {
                    CreateAndSetReferenceError(string.Format(
                                                   Strings.Runtime_SuitableConstructorOfHostTypeNotFound, typeName));
                    return(JsValue.Undefined);
                }

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitConstructor.GetParameters());

                try
                {
                    result = bestFitConstructor.Invoke(processedArgs);
                }
                catch (Exception e)
                {
                    Exception exception        = UnwrapException(e);
                    var       wrapperException = exception as WrapperException;
                    JsValue   errorValue       = wrapperException != null?
                                                 CreateErrorFromWrapperException(wrapperException)
                                                     :
                                                     JsErrorHelpers.CreateError(string.Format(
                                                                                    Strings.Runtime_HostTypeConstructorInvocationFailed, typeName, exception.Message))
                    ;

                    JsErrorHelpers.SetException(errorValue);

                    return(JsValue.Undefined);
                }

                resultValue = MapToScriptType(result);

                return(resultValue);
            };

            GCHandle embeddedTypeHandle = GCHandle.Alloc(type);
            IntPtr   embeddedTypePtr    = GCHandle.ToIntPtr(embeddedTypeHandle);
            JsValue  objValue           = JsValue.CreateExternalObject(embeddedTypePtr, _embeddedTypeFinalizeCallback);

            JsValue typeValue = JsValue.CreateFunction(nativeConstructorFunction);
            SetNonEnumerableProperty(typeValue, ExternalObjectPropertyName, objValue);

            var embeddedType = new EmbeddedType(type, typeValue,
                                                new List <JsNativeFunction> {
                nativeConstructorFunction
            });

            ProjectFields(embeddedType);
            ProjectProperties(embeddedType);
            ProjectMethods(embeddedType);
            FreezeObject(typeValue);

            return(embeddedType);
        }