FunctionConstructor.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #include "config.h"
  21. #include "FunctionConstructor.h"
  22. #include "Debugger.h"
  23. #include "ExceptionHelpers.h"
  24. #include "FunctionPrototype.h"
  25. #include "JSFunction.h"
  26. #include "JSGlobalObject.h"
  27. #include "JSString.h"
  28. #include "Lexer.h"
  29. #include "Nodes.h"
  30. #include "Operations.h"
  31. #include "Parser.h"
  32. #include <wtf/text/StringBuilder.h>
  33. namespace JSC {
  34. ASSERT_HAS_TRIVIAL_DESTRUCTOR(FunctionConstructor);
  35. const ClassInfo FunctionConstructor::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionConstructor) };
  36. FunctionConstructor::FunctionConstructor(JSGlobalObject* globalObject, Structure* structure)
  37. : InternalFunction(globalObject, structure)
  38. {
  39. }
  40. void FunctionConstructor::finishCreation(ExecState* exec, FunctionPrototype* functionPrototype)
  41. {
  42. Base::finishCreation(exec->vm(), functionPrototype->classInfo()->className);
  43. putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, functionPrototype, DontEnum | DontDelete | ReadOnly);
  44. // Number of arguments for constructor
  45. putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontDelete | DontEnum);
  46. }
  47. static EncodedJSValue JSC_HOST_CALL constructWithFunctionConstructor(ExecState* exec)
  48. {
  49. ArgList args(exec);
  50. return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
  51. }
  52. ConstructType FunctionConstructor::getConstructData(JSCell*, ConstructData& constructData)
  53. {
  54. constructData.native.function = constructWithFunctionConstructor;
  55. return ConstructTypeHost;
  56. }
  57. static EncodedJSValue JSC_HOST_CALL callFunctionConstructor(ExecState* exec)
  58. {
  59. ArgList args(exec);
  60. return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args));
  61. }
  62. // ECMA 15.3.1 The Function Constructor Called as a Function
  63. CallType FunctionConstructor::getCallData(JSCell*, CallData& callData)
  64. {
  65. callData.native.function = callFunctionConstructor;
  66. return CallTypeHost;
  67. }
  68. // ECMA 15.3.2 The Function Constructor
  69. JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const String& sourceURL, const TextPosition& position)
  70. {
  71. if (!globalObject->evalEnabled())
  72. return throwError(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage()));
  73. return constructFunctionSkippingEvalEnabledCheck(exec, globalObject, args, functionName, sourceURL, position);
  74. }
  75. JSObject* constructFunctionSkippingEvalEnabledCheck(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, const Identifier& functionName, const String& sourceURL, const TextPosition& position)
  76. {
  77. // Functions need to have a space following the opening { due to for web compatibility
  78. // see https://bugs.webkit.org/show_bug.cgi?id=24350
  79. // We also need \n before the closing } to handle // comments at the end of the last line
  80. String program;
  81. if (args.isEmpty())
  82. program = ASCIILiteral("(function() { \n})");
  83. else if (args.size() == 1)
  84. program = makeString("(function() { ", args.at(0).toString(exec)->value(exec), "\n})");
  85. else {
  86. StringBuilder builder;
  87. builder.appendLiteral("(function(");
  88. builder.append(args.at(0).toString(exec)->value(exec));
  89. for (size_t i = 1; i < args.size() - 1; i++) {
  90. builder.append(',');
  91. builder.append(args.at(i).toString(exec)->value(exec));
  92. }
  93. builder.appendLiteral(") { ");
  94. builder.append(args.at(args.size() - 1).toString(exec)->value(exec));
  95. builder.appendLiteral("\n})");
  96. program = builder.toString();
  97. }
  98. SourceCode source = makeSource(program, sourceURL, position);
  99. JSObject* exception = 0;
  100. FunctionExecutable* function = FunctionExecutable::fromGlobalCode(functionName, exec, exec->dynamicGlobalObject()->debugger(), source, &exception);
  101. if (!function) {
  102. ASSERT(exception);
  103. return throwError(exec, exception);
  104. }
  105. return JSFunction::create(exec, function, globalObject);
  106. }
  107. // ECMA 15.3.2 The Function Constructor
  108. JSObject* constructFunction(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
  109. {
  110. return constructFunction(exec, globalObject, args, exec->propertyNames().anonymous, String(), TextPosition::minimumPosition());
  111. }
  112. } // namespace JSC