StringConstructor.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
  3. * Copyright (C) 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 "StringConstructor.h"
  22. #include "Executable.h"
  23. #include "JITCode.h"
  24. #include "JSFunction.h"
  25. #include "JSGlobalObject.h"
  26. #include "Operations.h"
  27. #include "StringPrototype.h"
  28. namespace JSC {
  29. static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState*);
  30. }
  31. #include "StringConstructor.lut.h"
  32. namespace JSC {
  33. const ClassInfo StringConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::stringConstructorTable, CREATE_METHOD_TABLE(StringConstructor) };
  34. /* Source for StringConstructor.lut.h
  35. @begin stringConstructorTable
  36. fromCharCode stringFromCharCode DontEnum|Function 1
  37. @end
  38. */
  39. ASSERT_HAS_TRIVIAL_DESTRUCTOR(StringConstructor);
  40. StringConstructor::StringConstructor(JSGlobalObject* globalObject, Structure* structure)
  41. : InternalFunction(globalObject, structure)
  42. {
  43. }
  44. void StringConstructor::finishCreation(ExecState* exec, StringPrototype* stringPrototype)
  45. {
  46. Base::finishCreation(exec->vm(), stringPrototype->classInfo()->className);
  47. putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, stringPrototype, ReadOnly | DontEnum | DontDelete);
  48. putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
  49. }
  50. bool StringConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
  51. {
  52. return getStaticFunctionSlot<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(cell), propertyName, slot);
  53. }
  54. bool StringConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  55. {
  56. return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::stringConstructorTable(exec), jsCast<StringConstructor*>(object), propertyName, descriptor);
  57. }
  58. // ------------------------------ Functions --------------------------------
  59. static NEVER_INLINE JSValue stringFromCharCodeSlowCase(ExecState* exec)
  60. {
  61. unsigned length = exec->argumentCount();
  62. UChar* buf;
  63. PassRefPtr<StringImpl> impl = StringImpl::createUninitialized(length, buf);
  64. for (unsigned i = 0; i < length; ++i)
  65. buf[i] = static_cast<UChar>(exec->argument(i).toUInt32(exec));
  66. return jsString(exec, impl);
  67. }
  68. static EncodedJSValue JSC_HOST_CALL stringFromCharCode(ExecState* exec)
  69. {
  70. if (LIKELY(exec->argumentCount() == 1))
  71. return JSValue::encode(jsSingleCharacterString(exec, exec->argument(0).toUInt32(exec)));
  72. return JSValue::encode(stringFromCharCodeSlowCase(exec));
  73. }
  74. JSCell* JSC_HOST_CALL stringFromCharCode(ExecState* exec, int32_t arg)
  75. {
  76. return jsSingleCharacterString(exec, arg);
  77. }
  78. static EncodedJSValue JSC_HOST_CALL constructWithStringConstructor(ExecState* exec)
  79. {
  80. JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
  81. if (!exec->argumentCount())
  82. return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure()));
  83. return JSValue::encode(StringObject::create(exec, globalObject->stringObjectStructure(), exec->argument(0).toString(exec)));
  84. }
  85. ConstructType StringConstructor::getConstructData(JSCell*, ConstructData& constructData)
  86. {
  87. constructData.native.function = constructWithStringConstructor;
  88. return ConstructTypeHost;
  89. }
  90. static EncodedJSValue JSC_HOST_CALL callStringConstructor(ExecState* exec)
  91. {
  92. if (!exec->argumentCount())
  93. return JSValue::encode(jsEmptyString(exec));
  94. return JSValue::encode(exec->argument(0).toString(exec));
  95. }
  96. CallType StringConstructor::getCallData(JSCell*, CallData& callData)
  97. {
  98. callData.native.function = callStringConstructor;
  99. return CallTypeHost;
  100. }
  101. } // namespace JSC