Exemplo n.º 1
0
        public JSObjectDef(object target, Type targetType, string member, JSBindingOptions options)
        {
            if (string.IsNullOrEmpty(member))
            {
                throw new ArgumentNullException(member);
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            // validate options here

            if (!targetType.IsAssignableFrom(target.GetType()))
            {
                throw new JSBindingException(string.Format(
                                                 "ScriptableObject is not assignable from '{0}'.", target.GetType().FullName
                                                 ));
            }

            this.member     = member;
            this.target     = target;
            this.targetType = targetType;
            this.options    = options;
        }
Exemplo n.º 2
0
        public void BindJSObject(string name, object obj, Type type, JSBindingOptions options)
        {
            if (this.owner != null && (options & JSBindingOptions.Extension) != 0)
            {
                throw new NotSupportedException("Extension can be bound only on global context. Use Cef.JSBinding instead.");
            }

            var def = new JSObjectDef(obj, type, name, options);

            if (this.objects.ContainsKey(def.MemberName))
            {
                throw new JSBindingException(string.Format(
                                                 "JSObject with name '{0}' already registered.", name
                                                 ));
            }

            // force binder creation
            var binder = ScriptableObjectBinder.Get(def.TargetType, options);

            if (def.Extension)
            {
                // Register V8 extension
                var javaScriptCode = binder.CreateJavaScriptExtension(def.MemberName);
                var handler        = new ScriptableObjectV8Handler(binder, def.Target, true);

                if (!Cef.RegisterExtension(def.ExtensionName, javaScriptCode, (CefV8Handler)handler))
                {
                    throw new InvalidOperationException("Cef.RegisterExtension failed.");
                }
            }

            this.objects.Add(def.MemberName, def);
        }
        private ScriptableObjectBinder(Type type, JSBindingOptions options, NamingConvention namingConvention)
        {
            if (type == null) throw new ArgumentNullException("type");

            if (!type.IsClass && !type.IsInterface) throw new ArgumentException("The type must be a class or interface.");

            this.type = type;

            var attr = GetJSObjectAttribute(this.type);
            if (attr != null)
            {
                if (options == JSBindingContext.DefaultOptions)
                {
                    this.options = attr.Options;
                }
            }
            else
            {
                this.options = options;
            }

            this.namingConvention = namingConvention;

            this.Create();

            Debug.Assert(this.dispatchTable != null);
            Debug.Assert(this.propertyDispatchTable != null);

            if ((this.options & JSBindingOptions.LazyCompile) == 0)
            {
                this.Compile();
            }
        }
Exemplo n.º 4
0
        public void BindJSObject(string name, object obj, JSBindingOptions options)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            this.BindJSObject(name, obj, obj.GetType(), DefaultOptions);
        }
Exemplo n.º 5
0
        private JSObjectBinder(Type type, JSBindingOptions options, NamingConvention namingConvention)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (!type.IsInterface) throw new ArgumentException("Only interface type allowed.", "type");

            this.type = type;
            this.options = options;
            this.namingConvention = namingConvention;

            this.proxyType = JSObjectTypeBuilder.Create(type, options, namingConvention);
        }
Exemplo n.º 6
0
 public static JSObjectBinder Get(Type type, JSBindingOptions options)
 {
     lock (binders)
     {
         JSObjectBinder value;
         if (binders.TryGetValue(type, out value))
         {
             return value;
         }
         else
         {
             var binder = new JSObjectBinder(type, options);
             binders[type] = binder;
             return binder;
         }
     }
 }
Exemplo n.º 7
0
 public static ScriptableObjectBinder Get(Type type, JSBindingOptions options)
 {
     lock (binders)
     {
         ScriptableObjectBinder value;
         if (binders.TryGetValue(type, out value))
         {
             return(value);
         }
         else
         {
             var binder = new ScriptableObjectBinder(type, options);
             binders[type] = binder;
             return(binder);
         }
     }
 }
Exemplo n.º 8
0
        private JSObjectBinder(Type type, JSBindingOptions options, NamingConvention namingConvention)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!type.IsInterface)
            {
                throw new ArgumentException("Only interface type allowed.", "type");
            }

            this.type             = type;
            this.options          = options;
            this.namingConvention = namingConvention;

            this.proxyType = JSObjectTypeBuilder.Create(type, options, namingConvention);
        }
Exemplo n.º 9
0
        public JSObjectDef(object target, Type targetType, string member, JSBindingOptions options)
        {
            if (string.IsNullOrEmpty(member)) throw new ArgumentNullException(member);
            if (target == null) throw new ArgumentNullException("target");
            if (targetType == null) throw new ArgumentNullException("targetType");
            // validate options here

            if (!targetType.IsAssignableFrom(target.GetType()))
            {
                throw new JSBindingException(string.Format(
                    "ScriptableObject is not assignable from '{0}'.", target.GetType().FullName
                    ));
            }

            this.member = member;
            this.target = target;
            this.targetType = targetType;
            this.options = options;
        }
Exemplo n.º 10
0
        private ScriptableObjectBinder(Type type, JSBindingOptions options, NamingConvention namingConvention)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (!type.IsClass && !type.IsInterface)
            {
                throw new ArgumentException("The type must be a class or interface.");
            }

            this.type = type;

            var attr = GetJSObjectAttribute(this.type);

            if (attr != null)
            {
                if (options == JSBindingContext.DefaultOptions)
                {
                    this.options = attr.Options;
                }
            }
            else
            {
                this.options = options;
            }

            this.namingConvention = namingConvention;

            this.Create();

            Debug.Assert(this.dispatchTable != null);
            Debug.Assert(this.propertyDispatchTable != null);

            if ((this.options & JSBindingOptions.LazyCompile) == 0)
            {
                this.Compile();
            }
        }
Exemplo n.º 11
0
        public static Type Create(Type targetType, JSBindingOptions options, NamingConvention namingConvention)
        {
            Initialize();

            // create the type
            TypeBuilder type = module.DefineType(
                string.Format("{0}.{1}Proxy", namespaceName, targetType.FullName),
                TypeAttributes.Public | TypeAttributes.Sealed,
                typeof(JSObjectBase),
                new Type[] { targetType }
                );

            // TODO: properties

            // create members
            foreach (var method in targetType.GetMethods())
            {
                // explicit interface implementation
                MethodBuilder mb = type.DefineMethod(
                        targetType.Name + "." + method.Name,
                        MethodAttributes.Private | MethodAttributes.HideBySig |
                        MethodAttributes.NewSlot | MethodAttributes.Virtual |
                        MethodAttributes.Final,
                        method.ReturnType,
                        method.GetParameters().Select(_ => _.ParameterType).ToArray()
                        );
                type.DefineMethodOverride(mb, method);

                var emit = new EmitHelper(mb.GetILGenerator());
                emit.LdArg(1)
                    .Ret()
                    ;
            }

            return type.CreateType();
        }
Exemplo n.º 12
0
        public static Type Create(Type targetType, JSBindingOptions options, NamingConvention namingConvention)
        {
            Initialize();

            // create the type
            TypeBuilder type = module.DefineType(
                string.Format("{0}.{1}Proxy", namespaceName, targetType.FullName),
                TypeAttributes.Public | TypeAttributes.Sealed,
                typeof(JSObjectBase),
                new Type[] { targetType }
                );

            // TODO: properties

            // create members
            foreach (var method in targetType.GetMethods())
            {
                // explicit interface implementation
                MethodBuilder mb = type.DefineMethod(
                    targetType.Name + "." + method.Name,
                    MethodAttributes.Private | MethodAttributes.HideBySig |
                    MethodAttributes.NewSlot | MethodAttributes.Virtual |
                    MethodAttributes.Final,
                    method.ReturnType,
                    method.GetParameters().Select(_ => _.ParameterType).ToArray()
                    );
                type.DefineMethodOverride(mb, method);

                var emit = new EmitHelper(mb.GetILGenerator());
                emit.LdArg(1)
                .Ret()
                ;
            }

            return(type.CreateType());
        }
Exemplo n.º 13
0
 private ScriptableObjectBinder(Type type, JSBindingOptions options)
     : this(type, options, NamingConvention.Default)
 {
 }
Exemplo n.º 14
0
 public void BindJSObject(object obj, Type type, JSBindingOptions options)
 {
     this.JSBindingContext.BindJSObject(obj, type, options);
 }
Exemplo n.º 15
0
 public void BindJSObject <T>(T obj, JSBindingOptions options) where T : class
 {
     this.JSBindingContext.BindJSObject <T>(obj, options);
 }
Exemplo n.º 16
0
 public void BindJSObject(string name, object obj, JSBindingOptions options)
 {
     this.JSBindingContext.BindJSObject(name, obj, options);
 }
Exemplo n.º 17
0
 public JSObjectAttribute(JSBindingOptions bindingFlags)
 {
     this.options = bindingFlags;
 }
Exemplo n.º 18
0
 public void BindJSObject <T>(T obj, JSBindingOptions options) where T : class
 {
     this.BindJSObject(obj, typeof(T), options);
 }
Exemplo n.º 19
0
 public JSObjectAttribute(JSBindingOptions bindingFlags)
 {
     this.options = bindingFlags;
 }
 private ScriptableObjectBinder(Type type, JSBindingOptions options)
     : this(type, options, NamingConvention.Default)
 {
 }
Exemplo n.º 21
0
 public void BindJSObject(object obj, Type type, JSBindingOptions options)
 {
     this.BindJSObject(GetMemberNameForType(type), obj, type, options);
 }
Exemplo n.º 22
0
 public void BindJSObject <T>(string name, T obj, JSBindingOptions options) where T : class
 {
     this.JSBindingContext.BindJSObject <T>(name, obj, options);
 }