RuntimeFunction.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "RuntimeLibraryPch.h"
  6. namespace Js
  7. {
  8. RuntimeFunction::RuntimeFunction(DynamicType * type)
  9. : JavascriptFunction(type), functionNameId(nullptr)
  10. {}
  11. RuntimeFunction::RuntimeFunction(DynamicType * type, FunctionInfo * functionInfo)
  12. : JavascriptFunction(type, functionInfo), functionNameId(nullptr)
  13. {}
  14. RuntimeFunction::RuntimeFunction(DynamicType * type, FunctionInfo * functionInfo, ConstructorCache* cache)
  15. : JavascriptFunction(type, functionInfo, cache), functionNameId(nullptr)
  16. {}
  17. Var
  18. RuntimeFunction::EnsureSourceString()
  19. {
  20. JavascriptLibrary* library = this->GetLibrary();
  21. ScriptContext * scriptContext = library->GetScriptContext();
  22. if (this->functionNameId == nullptr)
  23. {
  24. this->functionNameId = library->GetFunctionDisplayString();
  25. }
  26. else
  27. {
  28. if (TaggedInt::Is(this->functionNameId))
  29. {
  30. if (this->GetScriptContext()->GetConfig()->IsES6FunctionNameEnabled() && this->GetTypeHandler()->IsDeferredTypeHandler())
  31. {
  32. JavascriptString* functionName = nullptr;
  33. DebugOnly(bool status = ) this->GetFunctionName(&functionName);
  34. Assert(status);
  35. this->SetPropertyWithAttributes(PropertyIds::name, functionName, PropertyConfigurable, nullptr);
  36. }
  37. this->functionNameId = GetNativeFunctionDisplayString(scriptContext, scriptContext->GetPropertyString(TaggedInt::ToInt32(this->functionNameId)));
  38. }
  39. }
  40. Assert(JavascriptString::Is(this->functionNameId));
  41. return this->functionNameId;
  42. }
  43. void
  44. RuntimeFunction::SetFunctionNameId(Var nameId)
  45. {
  46. Assert(functionNameId == NULL);
  47. Assert(TaggedInt::Is(nameId) || Js::JavascriptString::Is(nameId));
  48. // We are only reference the propertyId, it needs to be tracked to stay alive
  49. Assert(!TaggedInt::Is(nameId) || this->GetScriptContext()->IsTrackedPropertyId(TaggedInt::ToInt32(nameId)));
  50. this->functionNameId = nameId;
  51. }
  52. #if ENABLE_TTD
  53. void RuntimeFunction::MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor)
  54. {
  55. if(this->functionNameId != nullptr)
  56. {
  57. extractor->MarkVisitVar(this->functionNameId);
  58. }
  59. Var revokableProxy = nullptr;
  60. RuntimeFunction* function = const_cast<RuntimeFunction*>(this);
  61. if(function->GetInternalProperty(function, Js::InternalPropertyIds::RevocableProxy, &revokableProxy, nullptr, this->GetScriptContext()))
  62. {
  63. extractor->MarkVisitVar(revokableProxy);
  64. }
  65. }
  66. TTD::NSSnapObjects::SnapObjectType RuntimeFunction::GetSnapTag_TTD() const
  67. {
  68. Var revokableProxy = nullptr;
  69. RuntimeFunction* function = const_cast<RuntimeFunction*>(this);
  70. if(function->GetInternalProperty(function, Js::InternalPropertyIds::RevocableProxy, &revokableProxy, nullptr, this->GetScriptContext()))
  71. {
  72. return TTD::NSSnapObjects::SnapObjectType::SnapRuntimeRevokerFunctionObject;
  73. }
  74. else
  75. {
  76. return TTD::NSSnapObjects::SnapObjectType::SnapRuntimeFunctionObject;
  77. }
  78. }
  79. void RuntimeFunction::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  80. {
  81. //
  82. //TODO: need to add more promise support
  83. //
  84. Var revokableProxy = nullptr;
  85. RuntimeFunction* function = const_cast<RuntimeFunction*>(this);
  86. if(function->GetInternalProperty(function, Js::InternalPropertyIds::RevocableProxy, &revokableProxy, nullptr, this->GetScriptContext()))
  87. {
  88. TTD_PTR_ID* proxyId = alloc.SlabAllocateStruct<TTD_PTR_ID>();
  89. *proxyId = (JavascriptOperators::GetTypeId(revokableProxy) != TypeIds_Null) ? TTD_CONVERT_VAR_TO_PTR_ID(revokableProxy) : TTD_INVALID_PTR_ID;
  90. if(*proxyId == TTD_INVALID_PTR_ID)
  91. {
  92. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD_PTR_ID*, TTD::NSSnapObjects::SnapObjectType::SnapRuntimeRevokerFunctionObject>(objData, proxyId);
  93. }
  94. else
  95. {
  96. TTDAssert(TTD::JsSupport::IsVarComplexKind(revokableProxy), "Huh, it looks like we need to check before adding this as a dep on.");
  97. uint32 depOnCount = 1;
  98. TTD_PTR_ID* depOnArray = alloc.SlabAllocateArray<TTD_PTR_ID>(1);
  99. depOnArray[0] = TTD_CONVERT_VAR_TO_PTR_ID(revokableProxy);
  100. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD_PTR_ID*, TTD::NSSnapObjects::SnapObjectType::SnapRuntimeRevokerFunctionObject>(objData, proxyId, alloc, depOnCount, depOnArray);
  101. }
  102. }
  103. else
  104. {
  105. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<void*, TTD::NSSnapObjects::SnapObjectType::SnapRuntimeFunctionObject>(objData, nullptr);
  106. }
  107. }
  108. #endif
  109. };