Пример #1
0
        internal void KeepAliveInvoke(int slot, ref JsValue args, ref JsValue output)
        {
            // TODO: This is pretty slow: use a cache of generated code to make it faster.
#if DEBUG_TRACE_API
            Console.WriteLine("invoking");
#endif
            //Console.WriteLine(args);

            object obj = KeepAliveGet(slot);
            if (obj == null)
            {
                output.Type = JsValueType.Error;
                output.I64  = (int)JsManagedError.NotFoundManagedObjectId;
                return;
            }

            Type constructorType = obj as Type;
            if (constructorType != null)
            {
#if DEBUG_TRACE_API
                Console.WriteLine("constructing " + constructorType.Name);
#endif
                object[] constructorArgs = (object[])_convert.FromJsValue(ref args);
                //TODO: review here
                _convert.AnyToJsValue(
                    Activator.CreateInstance(constructorType, constructorArgs), ref output);
                return;
            }
            //expect slot is del
            WeakDelegate func = obj as WeakDelegate;
            if (func == null)
            {
                throw new Exception("not a function.");
            }

            //owner type of the delegate
            Type type = func.Target != null?func.Target.GetType() : func.Type;

#if DEBUG_TRACE_API
            Console.WriteLine("invoking " + obj.Target + " method " + obj.MethodName);
#endif

            //review delegate invocation again
            object[] argObjects = (object[])_convert.FromJsValue(ref args);

            int j = argObjects.Length;
            for (int i = 0; i < j; ++i)
            {
                object a_elem = argObjects[i];
                if (a_elem.GetType() == typeof(JsFunction))
                {
                    CheckAndResolveJsFunctions(func, (JsFunction)a_elem, obj, type, func.MethodName, argObjects);
                    break;
                }
            }

            throw new NotSupportedException();
            //try
            //{
            //    object result = type.InvokeMember(func.MethodName, flags, null, func.Target, a);
            //    _convert.AnyToJsValue(result, ref output);
            //    return;
            //}
            //catch (TargetInvocationException e)
            //{
            //    output.Type = JsValueType.Error;
            //    output.I64 = (int)JsManagedError.TargetInvocationError;
            //    return;
            //}
            //catch (Exception e)
            //{
            //    //review set error
            //    output.Type = JsValueType.Error;
            //    output.I64 = (int)JsManagedError.SetKeepAliveError;
            //    return;
            //}
        }