// Analogous to the C function of the same name created by mprep for C installable MathLink programs. // Note that the built-in DefineExternal function in Mathematica is not abort-safe, so .NET/Link // uses its own version, netlinkDefineExternal. private static void definePattern(IMathLink ml, string patt, string args, int index) { ml.PutFunction(KernelLinkImpl.PACKAGE_INTERNAL_CONTEXT + "netlinkDefineExternal", 3); ml.Put(patt); ml.Put(args); ml.Put(index); }
public static void WriteEvalToTypesetExpression(IMathLink ml, object obj, int pageWidth, string graphicsFmt, bool useStdForm) { ml.PutFunction("EvaluatePacket", 1); int numArgs = 2 + (useStdForm ? 0 : 1) + (pageWidth > 0 ? 1 : 0); ml.PutFunction("EvaluateToTypeset", numArgs); ml.Put(obj); if (!useStdForm) { ml.PutSymbol("TraditionalForm"); } if (pageWidth > 0) { ml.Put(pageWidth); } ml.Put(graphicsFmt); ml.EndPacket(); }
public static void WriteEvalToImageExpression(IMathLink ml, object obj, int width, int height, string graphicsFmt, int dpi, bool useFE) { ml.PutFunction("EvaluatePacket", 1); int numArgs = 2 + (useFE ? 1 : 0) + (dpi > 0 ? 1 : 0) + (width > 0 || height > 0 ? 1 : 0); ml.PutFunction("EvaluateToImage", numArgs); ml.Put(obj); if (useFE) { ml.Put(true); } ml.Put(graphicsFmt); if (dpi > 0) { ml.PutFunction("Rule", 2); ml.PutSymbol("ImageResolution"); ml.Put(dpi); } if (width > 0 || height > 0) { ml.PutFunction("Rule", 2); ml.PutSymbol("ImageSize"); ml.PutFunction("List", 2); if (width > 0) { ml.Put(width); } else { ml.PutSymbol("Automatic"); } if (height > 0) { ml.Put(height); } else { ml.PutSymbol("Automatic"); } } ml.EndPacket(); }
// These next methods are called by implementors of the KernelLink "evaluateTo" methods. It is useful to separate the // code to create the appropriate expression to send (which is what these methods do) and the code that runs the // reading loop. In these methods, obj must be a string or Expr. // This is ready to accommodate an evaluateToMathML() function, but I have not added such a function to KernelLink yet. public static void WriteEvalToStringExpression(IMathLink ml, Object obj, int pageWidth, string format) { ml.PutFunction("EvaluatePacket", 1); ml.PutFunction("ToString", 3); if (obj is string) { ml.PutFunction("ToExpression", 1); } ml.Put(obj); ml.PutFunction("Rule", 2); ml.PutSymbol("FormatType"); ml.PutSymbol(format); ml.PutFunction("Rule", 2); ml.PutSymbol("PageWidth"); if (pageWidth > 0) { ml.Put(pageWidth); } else { ml.PutSymbol("Infinity"); } ml.EndPacket(); }
/// <summary> /// Writes an instance of the class specified as the <see cref="IMathLink.ComplexType">ComplexType</see> on the link. /// </summary> /// public void PutComplex(IMathLink ml, object obj) { if (ComplexType == null) { throw new MathLinkException(MathLinkException.MLE_NO_COMPLEX); } double re = 0; double im = 0; try { re = getRealPart(obj); im = getImaginaryPart(obj); } catch (Exception e) { // Shouldn't get here. ml.PutSymbol("$Failed"); throw e; } ml.PutFunction("Complex", 2); // Use Put(double), because it has code to handle NaN, infinity. ml.Put(re); ml.Put(im); }
/// <summary> /// Analogous to the MLInstall function in mprep-generated C programs. /// </summary> /// <param name="ml"></param> /// <returns>Whether it succeeded or not.</returns> /// public static bool install(IMathLink ml) { // Adding nXX functions here requires also adding the symbols to the NETLink`NET.m file. try { ml.Connect(); ml.Put("Begin[\"" + KernelLinkImpl.PACKAGE_INTERNAL_CONTEXT + "\"]"); definePattern(ml, "nCall[typeName_String, obj_Symbol, callType_Integer, isByVal_, memberName_String, argCount_Integer, typesAndArgs___]", "{typeName, obj, callType, isByVal, memberName, argCount, typesAndArgs}", CALL); definePattern(ml, "nLoadType1[type_String, assemblyName_]", "{type, assemblyName}", LOADTYPE1); definePattern(ml, "nLoadType2[type_String, assemblyObj_]", "{type, assemblyObj}", LOADTYPE2); definePattern(ml, "nLoadExistingType[typeObject_?NETObjectQ]", "{typeObject}", LOADEXISTINGTYPE); definePattern(ml, "nLoadAssembly[assemblyNameOrPath_String, suppressErrors_]", "{assemblyNameOrPath, suppressErrors}", LOADASSEMBLY); definePattern(ml, "nLoadAssemblyFromDir[assemblyName_String, dir_String]", "{assemblyName, dir}", LOADASSEMBLYFROMDIR); definePattern(ml, "nGetAssemblyObject[asmName_String]", "{asmName}", GETASSEMBLYOBJ); definePattern(ml, "nGetTypeObject[aqTypeName_String]", "{aqTypeName}", GETTYPEOBJ); definePattern(ml, "nReleaseObject[instances:{__Symbol}]", "{instances}", RELEASEOBJECT); definePattern(ml, "nMakeObject[typeName_String, argType_Integer, val_]", "{typeName, argType, val}", MAKEOBJECT); definePattern(ml, "nVal[obj_?NETObjectQ]", "{obj}", VAL); definePattern(ml, "nReflectType[typeName_String]", "{typeName}", REFLECTTYPE); definePattern(ml, "nReflectAsm[asmName_String]", "{asmName}", REFLECTASM); definePattern(ml, "nSetComplex[typeName_String]", "{typeName}", SETCOMPLEX); definePattern(ml, "nSameQ[obj1_?NETObjectQ, obj2_?NETObjectQ]", "{obj1, obj2}", SAMEQ); definePattern(ml, "nInstanceOf[obj_?NETObjectQ, aqTypeName_String]", "{obj, aqTypeName}", INSTANCEOF); definePattern(ml, "nCast[obj_?NETObjectQ, aqTypeName_String]", "{obj, aqTypeName}", CAST); definePattern(ml, "nPeekTypes[]", "{}", PEEKTYPES); definePattern(ml, "nPeekObjects[]", "{}", PEEKOBJECTS); definePattern(ml, "nPeekAssemblies[]", "{}", PEEKASSEMBLIES); definePattern(ml, "nCreateDelegate[typeName_String, mFunc_String, sendTheseArgs_Integer, callsUnshare:(True | False), wrapInNETBlock:(True | False)]", "{typeName, mFunc, sendTheseArgs, callsUnshare, wrapInNETBlock}", CREATEDELEGATE); definePattern(ml, "nDefineDelegate[name_String, retTypeName_String, paramTypeNames_List]", "{name, retTypeName, paramTypeNames}", DEFINEDELEGATE); definePattern(ml, "nDlgTypeName[eventObject_?NETObjectQ, aqTypeName_String, evtName_String]", "{eventObject, aqTypeName, evtName}", DLGTYPENAME); definePattern(ml, "nAddHandler[eventObject_?NETObjectQ, aqTypeName_String, evtName_String, delegate_?NETObjectQ]", "{eventObject, aqTypeName, evtName, delegate}", ADDHANDLER); definePattern(ml, "nRemoveHandler[eventObject_?NETObjectQ, aqTypeName_String, evtName_String, delegate_?NETObjectQ]", "{eventObject, aqTypeName, evtName, delegate}", REMOVEHANDLER); definePattern(ml, "nCreateDLL1[funcName_String, dllName_String, callConv_String, retTypeName_String, argTypeNames_, areOutParams_, strFormat_String]", "{funcName, dllName, callConv, retTypeName, argTypeNames, areOutParams, strFormat}", CREATEDLL1); definePattern(ml, "nCreateDLL2[decl_String, refAsms_, lang_String]", "{decl, refAsms, lang}", CREATEDLL2); definePattern(ml, "nModal[modal:(True | False), formToActivate_?NETObjectQ]", "{modal, formToActivate}", MODAL); definePattern(ml, "nShow[formToActivate_?NETObjectQ]", "{formToActivate}", SHOW); definePattern(ml, "nShareKernel[sharing:(True | False)]", "{sharing}", SHAREKERNEL); definePattern(ml, "nAllowUIComputations[allow:(True | False)]", "{allow}", ALLOWUICOMPS); definePattern(ml, "nUILink[name_String, prot_String]", "{name, prot}", UILINK); definePattern(ml, "nIsCOMProp[obj_?NETObjectQ, memberName_String]", "{obj, memberName}", ISCOMPROP); definePattern(ml, "nCreateCOM[clsIDOrProgID_String]", "{clsIDOrProgID}", CREATECOM); definePattern(ml, "nGetActiveCOM[clsIDOrProgID_String]", "{clsIDOrProgID}", GETACTIVECOM); definePattern(ml, "nReleaseCOM[obj_?NETObjectQ]", "{obj}", RELEASECOM); definePattern(ml, "nLoadTypeLibrary[tlbPath_String, safeArrayAsArray_, assemblyFile_String]", "{tlbPath, safeArrayAsArray, assemblyFile}", LOADTYPELIBRARY); definePattern(ml, "nGetException[]", "{}", GETEXCEPTION); definePattern(ml, "nConnectToFEServer[linkName_String]", "{linkName}", CONNECTTOFE); definePattern(ml, "nDisconnectToFEServer[]", "{}", DISCONNECTTOFE); // For speed testing: definePattern(ml, "noop[]", "{}", NOOP); definePattern(ml, "noop2[argc_Integer, args___]", "{argc, args}", NOOP2); // Here we define the argTypeToInteger function by sending // MapThread[(argTypeToInteger[#1] = #2)&, {ToExpression /@ argTypePatterns, {... ARGTYPE_ constants ...}}] ml.PutFunction("MapThread", 2); ml.PutFunction("Function", 1); ml.PutFunction("Set", 2); ml.PutFunction("argTypeToInteger", 1); ml.PutFunction("Slot", 1); ml.Put(1); ml.PutFunction("Slot", 1); ml.Put(2); ml.PutFunction("List", 2); ml.PutFunction("Map", 2); ml.PutSymbol("ToExpression"); ml.Put(argTypePatterns); ml.PutFunction("List", 13); ml.Put(ARGTYPE_INTEGER); ml.Put(ARGTYPE_REAL); ml.Put(ARGTYPE_STRING); ml.Put(ARGTYPE_BOOLEAN); ml.Put(ARGTYPE_NULL); ml.Put(ARGTYPE_MISSING); ml.Put(ARGTYPE_OBJECTREF); ml.Put(ARGTYPE_VECTOR); ml.Put(ARGTYPE_MATRIX); ml.Put(ARGTYPE_TENSOR3); ml.Put(ARGTYPE_LIST); ml.Put(ARGTYPE_COMPLEX); ml.Put(ARGTYPE_OTHER); ml.Put("End[]"); ml.PutSymbol("End"); ml.Flush(); return(true); } catch (MathLinkException) { return(false); } }
public virtual void HandlePacket(PacketType pkt) { switch (pkt) { // If you ever change the default behavior on the 4 "answer" packets to read off the link, // you'll need to add a seekMark in NativeKernelLink.waitForAnswer... case PacketType.Return: case PacketType.InputName: case PacketType.ReturnText: case PacketType.ReturnExpression: case PacketType.Menu: case PacketType.Message: break; // From here on, the cases do actual work. case PacketType.Call: { ExpressionType type = GetExpressionType(); if (type == ExpressionType.Integer) { // A normal CallPacket representing a call to .NET via nCall. callPktHandler.handleCallPacket(this); } else { // A CallPacket destined for the FE via MathLink`CallFrontEnd[] and routed through // .NET due to ShareFrontEnd[]. This would only be in a 5.1 FE, as earlier // versions do not use CallPacket and later versions would use the FE's Service Link. IMathLink feServerLink = callPktHandler.FEServerLink; if (feServerLink != null) { feServerLink.PutFunction("CallPacket", 1); feServerLink.TransferExpression(this); // FE will always reply to a CallPacket. Note that it is technically possible for // the FE to send back an EvaluatePacket, which means that we really need to run a // little loop here, not just write the result back to the kernel. But this branch // is only for a 5.1 FE, and I don't think that the 5.1 FE ever does that. TransferExpression(feServerLink); } } break; } case PacketType.Display: case PacketType.DisplayEnd: { IMathLink feServerLink = callPktHandler.FEServerLink; if (feServerLink != null) { if (accumulatingPS == null) { accumulatingPS = new System.Text.StringBuilder(34000); // 34K is large enough to hold an entire packet } accumulatingPS.Append(GetString()); if (pkt == PacketType.DisplayEnd) { // XXXPacket[stuff] ---> Cell[GraphicsData["PostScript", stuff], "Graphics"] feServerLink.PutFunction("FrontEnd`FrontEndExecute", 1); feServerLink.PutFunction("FrontEnd`NotebookWrite", 2); feServerLink.PutFunction("FrontEnd`SelectedNotebook", 0); feServerLink.PutFunction("Cell", 2); feServerLink.PutFunction("GraphicsData", 2); feServerLink.Put("PostScript"); feServerLink.Put(accumulatingPS.ToString()); feServerLink.Put("Graphics"); feServerLink.Flush(); accumulatingPS = null; } } else { Debug.WriteLine("Got PacketType.Display in handlePacket, but no FE link"); } break; } case PacketType.Input: case PacketType.InputString: { IMathLink feServerLink = callPktHandler.FEServerLink; if (feServerLink != null) { feServerLink.PutFunction(pkt == PacketType.InputString ? "InputStringPacket" : "InputPacket", 1); feServerLink.Put(GetString()); feServerLink.Flush(); NewPacket(); Put(feServerLink.GetString()); Flush(); } break; } case PacketType.Text: case PacketType.Expression: { // Print output, or message text. IMathLink feServerLink = callPktHandler.FEServerLink; if (feServerLink != null) { // XXXPacket[stuff] ---> Cell[stuff, "Print"] feServerLink.PutFunction("FrontEnd`FrontEndExecute", 1); feServerLink.PutFunction("FrontEnd`NotebookWrite", 2); feServerLink.PutFunction("FrontEnd`SelectedNotebook", 0); feServerLink.PutFunction("Cell", 2); feServerLink.TransferExpression(this); feServerLink.Put((lastPktWasMsg) ? "Message" : "Print"); feServerLink.Flush(); } else { // For one type of PacketType.Expression, no part of it has been read yet. Thus we must "open" the // packet so that later calls to newPacket() throw it away. if (pkt == PacketType.Expression) { int ignore; GetFunction(out ignore); } } break; } case PacketType.FrontEnd: { // This case is different from the others. At the point of entry, the link is at the point // _before_ the "packet" has been read. As a result, we must at least open the packet. // Note that PacketType.FrontEnd is really just a fall-through for unrecognized packets. We don't have any // checks that it is truly intended for the FE. IMathLink feServerLink = callPktHandler.FEServerLink; if (feServerLink != null) { ILinkMark mark = CreateMark(); try { // Wrap FrontEndExecute around it if not already there. int ignore; string wrapper = GetFunction(out ignore); if (wrapper != "FrontEnd`FrontEndExecute") { feServerLink.PutFunction("FrontEnd`FrontEndExecute", 1); } } finally { SeekMark(mark); DestroyMark(mark); } feServerLink.TransferExpression(this); feServerLink.Flush(); // Wait until either the fe is ready (because what we just sent causes a return value) // or kernel is ready (the computation is continuing because the kernel is not waiting // for a return value). do { System.Threading.Thread.Sleep(50); } while (!feServerLink.Ready && !Ready); if (feServerLink.Ready) { // fe link has something to return to kernel from last PacketType.FrontEnd we sent it. TransferExpression(feServerLink); Flush(); } } else { // It's OK to get here. For example, this happens if you don't share the fe, but have a // button that calls NotebookCreate[]. This isn't a very good example, because that // function expects the fe to return something, so Java will hang. you will get into // trouble if you make calls on the fe that expect a return. Everything is OK for calls // that don't expect a return, though. int ignore; GetFunction(out ignore); // Must at least open the packet, so newPacket (back in caller) will get rid of it. } break; } default: break; } lastPktWasMsg = pkt == PacketType.Message; }