Exemple #1
0
        public object Run(Scope scope, string code)
        {
            if(_cache.ContainsKey(code))
            {
                return _cache[code].Invoke(scope ?? _scope);
            }

            dynamic tree = null;

            try
            {
                tree = new Esprima().Parse(code);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Invalid left-hand side in assignment"))
                    throw new ReferenceError(ex.Message);
                throw new SyntaxError(ex.Message);
            }

            var ast = AstTree.Factory(tree);

            _cache[code] = ast;

            return ((IOperation)ast).Invoke(scope ?? _scope);
        }
Exemple #2
0
        public object Get(Scope scope, string id)
        {
            object val = (scope ?? _scope).Get(id);
            //if((scope ?? _scope).VarSet.ContainsKey(id))
            //{
            //    val = (scope ?? _scope).VarSet[id];
            //}

            //if ((scope ?? _scope).ThisSet.ContainsKey(id))
            //{
            //    val = (scope ?? _scope).ThisSet[id];
            //}

            return val;
            //return Util.GetValue(val);
        }
Exemple #3
0
        public object Invoke(Scope scope)
        {
            var index = 0;
            if (Arguments != null)
                Arguments.ForEach(a =>
                                      {
                                          object val = null;
                                          if (a is Literal)
                                              val = ((Literal)a).Value;
                                          else
                                              val = ((IOperation) a).Invoke(scope);

                                          try
                                          {
                                              scope.SetArgument(((Identifier)Params[index]).Id, val);
                                          }
                                          catch (Exception)
                                          {
                                              scope.SetArgument(Guid.NewGuid().ToString(), val);
                                          }
                                          index++;
                                      });
            return ((IOperation)Body).Invoke(scope);
        }
Exemple #4
0
 public void SetCurrentScope(Scope scope)
 {
     _scope = scope;
 }
Exemple #5
0
 public void CloneVarSet(Scope newScope)
 {
     foreach (var m in VarSet) { newScope.VarSet[m.Key] = m.Value; }
 }
Exemple #6
0
 public void CloneThisSet(Scope newScope)
 {
     foreach (var m in ThisSet) { newScope.ThisSet[m.Key] = m.Value; }
 }
Exemple #7
0
 public void CloneFunctionSet(Scope newScope)
 {
     foreach (var m in FunctionSet) { newScope.FunctionSet[m.Key] = m.Value; }
 }
Exemple #8
0
 public void Set(Scope scope, string id, object instance)
 {
     (scope ?? _scope).SetThis(id, instance);
 }