Skip to content

Commit

Permalink
Restrict the user from using the wrapper name
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunayf committed Nov 6, 2023
1 parent 9747eee commit fb46ac9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @since 2.0.0
*/
public class ParserConstants {
public static final String WRAPPER_PREFIX = "__shell_wrapper__";

public static final Set<String> RESTRICTED_FUNCTION_NAMES = Set.of("main", "init", "__java_recall",
"__java_memorize", "__recall_any", "__recall_any_error", "__memorize", "__stmts", "__run");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public Collection<Node> parseDeclarations(String source) throws TreeParserExcept
private boolean isModuleDeclarationAllowed(ModuleMemberDeclarationNode declarationNode) {
if (declarationNode instanceof FunctionDefinitionNode) {
String functionName = ((FunctionDefinitionNode) declarationNode).functionName().text();
if (RESTRICTED_FUNCTION_NAMES.contains(functionName)) {
if (RESTRICTED_FUNCTION_NAMES.contains(functionName) ||
functionName.startsWith(ParserConstants.WRAPPER_PREFIX)) {
addWarnDiagnostic("Found '" + functionName + "' function in the declarations.\n" +
"Discarded '" + functionName + "' function without loading.");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public Collection<Node> parse(String source) throws ParserTrialFailedException {
private void validateModuleDeclaration(ModuleMemberDeclarationNode declarationNode) {
if (declarationNode instanceof FunctionDefinitionNode) {
String functionName = ((FunctionDefinitionNode) declarationNode).functionName().text();
if (RESTRICTED_FUNCTION_NAMES.contains(functionName)) {
if (RESTRICTED_FUNCTION_NAMES.contains(functionName) ||
functionName.startsWith(ParserConstants.WRAPPER_PREFIX)) {
String message = "Function name " + "'" + functionName + "'" + " not allowed in Ballerina Shell.\n";
throw new InvalidMethodException(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import io.ballerina.compiler.syntax.tree.WhileStatementNode;
import io.ballerina.compiler.syntax.tree.XMLNamespaceDeclarationNode;
import io.ballerina.shell.exceptions.SnippetException;
import io.ballerina.shell.parser.ParserConstants;
import io.ballerina.shell.snippet.SnippetSubKind;
import io.ballerina.shell.snippet.types.ExpressionSnippet;
import io.ballerina.shell.snippet.types.ImportDeclarationSnippet;
Expand Down Expand Up @@ -164,18 +165,19 @@ public List<VariableDeclarationSnippet> createVariableDeclarationSnippets(Node n
return null;
}

boolean hasCheck = ((VariableDeclarationNode) node).initializer().get().kind() == SyntaxKind.CHECK_ACTION;
boolean hasCheck =
((VariableDeclarationNode) node).initializer().get().kind() == SyntaxKind.CHECK_ACTION;
String functionBody = varNode.initializer().get().toString();
String functionDefinition = (hasCheck ? "function() returns error|" :
"function() returns ") + typeDescriptorNode;
String functionString =
functionDefinition + "f_" + varFunctionCount + " = " + functionDefinition + "{" +
typeDescriptorNode + bindingPatternNode.toString() + " = " + functionBody + ";" +
"return " + bindingPatternNode + ";};";
String functionName = ParserConstants.WRAPPER_PREFIX + varFunctionCount;
String functionString = String.format("%s %s = %s {%s %s = %s; return %s;};", functionDefinition,
functionName, functionDefinition, typeDescriptorNode, bindingPatternNode, functionBody,
bindingPatternNode);
varNode = (VariableDeclarationNode) NodeParser.parseStatement(functionString);
newNode = (VariableDeclarationNode) NodeParser
.parseStatement(typeDescriptorNode + " " + bindingPatternNode + " = " +
(hasCheck ? "check " : "") + " f_" + varFunctionCount + "();");
newNode = (VariableDeclarationNode) NodeParser.parseStatement(
String.format("%s %s = %s %s();", typeDescriptorNode, bindingPatternNode,
(hasCheck ? "check " : ""), functionName));
}

varFunctionCount += 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit fb46ac9

Please sign in to comment.