Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void AddGlobalType <T>(string name, Func <object[], T> func)
        {
            var ctor = new ClrFuncCtor <T>(_engine, args =>
            {
                var clrArgs = args.Select(x => _typeConverter.ConvertFromJs(x)).ToArray();
                var obj     = func(clrArgs);
                _typeConverter.TryConvert(obj, out var res);
                return(res.AsObject());
            }, _typeConverter.GetPrototype(typeof(T)))
            {
                Prototype = _engine.Function.PrototypeObject
            };

            _engine.Global.FastAddProperty(name, ctor, false, false, false);
        }