David Griffin | 21 Aug 13:14

Catching errors in a embedded script at runtime

My app executes scripts at runtime, taken from files.
Each script is in essence a class definition based on an abstract
class in my Java.

So whatever is in the script, I know it should have a some key methods
defined in the abstract base class, and these are called from my java
code.

I read the script into my interpreter
   myInterpreter.execfile(scriptStream);
and this is void with no exceptions so I cannot really tell at that
point if script is OK

Then I get a reference for the class
  PyObject cls = interpreter.get(nameOfMyClass);
If cls != null then I think can be reasonably sure it found a valid
class in the interpreter namespace

I try to instantiate the class
  PyObject cls_instance = cls.__call__();

Then I convert that into a java object
 Object o = cls_instance.__tojava__(myAbstractClass.class);

If  o != Py.NoConversion  then I presumably have a usable java object.

I then cast  o   to (myAbstractClass) and I can start treating it as
an instance of myAbstractClass and call the base class methods.

This works a treat.
(Continue reading)

David Griffin | 22 Aug 22:42

Re: Catching errors in a embedded script at runtime

My app executes scripts at runtime, taken from files.
Each script is in essence a class definition based on an abstract
class in my Java.

So whatever is in the script, I know it should have a some key methods
defined in the abstract base class, and these are called from my java
code.

I read the script into my interpreter
  myInterpreter.execfile(scriptStream, myScriptsName);
and this is void with no exceptions so I cannot really tell at that
point if script is OK

Then I get a reference for the class
 PyObject cls = interpreter.get(nameOfMyClass);
If cls != null then I think can be reasonably sure it found a valid
class in the interpreter namespace

I try to instantiate the class
 PyObject cls_instance = cls.__call__();

Then I convert that into a java object
 Object o = cls_instance.__tojava__(myAbstractClass.class);

If  o != Py.NoConversion  then I presumably have a usable java object.

I then cast  o   to (myAbstractClass) and I can start treating it as
an instance of myAbstractClass and call the base class methods.

This works a treat.
(Continue reading)


Gmane