Example #1
0
        public ScriptEventResult Execute()
        {
            if (_returnType == ReturnType.Unsupported)
            {
                //Can't use anything that's not void: SciterValue, Task<void> or Task<SciterValue>
                return(ScriptEventResult.Failed());
            }

            if (_methodInfo != null)
            {
                var parameters = BuildParameters();
                {
                    try
                    {
                        if (_isWrappedCallback && _isAwaitable)
                        {
                            ((Task)_methodInfo.Invoke(_owner, parameters))?.ContinueWith(
                                task =>
                            {
                                if (task.IsFaulted)
                                {
                                    return;
                                }

                                if (!task.GetType().IsGenericType)
                                {
                                    return;
                                }

                                _callbackValue?.Invoke((task as Task <SciterValue>)?.Result ?? SciterValue.Null, SciterValue.Null);
                            });

                            return(ScriptEventResult.Successful());
                        }

                        var ret = _methodInfo.Invoke(_owner, parameters);

                        // Awaitable Tasks should return Successful immediately, don't block the main Thread waiting for completion!
                        if (_isAwaitable)
                        {
                            return(ScriptEventResult.Successful());
                        }

                        var value = (ret as SciterValue) ?? SciterValue.Null;
                        return(ScriptEventResult.Successful(value));
                    }
                    catch (TargetInvocationException e)
                    {
                        return(ExceptionCallbackResult(e.InnerException ?? e));
                    }
                    catch (Exception e)
                    {
                        return(ExceptionCallbackResult(e));
                    }
                }
            }

            // not handled
            return(ScriptEventResult.Failed());
        }
Example #2
0
        private ScriptEventResult ExceptionCallbackResult(Exception e)
        {
            if (!_isWrappedCallback || !_isAwaitable)
            {
                return(ScriptEventResult.Successful(SciterValue.MakeError(e?.Message)));
            }

            //TODO: Clean this up, maybe change the Dictionary<> implementation?
            var properties = (e)
                             .GetType()
                             .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(w => typeof(IConvertible).IsAssignableFrom(w.PropertyType))
                             .ToDictionary(key => key.Name, value => value.GetValue(e) as IConvertible);

            //.ToDictionary(key => key.Name, value => SciterValue.Create(value.GetValue(e.InnerException)));
            properties.Add(nameof(Type), e?.GetType().FullName);

            _callbackValue?.Invoke(SciterValue.Null, SciterValue.Create(properties));

            return(ScriptEventResult.Successful());
        }
 /// <summary>
 /// This method is typically used with WinForms applications
 /// </summary>
 /// <param name="element"></param>
 /// <param name="methodName"></param>
 /// <param name="args"></param>
 // TODO: Should be specific to WinForms?
 protected virtual ScriptEventResult OnScriptCall(SciterElement element, string methodName, SciterValue[] args)
 {
     return(ScriptEventResult.Failed());
 }