JavascriptExternalFunction.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Forward declaration to avoid including scriptdirect.h
  6. typedef HRESULT(__cdecl *InitializeMethod)(Js::Var instance);
  7. namespace Js
  8. {
  9. struct StdCallJavascriptMethodInfo
  10. {
  11. Var thisArg;
  12. Var newTargetArg;
  13. bool isConstructCall;
  14. };
  15. typedef Var (__stdcall *StdCallJavascriptMethod)(Var callee, Var *args, USHORT cargs, StdCallJavascriptMethodInfo *info, void *callbackState);
  16. typedef int JavascriptTypeId;
  17. class JavascriptExternalFunction : public RuntimeFunction
  18. {
  19. private:
  20. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(JavascriptExternalFunction);
  21. protected:
  22. DEFINE_VTABLE_CTOR(JavascriptExternalFunction, RuntimeFunction);
  23. JavascriptExternalFunction(DynamicType * type);
  24. public:
  25. JavascriptExternalFunction(ExternalMethod nativeMethod, DynamicType* type);
  26. JavascriptExternalFunction(ExternalMethod entryPoint, DynamicType* type, InitializeMethod method, unsigned short deferredSlotCount, bool accessors);
  27. // this is default external function for DOM constructor that cannot be new'ed.
  28. JavascriptExternalFunction(DynamicType* type, InitializeMethod method, unsigned short deferredSlotCount, bool accessors);
  29. JavascriptExternalFunction(JavascriptExternalFunction* wrappedMethod, DynamicType* type);
  30. JavascriptExternalFunction(StdCallJavascriptMethod nativeMethod, DynamicType* type);
  31. virtual BOOL IsExternalFunction() override { return TRUE; }
  32. inline void SetSignature(Var signature) { this->signature = signature; }
  33. Var GetSignature() { return signature; }
  34. inline void SetCallbackState(void *callbackState) { this->callbackState = callbackState; }
  35. void *GetCallbackState() { return callbackState; }
  36. static const int ETW_MIN_COUNT_FOR_CALLER = 0x100; // power of 2
  37. class EntryInfo
  38. {
  39. public:
  40. static FunctionInfo ExternalFunctionThunk;
  41. static FunctionInfo WrappedFunctionThunk;
  42. static FunctionInfo StdCallExternalFunctionThunk;
  43. static FunctionInfo DefaultExternalFunctionThunk;
  44. };
  45. ExternalMethod GetNativeMethod() { return nativeMethod; }
  46. BOOL SetLengthProperty(Var length);
  47. void SetExternalFlags(UINT64 flags) { this->flags = flags; }
  48. unsigned char GetDeferredLength() const { return deferredLength; }
  49. void SetDeferredLength(unsigned char deferredLength)
  50. {
  51. AssertOrFailFastMsg(this->deferredLength == 0, "Deferred length is already pending");
  52. this->deferredLength = deferredLength;
  53. }
  54. void UndeferLength(ScriptContext *scriptContext);
  55. private:
  56. Field(UINT64) flags;
  57. Field(Var) signature;
  58. Field(void *) callbackState;
  59. union
  60. {
  61. FieldNoBarrier(ExternalMethod) nativeMethod;
  62. Field(JavascriptExternalFunction*) wrappedMethod;
  63. FieldNoBarrier(StdCallJavascriptMethod) stdCallNativeMethod;
  64. };
  65. Field(InitializeMethod) initMethod;
  66. // Ensure GC doesn't pin
  67. Field(unsigned int) oneBit:1;
  68. // Count of type slots for
  69. Field(unsigned int) typeSlots:15;
  70. // Determines if we need are a dictionary type
  71. Field(unsigned int) hasAccessors:1;
  72. Field(unsigned int) unused:7;
  73. // DOM Direct functions need to be able to set the length at construction time without undeferring the function.
  74. // We can store the length here so it can be set at the time of undeferral.
  75. Field(unsigned int) deferredLength:8;
  76. static Var ExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...);
  77. static Var WrappedFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...);
  78. static Var StdCallExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...);
  79. static Var DefaultExternalFunctionThunk(RecyclableObject* function, CallInfo callInfo, ...);
  80. static bool __cdecl DeferredLengthInitializer(DynamicObject* instance, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode);
  81. static bool __cdecl DeferredConstructorInitializer(DynamicObject* instance, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode);
  82. void PrepareExternalCall(Arguments * args);
  83. #if ENABLE_TTD
  84. public:
  85. virtual TTD::NSSnapObjects::SnapObjectType GetSnapTag_TTD() const override;
  86. virtual void ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc) override;
  87. static Var HandleRecordReplayExternalFunction_Thunk(Js::JavascriptFunction* function, CallInfo& callInfo, Arguments& args, ScriptContext* scriptContext);
  88. static Var HandleRecordReplayExternalFunction_StdThunk(Js::RecyclableObject* function, CallInfo& callInfo, Arguments& args, ScriptContext* scriptContext);
  89. static Var __stdcall TTDReplayDummyExternalMethod(Var callee, Var *args, USHORT cargs, StdCallJavascriptMethodInfo *info, void *callbackState);
  90. #endif
  91. friend class JavascriptLibrary;
  92. };
  93. }