changeset 22454:8445f67a8123

Add function mexEvalStringWithTrap to MEX API. * NEWS: Announce function. * mex.cc (mexEvalStringWithTrap): New function. * mex.cc (mexCallMATLABWithTrap): Move declaration of field_names close to usage. Move execution_error declaration to be near parse_status. * mexproto.h (mexEvalStringWithTrap): Prototype for new function.
author Rik <rik@octave.org>
date Thu, 08 Sep 2016 18:50:43 -0700
parents ca01dc025e2c
children 8924139b4f21
files NEWS libinterp/corefcn/mex.cc libinterp/corefcn/mexproto.h
diffstat 3 files changed, 42 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Thu Sep 08 18:45:55 2016 -0700
+++ b/NEWS	Thu Sep 08 18:50:43 2016 -0700
@@ -142,6 +142,8 @@
     instead will return execution to the MEX function for error
     handling.
 
+ ** Other new MEX API functions include mexEvalStringWithTrap.
+
  ** Other new functions added in 4.2:
 
       audioformats
--- a/libinterp/corefcn/mex.cc	Thu Sep 08 18:45:55 2016 -0700
+++ b/libinterp/corefcn/mex.cc	Thu Sep 08 18:50:43 2016 -0700
@@ -3117,13 +3117,13 @@
                        mxArray *argin[], const char *fname)
 {
   mxArray *mx = NULL;
-  const char *fields[] = {"identifier", "message", "case", "stack"};
 
   int old_flag = mex_context ? mex_context->trap_feval_error : 0;
   mexSetTrapFlag (1);
   if (mexCallMATLAB (nargout, argout, nargin, argin, fname))
     {
-      mx = mxCreateStructMatrix (1, 1, 4, fields);
+      const char *field_names[] = {"identifier", "message", "case", "stack"};
+      mx = mxCreateStructMatrix (1, 1, 4, field_names);
       mxSetFieldByNumber (mx, 0, 0, mxCreateString ("Octave:MEX"));
       std::string msg = "mexCallMATLABWithTrap: function call <"
                         + std::string (fname) + "> failed";
@@ -3149,11 +3149,10 @@
   int retval = 0;
 
   int parse_status;
+  bool execution_error = false;
 
   octave_value_list ret;
 
-  bool execution_error = false;
-
   try
     {
       ret = eval_string (s, false, parse_status, 0);
@@ -3171,6 +3170,42 @@
   return retval;
 }
 
+mxArray *
+mexEvalStringWithTrap (const char *s)
+{
+  mxArray *mx = NULL;
+
+  int parse_status;
+  bool execution_error = false;
+
+  octave_value_list ret;
+
+  try
+    {
+      ret = eval_string (s, false, parse_status, 0);
+    }
+  catch (const octave::execution_exception&)
+    {
+      recover_from_exception ();
+
+      execution_error = true;
+    }
+
+  if (parse_status || execution_error)
+    {
+      const char *field_names[] = {"identifier", "message", "case", "stack"};
+      mx = mxCreateStructMatrix (1, 1, 4, field_names);
+      mxSetFieldByNumber (mx, 0, 0, mxCreateString ("Octave:MEX"));
+      std::string msg = "mexEvalStringWithTrap: eval of <"
+                        + std::string (s) + "> failed";
+      mxSetFieldByNumber (mx, 0, 1, mxCreateString (msg.c_str ()));
+      mxSetFieldByNumber (mx, 0, 2, mxCreateCellMatrix (0, 0));
+      mxSetFieldByNumber (mx, 0, 3, mxCreateStructMatrix (0, 1, 0, NULL));
+    }
+
+  return mx;
+}
+
 void
 mexErrMsgTxt (const char *s)
 {
--- a/libinterp/corefcn/mexproto.h	Thu Sep 08 18:45:55 2016 -0700
+++ b/libinterp/corefcn/mexproto.h	Thu Sep 08 18:50:43 2016 -0700
@@ -88,6 +88,7 @@
 
 extern OCTINTERP_API void mexSetTrapFlag (int flag);
 extern OCTINTERP_API int mexEvalString (const char *s);
+extern OCTINTERP_API mxArray * mexEvalStringWithTrap (const char *s);
 extern OCTINTERP_API void mexErrMsgTxt (const char *s);
 extern OCTINTERP_API void mexErrMsgIdAndTxt (const char *id, const char *s,
                                              ...);