private void InvokeFunction(InvokeFunctionMessage msg) { try { object scope; if (msg.ScopeId != 0) { scope = this.refToObj[msg.ScopeId]; } else { scope = null; } List<object> args = new List<object>(); args.Add(msg.Name); args.Add(scope); foreach (JsValue arg in msg.Parameters) { object item = WrapRemoteValue(arg); args.Add(item); } object ret = this.helper.InvokeFunction(args.ToArray()); HandleReturn(ret); } catch (Exception ex) { Console.WriteLine(ex.Message); JsValue value = JsValue.FromPrimitive(ex.Message); ReturnMessage retMsg = new ReturnMessage { Value = value, IsException = true }; this.session.SendMessage(retMsg); } }
private void HandleReturn(object ret) { IReflect reflect = ret as IReflect; if (reflect == null) throw new InvalidOperationException(); MemberInfo[] members = reflect.GetMembers(BindingFlags.Default); PropertyInfo pi0 = reflect.GetProperty("0", BindingFlags.Default); bool isException = (bool)pi0.GetValue(ret, null); PropertyInfo pi1 = reflect.GetProperty("1", BindingFlags.Default); ReturnMessage retMsg; if (pi1 == null) { retMsg = new ReturnMessage { IsException = isException, Value = new JsValue(JsValueType.Void, null) }; } else { object result = pi1.GetValue(ret, null); retMsg = new ReturnMessage { IsException = isException, Value = WrapLocalValue(result) }; } this.session.SendMessage(retMsg); }
private void InvokeDelegate(InvokeDelegateMessage msg) { try { IReflect target = this.refToObj[msg.TargetId] as IReflect; List<object> args = new List<object>(); args.Add(target); foreach (JsValue arg in msg.Parameters) { object item = WrapRemoteValue(arg); args.Add(item); } object ret = this.helper.InvokeDelegate(args.ToArray()); HandleReturn(ret); } catch (Exception ex) { Console.WriteLine(ex.Message); JsValue value = JsValue.FromPrimitive(ex.Message); ReturnMessage retMsg = new ReturnMessage { Value = value, IsException = true }; this.session.SendMessage(retMsg); } }
private void DefineFunction(DefineFunctionMessage msg) { try { string definition = string.Format("__$helper.functions['{0}'] = function({1}) {{ {2} }};", msg.Name, msg.Parameters, msg.Body); this.Window.execScript(definition, null); } catch (Exception ex) { Console.WriteLine(ex.Message); JsValue value = JsValue.FromPrimitive(ex.Message); ReturnMessage retMsg = new ReturnMessage { Value = value, IsException = true }; this.session.SendMessage(retMsg); } }