-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal error / exception handling #10
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b598dfc
realm -> context
mikee47 9dd201e
Catch fatal errors with exceptions
mikee47 07b4e48
Modify Event_Jsvm to demonstrate exception handling
mikee47 b5f3bf9
Rename `context` -> `object`
mikee47 407c04e
Fix README
mikee47 ffe28a8
COMPONENT_VARS -> COMPONENT_RELINK_VARS
mikee47 f092264
Show alert/print in red/aqua
mikee47 8516e39
Fix doxygen comments
mikee47 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* ErrorHandler.cpp | ||
* | ||
* @author: Dec 2021 - Mikee47 <mike@sillyhouse.net> | ||
* | ||
*/ | ||
|
||
#include "include/Jerryscript/VirtualMachine.h" | ||
#include "include/Jerryscript/Except.h" | ||
#include <debug_progmem.h> | ||
#include <csetjmp> | ||
|
||
String toString(Jerryscript::FatalCode code) | ||
{ | ||
switch(code) { | ||
#define XX(jt, t) \ | ||
case Jerryscript::FatalCode::t: \ | ||
return F(#t); | ||
JERRY_FATAL_CODE_MAP(XX) | ||
#undef XX | ||
default: | ||
return String(unsigned(code)); | ||
} | ||
} | ||
|
||
namespace Jerryscript | ||
{ | ||
Except* Except::current; | ||
|
||
void Except::raise(jerry_fatal_code_t code) | ||
{ | ||
if(current == nullptr) { | ||
debug_e("[JS] Fatal Error %u: calling abort()", code); | ||
abort(); | ||
} | ||
current->mCode = FatalCode(code); | ||
longjmp(current->context, 1); | ||
} | ||
|
||
Except::operator String() const | ||
{ | ||
String s = F("Exception #"); | ||
s += unsigned(mCode); | ||
s += F(", "); | ||
s += toString(mCode); | ||
return s; | ||
} | ||
|
||
} // namespace Jerryscript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* Except.h | ||
* | ||
* @author: Dec 2021 - Mikee47 <mike@sillyhouse.net> | ||
* | ||
*/ | ||
|
||
#include "include/Jerryscript/Types.h" | ||
#include <csetjmp> | ||
|
||
namespace Jerryscript | ||
{ | ||
enum class FatalCode { | ||
#define XX(jt, t) t = jt, | ||
JERRY_FATAL_CODE_MAP(XX) | ||
#undef XX | ||
}; | ||
|
||
class Except | ||
{ | ||
public: | ||
Except() : prev(current) | ||
{ | ||
current = this; | ||
} | ||
|
||
~Except() | ||
{ | ||
current = prev; | ||
} | ||
|
||
[[noreturn]] static void raise(jerry_fatal_code_t code); | ||
|
||
FatalCode code() | ||
{ | ||
return mCode; | ||
} | ||
|
||
operator String() const; | ||
|
||
std::jmp_buf context; | ||
|
||
private: | ||
static Except* current; | ||
Except* prev; | ||
FatalCode mCode{}; | ||
}; | ||
|
||
#define JS_TRY() \ | ||
::Jerryscript::Except e; \ | ||
if(setjmp(e.context) == 0) | ||
#define JS_CATCH() else | ||
|
||
} // namespace Jerryscript | ||
|
||
String toString(Jerryscript::FatalCode code); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikee47 I am wondering isn't it possible to use try-catch directly in the JavaScript code and catch out-of-memory exception? We could expose a
reset()
JavaScript function that resets the VM and give the JS developers (end user) better control. On the C/C++ side we could check if the VM is in error state and restart the VM automatically if it stays in this error state for 8 seconds.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately fatal errors are not the same as javascript exceptions and cannot be caught in script. The engine is essentially halted. No attempt to even tidy up intermediate allocations so cleanup won't work. Try running the tests with
JERRY_ENABLE_DEBUG=1
and see what happens!A
reset()
function would be very easy to implement. However, depending on the use-case if we wanted multiple clients runninng together would we really want one of them to be able to pull the rug out from the others? Maybe, but that'll depend on usage.