public static string WritePlatformInvokeMethod(this PlatformInvokeMethod platformInvoke, ClosureEntities crRuntime) { var methodId = Import(platformInvoke.LibraryName, platformInvoke.MethodName, platformInvoke.CallingConvention, platformInvoke.EntryPoint); var codeOutput = new CodeOutput(); codeOutput.AppendFormat(platformInvoke.WritePInvokeDefinition(methodId)); codeOutput.BlankLine(); codeOutput.Append(platformInvoke.Method.WriteHeaderMethod(crRuntime, false)); // write PInvoke implementation codeOutput.BracketOpen(); var argumentsCall = platformInvoke.Method.GetParameters() .Select(CallMarshallerFactory.CreateMarshaller) .Each(marshaller => { codeOutput.Append(marshaller.GetTransformationCode()); }) .Once(marshallers => { codeOutput.BlankLine(); }) .Select(p => p.GetParameterString()) .Join(", "); if (!platformInvoke.Method.GetReturnType().IsVoid()) { codeOutput.Append("return "); } codeOutput.AppendFormat("{0}({1});", methodId, argumentsCall); codeOutput.BracketClose(); return(codeOutput.ToString()); }
public static string LoadDllMethods() { var sb = new CodeOutput(); sb.BlankLine() .Append("System_Void mapLibs()") .BracketOpen(); var pos = 0; foreach (var library in LinkingData.Libraries) { sb.Append("//---------------------------------------------------------\n") .AppendFormat("// {0} methods\n", library.DllName) .Append("//---------------------------------------------------------\n") .AppendFormat("auto lib_{0} = LoadNativeLibrary(L\"{1}\");\n", pos, library.DllName) .BlankLine(); foreach (var method in library.Methods.Values) { sb.AppendFormat("{0} = ({0}_type)LoadNativeMethod(lib_{2}, \"{1}\");\n", method.FormattedName(), method.EntryPoint, pos); } pos++; } return(sb.BracketClose() .BlankLine() .ToString()); }
static void WriteMainBody(MethodInterpreter interpreter, CodeOutput sb, ClosureEntities crRuntime) { sb.Append("System_Void initializeRuntime();\n"); sb.Append("int main(int argc, char**argv)").BracketOpen(); sb.Append("auto argsAsList = System_getArgumentsAsList(argc, argv);\n"); sb.Append("initializeRuntime();\n"); var entryPoint = interpreter.Method as MethodInfo; if (entryPoint.ReturnType != typeof(void)) { sb.Append("return "); } var parameterInfos = entryPoint.GetParameters(); var args = string.Empty; if (parameterInfos.Length != 0) { args = "argsAsList"; } sb.AppendFormat("{0}({1});\n", entryPoint.ClangMethodSignature(crRuntime), args); sb.BlankLine(); sb.Append("return 0;"); sb.BracketClose(); }
public string BuildStringTable() { var sb = new CodeOutput(); sb.BlankLine() .Append("System_Void buildStringTable()") .BracketOpen(); var stringDataBuilder = new List <string>(); var jump = 0; foreach (var strItem in _table) { sb.AppendFormat("_AddJumpAndLength({0}, {1});\n", jump, strItem.Length); var itemTextData = TextData(strItem); AddTextToStringTable(stringDataBuilder, itemTextData, strItem); jump += strItem.Length + 1; } sb.BracketClose(true) .Append(" // buildStringTable\n"); var stringTableContent = string.Join(", " + Environment.NewLine, stringDataBuilder); var length = jump == 0 ? 1 : jump; sb.BlankLine() .AppendFormat("const System_Char _stringTable[{0}] =", length) .BracketOpen(); sb.Append(jump == 0 ? "0" : stringTableContent); sb.BracketClose(true) .Append("; // _stringTable\n"); return(sb.ToString()); }
public static string BuildConstantTable() { var sb = new CodeOutput(); sb.BlankLine() .Append("System_Void RuntimeHelpersBuildConstantTable()") .BracketOpen(); foreach (var item in Instance.ItemList) { var rightArray = item.Data; var rightArrayItems = string.Join(", ", rightArray); sb.AppendFormat("AddConstantByteArray(new byte[{0}]);", rightArray.Length) .BracketOpen() .AppendFormat("{1}", rightArrayItems) .BracketClose(); } return(sb.BracketClose() .ToString()); }