public static IFunction GetFunctionAst(this ScriptFunctionInstance scriptFunctionInstance) { //TODO: expose this in Jint directly instead of reflection var theFuncAst = (IFunction)_funcDeclarationFieldInfo.GetValue(scriptFunctionInstance); return(theFuncAst); }
private JsValue[] GetParameters(ScriptFunctionInstance func, IDictionary <string, object>[] metas) { if (metas != null) { Helpers.CheckRequired(func.FormalParameters, metas); } var result = new List <JsValue>(); foreach (var item in func.FormalParameters) { object value; var meta = metas?.FirstOrDefault(f => item.Equals(f["name"])); if (meta == null) { value = GetDefaultValue(func, item); } else { value = new RootMeta(func.Engine, meta, _renderContext).Value; } var jsValue = JsValue.FromObject(func.Engine, value); result.Add(jsValue); } return(result.ToArray()); }
public JavaScriptReduceOperation(ScriptFunctionInstance reduce, ScriptFunctionInstance key, Engine engine, JintPreventResolvingTasksReferenceResolver resolver) { Reduce = reduce; Key = key; Engine = engine; _resolver = resolver; GetReduceFieldsNames(); _groupedItems = null; }
private (FunctionInstance Function, IFunction FunctionAst)? CheckIfSimpleMapExpression(Engine engine, IFunction function) { var field = function.TryGetFieldFromSimpleLambdaExpression(); if (field == null) { return(null); } var properties = new List <Expression> { new Property(PropertyKind.Data, new Identifier(field), false, new StaticMemberExpression(new Identifier("self"), new Identifier(field)), false, false) }; if (MoreArguments != null) { for (int i = 0; i < MoreArguments.Length; i++) { var arg = MoreArguments.Get(i.ToString()).As <FunctionInstance>(); if (!(arg is ScriptFunctionInstance sfi)) { continue; } var moreFuncAst = sfi.FunctionDeclaration; field = moreFuncAst.TryGetFieldFromSimpleLambdaExpression(); if (field != null) { properties.Add(new Property(PropertyKind.Data, new Identifier(field), false, new StaticMemberExpression(new Identifier("self"), new Identifier(field)), false, false)); } } } var functionExp = new FunctionExpression( function.Id, NodeList.Create(new List <Expression> { new Identifier("self") }), new BlockStatement(NodeList.Create(new List <Statement> { new ReturnStatement(new ObjectExpression(NodeList.Create(properties))) })), generator: false, function.Strict, async: false); var functionObject = new ScriptFunctionInstance( engine, functionExp, LexicalEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment), function.Strict ); return(functionObject, functionExp); }
public JavaScriptReduceOperation(ScriptFunctionInstance reduce, ScriptFunctionInstance key, Engine engine, JintPreventResolvingTasksReferenceResolver resolver, long indexVersion) { Reduce = reduce ?? throw new ArgumentNullException(nameof(reduce)); Key = key ?? throw new ArgumentNullException(nameof(key)); Engine = engine; _resolver = resolver; GetReduceFieldsNames(); _groupedItems = null; _indexVersion = indexVersion; }
protected override ExpressionResult EvaluateInternal(EvaluationContext context) { var engine = context.Engine; var scope = engine.ExecutionContext.LexicalEnvironment; var closure = new ScriptFunctionInstance( engine, _function, scope, FunctionThisMode.Lexical, proto: engine.Realm.Intrinsics.Function.PrototypeObject); if (_function.Name is null) { closure.SetFunctionName(JsString.Empty); } return(NormalCompletion(closure)); }
public override Completion GetValue(EvaluationContext context) { var engine = context.Engine; var funcEnv = JintEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment); var closure = new ScriptFunctionInstance( engine, _function, funcEnv, _function.ThisMode); closure.MakeConstructor(); if (_function.Name != null) { funcEnv.CreateMutableBindingAndInitialize(_function.Name, canBeDeleted: false, closure); } return(Completion.Normal(closure, _expression.Location)); }
private object GetDefaultValue(ScriptFunctionInstance func, string item) { object value; if (item.ToLower() == "body") { if (JsonHelper.IsJson(_renderContext.Request.Body)) { value = new JsonParser(func.Engine).Parse(_renderContext.Request.Body).ToObject(); } else { value = Helpers.FormToObject(_renderContext); } } else { value = _renderContext.Request.QueryString.Get(item); } return(value); }
protected override object EvaluateInternal() { var funcEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment); var functionThisMode = _function.Strict || _engine._isStrict ? FunctionInstance.FunctionThisMode.Strict : FunctionInstance.FunctionThisMode.Global; var closure = new ScriptFunctionInstance( _engine, _function, funcEnv, functionThisMode); if (_function.Name != null) { var envRec = (DeclarativeEnvironmentRecord)funcEnv._record; envRec.CreateMutableBindingAndInitialize(_function.Name, canBeDeleted: false, closure); } return(closure); }
public JsValue EvaluateFunctionExpression(FunctionExpression functionExpression) { var funcEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment); var envRec = (DeclarativeEnvironmentRecord)funcEnv.Record; if (functionExpression.Id != null && !String.IsNullOrEmpty(functionExpression.Id.Name)) { envRec.CreateMutableBinding(functionExpression.Id.Name); } var closure = new ScriptFunctionInstance( _engine, functionExpression, funcEnv, functionExpression.Strict ); if (functionExpression.Id != null && !String.IsNullOrEmpty(functionExpression.Id.Name)) { envRec.InitializeImmutableBinding(functionExpression.Id.Name, closure); } return(closure); }
public JsValue EvaluateObjectExpression(ObjectExpression objectExpression) { // http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5 var obj = _engine.Object.Construct(Arguments.Empty); foreach (var property in objectExpression.Properties) { var propName = property.Key.GetKey(); var previous = obj.GetOwnProperty(propName); PropertyDescriptor propDesc; switch (property.Kind) { case PropertyKind.Data: var exprValue = _engine.EvaluateExpression(property.Value); var propValue = _engine.GetValue(exprValue); propDesc = new PropertyDescriptor(propValue, true, true, true); break; case PropertyKind.Get: var getter = property.Value as FunctionExpression; if (getter == null) { throw new JavaScriptException(_engine.SyntaxError); } ScriptFunctionInstance get; using (new StrictModeScope(getter.Strict)) { get = new ScriptFunctionInstance( _engine, getter, _engine.ExecutionContext.LexicalEnvironment, StrictModeScope.IsStrictModeCode ); } propDesc = new PropertyDescriptor(get: get, set: null, enumerable: true, configurable: true); break; case PropertyKind.Set: var setter = property.Value as FunctionExpression; if (setter == null) { throw new JavaScriptException(_engine.SyntaxError); } ScriptFunctionInstance set; using (new StrictModeScope(setter.Strict)) { set = new ScriptFunctionInstance( _engine, setter, _engine.ExecutionContext.LexicalEnvironment, StrictModeScope.IsStrictModeCode ); } propDesc = new PropertyDescriptor(get: null, set: set, enumerable: true, configurable: true); break; default: throw new ArgumentOutOfRangeException(); } if (previous != PropertyDescriptor.Undefined) { if (StrictModeScope.IsStrictModeCode && previous.IsDataDescriptor() && propDesc.IsDataDescriptor()) { throw new JavaScriptException(_engine.SyntaxError); } if (previous.IsDataDescriptor() && propDesc.IsAccessorDescriptor()) { throw new JavaScriptException(_engine.SyntaxError); } if (previous.IsAccessorDescriptor() && propDesc.IsDataDescriptor()) { throw new JavaScriptException(_engine.SyntaxError); } if (previous.IsAccessorDescriptor() && propDesc.IsAccessorDescriptor()) { if (propDesc.Set != null && previous.Set != null) { throw new JavaScriptException(_engine.SyntaxError); } if (propDesc.Get != null && previous.Get != null) { throw new JavaScriptException(_engine.SyntaxError); } } } obj.DefineOwnProperty(propName, propDesc, false); } return(obj); }
private object BuildObjectNormal() { var obj = _engine.Object.Construct(_properties.Length); bool isStrictModeCode = _engine._isStrict || StrictModeScope.IsStrictModeCode; for (var i = 0; i < _properties.Length; i++) { var objectProperty = _properties[i]; if (objectProperty is null) { // spread if (_valueExpressions[i].GetValue() is ObjectInstance source) { source.CopyDataProperties(obj, null); } continue; } var property = objectProperty._value; var propName = objectProperty.KeyJsString ?? property.GetKey(_engine); PropertyDescriptor propDesc; if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data) { var expr = _valueExpressions[i]; var propValue = expr.GetValue().Clone(); if (expr._expression.IsFunctionWithName()) { var functionInstance = (FunctionInstance)propValue; functionInstance.SetFunctionName(propName); } propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable); } else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set) { var function = property.Value as IFunction ?? ExceptionHelper.ThrowSyntaxError <IFunction>(_engine); var functionInstance = new ScriptFunctionInstance( _engine, function, _engine.ExecutionContext.LexicalEnvironment, isStrictModeCode ); functionInstance.SetFunctionName(propName); functionInstance._prototypeDescriptor = null; propDesc = new GetSetPropertyDescriptor( get: property.Kind == PropertyKind.Get ? functionInstance : null, set: property.Kind == PropertyKind.Set ? functionInstance : null, PropertyFlag.Enumerable | PropertyFlag.Configurable); } else { return(ExceptionHelper.ThrowArgumentOutOfRangeException <object>()); } obj.DefineOwnProperty(propName, propDesc); } return(obj); }
public MelonVisitor(MelonEngine engine, ScriptFunctionInstance scriptFunction) : this(engine) { _scriptFunction = scriptFunction; }
public override ParseResult VisitFunctionDefinitionStatement(MelonParser.FunctionDefinitionStatementContext context) { string name = context.Name.GetText(); LexicalEnvironment functionEnvironment = new LexicalEnvironment(parseContext.LexicalEnvironment, true); var functionParameters = new List <FunctionParameter>(); if (context.Parameters != null) { for (var i = 0; i < context.Parameters.parameter().Length; i++) { var parameter = context.Parameters.parameter(i); bool isVarargs = parameter.VARARGS() != null; if (isVarargs && i < context.Parameters.parameter().Length - 1) { throw new MelonException("Varargs parameter can only appear once and has to be the last parameter"); } MelonType type = _engine.anyType; if (parameter.Type != null) { var typeName = parameter.Type.name().value; var typeKv = _engine.Types.FirstOrDefault(x => x.Value.Name == typeName); if (typeKv.Value == null) { throw new MelonException($"Could not find type '{typeName}'"); } type = typeKv.Value; } var typeRef = new TypeReference(_engine, type); functionEnvironment.AddVariable(parameter.Name.value, null, typeRef); functionParameters.Add(new FunctionParameter(parameter.Name.value, typeRef, isVarargs)); } } var function = new ScriptFunctionInstance(name, _engine) { ParameterTypes = functionParameters.ToArray() }; var variable = parseContext.LexicalEnvironment.AddVariable(name, function, new TypeReference(_engine, _engine.functionType)); parseContext.AddVariableReference(variable, VariableReferenceType.Local); if (context.ReturnType != null) { var typeName = context.ReturnType.value; var typeKv = _engine.Types.FirstOrDefault(x => x.Value.Name == typeName); if (typeKv.Value == null) { throw new MelonException($"Could not find type '{typeName}'"); } function.ReturnType = new TypeReference(_engine, typeKv.Value); } MelonVisitor visitor = new MelonVisitor(_engine, function); ParseContext functionParseContext = visitor.Parse(context.Block, functionEnvironment); function.SetContext(functionEnvironment, functionParseContext); return(DefaultResult); }
public void AddEventListener(string eventName, ScriptFunctionInstance callback) { Callbacks.Add(eventName, callback); }
public RecursiveJsFunction(Engine engine, JsValue item, ScriptFunctionInstance func) { _engine = engine ?? throw new ArgumentNullException(nameof(engine)); _item = item; _func = func ?? throw new ArgumentNullException(nameof(func)); }