Skip to content

Commit c4e1509

Browse files
committed
[[ Script ]] Added plumbing for 'throw' (currently just throws generic error).
1 parent b4fcff2 commit c4e1509

4 files changed

Lines changed: 68 additions & 37 deletions

File tree

libscript/include/script.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ void MCScriptEmitFetchLocalInModule(MCScriptModuleBuilderRef builder, uindex_t d
309309
void MCScriptEmitStoreLocalInModule(MCScriptModuleBuilderRef builder, uindex_t src_reg, uindex_t local_index);
310310
void MCScriptEmitFetchGlobalInModule(MCScriptModuleBuilderRef builder, uindex_t dst_reg, uindex_t glob_index);
311311
void MCScriptEmitStoreGlobalInModule(MCScriptModuleBuilderRef builder, uindex_t src_reg, uindex_t glob_index);
312+
void MCScriptEmitThrowInModule(MCScriptModuleBuilderRef builder, uindex_t err_reg);
312313

313314
void MCScriptEmitPositionInModule(MCScriptModuleBuilderRef builder, MCNameRef file, uindex_t line);
314315

libscript/src/script-builder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,14 @@ void MCScriptEmitStoreGlobalInModule(MCScriptModuleBuilderRef self, uindex_t p_s
14051405
__emit_instruction(self, kMCScriptBytecodeOpStoreGlobal, 2, p_src_reg, p_glob_index);
14061406
}
14071407

1408+
void MCScriptEmitThrowInModule(MCScriptModuleBuilderRef self, uindex_t p_reg)
1409+
{
1410+
if (self == nil || !self -> valid)
1411+
return;
1412+
1413+
__emit_instruction(self, kMCScriptBytecodeOpThrow, 1, p_reg);
1414+
}
1415+
14081416
void MCScriptEmitPositionInModule(MCScriptModuleBuilderRef self, MCNameRef p_file, uindex_t p_line)
14091417
{
14101418
if (self == nil || !self -> valid)

libscript/src/script-instance.cpp

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ bool MCScriptThrowUnableToResolveTypeError(MCScriptModuleRef module, uindex_t ad
219219
return MCErrorThrowGeneric();
220220
}
221221

222+
bool MCScriptThrowUserError(MCScriptModuleRef module, MCValueRef value)
223+
{
224+
return MCErrorThrowGeneric();
225+
}
226+
222227
///////////
223228

224229
MCScriptVariableDefinition *MCScriptDefinitionAsVariable(MCScriptDefinition *self)
@@ -1450,36 +1455,6 @@ bool MCScriptCallHandlerOfInstanceInternal(MCScriptInstanceRef self, MCScriptHan
14501455
MCScriptStoreToRegisterInFrame(t_frame, t_dst, t_value);
14511456
}
14521457
break;
1453-
case kMCScriptBytecodeOpAssignList:
1454-
{
1455-
int t_dst;
1456-
t_dst = t_arguments[0];
1457-
1458-
MCValueRef *t_values;
1459-
if (!MCMemoryNewArray(t_arity - 1, t_values))
1460-
t_success = false;
1461-
1462-
if (t_success)
1463-
{
1464-
for(uindex_t i = 1; i < t_arity; i++)
1465-
t_values[i - 1] = MCScriptFetchFromRegisterInFrame(t_frame, t_arguments[i]);
1466-
}
1467-
1468-
MCProperListRef t_list;
1469-
if (t_success)
1470-
{
1471-
t_success = MCProperListCreateAndRelease(t_values, t_arity - 1, t_list);
1472-
if (!t_success)
1473-
free(t_values);
1474-
}
1475-
1476-
if (t_success)
1477-
{
1478-
MCScriptStoreToRegisterInFrame(t_frame, t_dst, t_list);
1479-
MCValueRelease(t_list);
1480-
}
1481-
}
1482-
break;
14831458
case kMCScriptBytecodeOpAssign:
14841459
{
14851460
// assign <dst>, <src>
@@ -1790,6 +1765,47 @@ bool MCScriptCallHandlerOfInstanceInternal(MCScriptInstanceRef self, MCScriptHan
17901765
}
17911766
}
17921767
break;
1768+
case kMCScriptBytecodeOpAssignList:
1769+
{
1770+
int t_dst;
1771+
t_dst = t_arguments[0];
1772+
1773+
MCValueRef *t_values;
1774+
if (!MCMemoryNewArray(t_arity - 1, t_values))
1775+
t_success = false;
1776+
1777+
if (t_success)
1778+
{
1779+
for(uindex_t i = 1; i < t_arity; i++)
1780+
t_values[i - 1] = MCScriptFetchFromRegisterInFrame(t_frame, t_arguments[i]);
1781+
}
1782+
1783+
MCProperListRef t_list;
1784+
if (t_success)
1785+
{
1786+
t_success = MCProperListCreateAndRelease(t_values, t_arity - 1, t_list);
1787+
if (!t_success)
1788+
free(t_values);
1789+
}
1790+
1791+
if (t_success)
1792+
{
1793+
MCScriptStoreToRegisterInFrame(t_frame, t_dst, t_list);
1794+
MCValueRelease(t_list);
1795+
}
1796+
}
1797+
break;
1798+
case kMCScriptBytecodeOpThrow:
1799+
{
1800+
int t_err;
1801+
t_err = t_arguments[0];
1802+
1803+
MCValueRef t_value;
1804+
t_value = MCScriptFetchFromLocalInFrame(t_frame, t_err);
1805+
1806+
t_success = MCScriptThrowUserError(t_frame -> instance -> module, t_value);
1807+
}
1808+
break;
17931809
}
17941810

17951811
if (!t_success)

libscript/src/script-private.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,6 @@ enum MCScriptBytecodeOp
418418
// freed, and the constant value at the specified index is assigned to it.
419419
kMCScriptBytecodeOpAssignConstant,
420420

421-
// List creation assignment.
422-
// assign-list <dst>, <arg_1>, ..., <arg_n>
423-
// Dst is a register. The remaining arguments are registers and are used to
424-
// build a list. (This can be replaced by an invoke when variadic bindings are
425-
// implemented).
426-
kMCScriptBytecodeOpAssignList,
427-
428421
// Register assignment:
429422
// assign <dst>, <src>
430423
// Dst and Src are registers. The value in dst is freed, and src copied
@@ -496,6 +489,19 @@ enum MCScriptBytecodeOp
496489
// to the type of the target global variable.
497490
//
498491
kMCScriptBytecodeOpStoreGlobal,
492+
493+
// List creation assignment.
494+
// assign-list <dst>, <arg_1>, ..., <arg_n>
495+
// Dst is a register. The remaining arguments are registers and are used to
496+
// build a list. (This will be replaced by an invoke when variadic bindings are
497+
// implemented).
498+
kMCScriptBytecodeOpAssignList,
499+
// Error throwing
500+
// throw <reg>
501+
// Reg is a register containing the value to throw. (This opcode will be replaced
502+
// by an invoke when automatic exception handling is added to foreign handler
503+
// declarations).
504+
kMCScriptBytecodeOpThrow,
499505
};
500506

501507
bool MCScriptBytecodeIterate(byte_t*& x_bytecode, byte_t *p_bytecode_limit, MCScriptBytecodeOp& r_op, uindex_t& r_arity, uindex_t *r_arguments);

0 commit comments

Comments
 (0)