protected static FunctionExpression GetTranslatedAst( System.Linq.Expressions.Expression expression, object[] translators = null, object[] validationOptions = null) { var translation = Translate(expression, translators, validationOptions); var jsParser = new JavaScriptParser(); return(jsParser.ParseFunctionExpression(translation)); }
public ObjectInstance Construct(JsValue[] arguments) { var argCount = arguments.Length; string p = ""; string body = ""; if (argCount == 1) { body = TypeConverter.ToString(arguments[0]); } else if (argCount > 1) { var firstArg = arguments[0]; p = TypeConverter.ToString(firstArg); for (var k = 1; k < argCount - 1; k++) { var nextArg = arguments[k]; p += "," + TypeConverter.ToString(nextArg); } body = TypeConverter.ToString(arguments[argCount-1]); } var parameters = this.ParseArgumentNames(p); var parser = new JavaScriptParser(); FunctionExpression function; try { var functionExpression = "function(" + p + ") { " + body + "}"; function = parser.ParseFunctionExpression(functionExpression); } catch (ParserException) { throw new JavaScriptException(Engine.SyntaxError); } // todo: check if there is not a way to use the FunctionExpression directly instead of creating a FunctionDeclaration var functionObject = new ScriptFunctionInstance( Engine, new FunctionDeclaration { Type = SyntaxNodes.FunctionDeclaration, Body = new BlockStatement { Type = SyntaxNodes.BlockStatement, Body = new [] { function.Body } }, Parameters = parameters.Select(x => new Identifier { Type = SyntaxNodes.Identifier, Name = x }).ToArray(), FunctionDeclarations = function.FunctionDeclarations, VariableDeclarations = function.VariableDeclarations }, LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment), function.Strict ) { Extensible = true }; return functionObject; }
public ObjectInstance Construct(JsValue[] arguments) { var argCount = arguments.Length; string p = ""; string body = ""; if (argCount == 1) { body = TypeConverter.ToString(arguments[0]); } else if (argCount > 1) { var firstArg = arguments[0]; p = TypeConverter.ToString(firstArg); for (var k = 1; k < argCount - 1; k++) { var nextArg = arguments[k]; p += "," + TypeConverter.ToString(nextArg); } body = TypeConverter.ToString(arguments[argCount - 1]); } var parameters = this.ParseArgumentNames(p); var parser = new JavaScriptParser(); FunctionExpression function; try { var functionExpression = "function(" + p + ") { " + body + "}"; function = parser.ParseFunctionExpression(functionExpression); } catch (ParserException) { throw new JavaScriptException(Engine.SyntaxError); } // todo: check if there is not a way to use the FunctionExpression directly instead of creating a FunctionDeclaration var functionObject = new ScriptFunctionInstance( Engine, new FunctionDeclaration { Type = SyntaxNodes.FunctionDeclaration, Body = new BlockStatement { Type = SyntaxNodes.BlockStatement, Body = new [] { function.Body } }, Parameters = parameters.Select(x => new Identifier { Type = SyntaxNodes.Identifier, Name = x }).ToArray(), FunctionDeclarations = function.FunctionDeclarations, VariableDeclarations = function.VariableDeclarations }, LexicalEnvironment.NewDeclarativeEnvironment(Engine, Engine.ExecutionContext.LexicalEnvironment), function.Strict ) { Extensible = true }; return(functionObject); }