| 1507 | } |
| 1508 | |
| 1509 | ResultType Object::Construct(ResultToken &aResultToken, ExprTokenType *aParam[], int aParamCount) |
| 1510 | { |
| 1511 | ExprTokenType this_token(this); |
| 1512 | ResultType result; |
| 1513 | Line *curr_line = g_script.mCurrLine; |
| 1514 | |
| 1515 | // __Init was added so that instance variables can be initialized in the correct order |
| 1516 | // (beginning at the root class and ending at class_object) before __New is called. |
| 1517 | // It shouldn't be explicitly defined by the user, but auto-generated in DefineClassVars(). |
| 1518 | result = CallMeta(_T("__Init"), aResultToken, this_token, nullptr, 0); |
| 1519 | if (result != INVOKE_NOT_HANDLED) |
| 1520 | { |
| 1521 | // It's possible that __Init is user-defined (despite recommendations in the |
| 1522 | // documentation) or built-in, so make sure the return value, if any, is freed: |
| 1523 | aResultToken.Free(); |
| 1524 | // Reset to defaults for __New, invoked below. |
| 1525 | aResultToken.InitResult(aResultToken.buf); |
| 1526 | if (result == FAIL || result == EARLY_EXIT) // Checked only after Free() and InitResult() as caller might expect mem_to_free == NULL. |
| 1527 | { |
| 1528 | Release(); |
| 1529 | return aResultToken.SetExitResult(result); // SetExitResult is necessary because result was reset by InitResult. |
| 1530 | } |
| 1531 | g_script.mCurrLine = curr_line; // Prevent misleading error reports in __New or for our caller. |
| 1532 | } |
| 1533 | |
| 1534 | // __New may be defined by the script for custom initialization code. |
| 1535 | result = CallMeta(_T("__New"), aResultToken, this_token, aParam, aParamCount); |
| 1536 | aResultToken.Free(); |
| 1537 | if (result == INVOKE_NOT_HANDLED && aParamCount) |
| 1538 | { |
| 1539 | // Maybe the caller expects the parameters to be used in some way, but they won't |
| 1540 | // since there's no __New. Treat it the same as having __New without parameters. |
| 1541 | result = aResultToken.Error(ERR_TOO_MANY_PARAMS); |
| 1542 | } |
| 1543 | if (result == FAIL || result == EARLY_EXIT) |
| 1544 | { |
| 1545 | // An error was raised within __New() or while trying to call it, or Exit was called. |
| 1546 | Release(); |
| 1547 | return result; |
| 1548 | } |
| 1549 | g_script.mCurrLine = curr_line; // Prevent misleading error reports for our caller. |
| 1550 | |
| 1551 | aResultToken.SetValue(this); // No AddRef() since Object::New() would need to Release(). |
| 1552 | return aResultToken.SetResult(OK); |
| 1553 | } |
| 1554 | |
| 1555 | BIF_DECL(Any___Init) |
| 1556 | { |
no test coverage detected