/ API / JSCallbackObject.cpp
JSCallbackObject.cpp
  1  /*
  2   * Copyright (C) 2006-2017 Apple Inc.  All rights reserved.
  3   * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
  4   *
  5   * Redistribution and use in source and binary forms, with or without
  6   * modification, are permitted provided that the following conditions
  7   * are met:
  8   * 1. Redistributions of source code must retain the above copyright
  9   *    notice, this list of conditions and the following disclaimer.
 10   * 2. Redistributions in binary form must reproduce the above copyright
 11   *    notice, this list of conditions and the following disclaimer in the
 12   *    documentation and/or other materials provided with the distribution.
 13   *
 14   * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 15   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 17   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 18   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 19   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 20   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 21   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 22   * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 24   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 25   */
 26  
 27  #include "config.h"
 28  #include "JSCallbackObject.h"
 29  
 30  #include "JSCInlines.h"
 31  
 32  namespace JSC {
 33  
 34  static JSC_DECLARE_HOST_FUNCTION(callJSNonFinalObjectCallbackObject);
 35  static JSC_DECLARE_HOST_FUNCTION(constructJSNonFinalObjectCallbackObject);
 36  static JSC_DECLARE_HOST_FUNCTION(callJSGlobalObjectCallbackObject);
 37  static JSC_DECLARE_HOST_FUNCTION(constructJSGlobalObjectCallbackObject);
 38  
 39  static JSC_DECLARE_CUSTOM_GETTER(callbackGetterJSNonFinalObjectCallbackObject);
 40  static JSC_DECLARE_CUSTOM_GETTER(staticFunctionGetterJSNonFinalObjectCallbackObject);
 41  static JSC_DECLARE_CUSTOM_GETTER(callbackGetterJSGlobalObjectCallbackObject);
 42  static JSC_DECLARE_CUSTOM_GETTER(staticFunctionGetterJSGlobalObjectCallbackObject);
 43  
 44  // Define the two types of JSCallbackObjects we support.
 45  template <> const ClassInfo JSCallbackObject<JSNonFinalObject>::s_info = { "CallbackObject", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCallbackObject) };
 46  template <> const ClassInfo JSCallbackObject<JSGlobalObject>::s_info = { "CallbackGlobalObject", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCallbackObject) };
 47  template<> const bool JSCallbackObject<JSNonFinalObject>::needsDestruction = true;
 48  template<> const bool JSCallbackObject<JSGlobalObject>::needsDestruction = true;
 49  
 50  template<>
 51  RawNativeFunction JSCallbackObject<JSNonFinalObject>::getCallFunction()
 52  {
 53      return callJSNonFinalObjectCallbackObject;
 54  }
 55  
 56  template<>
 57  RawNativeFunction JSCallbackObject<JSNonFinalObject>::getConstructFunction()
 58  {
 59      return constructJSNonFinalObjectCallbackObject;
 60  }
 61  
 62  template <>
 63  PropertySlot::GetValueFunc JSCallbackObject<JSNonFinalObject>::getCallbackGetter()
 64  {
 65      return callbackGetterJSNonFinalObjectCallbackObject;
 66  }
 67  
 68  template <>
 69  PropertySlot::GetValueFunc JSCallbackObject<JSNonFinalObject>::getStaticFunctionGetter()
 70  {
 71      return staticFunctionGetterJSNonFinalObjectCallbackObject;
 72  }
 73  
 74  
 75  template<>
 76  RawNativeFunction JSCallbackObject<JSGlobalObject>::getCallFunction()
 77  {
 78      return callJSGlobalObjectCallbackObject;
 79  }
 80  
 81  template<>
 82  RawNativeFunction JSCallbackObject<JSGlobalObject>::getConstructFunction()
 83  {
 84      return constructJSGlobalObjectCallbackObject;
 85  }
 86  
 87  template <>
 88  PropertySlot::GetValueFunc JSCallbackObject<JSGlobalObject>::getCallbackGetter()
 89  {
 90      return callbackGetterJSGlobalObjectCallbackObject;
 91  }
 92  
 93  template <>
 94  PropertySlot::GetValueFunc JSCallbackObject<JSGlobalObject>::getStaticFunctionGetter()
 95  {
 96      return staticFunctionGetterJSGlobalObjectCallbackObject;
 97  }
 98  
 99  template<>
100  JSCallbackObject<JSGlobalObject>* JSCallbackObject<JSGlobalObject>::create(VM& vm, JSClassRef classRef, Structure* structure)
101  {
102      JSCallbackObject<JSGlobalObject>* callbackObject = new (NotNull, allocateCell<JSCallbackObject<JSGlobalObject>>(vm.heap)) JSCallbackObject(vm, classRef, structure);
103      callbackObject->finishCreation(vm);
104      return callbackObject;
105  }
106  
107  template <>
108  Structure* JSCallbackObject<JSNonFinalObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
109  { 
110      return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), info()); 
111  }
112      
113  template <>
114  Structure* JSCallbackObject<JSGlobalObject>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
115  { 
116      return Structure::create(vm, globalObject, proto, TypeInfo(GlobalObjectType, StructureFlags), info()); 
117  }
118  
119  template <>
120  IsoSubspace* JSCallbackObject<JSNonFinalObject>::subspaceForImpl(VM& vm, SubspaceAccess mode)
121  {
122      switch (mode) {
123      case SubspaceAccess::OnMainThread:
124          return vm.callbackObjectSpace<SubspaceAccess::OnMainThread>();
125      case SubspaceAccess::Concurrently:
126          return vm.callbackObjectSpace<SubspaceAccess::Concurrently>();
127      }
128      RELEASE_ASSERT_NOT_REACHED();
129      return nullptr;
130  }
131  
132  template <>
133  IsoSubspace* JSCallbackObject<JSGlobalObject>::subspaceForImpl(VM& vm, SubspaceAccess mode)
134  {
135      switch (mode) {
136      case SubspaceAccess::OnMainThread:
137          return vm.callbackGlobalObjectSpace<SubspaceAccess::OnMainThread>();
138      case SubspaceAccess::Concurrently:
139          return vm.callbackGlobalObjectSpace<SubspaceAccess::Concurrently>();
140      }
141      RELEASE_ASSERT_NOT_REACHED();
142      return nullptr;
143  }
144  
145  JSC_DEFINE_HOST_FUNCTION(callJSNonFinalObjectCallbackObject, (JSGlobalObject* globalObject, CallFrame* callFrame))
146  {
147      return JSCallbackObject<JSNonFinalObject>::callImpl(globalObject, callFrame);
148  }
149  
150  JSC_DEFINE_HOST_FUNCTION(constructJSNonFinalObjectCallbackObject, (JSGlobalObject* globalObject, CallFrame* callFrame))
151  {
152      return JSCallbackObject<JSNonFinalObject>::constructImpl(globalObject, callFrame);
153  }
154  
155  JSC_DEFINE_HOST_FUNCTION(callJSGlobalObjectCallbackObject, (JSGlobalObject* globalObject, CallFrame* callFrame))
156  {
157      return JSCallbackObject<JSGlobalObject>::callImpl(globalObject, callFrame);
158  }
159  
160  JSC_DEFINE_HOST_FUNCTION(constructJSGlobalObjectCallbackObject, (JSGlobalObject* globalObject, CallFrame* callFrame))
161  {
162      return JSCallbackObject<JSGlobalObject>::constructImpl(globalObject, callFrame);
163  }
164  
165  JSC_DEFINE_CUSTOM_GETTER(callbackGetterJSNonFinalObjectCallbackObject, (JSGlobalObject* globalObject, EncodedJSValue thisValue, PropertyName propertyName))
166  {
167      return JSCallbackObject<JSNonFinalObject>::callbackGetterImpl(globalObject, thisValue, propertyName);
168  }
169  
170  JSC_DEFINE_CUSTOM_GETTER(staticFunctionGetterJSNonFinalObjectCallbackObject, (JSGlobalObject* globalObject, EncodedJSValue thisValue, PropertyName propertyName))
171  {
172      return JSCallbackObject<JSNonFinalObject>::staticFunctionGetterImpl(globalObject, thisValue, propertyName);
173  }
174  
175  JSC_DEFINE_CUSTOM_GETTER(callbackGetterJSGlobalObjectCallbackObject, (JSGlobalObject* globalObject, EncodedJSValue thisValue, PropertyName propertyName))
176  {
177      return JSCallbackObject<JSGlobalObject>::callbackGetterImpl(globalObject, thisValue, propertyName);
178  }
179  
180  JSC_DEFINE_CUSTOM_GETTER(staticFunctionGetterJSGlobalObjectCallbackObject, (JSGlobalObject* globalObject, EncodedJSValue thisValue, PropertyName propertyName))
181  {
182      return JSCallbackObject<JSGlobalObject>::staticFunctionGetterImpl(globalObject, thisValue, propertyName);
183  }
184  
185  } // namespace JSC