BoundFunction.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. FunctionInfo BoundFunction::functionInfo(FORCE_NO_WRITE_BARRIER_TAG(BoundFunction::NewInstance), FunctionInfo::DoNotProfile);
  9. BoundFunction::BoundFunction(DynamicType * type)
  10. : JavascriptFunction(type, &functionInfo),
  11. targetFunction(nullptr),
  12. boundThis(nullptr),
  13. count(0),
  14. boundArgs(nullptr)
  15. {
  16. // Constructor used during copy on write.
  17. DebugOnly(VerifyEntryPoint());
  18. }
  19. BoundFunction::BoundFunction(Arguments args, DynamicType * type)
  20. : JavascriptFunction(type, &functionInfo),
  21. count(0),
  22. boundArgs(nullptr)
  23. {
  24. DebugOnly(VerifyEntryPoint());
  25. AssertMsg(args.Info.Count > 0, "wrong number of args in BoundFunction");
  26. ScriptContext *scriptContext = this->GetScriptContext();
  27. targetFunction = RecyclableObject::FromVar(args[0]);
  28. Assert(!CrossSite::NeedMarshalVar(targetFunction, scriptContext));
  29. // Let proto be targetFunction.[[GetPrototypeOf]]().
  30. RecyclableObject* proto = JavascriptOperators::GetPrototype(targetFunction);
  31. if (proto != type->GetPrototype())
  32. {
  33. if (type->GetIsShared())
  34. {
  35. this->ChangeType();
  36. type = this->GetDynamicType();
  37. }
  38. type->SetPrototype(proto);
  39. }
  40. // If targetFunction is proxy, need to make sure that traps are called in right order as per 19.2.3.2 in RC#4 dated April 3rd 2015.
  41. // Here although we won't use value of length, this is just to make sure that we call traps involved with HasOwnProperty(Target, "length") and Get(Target, "length")
  42. if (JavascriptProxy::Is(targetFunction))
  43. {
  44. if (JavascriptOperators::HasOwnProperty(targetFunction, PropertyIds::length, scriptContext, nullptr) == TRUE)
  45. {
  46. int len = 0;
  47. Var varLength;
  48. if (targetFunction->GetProperty(targetFunction, PropertyIds::length, &varLength, nullptr, scriptContext))
  49. {
  50. len = JavascriptConversion::ToInt32(varLength, scriptContext);
  51. }
  52. }
  53. GetTypeHandler()->EnsureObjectReady(this);
  54. }
  55. if (args.Info.Count > 1)
  56. {
  57. boundThis = args[1];
  58. // function object and "this" arg
  59. const uint countAccountedFor = 2;
  60. count = args.Info.Count - countAccountedFor;
  61. // Store the args excluding function obj and "this" arg
  62. if (args.Info.Count > 2)
  63. {
  64. boundArgs = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), count);
  65. for (uint i=0; i<count; i++)
  66. {
  67. boundArgs[i] = args[i+countAccountedFor];
  68. }
  69. }
  70. }
  71. else
  72. {
  73. // If no "this" is passed, "undefined" is used
  74. boundThis = scriptContext->GetLibrary()->GetUndefined();
  75. }
  76. }
  77. BoundFunction::BoundFunction(RecyclableObject* targetFunction, Var boundThis, Var* args, uint argsCount, DynamicType * type)
  78. : JavascriptFunction(type, &functionInfo),
  79. count(argsCount),
  80. boundArgs(nullptr)
  81. {
  82. DebugOnly(VerifyEntryPoint());
  83. this->targetFunction = targetFunction;
  84. this->boundThis = boundThis;
  85. if (argsCount != 0)
  86. {
  87. this->boundArgs = RecyclerNewArray(this->GetScriptContext()->GetRecycler(), Field(Var), argsCount);
  88. for (uint i = 0; i < argsCount; i++)
  89. {
  90. this->boundArgs[i] = args[i];
  91. }
  92. }
  93. }
  94. BoundFunction* BoundFunction::New(ScriptContext* scriptContext, ArgumentReader args)
  95. {
  96. Recycler* recycler = scriptContext->GetRecycler();
  97. BoundFunction* boundFunc = RecyclerNew(recycler, BoundFunction, args,
  98. scriptContext->GetLibrary()->GetBoundFunctionType());
  99. return boundFunc;
  100. }
  101. Var BoundFunction::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  102. {
  103. RUNTIME_ARGUMENTS(args, callInfo);
  104. ScriptContext* scriptContext = function->GetScriptContext();
  105. if (args.Info.Count == 0)
  106. {
  107. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedFunction /* TODO-ERROR: get arg name - args[0] */);
  108. }
  109. BoundFunction *boundFunction = (BoundFunction *) function;
  110. RecyclableObject *targetFunction = boundFunction->targetFunction;
  111. //
  112. // var o = new boundFunction()
  113. // a new object should be created using the actual function object
  114. //
  115. Var newVarInstance = nullptr;
  116. if (callInfo.Flags & CallFlags_New)
  117. {
  118. if (args.HasNewTarget())
  119. {
  120. // target has an overriden new target make a new object from the newTarget
  121. Var newTargetVar = args.GetNewTarget();
  122. AssertOrFailFastMsg(JavascriptOperators::IsConstructor(newTargetVar), "newTarget must be a constructor");
  123. RecyclableObject* newTarget = RecyclableObject::UnsafeFromVar(newTargetVar);
  124. // Class constructors expect newTarget to be in args slot 0 (usually "this"),
  125. // because "this" is not constructed until we reach the most-super superclass.
  126. FunctionInfo* functionInfo = JavascriptOperators::GetConstructorFunctionInfo(targetFunction, scriptContext);
  127. if (functionInfo && functionInfo->IsClassConstructor())
  128. {
  129. args.Values[0] = newVarInstance = newTarget;
  130. }
  131. else
  132. {
  133. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  134. {
  135. args.Values[0] = newVarInstance = JavascriptOperators::CreateFromConstructor(newTarget, scriptContext);
  136. }
  137. END_SAFE_REENTRANT_CALL
  138. }
  139. }
  140. else if (!JavascriptProxy::Is(targetFunction))
  141. {
  142. // No new target and target is not a proxy can make a new object in a "normal" way.
  143. // NewScObjectNoCtor will either construct an object or return targetFunction depending
  144. // on whether targetFunction is a class constructor.
  145. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  146. {
  147. args.Values[0] = newVarInstance = JavascriptOperators::NewScObjectNoCtor(targetFunction, scriptContext);
  148. }
  149. END_SAFE_REENTRANT_CALL
  150. }
  151. else
  152. {
  153. // target is a proxy without an overriden new target
  154. // give nullptr - FunctionCallTrap will make a new object
  155. args.Values[0] = newVarInstance;
  156. }
  157. }
  158. Js::Arguments actualArgs = args;
  159. if (boundFunction->count > 0)
  160. {
  161. // OACR thinks that this can change between here and the check in the for loop below
  162. const unsigned int argCount = args.Info.Count;
  163. uint32 newArgCount = UInt32Math::Add(boundFunction->count, args.GetLargeArgCountWithExtraArgs());
  164. if (newArgCount > CallInfo::kMaxCountArgs)
  165. {
  166. JavascriptError::ThrowRangeError(scriptContext, JSERR_ArgListTooLarge);
  167. }
  168. Field(Var) *newValues = RecyclerNewArray(scriptContext->GetRecycler(), Field(Var), newArgCount);
  169. uint index = 0;
  170. //
  171. // For [[Construct]] use the newly created var instance
  172. // For [[Call]] use the "this" to which bind bound it.
  173. //
  174. if (callInfo.Flags & CallFlags_New)
  175. {
  176. newValues[index++] = args[0];
  177. }
  178. else
  179. {
  180. newValues[index++] = boundFunction->boundThis;
  181. }
  182. for (uint i = 0; i < boundFunction->count; i++)
  183. {
  184. newValues[index++] = boundFunction->boundArgs[i];
  185. }
  186. // Copy the extra args
  187. for (uint i=1; i<argCount; i++)
  188. {
  189. newValues[index++] = args[i];
  190. }
  191. if (args.HasExtraArg())
  192. {
  193. newValues[index++] = args.Values[argCount];
  194. }
  195. actualArgs = Arguments(args.Info, unsafe_write_barrier_cast<Var*>(newValues));
  196. actualArgs.Info.Count = boundFunction->count + argCount;
  197. Assert(index == actualArgs.GetLargeArgCountWithExtraArgs());
  198. }
  199. else
  200. {
  201. if (!(callInfo.Flags & CallFlags_New))
  202. {
  203. actualArgs.Values[0] = boundFunction->boundThis;
  204. }
  205. }
  206. Var aReturnValue = nullptr;
  207. BEGIN_SAFE_REENTRANT_CALL(scriptContext->GetThreadContext())
  208. {
  209. // Number of arguments are allowed to be more than Constants::MaxAllowedArgs in runtime. Need to use the larger argcount logic for this call.
  210. aReturnValue = JavascriptFunction::CallFunction<true>(targetFunction, targetFunction->GetEntryPoint(), actualArgs, /* useLargeArgCount */ true);
  211. }
  212. END_SAFE_REENTRANT_CALL
  213. //
  214. // [[Construct]] and call returned a non-object
  215. // return the newly created var instance
  216. //
  217. if ((callInfo.Flags & CallFlags_New) && !JavascriptOperators::IsObject(aReturnValue))
  218. {
  219. aReturnValue = newVarInstance;
  220. }
  221. return aReturnValue;
  222. }
  223. JavascriptFunction * BoundFunction::GetTargetFunction() const
  224. {
  225. if (targetFunction != nullptr)
  226. {
  227. RecyclableObject* _targetFunction = targetFunction;
  228. while (JavascriptProxy::Is(_targetFunction))
  229. {
  230. _targetFunction = JavascriptProxy::FromVar(_targetFunction)->GetTarget();
  231. }
  232. if (JavascriptFunction::Is(_targetFunction))
  233. {
  234. return JavascriptFunction::FromVar(_targetFunction);
  235. }
  236. // targetFunction should always be a JavascriptFunction.
  237. Assert(FALSE);
  238. }
  239. return nullptr;
  240. }
  241. JavascriptString* BoundFunction::GetDisplayNameImpl() const
  242. {
  243. JavascriptString* displayName = GetLibrary()->GetEmptyString();
  244. if (targetFunction != nullptr)
  245. {
  246. Var value = JavascriptOperators::GetPropertyNoCache(targetFunction, PropertyIds::name, targetFunction->GetScriptContext());
  247. if (JavascriptString::Is(value))
  248. {
  249. displayName = JavascriptString::FromVar(value);
  250. }
  251. }
  252. return LiteralString::Concat(LiteralString::NewCopySz(_u("bound "), this->GetScriptContext()), displayName);
  253. }
  254. RecyclableObject* BoundFunction::GetBoundThis()
  255. {
  256. if (boundThis != nullptr && RecyclableObject::Is(boundThis))
  257. {
  258. return RecyclableObject::FromVar(boundThis);
  259. }
  260. return NULL;
  261. }
  262. inline BOOL BoundFunction::IsConstructor() const
  263. {
  264. if (this->targetFunction != nullptr)
  265. {
  266. return JavascriptOperators::IsConstructor(this->GetTargetFunction());
  267. }
  268. return false;
  269. }
  270. PropertyQueryFlags BoundFunction::HasPropertyQuery(PropertyId propertyId, _Inout_opt_ PropertyValueInfo* info)
  271. {
  272. if (propertyId == PropertyIds::length)
  273. {
  274. return PropertyQueryFlags::Property_Found;
  275. }
  276. return JavascriptFunction::HasPropertyQuery(propertyId, info);
  277. }
  278. PropertyQueryFlags BoundFunction::GetPropertyQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  279. {
  280. BOOL result;
  281. if (GetPropertyBuiltIns(originalInstance, propertyId, value, info, requestContext, &result))
  282. {
  283. return JavascriptConversion::BooleanToPropertyQueryFlags(result);
  284. }
  285. return JavascriptFunction::GetPropertyQuery(originalInstance, propertyId, value, info, requestContext);
  286. }
  287. PropertyQueryFlags BoundFunction::GetPropertyQuery(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  288. {
  289. BOOL result;
  290. PropertyRecord const* propertyRecord;
  291. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  292. if (propertyRecord != nullptr && GetPropertyBuiltIns(originalInstance, propertyRecord->GetPropertyId(), value, info, requestContext, &result))
  293. {
  294. return JavascriptConversion::BooleanToPropertyQueryFlags(result);
  295. }
  296. return JavascriptFunction::GetPropertyQuery(originalInstance, propertyNameString, value, info, requestContext);
  297. }
  298. bool BoundFunction::GetPropertyBuiltIns(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext, BOOL* result)
  299. {
  300. if (propertyId == PropertyIds::length)
  301. {
  302. // Get the "length" property of the underlying target function
  303. int len = 0;
  304. Var varLength;
  305. if (targetFunction->GetProperty(targetFunction, PropertyIds::length, &varLength, nullptr, requestContext))
  306. {
  307. len = JavascriptConversion::ToInt32(varLength, requestContext);
  308. }
  309. // Reduce by number of bound args
  310. len = len - this->count;
  311. len = max(len, 0);
  312. *value = JavascriptNumber::ToVar(len, requestContext);
  313. *result = true;
  314. return true;
  315. }
  316. return false;
  317. }
  318. PropertyQueryFlags BoundFunction::GetPropertyReferenceQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  319. {
  320. return BoundFunction::GetPropertyQuery(originalInstance, propertyId, value, info, requestContext);
  321. }
  322. BOOL BoundFunction::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  323. {
  324. BOOL result;
  325. if (SetPropertyBuiltIns(propertyId, value, flags, info, &result))
  326. {
  327. return result;
  328. }
  329. return JavascriptFunction::SetProperty(propertyId, value, flags, info);
  330. }
  331. BOOL BoundFunction::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  332. {
  333. BOOL result;
  334. PropertyRecord const* propertyRecord;
  335. this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
  336. if (propertyRecord != nullptr && SetPropertyBuiltIns(propertyRecord->GetPropertyId(), value, flags, info, &result))
  337. {
  338. return result;
  339. }
  340. return JavascriptFunction::SetProperty(propertyNameString, value, flags, info);
  341. }
  342. bool BoundFunction::SetPropertyBuiltIns(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info, BOOL* result)
  343. {
  344. if (propertyId == PropertyIds::length)
  345. {
  346. JavascriptError::ThrowCantAssignIfStrictMode(flags, this->GetScriptContext());
  347. *result = false;
  348. return true;
  349. }
  350. return false;
  351. }
  352. _Check_return_ _Success_(return) BOOL BoundFunction::GetAccessors(PropertyId propertyId, _Outptr_result_maybenull_ Var* getter, _Outptr_result_maybenull_ Var* setter, ScriptContext* requestContext)
  353. {
  354. return DynamicObject::GetAccessors(propertyId, getter, setter, requestContext);
  355. }
  356. DescriptorFlags BoundFunction::GetSetter(PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  357. {
  358. return DynamicObject::GetSetter(propertyId, setterValue, info, requestContext);
  359. }
  360. DescriptorFlags BoundFunction::GetSetter(JavascriptString* propertyNameString, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  361. {
  362. return DynamicObject::GetSetter(propertyNameString, setterValue, info, requestContext);
  363. }
  364. BOOL BoundFunction::InitProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  365. {
  366. return SetProperty(propertyId, value, PropertyOperation_None, info);
  367. }
  368. BOOL BoundFunction::DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags)
  369. {
  370. if (propertyId == PropertyIds::length)
  371. {
  372. return false;
  373. }
  374. return JavascriptFunction::DeleteProperty(propertyId, flags);
  375. }
  376. BOOL BoundFunction::DeleteProperty(JavascriptString *propertyNameString, PropertyOperationFlags flags)
  377. {
  378. if (BuiltInPropertyRecords::length.Equals(propertyNameString))
  379. {
  380. return false;
  381. }
  382. return JavascriptFunction::DeleteProperty(propertyNameString, flags);
  383. }
  384. BOOL BoundFunction::IsWritable(PropertyId propertyId)
  385. {
  386. if (propertyId == PropertyIds::length)
  387. {
  388. return false;
  389. }
  390. return JavascriptFunction::IsWritable(propertyId);
  391. }
  392. BOOL BoundFunction::IsConfigurable(PropertyId propertyId)
  393. {
  394. if (propertyId == PropertyIds::length)
  395. {
  396. return false;
  397. }
  398. return JavascriptFunction::IsConfigurable(propertyId);
  399. }
  400. BOOL BoundFunction::IsEnumerable(PropertyId propertyId)
  401. {
  402. if (propertyId == PropertyIds::length)
  403. {
  404. return false;
  405. }
  406. return JavascriptFunction::IsEnumerable(propertyId);
  407. }
  408. BOOL BoundFunction::HasInstance(Var instance, ScriptContext* scriptContext, IsInstInlineCache* inlineCache)
  409. {
  410. return this->targetFunction->HasInstance(instance, scriptContext, inlineCache);
  411. }
  412. #if ENABLE_TTD
  413. void BoundFunction::MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor)
  414. {
  415. extractor->MarkVisitVar(this->targetFunction);
  416. if(this->boundThis != nullptr)
  417. {
  418. extractor->MarkVisitVar(this->boundThis);
  419. }
  420. for(uint32 i = 0; i < this->count; ++i)
  421. {
  422. extractor->MarkVisitVar(this->boundArgs[i]);
  423. }
  424. }
  425. void BoundFunction::ProcessCorePaths()
  426. {
  427. this->GetScriptContext()->TTDWellKnownInfo->EnqueueNewPathVarAsNeeded(this, this->targetFunction, _u("!targetFunction"));
  428. this->GetScriptContext()->TTDWellKnownInfo->EnqueueNewPathVarAsNeeded(this, this->boundThis, _u("!boundThis"));
  429. TTDAssert(this->count == 0, "Should only have empty args in core image");
  430. }
  431. TTD::NSSnapObjects::SnapObjectType BoundFunction::GetSnapTag_TTD() const
  432. {
  433. return TTD::NSSnapObjects::SnapObjectType::SnapBoundFunctionObject;
  434. }
  435. void BoundFunction::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
  436. {
  437. TTD::NSSnapObjects::SnapBoundFunctionInfo* bfi = alloc.SlabAllocateStruct<TTD::NSSnapObjects::SnapBoundFunctionInfo>();
  438. bfi->TargetFunction = TTD_CONVERT_VAR_TO_PTR_ID(static_cast<RecyclableObject*>(this->targetFunction));
  439. bfi->BoundThis = (this->boundThis != nullptr) ?
  440. TTD_CONVERT_VAR_TO_PTR_ID(static_cast<Var>(this->boundThis)) : TTD_INVALID_PTR_ID;
  441. bfi->ArgCount = this->count;
  442. bfi->ArgArray = nullptr;
  443. if(bfi->ArgCount > 0)
  444. {
  445. bfi->ArgArray = alloc.SlabAllocateArray<TTD::TTDVar>(bfi->ArgCount);
  446. }
  447. TTD_PTR_ID* depArray = alloc.SlabReserveArraySpace<TTD_PTR_ID>(bfi->ArgCount + 2 /*this and bound function*/);
  448. depArray[0] = bfi->TargetFunction;
  449. uint32 depCount = 1;
  450. if(this->boundThis != nullptr && TTD::JsSupport::IsVarComplexKind(this->boundThis))
  451. {
  452. depArray[depCount] = bfi->BoundThis;
  453. depCount++;
  454. }
  455. if(bfi->ArgCount > 0)
  456. {
  457. for(uint32 i = 0; i < bfi->ArgCount; ++i)
  458. {
  459. bfi->ArgArray[i] = this->boundArgs[i];
  460. //Primitive kinds always inflated first so we only need to deal with complex kinds as depends on
  461. if(TTD::JsSupport::IsVarComplexKind(this->boundArgs[i]))
  462. {
  463. depArray[depCount] = TTD_CONVERT_VAR_TO_PTR_ID(this->boundArgs[i]);
  464. depCount++;
  465. }
  466. }
  467. }
  468. alloc.SlabCommitArraySpace<TTD_PTR_ID>(depCount, depCount + bfi->ArgCount);
  469. TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapBoundFunctionInfo*, TTD::NSSnapObjects::SnapObjectType::SnapBoundFunctionObject>(objData, bfi, alloc, depCount, depArray);
  470. }
  471. BoundFunction* BoundFunction::InflateBoundFunction(
  472. ScriptContext* ctx, RecyclableObject* function, Var bThis, uint32 ct, Field(Var)* args)
  473. {
  474. BoundFunction* res = RecyclerNew(ctx->GetRecycler(), BoundFunction, ctx->GetLibrary()->GetBoundFunctionType());
  475. res->boundThis = bThis;
  476. res->count = ct;
  477. res->boundArgs = args;
  478. res->targetFunction = function;
  479. return res;
  480. }
  481. #endif
  482. } // namespace Js