Пример #1
0
        public JavaScriptValue CallFunction(string name, params object[] parameters)
        {
            ResetContext();

            JavaScriptValue globalObject;

            Native.JsGetGlobalObject(out globalObject);

            JavaScriptPropertyId functionId = JavaScriptPropertyId.FromString(name);

            var function = globalObject.GetProperty(functionId);

            // Parameters
            var javascriptParameters = new List <JavaScriptValue>();

            javascriptParameters.Add(globalObject); // this value

            foreach (var parameter in parameters)
            {
                var parameterType = parameter.GetType().Name;
                switch (parameterType)
                {
                case "Int32":
                    javascriptParameters.Add(JavaScriptValue.FromInt32((int)parameter));
                    break;

                case "Double":
                    javascriptParameters.Add(JavaScriptValue.FromDouble((double)parameter));
                    break;

                case "Boolean":
                    javascriptParameters.Add(JavaScriptValue.FromBoolean((bool)parameter));
                    break;

                case "String":
                    javascriptParameters.Add(JavaScriptValue.FromString((string)parameter));
                    break;

                default:
                    throw new Exception("Not supported type: " + parameterType);
                }
            }

            // call function
            return(function.CallFunction(javascriptParameters.ToArray()));
        }
Пример #2
0
        public object CallFunction(string name, params object[] parameters)
        {
            try
            {
                SetContextOnCurrentThread();
                Native.JsGetGlobalObject(out JavaScriptValue globalObject);

                var functionId = JavaScriptPropertyId.FromString(name);

                var function = globalObject.GetProperty(functionId);

                // Parameters
                var javascriptParameters = new List <JavaScriptValue>
                {
                    globalObject // this value
                };
                foreach (var parameter in parameters)
                {
                    var parameterType = parameter.GetType().Name;
                    switch (parameterType)
                    {
                    case "Int32":
                        javascriptParameters.Add(JavaScriptValue.FromInt32((int)parameter));
                        break;

                    case "Double":
                        javascriptParameters.Add(JavaScriptValue.FromDouble((double)parameter));
                        break;

                    case "Boolean":
                        javascriptParameters.Add(JavaScriptValue.FromBoolean((bool)parameter));
                        break;

                    case "String":
                        javascriptParameters.Add(JavaScriptValue.FromString((string)parameter));
                        break;

                    default:
                        //throw new Exception("Not supported type: " + parameterType);
                        javascriptParameters.Add(JavaScriptValue.FromInspectable(parameter));
                        break;
                    }
                }
                var retval = function.CallFunction(javascriptParameters.ToArray());
                Native.ThrowIfError(
                    Native.JsGetValueType(
                        retval,
                        out JavaScriptValueType type));
                switch (type)
                {
                case JavaScriptValueType.Array:
                    return(retval);

                case JavaScriptValueType.String:
                    return(retval.ToString());

                case JavaScriptValueType.Null:
                case JavaScriptValueType.Undefined:
                default:
                    return(null);
                }
            }
            finally
            {
                RemoveContextOnCurrentThread();
            }
        }