JSBoundFunction.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "JSBoundFunction.h"
  27. #include "GetterSetter.h"
  28. #include "JSGlobalObject.h"
  29. #include "Operations.h"
  30. namespace JSC {
  31. const ClassInfo JSBoundFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSBoundFunction) };
  32. EncodedJSValue JSC_HOST_CALL boundFunctionCall(ExecState* exec)
  33. {
  34. JSBoundFunction* boundFunction = jsCast<JSBoundFunction*>(exec->callee());
  35. ASSERT(isJSArray(boundFunction->boundArgs())); // Currently this is true!
  36. JSArray* boundArgs = asArray(boundFunction->boundArgs());
  37. MarkedArgumentBuffer args;
  38. for (unsigned i = 0; i < boundArgs->length(); ++i)
  39. args.append(boundArgs->getIndexQuickly(i));
  40. for (unsigned i = 0; i < exec->argumentCount(); ++i)
  41. args.append(exec->argument(i));
  42. JSObject* targetFunction = boundFunction->targetFunction();
  43. CallData callData;
  44. CallType callType = getCallData(targetFunction, callData);
  45. ASSERT(callType != CallTypeNone);
  46. return JSValue::encode(call(exec, targetFunction, callType, callData, boundFunction->boundThis(), args));
  47. }
  48. EncodedJSValue JSC_HOST_CALL boundFunctionConstruct(ExecState* exec)
  49. {
  50. JSBoundFunction* boundFunction = jsCast<JSBoundFunction*>(exec->callee());
  51. ASSERT(isJSArray(boundFunction->boundArgs())); // Currently this is true!
  52. JSArray* boundArgs = asArray(boundFunction->boundArgs());
  53. MarkedArgumentBuffer args;
  54. for (unsigned i = 0; i < boundArgs->length(); ++i)
  55. args.append(boundArgs->getIndexQuickly(i));
  56. for (unsigned i = 0; i < exec->argumentCount(); ++i)
  57. args.append(exec->argument(i));
  58. JSObject* targetFunction = boundFunction->targetFunction();
  59. ConstructData constructData;
  60. ConstructType constructType = getConstructData(targetFunction, constructData);
  61. ASSERT(constructType != ConstructTypeNone);
  62. return JSValue::encode(construct(exec, targetFunction, constructType, constructData, args));
  63. }
  64. JSBoundFunction* JSBoundFunction::create(ExecState* exec, JSGlobalObject* globalObject, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs, int length, const String& name)
  65. {
  66. ConstructData constructData;
  67. ConstructType constructType = JSC::getConstructData(targetFunction, constructData);
  68. bool canConstruct = constructType != ConstructTypeNone;
  69. NativeExecutable* executable = exec->vm().getHostFunction(boundFunctionCall, canConstruct ? boundFunctionConstruct : callHostFunctionAsConstructor);
  70. JSBoundFunction* function = new (NotNull, allocateCell<JSBoundFunction>(*exec->heap())) JSBoundFunction(exec, globalObject, globalObject->boundFunctionStructure(), targetFunction, boundThis, boundArgs);
  71. function->finishCreation(exec, executable, length, name);
  72. return function;
  73. }
  74. void JSBoundFunction::destroy(JSCell* cell)
  75. {
  76. static_cast<JSBoundFunction*>(cell)->JSBoundFunction::~JSBoundFunction();
  77. }
  78. bool JSBoundFunction::customHasInstance(JSObject* object, ExecState* exec, JSValue value)
  79. {
  80. return jsCast<JSBoundFunction*>(object)->m_targetFunction->hasInstance(exec, value);
  81. }
  82. JSBoundFunction::JSBoundFunction(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs)
  83. : Base(exec, globalObject, structure)
  84. , m_targetFunction(exec->vm(), this, targetFunction)
  85. , m_boundThis(exec->vm(), this, boundThis)
  86. , m_boundArgs(exec->vm(), this, boundArgs)
  87. {
  88. }
  89. void JSBoundFunction::finishCreation(ExecState* exec, NativeExecutable* executable, int length, const String& name)
  90. {
  91. Base::finishCreation(exec, executable, length, name);
  92. ASSERT(inherits(&s_info));
  93. putDirectAccessor(exec, exec->propertyNames().arguments, globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Accessor);
  94. putDirectAccessor(exec, exec->propertyNames().caller, globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Accessor);
  95. }
  96. void JSBoundFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
  97. {
  98. JSBoundFunction* thisObject = jsCast<JSBoundFunction*>(cell);
  99. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  100. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  101. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  102. Base::visitChildren(thisObject, visitor);
  103. visitor.append(&thisObject->m_targetFunction);
  104. visitor.append(&thisObject->m_boundThis);
  105. visitor.append(&thisObject->m_boundArgs);
  106. }
  107. } // namespace JSC