123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998 |
- using System;
- using System.Linq;
- using Jint.Native;
- using Jint.Native.Function;
- using Jint.Native.Number;
- using Jint.Native.Object;
- using Jint.Parser.Ast;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Environments;
- using Jint.Runtime.References;
- namespace Jint.Runtime
- {
- public class ExpressionInterpreter
- {
- private readonly Engine _engine;
- public ExpressionInterpreter(Engine engine)
- {
- _engine = engine;
- }
- private object EvaluateExpression(Expression expression)
- {
- return _engine.EvaluateExpression(expression);
- }
- public object EvaluateConditionalExpression(ConditionalExpression conditionalExpression)
- {
- var test = _engine.EvaluateExpression(conditionalExpression.Test);
- var evaluate = TypeConverter.ToBoolean(test) ? conditionalExpression.Consequent : conditionalExpression.Alternate;
-
- return _engine.EvaluateExpression(evaluate);
- }
- public object EvaluateAssignmentExpression(AssignmentExpression assignmentExpression)
- {
- object rval = _engine.GetValue(EvaluateExpression(assignmentExpression.Right));
- var lref = EvaluateExpression(assignmentExpression.Left) as Reference;
- if (assignmentExpression.Operator == "=")
- {
- if(lref != null && lref.IsStrict() && lref.GetBase() is EnvironmentRecord && (lref.GetReferencedName() == "eval" || lref.GetReferencedName() == "arguments"))
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- _engine.PutValue(lref, rval);
- return rval;
- }
- object lval = _engine.GetValue(lref);
- switch (assignmentExpression.Operator)
- {
- case "+=":
- var lprim = TypeConverter.ToPrimitive(lval);
- var rprim = TypeConverter.ToPrimitive(rval);
- if (TypeConverter.GetType(lprim) == Types.String ||
- TypeConverter.GetType(rprim) == Types.String)
- {
- lval = TypeConverter.ToString(lprim) + TypeConverter.ToString(rprim);
- }
- else
- {
- lval = TypeConverter.ToNumber(lprim) + TypeConverter.ToNumber(rprim);
- }
- break;
- case "-=":
- lval = TypeConverter.ToNumber(lval) - TypeConverter.ToNumber(rval);
- break;
- case "*=":
- if (lval == Undefined.Instance || rval == Undefined.Instance)
- {
- lval = Undefined.Instance;
- }
- else
- {
- lval = TypeConverter.ToNumber(lval) * TypeConverter.ToNumber(rval);
- }
- break;
- case "/=":
- lval = Divide(lval, rval);
- break;
- case "%=":
- if (lval == Undefined.Instance || rval == Undefined.Instance)
- {
- lval = Undefined.Instance;
- }
- else
- {
- lval = TypeConverter.ToNumber(lval) % TypeConverter.ToNumber(rval);
- }
- break;
- case "&=":
- lval = TypeConverter.ToInt32(lval) & TypeConverter.ToInt32(rval);
- break;
- case "|=":
- lval = TypeConverter.ToInt32(lval) | TypeConverter.ToInt32(rval);
- break;
- case "^=":
- lval = TypeConverter.ToInt32(lval) ^ TypeConverter.ToInt32(rval);
- break;
- case "<<=":
- lval = TypeConverter.ToInt32(lval) << (int)(TypeConverter.ToUint32(rval) & 0x1F);
- break;
- case ">>=":
- lval = TypeConverter.ToInt32(lval) >> (int)(TypeConverter.ToUint32(rval) & 0x1F);
- break;
- case ">>>=":
- lval = (uint)TypeConverter.ToInt32(lval) >> (int)(TypeConverter.ToUint32(rval) & 0x1F);
- break;
-
- default:
- throw new NotImplementedException();
- }
- _engine.PutValue(lref, lval);
- return lval;
- }
- private object Divide(object lval, object rval)
- {
- if (lval == Undefined.Instance || rval == Undefined.Instance)
- {
- return Undefined.Instance;
- }
- else
- {
- var rN = TypeConverter.ToNumber(rval);
- var lN = TypeConverter.ToNumber(lval);
- if (double.IsNaN(rN) || double.IsNaN(lN))
- {
- return double.NaN;
- }
- if (double.IsInfinity(lN) && double.IsInfinity(rN))
- {
- return double.NaN;
- }
- if (double.IsInfinity(lN) && rN == 0)
- {
- if (NumberInstance.IsNegativeZero(rN))
- {
- return -lN;
- }
- return lN;
- }
- if (lN == 0 && rN == 0)
- {
- return double.NaN;
- }
- if (rN == 0)
- {
- if (NumberInstance.IsNegativeZero(rN))
- {
- return lN > 0 ? -double.PositiveInfinity : -double.NegativeInfinity;
- }
- return lN > 0 ? double.PositiveInfinity : double.NegativeInfinity;
- }
- return lN/rN;
- }
- }
- public object EvaluateBinaryExpression(BinaryExpression expression)
- {
- object left = _engine.GetValue(EvaluateExpression(expression.Left));
- object right = _engine.GetValue(EvaluateExpression(expression.Right));
- object value;
- switch (expression.Operator)
- {
- case "+":
- var lprim = TypeConverter.ToPrimitive(left);
- var rprim = TypeConverter.ToPrimitive(right);
- if (TypeConverter.GetType(lprim) == Types.String || TypeConverter.GetType(rprim) == Types.String)
- {
- value = TypeConverter.ToString(lprim) + TypeConverter.ToString(rprim);
- }
- else
- {
- value = TypeConverter.ToNumber(lprim) + TypeConverter.ToNumber(rprim);
- }
- break;
-
- case "-":
- value = TypeConverter.ToNumber(left) - TypeConverter.ToNumber(right);
- break;
-
- case "*":
- if (left == Undefined.Instance || right == Undefined.Instance)
- {
- value = Undefined.Instance;
- }
- else
- {
- value = TypeConverter.ToNumber(left) * TypeConverter.ToNumber(right);
- }
- break;
-
- case "/":
- value = Divide(left, right);
- break;
- case "%":
- if (left == Undefined.Instance || right == Undefined.Instance)
- {
- value = Undefined.Instance;
- }
- else
- {
- value = TypeConverter.ToNumber(left) % TypeConverter.ToNumber(right);
- }
- break;
- case "==":
- value = Equal(left, right);
- break;
-
- case "!=":
- value = !Equal(left, right);
- break;
-
- case ">":
- value = Compare(right, left, false);
- if (value == Undefined.Instance)
- {
- value = false;
- }
- break;
- case ">=":
- value = Compare(left, right);
- if (value == Undefined.Instance || (bool) value == true)
- {
- value = false;
- }
- else
- {
- value = true;
- }
- break;
-
- case "<":
- value = Compare(left, right);
- if (value == Undefined.Instance)
- {
- value = false;
- }
- break;
-
- case "<=":
- value = Compare(right, left, false);
- if (value == Undefined.Instance || (bool) value == true)
- {
- value = false;
- }
- else
- {
- value = true;
- }
- break;
-
- case "===":
- return StriclyEqual(left, right);
-
- case "!==":
- return !StriclyEqual(left, right);
- case "&":
- return TypeConverter.ToInt32(left) & TypeConverter.ToInt32(right);
- case "|":
- return TypeConverter.ToInt32(left) | TypeConverter.ToInt32(right);
- case "^":
- return TypeConverter.ToInt32(left) ^ TypeConverter.ToInt32(right);
- case "<<":
- return TypeConverter.ToInt32(left) << (int)(TypeConverter.ToUint32(right) & 0x1F);
- case ">>":
- return TypeConverter.ToInt32(left) >> (int)(TypeConverter.ToUint32(right) & 0x1F);
- case ">>>":
- return (uint)TypeConverter.ToInt32(left) >> (int)(TypeConverter.ToUint32(right) & 0x1F);
- case "instanceof":
- var f = right as FunctionInstance;
- if (f == null)
- {
- throw new JavaScriptException(_engine.TypeError, "instanceof can only be used with a function object");
- }
- value = f.HasInstance(left);
- break;
-
- case "in":
- var o = right as ObjectInstance;
- if (o == null)
- {
- throw new JavaScriptException(_engine.TypeError, "in can only be used with an object");
- }
- value = o.HasProperty(TypeConverter.ToString(left));
- break;
-
- default:
- throw new NotImplementedException();
- }
- return value;
- }
- public object EvaluateLogicalExpression(LogicalExpression logicalExpression)
- {
- var left = _engine.GetValue(EvaluateExpression(logicalExpression.Left));
- switch (logicalExpression.Operator)
- {
- case "&&":
- if (!TypeConverter.ToBoolean(left))
- {
- return left;
- }
- return _engine.GetValue(EvaluateExpression(logicalExpression.Right));
- case "||":
- if (TypeConverter.ToBoolean(left))
- {
- return left;
- }
- return _engine.GetValue(EvaluateExpression(logicalExpression.Right));
- default:
- throw new NotImplementedException();
- }
- }
- public static bool Equal(object x, object y)
- {
- var typex = TypeConverter.GetType(x);
- var typey = TypeConverter.GetType(y);
- if (typex == typey)
- {
- if (typex == Types.Undefined || typex == Types.Null)
- {
- return true;
- }
- if (typex == Types.Number)
- {
- var nx = TypeConverter.ToNumber(x);
- var ny = TypeConverter.ToNumber(y);
- if (double.IsNaN(nx) || double.IsNaN(ny))
- {
- return false;
- }
- if (nx == ny)
- {
- return true;
- }
- return false;
- }
- if (typex == Types.String)
- {
- return TypeConverter.ToString(x) == TypeConverter.ToString(y);
- }
- if (typex == Types.Boolean)
- {
- return (bool) x == (bool) y;
- }
- return x == y;
- }
- if (x == Null.Instance && y == Undefined.Instance)
- {
- return true;
- }
- if (x == Undefined.Instance && y == Null.Instance)
- {
- return true;
- }
- if (typex == Types.Number && typey == Types.String)
- {
- return Equal(x, TypeConverter.ToNumber(y));
- }
- if (typex == Types.String && typey == Types.Number)
- {
- return Equal(TypeConverter.ToNumber(x), y);
- }
- if (typex == Types.Boolean)
- {
- return Equal(TypeConverter.ToNumber(x), y);
- }
- if (typey == Types.Boolean)
- {
- return Equal(x, TypeConverter.ToNumber(y));
- }
- if (typey == Types.Object && (typex == Types.String || typex == Types.Number))
- {
- return Equal(x, TypeConverter.ToPrimitive(y));
- }
- if (typex == Types.Object && (typey == Types.String || typey == Types.Number))
- {
- return Equal(TypeConverter.ToPrimitive(x), y);
- }
- return false;
- }
- public static bool StriclyEqual(object x, object y)
- {
- var typea = TypeConverter.GetType(x);
- var typeb = TypeConverter.GetType(y);
- if (typea != typeb)
- {
- return false;
- }
- if (typea == Types.Undefined || typea == Types.Null)
- {
- return true;
- }
- if (typea == Types.None)
- {
- return true;
- }
- if (typea == Types.Number)
- {
- var nx = TypeConverter.ToNumber(x);
- var ny = TypeConverter.ToNumber(y);
- if (double.IsNaN(nx) || double.IsNaN(ny))
- {
- return false;
- }
- if (nx == ny)
- {
- return true;
- }
- return false;
- }
- if (typea == Types.String)
- {
- return TypeConverter.ToString(x) == TypeConverter.ToString(y);
- }
- if (typea == Types.Boolean)
- {
- return TypeConverter.ToBoolean(x) == TypeConverter.ToBoolean(y);
- }
- return x == y;
- }
- public static bool SameValue(object x, object y)
- {
- var typea = TypeConverter.GetType(x);
- var typeb = TypeConverter.GetType(y);
- if (typea != typeb)
- {
- return false;
- }
- if (typea == Types.None)
- {
- return true;
- }
- if (typea == Types.Number)
- {
- var nx = TypeConverter.ToNumber(x);
- var ny = TypeConverter.ToNumber(y);
- if (double.IsNaN(nx) && double.IsNaN(ny))
- {
- return true;
- }
- if (nx == ny)
- {
- if (nx == 0)
- {
- // +0 !== -0
- return NumberInstance.IsNegativeZero(nx) == NumberInstance.IsNegativeZero(ny);
- }
- return true;
- }
- return false;
- }
- if (typea == Types.String)
- {
- return TypeConverter.ToString(x) == TypeConverter.ToString(y);
- }
- if (typea == Types.Boolean)
- {
- return TypeConverter.ToBoolean(x) == TypeConverter.ToBoolean(y);
- }
- return x == y;
- }
- public static object Compare(object x, object y, bool leftFirst = true)
- {
- object px, py;
- if (leftFirst)
- {
- px = TypeConverter.ToPrimitive(x, Types.Number);
- py = TypeConverter.ToPrimitive(y, Types.Number);
- }
- else
- {
- py = TypeConverter.ToPrimitive(y, Types.Number);
- px = TypeConverter.ToPrimitive(x, Types.Number);
- }
- var typea = TypeConverter.GetType(px);
- var typeb = TypeConverter.GetType(py);
- if (typea != Types.String || typeb != Types.String)
- {
- var nx = TypeConverter.ToNumber(px);
- var ny = TypeConverter.ToNumber(py);
- if (double.IsNaN(nx) || double.IsNaN(ny))
- {
- return Undefined.Instance;
- }
- if (nx == ny)
- {
- return false;
- }
- if (nx == double.PositiveInfinity)
- {
- return false;
- }
- if (ny == double.PositiveInfinity)
- {
- return true;
- }
- if (ny == double.NegativeInfinity)
- {
- return false;
- }
- if (nx == double.NegativeInfinity)
- {
- return true;
- }
- return nx < ny;
- }
- else
- {
- return String.CompareOrdinal(TypeConverter.ToString(x), TypeConverter.ToString(y)) < 0;
- }
- }
- public object EvaluateIdentifier(Identifier identifier)
- {
- return _engine.ExecutionContext.LexicalEnvironment.GetIdentifierReference(identifier.Name, _engine.Options.IsStrict());
- }
- public object EvaluateLiteral(Literal literal)
- {
- return literal.Value ?? Null.Instance;
- }
- public object 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 DataDescriptor(propValue) {Writable=true, Enumerable=true,Configurable = true};
- break;
- case PropertyKind.Get:
- var getter = property.Value as FunctionExpression;
- if (getter == null)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- var get = new ScriptFunctionInstance(
- _engine,
- getter,
- _engine.ExecutionContext.LexicalEnvironment,
- getter.Strict || _engine.Options.IsStrict()
- );
- propDesc = new AccessorDescriptor(get) { Enumerable = true, Configurable = true};
- break;
-
- case PropertyKind.Set:
- var setter = property.Value as FunctionExpression;
- if (setter == null)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- var set = new ScriptFunctionInstance(
- _engine,
- setter,
- _engine.ExecutionContext.LexicalEnvironment,
- setter.Strict || _engine.Options.IsStrict()
- );
- propDesc = new AccessorDescriptor(null, set) { Enumerable = true, Configurable = true};
- break;
- default:
- throw new ArgumentOutOfRangeException();
- }
- if (previous != Undefined.Instance)
- {
- var previousIsData = previous.IsDataDescriptor();
- var previousIsAccessor = previous.IsAccessorDescriptor();
- var propIsData = propDesc.IsDataDescriptor();
- var propIsAccessor = propDesc.IsAccessorDescriptor();
- if (_engine.Options.IsStrict() && previousIsData && propIsData)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- if (previousIsData && propIsAccessor)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- if (previousIsAccessor && propIsData)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- if (previousIsAccessor && propIsAccessor)
- {
- var previousAccessor = previous.As<AccessorDescriptor>();
- var propAccessor = propDesc.As<AccessorDescriptor>();
- if (propAccessor.Set != null)
- {
- if (previousAccessor.Set != null)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- if (previousAccessor.Get != null)
- {
- propAccessor.Get = previousAccessor.Get;
- }
- }
- else if (propAccessor.Get != null)
- {
- if (previousAccessor.Get != null)
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- if (previousAccessor.Set != null)
- {
- propAccessor.Set = previousAccessor.Set;
- }
- }
- }
- }
- obj.DefineOwnProperty(propName, propDesc, false);
- }
- return obj;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1
- /// </summary>
- /// <param name="memberExpression"></param>
- /// <returns></returns>
- public object EvaluateMemberExpression(MemberExpression memberExpression)
- {
- var baseReference = EvaluateExpression(memberExpression.Object);
- var baseValue = _engine.GetValue(baseReference);
- string propertyNameString;
- if (!memberExpression.Computed) // index accessor ?
- {
- propertyNameString = memberExpression.Property.As<Identifier>().Name;
- }
- else
- {
- var propertyNameReference = EvaluateExpression(memberExpression.Property);
- var propertyNameValue = _engine.GetValue(propertyNameReference);
- TypeConverter.CheckObjectCoercible(_engine, baseValue);
- propertyNameString = TypeConverter.ToString(propertyNameValue);
- }
- return new Reference(baseValue, propertyNameString, _engine.Options.IsStrict());
- }
- public object EvaluateFunctionExpression(FunctionExpression functionExpression)
- {
- return new ScriptFunctionInstance(
- _engine,
- functionExpression,
- LexicalEnvironment.NewDeclarativeEnvironment(_engine, _engine.ExecutionContext.LexicalEnvironment),
- functionExpression.Strict
- );
- }
- public object EvaluateCallExpression(CallExpression callExpression)
- {
- var callee = EvaluateExpression(callExpression.Callee);
- var func = _engine.GetValue(callee);
- if (func == Undefined.Instance)
- {
- throw new JavaScriptException(_engine.TypeError);
- }
- object thisObject;
- // todo: implement as in http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.4
- var arguments = callExpression.Arguments.Select(EvaluateExpression).Select(_engine.GetValue).ToArray();
- if (TypeConverter.GetType(func) != Types.Object)
- {
- throw new JavaScriptException(_engine.TypeError);
- }
- var callable = func as ICallable;
- if (callable == null)
- {
- throw new JavaScriptException(_engine.TypeError);
- }
- var r = callee as Reference;
- if (r != null)
- {
- if (r.IsPropertyReference())
- {
- thisObject = r.GetBase();
- }
- else
- {
- var env = r.GetBase() as EnvironmentRecord;
- thisObject = env.ImplicitThisValue();
- }
- }
- else
- {
- thisObject = Undefined.Instance;
- }
- return callable.Call(thisObject, arguments);
- }
- public object EvaluateSequenceExpression(SequenceExpression sequenceExpression)
- {
- foreach (var expression in sequenceExpression.Expressions)
- {
- _engine.EvaluateExpression(expression);
- }
- return Undefined.Instance;
- }
- public object EvaluateUpdateExpression(UpdateExpression updateExpression)
- {
- var value = _engine.EvaluateExpression(updateExpression.Argument);
- Reference r;
- switch (updateExpression.Operator)
- {
- case "++":
- r = value as Reference;
- if (r != null
- && r.IsStrict()
- && (r.GetBase() is EnvironmentRecord)
- && (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- var oldValue = _engine.GetValue(value);
- var newValue = TypeConverter.ToNumber(oldValue) + 1;
- _engine.PutValue(r, newValue);
- return updateExpression.Prefix ? newValue : oldValue;
- case "--":
- r = value as Reference;
- if (r != null
- && r.IsStrict()
- && (r.GetBase() is EnvironmentRecord)
- && (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- oldValue = _engine.GetValue(value);
- newValue = TypeConverter.ToNumber(oldValue) - 1;
- _engine.PutValue(r, newValue);
- return updateExpression.Prefix ? newValue : oldValue;
- default:
- throw new ArgumentException();
- }
- }
- public object EvaluateThisExpression(ThisExpression thisExpression)
- {
- return _engine.ExecutionContext.ThisBinding;
- }
- public object EvaluateNewExpression(NewExpression newExpression)
- {
- var arguments = newExpression.Arguments.Select(EvaluateExpression).Select(_engine.GetValue).ToArray();
-
- // todo: optimize by defining a common abstract class or interface
- var callee = _engine.GetValue(EvaluateExpression(newExpression.Callee)) as IConstructor;
-
- if (callee == null)
- {
- throw new JavaScriptException(_engine.TypeError, "The object can't be used as constructor.");
- }
- // construct the new instance using the Function's constructor method
- var instance = callee.Construct(arguments);
- return instance;
- }
- public object EvaluateArrayExpression(ArrayExpression arrayExpression)
- {
- var a = _engine.Array.Construct(new object[] { arrayExpression.Elements.Count() });
- var n = 0;
- foreach (var expr in arrayExpression.Elements)
- {
- if (expr != null)
- {
- var value = _engine.GetValue(EvaluateExpression(expr));
- a.DefineOwnProperty(n.ToString(),
- new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true}, false);
- }
- n++;
- }
-
- return a;
- }
- public object EvaluateUnaryExpression(UnaryExpression unaryExpression)
- {
- var value = _engine.EvaluateExpression(unaryExpression.Argument);
- Reference r;
- switch (unaryExpression.Operator)
- {
- case "+":
- return TypeConverter.ToNumber(_engine.GetValue(value));
-
- case "-":
- var n = TypeConverter.ToNumber(_engine.GetValue(value));
- return double.IsNaN(n) ? double.NaN : n*-1;
-
- case "~":
- return ~TypeConverter.ToInt32(_engine.GetValue(value));
-
- case "!":
- return !TypeConverter.ToBoolean(_engine.GetValue(value));
-
- case "delete":
- r = value as Reference;
- if (r == null)
- {
- return true;
- }
- if (r.IsUnresolvableReference())
- {
- if (r.IsStrict())
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- return true;
- }
- if (r.IsPropertyReference())
- {
- var o = TypeConverter.ToObject(_engine, r.GetBase());
- return o.Delete(r.GetReferencedName(), r.IsStrict());
- }
- if (r.IsStrict())
- {
- throw new JavaScriptException(_engine.SyntaxError);
- }
- var bindings = r.GetBase() as EnvironmentRecord;
- return bindings.DeleteBinding(r.GetReferencedName());
-
- case "void":
- _engine.GetValue(value);
- return Undefined.Instance;
- case "typeof":
- r = value as Reference;
- if (r != null)
- {
- if (r.IsUnresolvableReference())
- {
- return "undefined";
- }
- }
- var v = _engine.GetValue(value);
- if (v == Undefined.Instance)
- {
- return "undefined";
- }
- if (v == Null.Instance)
- {
- return "object";
- }
- switch (TypeConverter.GetType(v))
- {
- case Types.Boolean: return "boolean";
- case Types.Number: return "number";
- case Types.String: return "string";
- }
- if (v is ICallable)
- {
- return "function";
- }
- return "object";
- default:
- throw new ArgumentException();
- }
- }
- }
- }
|