public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget) { var argCount = arguments.Length; string p = ""; string body = ""; if (argCount == 1) { body = TypeConverter.ToString(arguments[0]); } else if (argCount > 1) { var firstArg = arguments[0]; p = TypeConverter.ToString(firstArg); for (var k = 1; k < argCount - 1; k++) { var nextArg = arguments[k]; p += "," + TypeConverter.ToString(nextArg); } body = TypeConverter.ToString(arguments[argCount - 1]); } IFunction function; try { string functionExpression; if (argCount == 0) { functionExpression = "function f(){}"; } else { functionExpression = "function f("; if (p.IndexOf('/') != -1) { // ensure comments don't screw up things functionExpression += "\n" + p + "\n"; } else { functionExpression += p; } functionExpression += ")"; if (body.IndexOf('/') != -1) { // ensure comments don't screw up things functionExpression += "{\n" + body + "\n}"; } else { functionExpression += "{" + body + "}"; } } var parser = new JavaScriptParser(functionExpression, ParserOptions); function = (IFunction)parser.ParseScript().Body[0]; } catch (ParserException) { return(ExceptionHelper.ThrowSyntaxError <ObjectInstance>(_engine)); } var functionObject = new ScriptFunctionInstance( Engine, function, _engine.GlobalEnvironment, function.Strict); // the function is not actually a named function functionObject.SetFunctionName(_functionNameAnonymous, force: true); return(functionObject); }
/// <summary> /// https://tc39.es/ecma262/#sec-createdynamicfunction /// </summary> private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget) { var argCount = arguments.Length; string p = ""; string body = ""; if (argCount == 1) { body = TypeConverter.ToString(arguments[0]); } else if (argCount > 1) { var firstArg = arguments[0]; p = TypeConverter.ToString(firstArg); for (var k = 1; k < argCount - 1; k++) { var nextArg = arguments[k]; p += "," + TypeConverter.ToString(nextArg); } body = TypeConverter.ToString(arguments[argCount - 1]); } IFunction function = null; try { string functionExpression; if (argCount == 0) { functionExpression = "function f(){}"; } else { functionExpression = "function f("; if (p.IndexOf('/') != -1) { // ensure comments don't screw up things functionExpression += "\n" + p + "\n"; } else { functionExpression += p; } functionExpression += ")"; if (body.IndexOf('/') != -1) { // ensure comments don't screw up things functionExpression += "{\n" + body + "\n}"; } else { functionExpression += "{" + body + "}"; } } var parser = new JavaScriptParser(functionExpression, ParserOptions); function = (IFunction)parser.ParseScript().Body[0]; } catch (ParserException) { ExceptionHelper.ThrowSyntaxError(_realm); } // TODO generators etc, rewrite logic var proto = GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.Function.PrototypeObject); var functionObject = new ScriptFunctionInstance( Engine, function, _realm.GlobalEnv, function.Strict, proto) { _realm = _realm }; functionObject.MakeConstructor(); // the function is not actually a named function functionObject.SetFunctionName(_functionNameAnonymous, force: true); return(functionObject); }