roblox-ts
Version:
<div align="center"><img width=25% src="https://i.imgur.com/yCjHmng.png"></div> <h1 align="center"><a href="https://roblox-ts.github.io/">roblox-ts</a></h1> <div align="center">A TypeScript-to-Lua Compiler for Roblox</div> <br> <div align="center"> <a hr
77 lines • 3.16 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const ts = __importStar(require("ts-morph"));
const _1 = require(".");
const CompilerError_1 = require("../errors/CompilerError");
const typeUtilities_1 = require("../typeUtilities");
const utility_1 = require("../utility");
function compileThrowStatement(state, node) {
const expression = utility_1.skipNodesDownwards(node.getExpression());
if (!expression || !typeUtilities_1.isStringType(typeUtilities_1.getType(expression))) {
throw new CompilerError_1.CompilerError("Non-string throws are not supported!", node, CompilerError_1.CompilerErrorType.NonStringThrow);
}
state.enterPrecedingStatementContext();
const err = _1.compileExpression(state, expression);
return state.exitPrecedingStatementContextAndJoin() + state.indent + `error(${err});\n`;
}
exports.compileThrowStatement = compileThrowStatement;
function compileTryStatement(state, node) {
const tryBlock = node.getTryBlock();
const returnStatement = tryBlock
.getDescendantStatements()
.find(statement => ts.TypeGuards.isReturnStatement(statement));
if (returnStatement) {
throw new CompilerError_1.CompilerError("Try blocks cannot have return statements!", returnStatement, CompilerError_1.CompilerErrorType.TryReturn);
}
let result = "";
let hasErrVar = false;
const catchClause = node.getCatchClause();
if (catchClause) {
hasErrVar = catchClause.getVariableDeclaration() !== undefined;
}
const successId = catchClause ? state.getNewId() : "";
const errMsgId = catchClause ? state.getNewId() : "";
result += state.indent;
if (catchClause) {
if (hasErrVar) {
result += `local ${successId}, ${errMsgId}`;
}
else {
result += `local ${successId}`;
}
result += ` = `;
}
result += `pcall(function()\n`;
state.pushIndent();
result += _1.compileStatementedNode(state, tryBlock);
state.popIndent();
result += state.indent + `end);\n`;
if (catchClause) {
result += state.indent + `if not ${successId} then\n`;
state.pushIndent();
if (hasErrVar) {
result += state.indent + `local ${catchClause.getVariableDeclarationOrThrow().getName()} = ${errMsgId};\n`;
}
result += _1.compileStatementedNode(state, catchClause.getBlock());
state.popIndent();
result += state.indent + `end;\n`;
}
const finallyBlock = node.getFinallyBlock();
if (finallyBlock) {
result += state.indent + `do\n`;
state.pushIndent();
result += _1.compileStatementedNode(state, finallyBlock);
state.popIndent();
result += state.indent + `end;\n`;
}
return result;
}
exports.compileTryStatement = compileTryStatement;
//# sourceMappingURL=try.js.map