public IBELObject ValueOf(string name, ArrayList arguments, ExecutionContext ctx) { BELMember mi = Type.BELMembers[name] as BELMember; if (mi== null) return null; ParameterInfo []parms = mi.MethodInfo.GetParameters(); int need = parms.Length; int got = (arguments == null) ? 0 : arguments.Count; bool needExecutionContext = mi.NeedsExecutionContext; if (needExecutionContext && (parms.Length == 0 || parms[0].ParameterType != typeof(ExecutionContext))) throw new ImplementationException("Incorrectly defined ExposedMethod property or function. First parameter of " + mi.MethodInfo.DeclaringType.FullName + "." + mi.MethodInfo.Name + " must be an ExecutionContext"); if (needExecutionContext) need--; if (got > need && !(mi.ExposedMethod.AllowsVariableArguments)) throw new MemberInvocationException(ctx.CurrentLocation, "Incorrect number of arguments (too many) for " + ExternalTypeName + "." + name + " (need " + need + ", got " + got + ")"); // Figure out what arguments we should be passing ArrayList args = new ArrayList(); InvocationFrame invocationFrame = new InvocationFrame(); if (needExecutionContext) args.Add(ctx); ArrayList parameterPresentFlags = null; if (needExecutionContext) { parameterPresentFlags = new ArrayList(); parameterPresentFlags.Add(true); // we know for sure the first arg is supplied; it's the execution context :-) } int offset = (needExecutionContext ? 1 : 0); for (int each = offset; each < parms.Length; each++) { object arg = null; if (arguments != null && (each - offset) < arguments.Count) arg = (ParseTreeNode)(arguments[each - offset]); if (!BELMember.IsOptionalParameter(parms[each]) && arg == null) throw new MemberInvocationException(ctx.CurrentLocation, "Missing argument " + (each - offset) + " for " + ExternalTypeName + "." + name); if (parameterPresentFlags != null) parameterPresentFlags.Add(arg != null); if (mi.ExposedMethod.IsCustomArgumentProcessor) { args.Add(arg); } else { if (arg == null) args.Add(AbsentValueForParameter(parms[each])); else args.Add(ConvertFromBELObjectIfNeeded(((ExposableParseTreeNode)arg).Expose(ctx))); } } invocationFrame.WasParameterSuppliedFlags = parameterPresentFlags; // If we have extras (beyond those needed) and they're allowed, stash them in the MIC, too if (mi.ExposedMethod.AllowsVariableArguments) { ArrayList extras = new ArrayList(); int extraCount = got - need; if (arguments != null) { for (int i = need; i < got; i++) { object arg = arguments[i]; if (mi.ExposedMethod.IsCustomArgumentProcessor) { extras.Add(arg); } else { if (arg == null) extras.Add(null); else extras.Add(ConvertFromBELObjectIfNeeded(((ExposableParseTreeNode)arg).Expose(ctx))); } } } invocationFrame.ExtraArguments = extras; } // Check types for (int each = 0; each < parms.Length; each++) { bool bad = false; if (args[each] == null) { if (parms[each].ParameterType.IsValueType) bad = true; } else { if (!parms[each].ParameterType.IsAssignableFrom(args[each].GetType())) bad = true; } if (bad) throw new MemberInvocationException(ctx.CurrentLocation, "Argument " + (each + 1) + " for " + ExternalTypeName + "." + name + " is not of the correct type (was " + ExternalTypeNameForType(args[each].GetType()) + ", but needed " + ExternalTypeNameForType(parms[each].ParameterType) + ")"); } // And now, invoke away!! object result = null; invocationFrame.PushScope(ctx.CurrentScope); // the new frame starts with the same scope as this one ctx.PushFrame(invocationFrame); if (Federation.GetPerformanceCounter(Federation.PerformanceCounterNames.MethodInvocation) != null) Federation.GetPerformanceCounter(Federation.PerformanceCounterNames.MethodInvocation).Increment(); try { result = mi.MethodInfo.Invoke(this, args.ToArray()); } catch (TargetInvocationException ex) { throw ex.InnerException; } ctx.PopFrame(); if (mi.ExposedMethod.CachePolicy == ExposedMethodFlags.CachePolicyNever) ctx.AddCacheRule(new CacheRuleNever()); return Wrap(result); }
public override IBELObject ValueOf(string name, System.Collections.ArrayList arguments, ExecutionContext ctx) { Hashtable members = ctx.CurrentFederation.GetTopicProperties(Name); string val = (string)(members[name]); if (val == null) return null; val = val.Trim(); bool isBlock = val.StartsWith("{"); if (!isBlock) return new BELString(val); // It's a block, so fire up the interpreter if (!val.EndsWith("}")) throw new ExecutionException("Topic member " + name + " defined in " + Name.Fullname + " is not well-formed; missing closing '}' for code block."); ContentBase cb = CurrentFederation.ContentBaseForTopic(Name); TopicContext newContext = new TopicContext(ctx.CurrentFederation, cb, CurrentTopicInfo); BehaviorInterpreter interpreter = new BehaviorInterpreter(val, CurrentFederation, CurrentFederation.WikiTalkVersion, ctx.Presenter); if (!interpreter.Parse()) throw new ExecutionException("Parsing error evaluating topic member " + name + " defined in " + Name.Fullname + ": " + interpreter.ErrorString); IBELObject b1 = interpreter.EvaluateToObject(newContext, ctx.ExternalWikiMap); if (b1 == null) throw new ExecutionException("Error while evaluating topic member " + name + " defined in " + Name.Fullname + ": " + interpreter.ErrorString); Block block = (Block)b1; ArrayList evaluatedArgs = new ArrayList(); foreach (object each in arguments) { IBELObject add = null; if (each != null && each is IBELObject) add = each as IBELObject; else { ExposableParseTreeNode ptn = each as ExposableParseTreeNode; add = ptn.Expose(ctx); } evaluatedArgs.Add(add); } InvocationFrame invocationFrame = new InvocationFrame(); ctx.PushFrame(invocationFrame); TopicScope topicScope = new TopicScope(null, this); ctx.PushScope(topicScope); // make sure we can use local references IBELObject answer = block.Value(ctx, evaluatedArgs); ctx.PopScope(); ctx.PopFrame(); // make sure to transfer any new cache rules // BELTODO - want a test case for this foreach (CacheRule r in interpreter.CacheRules) ctx.AddCacheRule(r); return answer; }
public override IBELObject ValueOf(string name, System.Collections.ArrayList arguments, ExecutionContext ctx) { TopicPropertyCollection members = ctx.CurrentFederation.GetTopicProperties(Name); string val = members[name].LastValue; if (val == null) return null; val = val.Trim(); bool isBlock = val.StartsWith("{"); if (!isBlock) return new BELString(val); // It's a block, so fire up the interpreter if (!val.EndsWith("}")) throw new ExecutionException(ctx.CurrentLocation, "Topic member " + name + " defined in " + Name.DottedName + " is not well-formed; missing closing '}' for code block."); NamespaceManager cb = CurrentFederation.NamespaceManagerForTopic(Name); TopicContext newContext = new TopicContext(ctx.CurrentFederation, cb, CurrentTopicInfo); BehaviorInterpreter interpreter = new BehaviorInterpreter(Name.DottedName + "#" + name, val, CurrentFederation, CurrentFederation.WikiTalkVersion, ctx.Presenter); if (!interpreter.Parse()) throw new ExecutionException(ctx.CurrentLocation, "Syntax error in " + interpreter.ErrorString); IBELObject b1 = interpreter.EvaluateToObject(newContext, ctx.ExternalWikiMap); if (b1 == null) throw new ExecutionException(ctx.CurrentLocation, "Execution error in " + interpreter.ErrorString); Block block = (Block) b1; ArrayList evaluatedArgs = new ArrayList(); foreach (object each in arguments) { IBELObject add = null; if (each != null && each is IBELObject) add = each as IBELObject; else { ExposableParseTreeNode ptn = each as ExposableParseTreeNode; add = ptn.Expose(ctx); } evaluatedArgs.Add(add); } InvocationFrame invocationFrame = new InvocationFrame(); ctx.PushFrame(invocationFrame); TopicScope topicScope = new TopicScope(null, this); ctx.PushScope(topicScope); // make sure we can use local references IBELObject answer = block.Value(ctx, evaluatedArgs); ctx.PopScope(); ctx.PopFrame(); return answer; }