示例#1
0
 public JSFunction (JSContext context, string name, JSFunctionHandler handler) : base (context, IntPtr.Zero)
 {
     this.handler = handler;
     var native_name = JSString.New (name);
     Raw = JSObjectMakeFunctionWithCallback (context.Raw, native_name,
         native_callback = new JSObject.CallAsFunctionCallback (JSCallback));
     native_name.Release ();
 }
        public JSFunction(JSContext context, string name, JSFunctionHandler handler) : base(context, IntPtr.Zero)
        {
            this.handler = handler;
            var native_name = JSString.New(name);

            Raw = JSObjectMakeFunctionWithCallback(context.Raw, native_name,
                                                   native_callback = new JSObject.CallAsFunctionCallback(JSCallback));
            native_name.Release();
        }
示例#3
0
        private void InstallStaticMethods()
        {
            List <JSStaticFunction> methods = null;

            foreach (var method in GetType().GetMethods(
                         BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic))
            {
                foreach (var _attr in method.GetCustomAttributes(typeof(JSStaticFunctionAttribute), false))
                {
                    var attr = (JSStaticFunctionAttribute)_attr;
                    var p    = method.GetParameters();

                    if (method.ReturnType != typeof(JSValue) || p.Length != 3 &&
                        p[0].ParameterType != typeof(JSObject) ||
                        p[1].ParameterType != typeof(JSObject) ||
                        p[2].ParameterType != typeof(JSValue []))
                    {
                        throw new Exception(String.Format("Invalid signature for method annotated " +
                                                          "with JSStaticFunctionAttribute: {0}:{1} ('{2}'); signature should be " +
                                                          "'JSValue:JSFunction,JSObject,JSValue[]'",
                                                          GetType().FullName, method.Name, attr.Name));
                    }

                    if (static_methods == null)
                    {
                        static_methods = new Dictionary <string, MethodInfo> ();
                    }
                    else if (static_methods.ContainsKey(attr.Name))
                    {
                        throw new Exception("Class already contains static method named '" + attr.Name + "'");
                    }

                    static_methods.Add(attr.Name, method);

                    if (methods == null)
                    {
                        methods = new List <JSStaticFunction> ();
                    }

                    if (static_function_callback == null)
                    {
                        static_function_callback = new JSObject.CallAsFunctionCallback(OnStaticFunctionCallback);
                    }

                    methods.Add(new JSStaticFunction()
                    {
                        Name       = attr.Name,
                        Attributes = attr.Attributes,
                        Callback   = static_function_callback
                    });
                }
            }

            if (methods != null && methods.Count > 0)
            {
                var size = Marshal.SizeOf(typeof(JSStaticFunction));
                var ptr  = Marshal.AllocHGlobal(size * (methods.Count + 1));

                for (int i = 0; i < methods.Count; i++)
                {
                    Marshal.StructureToPtr(methods[i],
                                           new IntPtr(ptr.ToInt64() + size * i), false);
                }

                Marshal.StructureToPtr(new JSStaticFunction(),
                                       new IntPtr(ptr.ToInt64() + size * methods.Count), false);

                raw.static_functions = ptr;
            }
        }
示例#4
0
        private void InstallStaticMethods ()
        {
            List<JSStaticFunction> methods = null;

            foreach (var method in GetType ().GetMethods (
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic)) {
                foreach (var _attr in method.GetCustomAttributes (typeof (JSStaticFunctionAttribute), false)) {
                    var attr = (JSStaticFunctionAttribute)_attr;
                    var p = method.GetParameters ();

                    if (method.ReturnType != typeof (JSValue) || p.Length != 3 &&
                        p[0].ParameterType != typeof (JSObject) ||
                        p[1].ParameterType != typeof (JSObject) ||
                        p[2].ParameterType != typeof (JSValue [])) {
                        throw new Exception (String.Format ("Invalid signature for method annotated " +
                            "with JSStaticFunctionAttribute: {0}:{1} ('{2}'); signature should be " +
                            "'JSValue:JSFunction,JSObject,JSValue[]'",
                                GetType ().FullName, method.Name, attr.Name));
                    }

                    if (static_methods == null) {
                        static_methods = new Dictionary<string, MethodInfo> ();
                    } else if (static_methods.ContainsKey (attr.Name)) {
                        throw new Exception ("Class already contains static method named '" + attr.Name  + "'");
                    }

                    static_methods.Add (attr.Name, method);

                    if (methods == null) {
                        methods = new List<JSStaticFunction> ();
                    }

                    if (static_function_callback == null) {
                        static_function_callback = new JSObject.CallAsFunctionCallback (OnStaticFunctionCallback);
                    }

                    methods.Add (new JSStaticFunction () {
                        Name = attr.Name,
                        Attributes = attr.Attributes,
                        Callback = static_function_callback
                    });
                }
            }

            if (methods != null && methods.Count > 0) {
                var size = Marshal.SizeOf (typeof (JSStaticFunction));
                var ptr = Marshal.AllocHGlobal (size * (methods.Count + 1));

                for (int i = 0; i < methods.Count; i++) {
                    Marshal.StructureToPtr (methods[i],
                        new IntPtr (ptr.ToInt64 () + size * i), false);
                }

                Marshal.StructureToPtr (new JSStaticFunction (),
                    new IntPtr (ptr.ToInt64 () + size * methods.Count), false);

                raw.static_functions = ptr;
            }
        }