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); }
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); }
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); }
public void SetCurrentScope(Scope scope) { _scope = scope; }
public void CloneVarSet(Scope newScope) { foreach (var m in VarSet) { newScope.VarSet[m.Key] = m.Value; } }
public void CloneThisSet(Scope newScope) { foreach (var m in ThisSet) { newScope.ThisSet[m.Key] = m.Value; } }
public void CloneFunctionSet(Scope newScope) { foreach (var m in FunctionSet) { newScope.FunctionSet[m.Key] = m.Value; } }
public void Set(Scope scope, string id, object instance) { (scope ?? _scope).SetThis(id, instance); }