Esempio n. 1
0
        public ClrEvent(global::Jint.Engine engine, DomConverter converter, EventInfo eventInfo)
        {
            Get = new ClrFunctionInstance(engine, (clrThis, args) =>
            {
                var attachedEvents = converter.GetAttachedEventsFor(clrThis);
                return(attachedEvents.TryGetValue(eventInfo, out var func) ? func : JsValue.Null);
            });

            Set = new ClrFunctionInstance(engine, (jsThis, args) =>
            {
                var objectInstance = jsThis.AsObject();
                var clrThis        = jsThis.ToObject();

                var attachedEvents = converter.GetAttachedEventsFor(objectInstance);

                if (attachedEvents.TryGetValue(eventInfo, out var existHandler))
                {
                    var clrHandler = converter.ConvertToClr(existHandler, eventInfo.EventHandlerType, jsThis);
                    eventInfo.RemoveMethod.Invoke(clrThis, new object[] { clrHandler });
                    attachedEvents.Remove(eventInfo);
                }

                if (args.Length != 0 && !args[0].IsNull() && !args[0].IsUndefined() && args[0].AsObject() is FunctionInstance functionInstance)
                {
                    var clrHandler = converter.ConvertToClr(functionInstance, eventInfo.EventHandlerType, jsThis);
                    eventInfo.AddMethod.Invoke(clrThis, new object[] { clrHandler });
                    attachedEvents[eventInfo] = functionInstance;
                }

                return(null);
            });
        }
Esempio n. 2
0
        public ClrProperty(global::Jint.Engine engine, DomConverter converter, PropertyInfo property)
        {
            Get =
                property.GetMethod != null && property.GetMethod.IsPublic ?  new ClrFunctionInstance(engine, (jsThis, values) =>
            {
                var clrThis = converter.ConvertFromJs(jsThis);
                return(JsValue.FromObject(engine, property.GetValue(clrThis)));
            }) : null;

            var setter = new Lazy <Action <object, JsValue> >(() => CreateSetter(converter, property));

            Set =
                property.SetMethod != null && property.SetMethod.IsPublic ? new ClrFunctionInstance(engine, (jsThis, values) =>
            {
                try
                {
                    var clrThis = converter.ConvertFromJs(jsThis);
                    if (values.Length == 0)
                    {
                        return(JsValue.Undefined);
                    }

                    setter.Value(clrThis, values[0]);

                    return(JsValue.Undefined);
                }
                catch (Exception e)
                {
                    throw new JavaScriptException(e.Message);
                }
            }) : new ClrFunctionInstance(engine, (value, values) => JsValue.Undefined);
        }
Esempio n. 3
0
 public ClrPrototype(global::Jint.Engine engine, DomConverter converter, Type type, ObjectInstance prototype) :
     base(engine)
 {
     Prototype  = prototype;
     Class      = type.GetJsName();
     Extensible = true;
     converter.DefineProperties(this, type);
 }
Esempio n. 4
0
        public ClrPrototype(global::Jint.Engine engine, DomConverter converter, Type type, ObjectInstance prototype) :
            base(engine)
        {
            Prototype  = prototype;
            _name      = (type.GetCustomAttribute <JsNameAttribute>()?.Name ?? type.Name) + "Prototype";
            Extensible = true;

            converter.DefineProperties(this, type);
        }
Esempio n. 5
0
        public ClrMethodInfoFunc(DomConverter converter, global::Jint.Engine engine, MethodInfo[] methods, JsValue owner)
            : base(engine, null, null, false)
        {
            _converter = converter;
            _owner     = owner;
            _methods   = methods.OrderByDescending(x => x.GetParameters().Length).ToArray();
            _invokers  = new Func <object, JsValue[], object> [methods.Length];
            Prototype  = engine.Function.PrototypeObject;

            var name = methods.First().GetName();

            FastAddProperty("name", new JsValue(name), false, false, false);
        }
Esempio n. 6
0
 public ReadonlyListAdapter(global::Jint.Engine engine, IReadOnlyList <T> list, ClrPrototype prototype)
     : base(engine)
 {
     _engine           = engine;
     _list             = list;
     Prototype         = prototype;
     _indexPropertyStr =
         Target.GetType().GetRecursive(x => x.BaseType).SelectMany(x =>
                                                                   x.GetProperties().Where(p => p.GetIndexParameters().Length != 0 &&
                                                                                           p.GetCustomAttribute <JsHiddenAttribute>() == null &&
                                                                                           p.GetIndexParameters()[0].ParameterType == typeof(string)))
         .FirstOrDefault();
 }
Esempio n. 7
0
        public static PropertyDescriptor Create(global::Jint.Engine engine,
                                                DomConverter converter,
                                                string propertyName,
                                                MethodInfo[] methods,
                                                JsValue owner)
        {
            var func = new ClrMethodInfoFunc(converter, engine, methods, owner);

            //todo: check if this necessary
            func.FastAddProperty("toString",
                                 new JsValue(new ClrFunctionInstance(engine,
                                                                     (value, values) => new JsValue("function " + propertyName + "() { [native code] }"))),
                                 false, false, false);

            return(new PropertyDescriptor(func, false, true, false));
        }