Example #1
0
        /// <summary>
        /// Creates a scripting method definition from the reflection info
        /// </summary>
        private static SciterNativeMethodDef DefineScriptingMethod(MethodInfo methodInfo, ScriptingMethodAttribute methodAttr)
        {
            return(new SciterNativeMethodDef()
            {
                name = methodAttr.Name ?? methodInfo.Name,

                // Method callback implementation
                method = (IntPtr hvm, ref JsonValue p_data_slot, IntPtr argv, int argc, ref JsonValue retval) =>
                {
                    try
                    {
                        var instance = default(object);
                        if (!methodInfo.IsStatic)
                        {
                            instance = InstanceProtector.GetInstance(p_data_slot.GetNativeObject());
                        }

                        var result = methodInfo.Invoke(instance, JsonPtrToArray(argv, argc));
                        retval = result == null ? new JsonValue() : new JsonValue(result);
                    }
                    catch (Exception ex)
                    {
                        SciterHostApi.SciterNativeThrow(hvm, ex.Message);
                    }
                }
            });
        }
Example #2
0
        /// <summary>
        /// Creates a scripting property definitions
        /// </summary>
        private static SciterNativePropertyDef DefineScriptingProperty(PropertyInfo propertyInfo, ScriptingPropertyAttribute propertyAttr)
        {
            return(new SciterNativePropertyDef()
            {
                name = propertyAttr.Name ?? propertyInfo.Name,

                // Property callback implementation
                property = (IntPtr hvm, ref JsonValue p_data_slot, bool set, ref JsonValue retval) =>
                {
                    try
                    {
                        var instance = default(object);
                        if (!propertyInfo.GetGetMethod().IsStatic)
                        {
                            instance = InstanceProtector.GetInstance(p_data_slot.GetNativeObject());
                        }

                        retval = new JsonValue();
                        if (set)
                        {
                            propertyInfo.SetValue(instance, retval.GetValue(), null);
                        }
                        else
                        {
                            retval = new JsonValue(propertyInfo.GetValue(instance, null));
                        }
                    }
                    catch (Exception ex)
                    {
                        SciterHostApi.SciterNativeThrow(hvm, ex.Message);
                    }
                }
            });
        }
Example #3
0
        /// <summary>
        /// Registers scripting class
        /// </summary>
        public static bool RegisterClass <TType>(SciterView view)
        {
            Debug.Assert(view != null && view.HandleInternal != IntPtr.Zero, "View cannot be null");

            var result = false;
            var hvm    = SciterHostApi.SciterGetVM(view.HandleInternal);

            if (hvm != IntPtr.Zero)
            {
                var scripting = GetScriptingClasses(view, hvm);

                var sciterClassDef = GetClassDef(typeof(TType));
                if (!scripting.IsExists(sciterClassDef))
                {
                    var nativeClass = sciterClassDef.ToNative();
                    try
                    {
                        result = SciterHostApi.SciterNativeDefineClass(hvm, nativeClass);
                        scripting.ClassDefs.Add(sciterClassDef);
                    }
                    finally { SciterClassDef.FreeNative(nativeClass); }
                }
                else
                {
                    throw new ArgumentException("Class already registered", "TType");
                }
            }

            return(result);
        }
Example #4
0
        /// <summary>
        /// Creates a scripting ctor definition from the reflection info
        /// </summary>
        private static SciterNativeMethodDef DefineCtorMethod(Type type)
        {
            return(new SciterNativeMethodDef()
            {
                name = "this",

                // Construction callback implementation
                method = (IntPtr hvm, ref JsonValue p_data_slot, IntPtr argv, int argc, ref JsonValue retval) =>
                {
                    try
                    {
                        var result = Activator.CreateInstance(type, JsonPtrToArray(argv, argc));
                        var data_slot_value = InstanceProtector.Protect(result);

                        p_data_slot.SetNativeObject(data_slot_value);
                        _registrations[hvm].Instances.Add(data_slot_value, result);
                    }
                    catch (Exception ex)
                    {
                        SciterHostApi.SciterNativeThrow(hvm, ex.Message);
                    }
                }
            });
        }