123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Reflection;
- using Jint.Native;
- using Jint.Native.Function;
- using Jint.Native.Object;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- namespace Jint.Runtime.Interop
- {
- public sealed class TypeReference : FunctionInstance, IConstructor, IObjectWrapper
- {
- private TypeReference(Engine engine)
- : base(engine, null, null, false, "TypeReference")
- {
- }
- public Type ReferenceType { get; set; }
- public static TypeReference CreateTypeReference(Engine engine, Type type)
- {
- var obj = new TypeReference(engine);
- obj.Extensible = false;
- obj.ReferenceType = type;
- // The value of the [[Prototype]] internal property of the TypeReference constructor is the Function prototype object
- obj.Prototype = engine.Function.PrototypeObject;
- obj.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
- // The initial value of Boolean.prototype is the Boolean prototype object
- obj.SetOwnProperty("prototype", new PropertyDescriptor(engine.Object.PrototypeObject, PropertyFlag.AllForbidden));
- return obj;
- }
- public override JsValue Call(JsValue thisObject, JsValue[] arguments)
- {
- // direct calls on a TypeReference constructor object is equivalent to the new operator
- return Construct(arguments);
- }
- public ObjectInstance Construct(JsValue[] arguments)
- {
- if (arguments.Length == 0 && ReferenceType.IsValueType())
- {
- var instance = Activator.CreateInstance(ReferenceType);
- var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
- return result;
- }
- var constructors = ReferenceType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
- var methods = TypeConverter.FindBestMatch(Engine, constructors, arguments).ToList();
- foreach (var method in methods)
- {
- var parameters = new object[arguments.Length];
- try
- {
- for (var i = 0; i < arguments.Length; i++)
- {
- var parameterType = method.GetParameters()[i].ParameterType;
- if (typeof(JsValue).IsAssignableFrom(parameterType))
- {
- parameters[i] = arguments[i];
- }
- else
- {
- parameters[i] = Engine.ClrTypeConverter.Convert(
- arguments[i].ToObject(),
- parameterType,
- CultureInfo.InvariantCulture);
- }
- }
- var constructor = (ConstructorInfo)method;
- var instance = constructor.Invoke(parameters.ToArray());
- var result = TypeConverter.ToObject(Engine, JsValue.FromObject(Engine, instance));
- // todo: cache method info
- return result;
- }
- catch
- {
- // ignore method
- }
- }
- ExceptionHelper.ThrowTypeError(_engine, "No public methods with the specified arguments were found.");
- return null;
- }
- public override bool HasInstance(JsValue v)
- {
- ObjectWrapper wrapper = v.As<ObjectWrapper>();
- if (ReferenceEquals(wrapper, null))
- {
- return base.HasInstance(v);
- }
- return wrapper.Target.GetType() == ReferenceType;
- }
- public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(_engine, "Can't define a property of a TypeReference");
- }
- return false;
- }
- public override bool Delete(string propertyName, bool throwOnError)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(_engine, "Can't delete a property of a TypeReference");
- }
- return false;
- }
- public override void Put(string propertyName, JsValue value, bool throwOnError)
- {
- if (!CanPut(propertyName))
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return;
- }
- var ownDesc = GetOwnProperty(propertyName);
- if (ownDesc == null)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(_engine, "Unknown member: " + propertyName);
- }
- else
- {
- return;
- }
- }
- ownDesc.Value = value;
- }
- public override PropertyDescriptor GetOwnProperty(string propertyName)
- {
- // todo: cache members locally
- if (ReferenceType.IsEnum())
- {
- Array enumValues = Enum.GetValues(ReferenceType);
- Array enumNames = Enum.GetNames(ReferenceType);
- for (int i = 0; i < enumValues.Length; i++)
- {
- if (enumNames.GetValue(i) as string == propertyName)
- {
- return new PropertyDescriptor((int) enumValues.GetValue(i), PropertyFlag.AllForbidden);
- }
- }
- return PropertyDescriptor.Undefined;
- }
- var propertyInfo = ReferenceType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static);
- if (propertyInfo != null)
- {
- return new PropertyInfoDescriptor(Engine, propertyInfo, Type);
- }
- var fieldInfo = ReferenceType.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
- if (fieldInfo != null)
- {
- return new FieldInfoDescriptor(Engine, fieldInfo, Type);
- }
- var methodInfo = ReferenceType
- .GetMethods(BindingFlags.Public | BindingFlags.Static)
- .Where(mi => mi.Name == propertyName)
- .ToArray();
- if (methodInfo.Length == 0)
- {
- return PropertyDescriptor.Undefined;
- }
- return new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, methodInfo), PropertyFlag.AllForbidden);
- }
- public object Target => ReferenceType;
- }
- }
|