Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

[[ Perf ]] General features and improvements aimed at optimization #6671

Open
wants to merge 19 commits into
base: develop
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[[ Feature ]] Implement 'try' function
This patch implements a new function 'try'. The try function
takes one or two parameters. The first parameter is the
expression to try and evaluate, and the second (optional)
parameter is the failure expression.

The first expression is executed and if an error is thrown
during evaluation, the second expression (if specified) is
evaluated. If there is no second expression then empty is
returned, and no error propagates.

The second expression may contain the literal 'error',
which will evaluate to the error which was thrown during
evaluation of the first expression.

If evaluating the second expression throws an error, then
that error is propagated.
  • Loading branch information
runrevmark committed Nov 9, 2018
commit 24c92de2c3262ff40957a0f627f348ee436e4e2b
3 changes: 3 additions & 0 deletions 3 engine/src/executionerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -2791,6 +2791,9 @@ enum Exec_errors

// {EE-0914} literal: error in value expression
EE_ARRLITERAL_BADVALEXPR,

// {EE-0915} try: error in error expression
EE_TRYFUNC_ERRINERR,
};

extern const char *MCexecutionerrors;
Expand Down
60 changes: 60 additions & 0 deletions 60 engine/src/funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,66 @@ void MCTopStack::eval_ctxt(MCExecContext &ctxt, MCExecValue &r_value)
}
}

Parse_stat MCTryFunc::parse(MCScriptPoint& sp, Boolean the)
{
initpoint(sp);
if (sp.skip_token(SP_FACTOR, TT_LPAREN) != PS_NORMAL)
{
MCperror->add(PE_FACTOR_NOLPAREN, sp);
return PS_ERROR;
}
if (sp.parseexp(False, False, &(&expression)) != PS_NORMAL)
{
delete *expression;
MCperror->add(PE_FACTOR_BADPARAM, sp);
return PS_ERROR;
}

Symbol_type type;
if (sp.next(type) == PS_NORMAL && type == ST_SEP)
{
bool t_old_alias_error = sp.alias_error(true);
if (sp.parseexp(False, False, &(&on_error)) != PS_NORMAL)
{
delete *on_error;
sp.alias_error(t_old_alias_error);
MCperror->add(PE_FACTOR_BADPARAM, sp);
return PS_ERROR;
}
sp.alias_error(t_old_alias_error);
}

if (sp.skip_token(SP_FACTOR, TT_RPAREN) != PS_NORMAL)
{
MCperror->add(PE_FACTOR_NORPAREN, sp);
return PS_ERROR;
}

return PS_NORMAL;
}

void MCTryFunc::eval_ctxt(MCExecContext& ctxt, MCExecValue& r_value)
{
expression->eval_ctxt(ctxt, r_value);
if (ctxt.HasError())
{
ctxt.IgnoreLastError();

if (*on_error != nullptr)
{
MCAutoValueRef t_old_error = MClasterror->getvalueref();
MCeerror->givetovariable(MClasterror);
ctxt.EvaluateExpression(*on_error, EE_TRYFUNC_ERRINERR, r_value);
MClasterror->setvalueref(*t_old_error);
}
else
{
r_value.type = kMCExecValueTypeNone;
r_value.valueref_value = nullptr;
}
}
}

MCUniDecode::~MCUniDecode()
{
delete source;
Expand Down
9 changes: 9 additions & 0 deletions 9 engine/src/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,15 @@ class MCTrunc : public MCUnaryFunctionCtxt<double, double, MCMathEvalTrunc, EE_T
virtual ~MCTrunc(){}
};

class MCTryFunc : public MCFunction
{
MCAutoPointer<MCExpression> expression;
MCAutoPointer<MCExpression> on_error;
public:
virtual Parse_stat parse(MCScriptPoint &, Boolean the);
virtual void eval_ctxt(MCExecContext &, MCExecValue &);
};

// MDW-2014-08-23 : [[ feature_floor ]]
class MCFloor : public MCUnaryFunctionCtxt<double, double, MCMathEvalFloor, EE_FLOOR_BADSOURCE, PE_FLOOR_BADPARAM>
{
Expand Down
3 changes: 3 additions & 0 deletions 3 engine/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ MCRectangle MCcur_effects_rect;
MCEffectList *MCcur_effects;
MCError *MCperror;
MCError *MCeerror;
MCVariable *MClasterror;
MCVariable *MCglobals;
MCVariable *MCmb;
MCVariable *MCeach;
Expand Down Expand Up @@ -1063,6 +1064,7 @@ bool X_open(int argc, MCStringRef argv[], MCStringRef envp[])
{
MCperror = new (nothrow) MCError();
MCeerror = new (nothrow) MCError();
/* UNCHECKED */ MCVariable::create(MClasterror);
/* UNCHECKED */ MCVariable::createwithname(MCNAME("MCresult"), MCresult);
MCresultmode = kMCExecResultModeReturn;

Expand Down Expand Up @@ -1414,6 +1416,7 @@ int X_close(void)

delete MCperror;
delete MCeerror;
delete MClasterror;

MCclipboard->Release();
MCselection->Release();
Expand Down
1 change: 1 addition & 0 deletions 1 engine/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ extern MCRectangle MCcur_effects_rect;
extern MCEffectList *MCcur_effects;
extern MCError *MCperror;
extern MCError *MCeerror;
extern MCVariable *MClasterror;
extern MCVariable *MCmb;
extern MCVariable *MCeach;
extern MCVariable *MCresult;
Expand Down
1 change: 1 addition & 0 deletions 1 engine/src/lextable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ const LT factor_table[] =
{"truewordoffset", TT_FUNCTION, F_TRUEWORD_OFFSET},
{"truewords", TT_CLASS, CT_TRUEWORD},
{"trunc", TT_FUNCTION, F_TRUNC},
{"try", TT_FUNCTION, F_TRY},
{"twelvehourtime", TT_PROPERTY, P_TWELVE_TIME},
{"typingrate", TT_PROPERTY, P_TYPE_RATE},
{"umask", TT_PROPERTY, P_UMASK},
Expand Down
7 changes: 7 additions & 0 deletions 7 engine/src/mcerror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */

#include "mcerror.h"
#include "util.h"
#include "variable.h"

#include "globals.h"

Expand Down Expand Up @@ -119,6 +120,12 @@ bool MCError::copyasstringref(MCStringRef &r_string)
return MCStringCopy(*buffer, r_string);
}

void MCError::givetovariable(MCVariable* p_var)
{
p_var->setvalueref(*buffer);
clear();
}

void MCError::clear()
{
errorline = errorpos = 0;
Expand Down
1 change: 1 addition & 0 deletions 1 engine/src/mcerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class MCError
void append(MCError& string);
void copystringref(MCStringRef s, Boolean t);
bool copyasstringref(MCStringRef &r_string);
void givetovariable(MCVariable* p_var);
bool isthrown(void) const {return thrown;}
void clear();
Boolean isempty()
Expand Down
2 changes: 2 additions & 0 deletions 2 engine/src/newobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,8 @@ MCExpression *MCN_new_function(int2 which)
return new MCTrueWordOffset;
case F_TRUNC:
return new MCTrunc;
case F_TRY:
return new MCTryFunc;
case F_UNICODE_CHAR_TO_NUM:
return new MCUnicodeCharToNum;
// MDW-2014-08-23 : [[ feature_floor ]]
Expand Down
2 changes: 2 additions & 0 deletions 2 engine/src/parsedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ enum Functions {
F_EVENT_CONTROL_KEY,
F_EVENT_OPTION_KEY,
F_EVENT_SHIFT_KEY,

F_TRY,
};

/* The HT_MIN and HT_MAX elements of the enum delimit the range of the handler
Expand Down
3 changes: 3 additions & 0 deletions 3 engine/src/parseerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,9 @@ enum Parse_errors

// {PE-0593} literal: array keys must be string or integer literals
PE_ARRLITERAL_BADKEY,

// {PE-0594} try: bad parameters
PE_TRYFUNC_BADPARAM,
};

extern const char *MCparsingerrors;
Expand Down
6 changes: 6 additions & 0 deletions 6 engine/src/scriptpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,12 @@ Parse_stat MCScriptPoint::parseexp(Boolean single, Boolean items,

Parse_stat MCScriptPoint::findvar(MCNameRef p_name, MCVarref** r_var)
{
if (m_alias_error && p_name == MCN_error)
{
*r_var = new(nothrow) MCVarref(MClasterror);
return PS_NORMAL;
}

if (curhandler != NULL)
return curhandler -> findvar(p_name, r_var);

Expand Down
9 changes: 9 additions & 0 deletions 9 engine/src/scriptpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class MCScriptPoint
Boolean in_tag;
// MW-2011-06-23: This is true if we backed up over a tag change point.
Boolean was_in_tag;

bool m_alias_error : 1;

public:
MCScriptPoint(MCScriptPoint &sp);
Expand All @@ -87,6 +89,13 @@ class MCScriptPoint
tagged = which;
}

bool alias_error(bool p_alias)
{
bool t_old = m_alias_error;
m_alias_error = p_alias;
return t_old;
}

uint2 getline()
{
return line;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.