コード例 #1
0
ファイル: ExposedFunctions.cs プロジェクト: sdpython/csharpy
 public static unsafe int CaptureStdOutput(CompiledFunctionDelegate fct, DataStructure *data)
 {
     if (data->printf_fct == null)
     {
         return(fct(data));
     }
     else
     {
         Printf cprint = MarshalDelegate <Printf>(data->printf_fct);
         int    res;
         var    keep = data->printf_fct;
         data->printf_fct = null;
         var std = new StdCapture();
         {
             res = fct(data);
             string sout = std.StdOut;
             if (!string.IsNullOrEmpty(sout))
             {
                 CPrintf(cprint, sout);
             }
             string err = std.StdErr;
             if (!string.IsNullOrEmpty(err))
             {
                 CPrintf(cprint, string.Format("--ERR--\n{0}", err));
             }
         }
         std.Dispose();
         data->printf_fct = keep;
         return(res);
     }
 }
コード例 #2
0
        public static unsafe int ObjectIncref(DataStructure *data)
        {
            Int64 oid = *(Int64 *)data->inputs;

            ObjectStorage.Inst.Incref(oid);
            return(0);
        }
コード例 #3
0
        public static unsafe int CallArrayDoubleArrayDouble(DataStructure *data)
        {
            if (data->printf_fct != null)
            {
                return(CaptureStdOutput(CallArrayDoubleArrayDouble, data));
            }

            CallArrayDoubleArrayDoubleIO *input = (CallArrayDoubleArrayDoubleIO *)data->inputs;
            var     vec = new double[input->nb];
            double *src = (double *)input->p;

            for (int i = 0; i < vec.Length; ++i)
            {
                vec[i] = src[i];
            }

            Int64 *p_fct     = (Int64 *)data->exc;
            var    fctref    = ObjectStorage.Inst.Get(*p_fct);
            var    fct       = fctref.MethodInfo;
            var    cs_output = fct.Invoke(null, new object[] { vec }) as double[];
            CallArrayDoubleArrayDoubleIO *output = (CallArrayDoubleArrayDoubleIO *)data->outputs;

            output->nb = cs_output.Length;
            if (cs_output.Length > 0)
            {
                NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);
                int size = cs_output.Length * sizeof(double);
                allocate(size, out output->p);
                Marshal.Copy(cs_output, 0, (IntPtr)output->p, cs_output.Length);
            }
            return(0);
        }
コード例 #4
0
ファイル: ExposedFunctions.cs プロジェクト: sdpython/csharpy
        public static unsafe int RandomString(DataStructure *data)
        {
            string           text     = "Français";
            var              raw      = StringToNullTerminatedBytesUTF8(text);
            NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);

            allocate(raw.Length, out data->outputs);
            data->exc = null;
            Marshal.Copy(raw, 0, (IntPtr)data->outputs, raw.Length);
            return(0);
        }
コード例 #5
0
ファイル: ExposedFunctions.cs プロジェクト: sdpython/csharpy
        public static unsafe int CsUpper(DataStructure *data)
        {
            string text = BytesToString((sbyte *)data->inputs);

            text = text.ToUpper();
            var raw = StringToNullTerminatedBytesUTF8(text);
            NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);

            allocate(raw.Length, out data->outputs);
            data->exc = null;
            Marshal.Copy(raw, 0, (IntPtr)data->outputs, raw.Length);
            return(0);
        }
コード例 #6
0
        public static unsafe int CallVoid(DataStructure *data)
        {
            if (data->printf_fct != null)
            {
                return(CaptureStdOutput(CallVoid, data));
            }
            Int64 *p_fct  = (Int64 *)data->exc;
            var    fctref = ObjectStorage.Inst.Get(*p_fct);
            var    fct    = fctref.MethodInfo;

            fct.Invoke(null, null);
            return(0);
        }
コード例 #7
0
        public static unsafe int CallDoubleDouble(DataStructure *data)
        {
            if (data->printf_fct != null)
            {
                return(CaptureStdOutput(CallDoubleDouble, data));
            }
            double *p_x    = (double *)data->inputs;
            Int64 * p_fct  = (Int64 *)data->exc;
            var     fctref = ObjectStorage.Inst.Get(*p_fct);
            var     fct    = fctref.MethodInfo;
            double *output = (double *)data->outputs;

            *output = (double)fct.Invoke(null, new object[] { *p_x });
            return(0);
        }
コード例 #8
0
        public static unsafe int CallArrayInt32String(DataStructure *data)
        {
            if (data->printf_fct != null)
            {
                return(CaptureStdOutput(CallArrayInt32String, data));
            }
            string content   = BytesToString((sbyte *)data->inputs);
            Int64 *p_fct     = (Int64 *)data->exc;
            var    fctref    = ObjectStorage.Inst.Get(*p_fct);
            var    fct       = fctref.MethodInfo;
            var    cs_output = fct.Invoke(null, new object[] { content }) as int[];
            CallArrayInt32StringOutput *output = (CallArrayInt32StringOutput *)data->outputs;

            output->nb = cs_output.Length;
            if (cs_output.Length > 0)
            {
                NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);
                int size = cs_output.Length * sizeof(int);
                allocate(size, out output->p);
                Marshal.Copy(cs_output, 0, (IntPtr)output->p, cs_output.Length);
            }
            return(0);
        }
コード例 #9
0
ファイル: ExposedFunctions.cs プロジェクト: sdpython/csharpy
        public static unsafe int CreateFunction(DataStructure *data)
        {
            CreateFunctionInput *inputPtr = (CreateFunctionInput *)data->inputs;
            Int64 *outputPtr = (Int64 *)data->outputs;
            string name      = BytesToString((sbyte *)inputPtr->namePointer);
            string code      = BytesToString((sbyte *)inputPtr->codePointer);
            string clrPath   = BytesToString((sbyte *)inputPtr->clrPath);

            sbyte **c_usings = (sbyte **)inputPtr->usingsPointer;
            var     usings   = new List <string>();

            while (*c_usings != null)
            {
                usings.Add(BytesToString(*c_usings));
                c_usings++;
            }

            sbyte **c_dependencies = (sbyte **)inputPtr->dependenciesPointer;
            var     dependencies   = new List <string>();

            while (*c_dependencies != null)
            {
                dependencies.Add(BytesToString(*c_dependencies));
                c_dependencies++;
            }

            string     text = "";
            MethodInfo meth;

            try
            {
                meth = DynamicFunction.CreateFunction(name, code, usings.ToArray(), dependencies.ToArray(), clrPath);
            }
            catch (Exception exc)
            {
                meth = null;
                text = exc.ToString();
                text = string.Format("Unable to compile function '{0}' due to {1}\n---CODE---\n{2}\n---USINGS---\n{3}\n---DEPENDENCIES---\n{4}\n---",
                                     name, exc.ToString(), code, string.Join("\n", usings), string.Join("\n", dependencies));
            }

            if (meth != null)
            {
                try
                {
                    text = DynamicFunction.MethodSignature(meth);
                }
                catch (Exception exc)
                {
                    meth = null;
                    text = string.Format("Unable to get the signature due to: {0}.", exc.ToString());
                }
            }
            else if (string.IsNullOrEmpty(text))
            {
                text = string.Format("Method '{0}' is null\n---CODE---\n{1}\n---USINGS---\n{2}\n---DEPENDENCIES---\n{3}\n---",
                                     name, code, string.Join("\n", usings), string.Join("\n", dependencies));
            }

            *outputPtr = meth == null ? -1 : ObjectStorage.Inst.AddIncref(meth);
            if (meth == null)
            {
                text = text.Replace("\r", "").Replace("\n\n", "\n");
            }
            var raw = StringToNullTerminatedBytesUTF8(text);
            NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);

            allocate(raw.Length, out data->exc);
            Marshal.Copy(raw, 0, (IntPtr)data->exc, raw.Length);
            return(0);
        }