public IValue CreateClassInstance(BasicClepsType instanceType, List <IValue> parameters) { string parameterString = String.Join(", ", parameters.Select(v => (v as JavaScriptValue).Expression).ToList()); string code = String.Format("new {0}({1})", instanceType.GetClepsTypeString(), parameterString); JavaScriptValue ret = new JavaScriptValue(code, instanceType); return(ret); }
private IValue doFunctionCall(ParserRuleContext context, string targetFunctionName, List <IValue> parameters, IValue target, ClepsType targetType, bool allowVoidReturn) { IValue dereferencedTarget = target == null? null : GetDereferencedRegisterOrNull(target); BasicClepsType dereferencedType = target == null? targetType as BasicClepsType : dereferencedTarget.ExpressionType as BasicClepsType; if (dereferencedType == null) { string errorMessage = String.Format("Could not dereference expression on type {0}", targetType.GetClepsTypeString()); Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage)); //just return something to avoid stalling return(CodeGenerator.CreateByte(0)); } ClepsClass targetClepsClass = ClassManager.GetClass(dereferencedType.GetClepsTypeString()); List <ClepsVariable> functionOverloads; bool isStatic; if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName)) { isStatic = true; functionOverloads = targetClepsClass.StaticMemberMethods[targetFunctionName]; } else if (target != null && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName)) { isStatic = false; functionOverloads = targetClepsClass.MemberMethods[targetFunctionName]; } else { string errorMessage = String.Format("Class {0} does not contain a {1}static function called {2}.", targetClepsClass.FullyQualifiedName, target == null? "" : "member or ", targetFunctionName); Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage)); //Just return something to avoid stalling compilation return(CodeGenerator.CreateByte(0)); } int matchedPosition; string fnMatchErrorMessage; if (!FunctionOverloadManager.FindMatchingFunctionType(TypeManager, functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage)) { Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, fnMatchErrorMessage)); //Just return something to avoid stalling compilation return(CodeGenerator.CreateByte(0)); } FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition].VariableType as FunctionClepsType; if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidClepsType.GetVoidType()) { string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName); Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage)); //Just return something to avoid stalling compilation return(CodeGenerator.CreateByte(0)); } IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(isStatic? null : dereferencedTarget, dereferencedType, targetFunctionName, chosenFunctionType, parameters); return(returnValue); }
public IValue GetFunctionCallReturnValue(IValue target, BasicClepsType targetType, string targetFunctionName, FunctionClepsType clepsType, List <IValue> parameters) { string code; if (CompilerConstants.SystemSupportedTypes.Contains(targetType) && target != null) { string fullFunctionName = String.Format("{0}.{1}.prototype.{2}", JavaScriptCodeParameters.TOPLEVELNAMESPACE, targetType.GetClepsTypeString(), JavaScriptCodeParameters.GetMangledFunctionName(targetFunctionName, clepsType)); string functionTarget = target != null ? (target as JavaScriptValue).Expression : String.Format("{0}.{1}", JavaScriptCodeParameters.TOPLEVELNAMESPACE, targetType.GetClepsTypeString()); string parameterString = String.Join("", parameters.Select(v => ", " + (v as JavaScriptValue).Expression).ToList()); code = String.Format("{0}.call({1}{2})", fullFunctionName, functionTarget, parameterString); } else { string functionTarget = target != null ? (target as JavaScriptValue).Expression : String.Format("{0}.{1}", JavaScriptCodeParameters.TOPLEVELNAMESPACE, targetType.GetClepsTypeString()); string fullFunctionName = String.Format("{0}.{1}", functionTarget, JavaScriptCodeParameters.GetMangledFunctionName(targetFunctionName, clepsType)); string parameterString = String.Join(", ", parameters.Select(v => (v as JavaScriptValue).Expression).ToList()); code = String.Format("{0}({1})", fullFunctionName, parameterString); } JavaScriptValue ret = new JavaScriptValue(code, clepsType.ReturnType); return(ret); }