typescript-to-lua 1.37.0 → 1.37.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -147,6 +147,7 @@ const transformTryStatement = (statement, context) => {
147
147
  }
148
148
  const tryResultIdentifier = lua.createIdentifier("____try");
149
149
  const returnValueIdentifier = lua.createIdentifier("____returnValue");
150
+ const rethrowIdentifier = lua.createIdentifier("____rethrow");
150
151
  const result = [];
151
152
  const returnedIdentifier = lua.createIdentifier("____hasReturned");
152
153
  let returnCondition;
@@ -167,12 +168,18 @@ const transformTryStatement = (statement, context) => {
167
168
  }
168
169
  }
169
170
  result.push(lua.createVariableDeclarationStatement(tryReturnIdentifiers, tryCall));
171
+ if (statement.finallyBlock) {
172
+ result.push(lua.createVariableDeclarationStatement(rethrowIdentifier));
173
+ }
170
174
  const catchCall = lua.createCallExpression(catchIdentifier, statement.catchClause.variableDeclaration ? [lua.cloneIdentifier(returnedIdentifier)] : []);
171
175
  const catchCallStatement = hasReturn
172
176
  ? lua.createAssignmentStatement([lua.cloneIdentifier(returnedIdentifier), lua.cloneIdentifier(returnValueIdentifier)], catchCall)
173
177
  : lua.createExpressionStatement(catchCall);
178
+ const catchCallFunction = lua.createFunctionExpression(lua.createBlock([catchCallStatement]));
179
+ const tryCatchCall = lua.createCallExpression(pCall, [catchCallFunction]);
180
+ const tryCatchCallStatement = lua.createAssignmentStatement([lua.cloneIdentifier(tryResultIdentifier), lua.cloneIdentifier(rethrowIdentifier)], tryCatchCall);
174
181
  const notTryCondition = lua.createUnaryExpression(tryResultIdentifier, lua.SyntaxKind.NotOperator);
175
- result.push(lua.createIfStatement(notTryCondition, lua.createBlock([catchCallStatement])));
182
+ result.push(lua.createIfStatement(notTryCondition, lua.createBlock(statement.finallyBlock ? [tryCatchCallStatement] : [catchCallStatement])));
176
183
  }
177
184
  else if (tryScope.functionReturned) {
178
185
  // try with return, but no catch
@@ -194,16 +201,26 @@ const transformTryStatement = (statement, context) => {
194
201
  if (statement.finallyBlock && statement.finallyBlock.statements.length > 0) {
195
202
  result.push(...context.transformStatements(statement.finallyBlock));
196
203
  }
197
- // Re-throw error if try had no catch but had a finally.
204
+ // Re-throw error if try had a finally.
198
205
  // On pcall failure the error is the second return value, which lands in
199
206
  // ____hasReturned (when functionReturned) or ____error (otherwise).
200
- if (!statement.catchClause && statement.finallyBlock) {
207
+ if (statement.finallyBlock) {
201
208
  const notTryCondition = lua.createUnaryExpression(lua.cloneIdentifier(tryResultIdentifier), lua.SyntaxKind.NotOperator);
202
- const errorIdentifier = tryScope.functionReturned
203
- ? lua.cloneIdentifier(returnedIdentifier)
204
- : lua.createIdentifier("____error");
205
- const rethrow = lua.createExpressionStatement(lua.createCallExpression(lua.createIdentifier("error"), [errorIdentifier, lua.createNumericLiteral(0)]));
206
- result.push(lua.createIfStatement(notTryCondition, lua.createBlock([rethrow])));
209
+ if (!statement.catchClause) {
210
+ const errorIdentifier = tryScope.functionReturned
211
+ ? lua.cloneIdentifier(returnedIdentifier)
212
+ : lua.createIdentifier("____error");
213
+ const rethrow = lua.createExpressionStatement(lua.createCallExpression(lua.createIdentifier("error"), [errorIdentifier, lua.createNumericLiteral(0)]));
214
+ result.push(lua.createIfStatement(notTryCondition, lua.createBlock([rethrow])));
215
+ }
216
+ else if (statement.catchClause.block.statements.length > 0) {
217
+ // Re-throw is possible only from non-empty blocks.
218
+ const rethrow = lua.createExpressionStatement(lua.createCallExpression(lua.createIdentifier("error"), [
219
+ rethrowIdentifier,
220
+ lua.createNumericLiteral(0),
221
+ ]));
222
+ result.push(lua.createIfStatement(notTryCondition, lua.createBlock([rethrow])));
223
+ }
207
224
  }
208
225
  if (returnCondition && returnedIdentifier) {
209
226
  const returnValues = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-lua",
3
- "version": "1.37.0",
3
+ "version": "1.37.1",
4
4
  "description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
5
5
  "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
6
6
  "homepage": "https://typescripttolua.github.io/",