Skip to content

Commit

Permalink
Add ES9 optional catch binding (try {} catch {})
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Oct 9, 2023
1 parent f6894cd commit bed1d37
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Fix issue using `String.concat` with flash strings (fix #2268)
jsvFindChildFromString enhancement - adds 10% performance on minified code
Remove createChild argument from jsvFindChildFromString, add jsvFindOrAddChildFromString instead
Add ES9 optional catch binding (`try {} catch {}`)

2v19 : Fix Object.values/entries for numeric keys after 2v18 regression (fix #2375)
nRF52: for SD>5 use static buffers for advertising and scan response data (#2367)
Expand Down
39 changes: 21 additions & 18 deletions src/jsparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2831,27 +2831,30 @@ NO_INLINE JsVar *jspeStatementTry() {
if (lex->tk == LEX_R_CATCH) {
JSP_ASSERT_MATCH(LEX_R_CATCH);
hadCatch = true;
JSP_MATCH('(');
JsVar *scope = 0;
JsVar *exceptionVar = 0;
if (hadException) {
scope = jsvNewObject();
if (scope)
exceptionVar = jsvFindOrAddChildFromString(scope, jslGetTokenValueAsString());
}
JSP_MATCH_WITH_CLEANUP_AND_RETURN(LEX_ID,jsvUnLock2(scope,exceptionVar),0);
JSP_MATCH_WITH_CLEANUP_AND_RETURN(')',jsvUnLock2(scope,exceptionVar),0);
if (exceptionVar) {
// set the exception var up properly
JsVar *exception = jspGetException();
if (exception) {
jsvSetValueOfName(exceptionVar, exception);
jsvUnLock(exception);
JsVar *scope = 0;
JsVar *exception = jspGetException();
if (lex->tk == '(') {
JSP_MATCH('(');
if (hadException) {
scope = jsvNewObject();
if (scope)
exceptionVar = jsvFindOrAddChildFromString(scope, jslGetTokenValueAsString());
}
JSP_MATCH_WITH_CLEANUP_AND_RETURN(LEX_ID,jsvUnLock2(scope,exceptionVar),0);
JSP_MATCH_WITH_CLEANUP_AND_RETURN(')',jsvUnLock2(scope,exceptionVar),0);
if (exceptionVar) {
// set the exception var up properly
if (exception)
jsvSetValueOfName(exceptionVar, exception);

jsvUnLock(exceptionVar);
}
// Now clear the exception flag (it's handled - we hope!)
execInfo.execute = execInfo.execute & (JsExecFlags)~(EXEC_EXCEPTION|EXEC_ERROR_LINE_REPORTED);
jsvUnLock(exceptionVar);

}
// Now clear the exception flag (it's handled - we hope!)
execInfo.execute = execInfo.execute & (JsExecFlags)~(EXEC_EXCEPTION|EXEC_ERROR_LINE_REPORTED);
jsvUnLock(exception);

if (shouldExecuteBefore && !hadException) {
JSP_SAVE_EXECUTE();
Expand Down
13 changes: 12 additions & 1 deletion tests/test_exception_3.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,16 @@ try {
}
a.push("After");

try { // Optional catch Binding
a.push("Before");
throw "Oh Noes!";
a.push("After");
} catch {
a.push("Catch");
} finally {
a.push("Finally");
}
a.push("After");

a = a.join(",");
result = a=="Before,Catch,Finally,After";
result = a=="Before,Catch,Finally,After,Before,Catch,Finally,After";

0 comments on commit bed1d37

Please sign in to comment.