Пример #1
0
 public NativeMethod(JsMethodImpl impl, JsObject prototype) :
     this(impl, null, prototype)
 {
     foreach (var item in impl.Method.GetParameters())
     {
         Arguments.Add(item.Name);
     }
 }
Пример #2
0
 public NativeMethod(JsMethodImpl impl, JsObject prototype)
     : this(impl, (MethodInfo)null, prototype)
 {
     foreach (ParameterInfo parameter in impl.Method.GetParameters())
     {
         this.Arguments.Add(parameter.Name);
     }
 }
Пример #3
0
 public NativeMethod(JsMethodImpl impl, MethodInfo nativeMethod , JsObject prototype)
     : base(prototype)
 {
     if (impl == null)
         throw new ArgumentNullException("impl");
     m_nativeMethod = nativeMethod;
     m_impl = impl;
     if (nativeMethod != null)
     {
         Name = nativeMethod.Name;
         foreach (var item in nativeMethod.GetParameters())
             Arguments.Add(item.Name);
     }
 }
Пример #4
0
        public NativeMethod(MethodInfo info, JsObject prototype, IGlobal global)
            : base(prototype)
        {
            if (info == null)
                throw new ArgumentNullException("info");
            if (global == null)
                throw new ArgumentNullException("global");

            m_nativeMethod = info;
            m_impl = global.Marshaller.WrapMethod(info, true);
            Name = info.Name;

            foreach (var item in info.GetParameters())
                Arguments.Add(item.Name);
        }
Пример #5
0
 public override JsInstance Execute(IJintVisitor visitor, JsDictionaryObject that, JsInstance[] parameters, Type[] genericArguments)
 {
     if (m_generics.Count == 0 && (genericArguments != null && genericArguments.Length > 0))
     {
         return(base.Execute(visitor, that, parameters, genericArguments));
     }
     else
     {
         JsMethodImpl impl = m_overloads.ResolveOverload(parameters, genericArguments);
         if (impl == null)
         {
             throw new JintException(String.Format("No matching overload found {0}<{1}>", Name, genericArguments));
         }
         visitor.Return(impl(visitor.Global, that, parameters));
         return(that);
     }
 }
Пример #6
0
        public JsFunction New(Delegate d)
        {
            if (d == null)
            {
                throw new ArgumentNullException();
            }
            //JsFunction function = new ClrFunction(d, PrototypeProperty);

            JsMethodImpl impl    = Global.Marshaller.WrapMethod(d.GetType().GetMethod("Invoke"), false);
            JsObject     wrapper = new JsObject(d, JsNull.Instance);

            JsFunction function = New <JsInstance>((that, args) => impl(Global, wrapper, args));

            function.PrototypeProperty = Global.ObjectClass.New(function);

            //function.Scope = new JsScope(PrototypeProperty);
            return(function);
        }
Пример #7
0
 public NativeMethod(JsMethodImpl impl, MethodInfo nativeMethod, JsObject prototype) :
     base(prototype)
 {
     if (impl == null)
     {
         throw new ArgumentNullException("impl");
     }
     m_nativeMethod = nativeMethod;
     m_impl         = impl;
     if (nativeMethod != null)
     {
         Name = nativeMethod.Name;
         foreach (var item in nativeMethod.GetParameters())
         {
             Arguments.Add(item.Name);
         }
     }
 }
Пример #8
0
        public override JsInstance Execute(
            IJintVisitor visitor,
            JsDictionaryObject that,
            JsInstance[] parameters,
            Type[] genericArguments)
        {
            if (this.m_generics.Count == 0 && (genericArguments != null && (uint)genericArguments.Length > 0U))
            {
                return(base.Execute(visitor, that, parameters, genericArguments));
            }
            JsMethodImpl jsMethodImpl = this.m_overloads.ResolveOverload(parameters, genericArguments);

            if (jsMethodImpl == null)
            {
                throw new JintException(string.Format("No matching overload found {0}<{1}>", (object)this.Name, (object)genericArguments));
            }
            visitor.Return(jsMethodImpl(visitor.Global, (JsInstance)that, parameters));
            return((JsInstance)that);
        }
Пример #9
0
 public NativeMethod(MethodInfo info, JsObject prototype, IGlobal global)
     : base(prototype)
 {
     if (info == null)
     {
         throw new ArgumentNullException(nameof(info));
     }
     if (global == null)
     {
         throw new ArgumentNullException(nameof(global));
     }
     this.m_nativeMethod = info;
     this.m_impl         = global.Marshaller.WrapMethod(info, true);
     this.Name           = info.Name;
     foreach (ParameterInfo parameter in info.GetParameters())
     {
         this.Arguments.Add(parameter.Name);
     }
 }
Пример #10
0
 public NativeMethod(JsMethodImpl impl, MethodInfo nativeMethod, JsObject prototype)
     : base(prototype)
 {
     if (impl == null)
     {
         throw new ArgumentNullException(nameof(impl));
     }
     this.m_nativeMethod = nativeMethod;
     this.m_impl         = impl;
     if (nativeMethod == null)
     {
         return;
     }
     this.Name = nativeMethod.Name;
     foreach (ParameterInfo parameter in nativeMethod.GetParameters())
     {
         this.Arguments.Add(parameter.Name);
     }
 }
Пример #11
0
        public NativeMethod(MethodInfo info, JsObject prototype, IGlobal global) :
            base(prototype)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (global == null)
            {
                throw new ArgumentNullException("global");
            }

            m_nativeMethod = info;
            m_impl         = global.Marshaller.WrapMethod(info, true);
            Name           = info.Name;

            foreach (var item in info.GetParameters())
            {
                Arguments.Add(item.Name);
            }
        }
Пример #12
0
        public JsMethodImpl WrapMethod(MethodInfo info, bool passGlobal)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (info.ContainsGenericParameters)
            {
                throw new InvalidOperationException("Can't wrap an unclosed generic");
            }
            lock (this.methodCache)
            {
                JsMethodImpl jsMethodImpl;
                if (this.methodCache.TryGetValue(info, out jsMethodImpl))
                {
                    return(jsMethodImpl);
                }
            }
            LinkedList <ParameterInfo> linkedList1 = new LinkedList <ParameterInfo>((IEnumerable <ParameterInfo>)info.GetParameters());
            LinkedList <ProxyHelper.MarshalledParameter> linkedList2 = new LinkedList <ProxyHelper.MarshalledParameter>();
            DynamicMethod dynamicMethod = new DynamicMethod("jsWrapper", typeof(JsInstance), new Type[3] {
                typeof(IGlobal), typeof(JsInstance), typeof(JsInstance[])
            }, this.GetType());
            ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

            ilGenerator.DeclareLocal(typeof(int));
            ilGenerator.DeclareLocal(typeof(Marshaller));
            ilGenerator.Emit(OpCodes.Ldarg_0);
            ilGenerator.Emit(OpCodes.Call, typeof(IGlobal).GetProperty("Marshaller").GetGetMethod());
            if (!info.ReturnType.Equals(typeof(void)))
            {
                ilGenerator.Emit(OpCodes.Dup);
            }
            ilGenerator.Emit(OpCodes.Stloc_1);
            if (!info.IsStatic)
            {
                Label label1 = ilGenerator.DefineLabel();
                Label label2 = ilGenerator.DefineLabel();
                ilGenerator.Emit(OpCodes.Ldloc_1);
                ilGenerator.Emit(OpCodes.Ldarg_1);
                if (info.DeclaringType.IsValueType)
                {
                    ilGenerator.Emit(OpCodes.Call, typeof(Marshaller).GetMethod("MarshalJsValueBoxed").MakeGenericMethod(info.DeclaringType));
                }
                else
                {
                    ilGenerator.Emit(OpCodes.Call, typeof(Marshaller).GetMethod("MarshalJsValue").MakeGenericMethod(info.DeclaringType));
                }
                ilGenerator.Emit(OpCodes.Dup);
                ilGenerator.Emit(OpCodes.Ldnull);
                ilGenerator.Emit(OpCodes.Beq, label2);
                ilGenerator.Emit(OpCodes.Br, label1);
                ilGenerator.MarkLabel(label2);
                ilGenerator.Emit(OpCodes.Ldstr, "The specified 'that' object is not acceptable for this method");
                ilGenerator.Emit(OpCodes.Newobj, typeof(JintException).GetConstructor(new Type[1]
                {
                    typeof(string)
                }));
                ilGenerator.Emit(OpCodes.Throw);
                ilGenerator.MarkLabel(label1);
                if (info.DeclaringType.IsValueType)
                {
                    ilGenerator.Emit(OpCodes.Unbox, info.DeclaringType);
                }
            }
            if (passGlobal && linkedList1.First != null && typeof(IGlobal).IsAssignableFrom(linkedList1.First.Value.ParameterType))
            {
                linkedList1.RemoveFirst();
                ilGenerator.Emit(OpCodes.Ldarg_0);
                ilGenerator.Emit(OpCodes.Isinst, typeof(IGlobal));
            }
            ilGenerator.Emit(OpCodes.Ldarg_2);
            ilGenerator.Emit(OpCodes.Ldlen);
            ilGenerator.Emit(OpCodes.Stloc_0);
            int index = 0;

            foreach (ParameterInfo parameterInfo in linkedList1)
            {
                ilGenerator.Emit(OpCodes.Ldloc_1);
                Label label1 = ilGenerator.DefineLabel();
                Label label2 = ilGenerator.DefineLabel();
                ilGenerator.Emit(OpCodes.Ldloc_0);
                ilGenerator.Emit(OpCodes.Ldc_I4, index);
                ilGenerator.Emit(OpCodes.Ble, label1);
                ilGenerator.Emit(OpCodes.Ldarg_2);
                ilGenerator.Emit(OpCodes.Ldc_I4, index);
                ilGenerator.Emit(OpCodes.Ldelem, typeof(JsInstance));
                ilGenerator.Emit(OpCodes.Br, label2);
                ilGenerator.MarkLabel(label1);
                ilGenerator.Emit(OpCodes.Ldsfld, typeof(JsUndefined).GetField("Instance"));
                ilGenerator.MarkLabel(label2);
                if (parameterInfo.ParameterType.IsByRef)
                {
                    Type         elementType = parameterInfo.ParameterType.GetElementType();
                    LocalBuilder temp        = ilGenerator.DeclareLocal(elementType);
                    ilGenerator.Emit(OpCodes.Call, typeof(Marshaller).GetMethod("MarshalJsValue").MakeGenericMethod(elementType));
                    ilGenerator.Emit(OpCodes.Stloc, temp.LocalIndex);
                    ilGenerator.Emit(OpCodes.Ldloca, temp.LocalIndex);
                    if (parameterInfo.IsOut)
                    {
                        linkedList2.AddLast(new ProxyHelper.MarshalledParameter(temp, index));
                    }
                }
                else
                {
                    ilGenerator.Emit(OpCodes.Call, typeof(Marshaller).GetMethod("MarshalJsValue").MakeGenericMethod(parameterInfo.ParameterType));
                }
                ++index;
            }
            if (!info.IsStatic)
            {
                ilGenerator.Emit(OpCodes.Callvirt, info);
            }
            else
            {
                ilGenerator.Emit(OpCodes.Call, info);
            }
            foreach (ProxyHelper.MarshalledParameter marshalledParameter in linkedList2)
            {
                Label label = ilGenerator.DefineLabel();
                ilGenerator.Emit(OpCodes.Ldloc_0);
                ilGenerator.Emit(OpCodes.Ldc_I4, marshalledParameter.index);
                ilGenerator.Emit(OpCodes.Ble, label);
                ilGenerator.Emit(OpCodes.Ldarg_2);
                ilGenerator.Emit(OpCodes.Ldc_I4, marshalledParameter.index);
                ilGenerator.Emit(OpCodes.Ldloc_1);
                ilGenerator.Emit(OpCodes.Ldloc, marshalledParameter.tempLocal);
                ilGenerator.Emit(OpCodes.Call, typeof(Marshaller).GetMethod("MarshalClrValue").MakeGenericMethod(marshalledParameter.tempLocal.LocalType));
                ilGenerator.Emit(OpCodes.Stelem, typeof(JsInstance));
                ilGenerator.MarkLabel(label);
            }
            if (!info.ReturnType.Equals(typeof(void)))
            {
                ilGenerator.Emit(OpCodes.Call, typeof(Marshaller).GetMethod("MarshalClrValue").MakeGenericMethod(info.ReturnType));
            }
            else
            {
                ilGenerator.Emit(OpCodes.Ldnull);
                ilGenerator.Emit(OpCodes.Ldfld, typeof(JsUndefined).GetField("Instance"));
            }
            ilGenerator.Emit(OpCodes.Ret);
            JsMethodImpl jsMethodImpl1 = (JsMethodImpl)dynamicMethod.CreateDelegate(typeof(JsMethodImpl));

            lock (this.methodCache)
                this.methodCache[info] = jsMethodImpl1;
            return(jsMethodImpl1);
        }
Пример #13
0
 public NativeMethod(JsMethodImpl impl, JsObject prototype)
     : this(impl,null,prototype)
 {
     foreach (var item in impl.Method.GetParameters())
         Arguments.Add(item.Name);
 }