RE: How can I evaluate code?
William Welliver <[email protected]> Thu, 25 Apr 2019 14:35:58 -0400 (EDT)
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Message-ID | <[email protected]> |
there are 2 possible concerns:
1) is that the code supplied is not valid and won't compile properly.
You'd need to catch the exception thrown by compile string.
2) the code, when executed throws an error. you'd need to catch the
exception thrown when executing the method.
program p;
mixed compile_error = catch(p = compile_string(somestring));
if(compile_error) handle_the_error();
mixed runtime_error = catch(p->myfunc());
if(runtime_error) handle_the_error();
Have a look at the modref info for catch() and Error.Generic(). If you
need to capture details of the compilation failture, that's a bit more
involved and a different conversation altogether.
Bill
On Thu, 25 Apr 2019, MineMoreGames wrote:
>
> I?ve actually have an issue, let?s say the code evaluates but throws an error, I want to be able to return that error to the user.
>
> Is it even possible?
>
> From: H. William Welliver III
> Sent: Thursday, April 25, 2019 5:03 PM
> To: MineMoreGames; Pike User List
> Subject: Re: How can I evaluate code?
>
>
>
> You can use compile_string() to compile a string that contains a method constructed by you that contains the input you received,
> wrapped in a method. The simplest version of this might be:
>
>
>
> program p = compile_string(?mixed runIt(){ return ? + myinput + ?;}?);
>
> mixed result = p()->runIt();
>
>
>
> Since you are constructing the class around the input, and indeed the function that?s being evaluated, you can pass whatever you want
> to it.
>
>
>
> Bill
>
>
>
>
>