public JsValue 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)).TryCast<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; }
private void EmitNewExpression(NewExpression e) { Write("new "); Emit(e.Callee); EmitFunctionCallArguments(e.Arguments); }