jsfriendapi.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "jsfriendapi.h"
  6. #include "mozilla/PodOperations.h"
  7. #include <stdint.h>
  8. #include "jscntxt.h"
  9. #include "jscompartment.h"
  10. #include "jsgc.h"
  11. #include "jsobj.h"
  12. #include "jsprf.h"
  13. #include "jsweakmap.h"
  14. #include "jswrapper.h"
  15. #include "builtin/Promise.h"
  16. #include "builtin/TestingFunctions.h"
  17. #include "js/Proxy.h"
  18. #include "proxy/DeadObjectProxy.h"
  19. #include "vm/ArgumentsObject.h"
  20. #include "vm/Time.h"
  21. #include "vm/WrapperObject.h"
  22. #include "jsobjinlines.h"
  23. #include "jsscriptinlines.h"
  24. #include "vm/EnvironmentObject-inl.h"
  25. #include "vm/NativeObject-inl.h"
  26. using namespace js;
  27. using mozilla::Move;
  28. using mozilla::PodArrayZero;
  29. js::ContextFriendFields::ContextFriendFields(bool isJSContext)
  30. : JS::RootingContext(isJSContext),
  31. compartment_(nullptr), zone_(nullptr)
  32. {
  33. PodArrayZero(nativeStackLimit);
  34. #if JS_STACK_GROWTH_DIRECTION > 0
  35. for (int i=0; i<StackKindCount; i++)
  36. nativeStackLimit[i] = UINTPTR_MAX;
  37. #endif
  38. }
  39. JS_FRIEND_API(void)
  40. js::SetSourceHook(JSContext* cx, mozilla::UniquePtr<SourceHook> hook)
  41. {
  42. cx->sourceHook = Move(hook);
  43. }
  44. JS_FRIEND_API(mozilla::UniquePtr<SourceHook>)
  45. js::ForgetSourceHook(JSContext* cx)
  46. {
  47. return Move(cx->sourceHook);
  48. }
  49. JS_FRIEND_API(void)
  50. JS_SetGrayGCRootsTracer(JSContext* cx, JSTraceDataOp traceOp, void* data)
  51. {
  52. cx->gc.setGrayRootsTracer(traceOp, data);
  53. }
  54. JS_FRIEND_API(JSObject*)
  55. JS_FindCompilationScope(JSContext* cx, HandleObject objArg)
  56. {
  57. RootedObject obj(cx, objArg);
  58. /*
  59. * We unwrap wrappers here. This is a little weird, but it's what's being
  60. * asked of us.
  61. */
  62. if (obj->is<WrapperObject>())
  63. obj = UncheckedUnwrap(obj);
  64. /*
  65. * Get the Window if `obj` is a WindowProxy so that we compile in the
  66. * correct (global) scope.
  67. */
  68. return ToWindowIfWindowProxy(obj);
  69. }
  70. JS_FRIEND_API(JSFunction*)
  71. JS_GetObjectFunction(JSObject* obj)
  72. {
  73. if (obj->is<JSFunction>())
  74. return &obj->as<JSFunction>();
  75. return nullptr;
  76. }
  77. JS_FRIEND_API(bool)
  78. JS_SplicePrototype(JSContext* cx, HandleObject obj, HandleObject proto)
  79. {
  80. /*
  81. * Change the prototype of an object which hasn't been used anywhere
  82. * and does not share its type with another object. Unlike JS_SetPrototype,
  83. * does not nuke type information for the object.
  84. */
  85. CHECK_REQUEST(cx);
  86. if (!obj->isSingleton()) {
  87. /*
  88. * We can see non-singleton objects when trying to splice prototypes
  89. * due to mutable __proto__ (ugh).
  90. */
  91. return JS_SetPrototype(cx, obj, proto);
  92. }
  93. Rooted<TaggedProto> tagged(cx, TaggedProto(proto));
  94. return JSObject::splicePrototype(cx, obj, obj->getClass(), tagged);
  95. }
  96. JS_FRIEND_API(JSObject*)
  97. JS_NewObjectWithUniqueType(JSContext* cx, const JSClass* clasp, HandleObject proto)
  98. {
  99. /*
  100. * Create our object with a null proto and then splice in the correct proto
  101. * after we setSingleton, so that we don't pollute the default
  102. * ObjectGroup attached to our proto with information about our object, since
  103. * we're not going to be using that ObjectGroup anyway.
  104. */
  105. RootedObject obj(cx, NewObjectWithGivenProto(cx, (const js::Class*)clasp, nullptr,
  106. SingletonObject));
  107. if (!obj)
  108. return nullptr;
  109. if (!JS_SplicePrototype(cx, obj, proto))
  110. return nullptr;
  111. return obj;
  112. }
  113. JS_FRIEND_API(JSObject*)
  114. JS_NewObjectWithoutMetadata(JSContext* cx, const JSClass* clasp, JS::Handle<JSObject*> proto)
  115. {
  116. AutoSuppressAllocationMetadataBuilder suppressMetadata(cx);
  117. return JS_NewObjectWithGivenProto(cx, clasp, proto);
  118. }
  119. JS_FRIEND_API(bool)
  120. JS_GetIsSecureContext(JSCompartment* compartment)
  121. {
  122. return compartment->creationOptions().secureContext();
  123. }
  124. JS_FRIEND_API(JSPrincipals*)
  125. JS_GetCompartmentPrincipals(JSCompartment* compartment)
  126. {
  127. return compartment->principals();
  128. }
  129. JS_FRIEND_API(void)
  130. JS_SetCompartmentPrincipals(JSCompartment* compartment, JSPrincipals* principals)
  131. {
  132. // Short circuit if there's no change.
  133. if (principals == compartment->principals())
  134. return;
  135. // Any compartment with the trusted principals -- and there can be
  136. // multiple -- is a system compartment.
  137. const JSPrincipals* trusted = compartment->runtimeFromMainThread()->trustedPrincipals();
  138. bool isSystem = principals && principals == trusted;
  139. // Clear out the old principals, if any.
  140. if (compartment->principals()) {
  141. JS_DropPrincipals(compartment->contextFromMainThread(), compartment->principals());
  142. compartment->setPrincipals(nullptr);
  143. // We'd like to assert that our new principals is always same-origin
  144. // with the old one, but JSPrincipals doesn't give us a way to do that.
  145. // But we can at least assert that we're not switching between system
  146. // and non-system.
  147. MOZ_ASSERT(compartment->isSystem() == isSystem);
  148. }
  149. // Set up the new principals.
  150. if (principals) {
  151. JS_HoldPrincipals(principals);
  152. compartment->setPrincipals(principals);
  153. }
  154. // Update the system flag.
  155. compartment->setIsSystem(isSystem);
  156. }
  157. JS_FRIEND_API(JSPrincipals*)
  158. JS_GetScriptPrincipals(JSScript* script)
  159. {
  160. return script->principals();
  161. }
  162. JS_FRIEND_API(bool)
  163. JS_ScriptHasMutedErrors(JSScript* script)
  164. {
  165. return script->mutedErrors();
  166. }
  167. JS_FRIEND_API(bool)
  168. JS_WrapPropertyDescriptor(JSContext* cx, JS::MutableHandle<js::PropertyDescriptor> desc)
  169. {
  170. return cx->compartment()->wrap(cx, desc);
  171. }
  172. JS_FRIEND_API(void)
  173. JS_TraceShapeCycleCollectorChildren(JS::CallbackTracer* trc, JS::GCCellPtr shape)
  174. {
  175. MOZ_ASSERT(shape.is<Shape>());
  176. TraceCycleCollectorChildren(trc, &shape.as<Shape>());
  177. }
  178. JS_FRIEND_API(void)
  179. JS_TraceObjectGroupCycleCollectorChildren(JS::CallbackTracer* trc, JS::GCCellPtr group)
  180. {
  181. MOZ_ASSERT(group.is<ObjectGroup>());
  182. TraceCycleCollectorChildren(trc, &group.as<ObjectGroup>());
  183. }
  184. static bool
  185. DefineHelpProperty(JSContext* cx, HandleObject obj, const char* prop, const char* value)
  186. {
  187. RootedAtom atom(cx, Atomize(cx, value, strlen(value)));
  188. if (!atom)
  189. return false;
  190. return JS_DefineProperty(cx, obj, prop, atom,
  191. JSPROP_READONLY | JSPROP_PERMANENT,
  192. JS_STUBGETTER, JS_STUBSETTER);
  193. }
  194. JS_FRIEND_API(bool)
  195. JS_DefineFunctionsWithHelp(JSContext* cx, HandleObject obj, const JSFunctionSpecWithHelp* fs)
  196. {
  197. MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
  198. CHECK_REQUEST(cx);
  199. assertSameCompartment(cx, obj);
  200. for (; fs->name; fs++) {
  201. JSAtom* atom = Atomize(cx, fs->name, strlen(fs->name));
  202. if (!atom)
  203. return false;
  204. Rooted<jsid> id(cx, AtomToId(atom));
  205. RootedFunction fun(cx, DefineFunction(cx, obj, id, fs->call, fs->nargs, fs->flags));
  206. if (!fun)
  207. return false;
  208. if (fs->jitInfo)
  209. fun->setJitInfo(fs->jitInfo);
  210. if (fs->usage) {
  211. if (!DefineHelpProperty(cx, fun, "usage", fs->usage))
  212. return false;
  213. }
  214. if (fs->help) {
  215. if (!DefineHelpProperty(cx, fun, "help", fs->help))
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. JS_FRIEND_API(bool)
  222. js::GetBuiltinClass(JSContext* cx, HandleObject obj, ESClass* cls)
  223. {
  224. if (MOZ_UNLIKELY(obj->is<ProxyObject>()))
  225. return Proxy::getBuiltinClass(cx, obj, cls);
  226. if (obj->is<PlainObject>() || obj->is<UnboxedPlainObject>())
  227. *cls = ESClass::Object;
  228. else if (obj->is<ArrayObject>() || obj->is<UnboxedArrayObject>())
  229. *cls = ESClass::Array;
  230. else if (obj->is<NumberObject>())
  231. *cls = ESClass::Number;
  232. else if (obj->is<StringObject>())
  233. *cls = ESClass::String;
  234. else if (obj->is<BooleanObject>())
  235. *cls = ESClass::Boolean;
  236. else if (obj->is<RegExpObject>())
  237. *cls = ESClass::RegExp;
  238. else if (obj->is<ArrayBufferObject>())
  239. *cls = ESClass::ArrayBuffer;
  240. else if (obj->is<SharedArrayBufferObject>())
  241. *cls = ESClass::SharedArrayBuffer;
  242. else if (obj->is<DateObject>())
  243. *cls = ESClass::Date;
  244. else if (obj->is<SetObject>())
  245. *cls = ESClass::Set;
  246. else if (obj->is<MapObject>())
  247. *cls = ESClass::Map;
  248. else if (obj->is<PromiseObject>())
  249. *cls = ESClass::Promise;
  250. else if (obj->is<MapIteratorObject>())
  251. *cls = ESClass::MapIterator;
  252. else if (obj->is<SetIteratorObject>())
  253. *cls = ESClass::SetIterator;
  254. else if (obj->is<ArgumentsObject>())
  255. *cls = ESClass::Arguments;
  256. else if (obj->is<ErrorObject>())
  257. *cls = ESClass::Error;
  258. else
  259. *cls = ESClass::Other;
  260. return true;
  261. }
  262. JS_FRIEND_API(const char*)
  263. js::ObjectClassName(JSContext* cx, HandleObject obj)
  264. {
  265. return GetObjectClassName(cx, obj);
  266. }
  267. JS_FRIEND_API(JS::Zone*)
  268. js::GetCompartmentZone(JSCompartment* comp)
  269. {
  270. return comp->zone();
  271. }
  272. JS_FRIEND_API(bool)
  273. js::IsSystemCompartment(JSCompartment* comp)
  274. {
  275. return comp->isSystem();
  276. }
  277. JS_FRIEND_API(bool)
  278. js::IsSystemZone(Zone* zone)
  279. {
  280. return zone->isSystem;
  281. }
  282. JS_FRIEND_API(bool)
  283. js::IsAtomsCompartment(JSCompartment* comp)
  284. {
  285. return comp->runtimeFromAnyThread()->isAtomsCompartment(comp);
  286. }
  287. JS_FRIEND_API(bool)
  288. js::IsAtomsZone(JS::Zone* zone)
  289. {
  290. return zone->runtimeFromAnyThread()->isAtomsZone(zone);
  291. }
  292. JS_FRIEND_API(bool)
  293. js::IsFunctionObject(JSObject* obj)
  294. {
  295. return obj->is<JSFunction>();
  296. }
  297. JS_FRIEND_API(JSObject*)
  298. js::GetGlobalForObjectCrossCompartment(JSObject* obj)
  299. {
  300. return &obj->global();
  301. }
  302. JS_FRIEND_API(JSObject*)
  303. js::GetPrototypeNoProxy(JSObject* obj)
  304. {
  305. MOZ_ASSERT(!obj->is<js::ProxyObject>());
  306. return obj->staticPrototype();
  307. }
  308. JS_FRIEND_API(void)
  309. js::AssertSameCompartment(JSContext* cx, JSObject* obj)
  310. {
  311. assertSameCompartment(cx, obj);
  312. }
  313. #ifdef DEBUG
  314. JS_FRIEND_API(void)
  315. js::AssertSameCompartment(JSObject* objA, JSObject* objB)
  316. {
  317. MOZ_ASSERT(objA->compartment() == objB->compartment());
  318. }
  319. #endif
  320. JS_FRIEND_API(void)
  321. js::NotifyAnimationActivity(JSObject* obj)
  322. {
  323. int64_t timeNow = PRMJ_Now();
  324. obj->compartment()->lastAnimationTime = timeNow;
  325. obj->runtimeFromMainThread()->lastAnimationTime = timeNow;
  326. }
  327. JS_FRIEND_API(uint32_t)
  328. js::GetObjectSlotSpan(JSObject* obj)
  329. {
  330. return obj->as<NativeObject>().slotSpan();
  331. }
  332. JS_FRIEND_API(bool)
  333. js::IsObjectInContextCompartment(JSObject* obj, const JSContext* cx)
  334. {
  335. return obj->compartment() == cx->compartment();
  336. }
  337. JS_FRIEND_API(bool)
  338. js::RunningWithTrustedPrincipals(JSContext* cx)
  339. {
  340. return cx->runningWithTrustedPrincipals();
  341. }
  342. JS_FRIEND_API(JSFunction*)
  343. js::GetOutermostEnclosingFunctionOfScriptedCaller(JSContext* cx)
  344. {
  345. ScriptFrameIter iter(cx);
  346. // Skip eval frames.
  347. while (!iter.done() && iter.isEvalFrame())
  348. ++iter;
  349. if (iter.done())
  350. return nullptr;
  351. if (!iter.isFunctionFrame())
  352. return nullptr;
  353. if (iter.compartment() != cx->compartment())
  354. return nullptr;
  355. RootedFunction curr(cx, iter.callee(cx));
  356. for (ScopeIter si(curr->nonLazyScript()); si; si++) {
  357. if (si.kind() == ScopeKind::Function)
  358. curr = si.scope()->as<FunctionScope>().canonicalFunction();
  359. }
  360. assertSameCompartment(cx, curr);
  361. return curr;
  362. }
  363. JS_FRIEND_API(JSFunction*)
  364. js::DefineFunctionWithReserved(JSContext* cx, JSObject* objArg, const char* name, JSNative call,
  365. unsigned nargs, unsigned attrs)
  366. {
  367. RootedObject obj(cx, objArg);
  368. MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
  369. CHECK_REQUEST(cx);
  370. assertSameCompartment(cx, obj);
  371. JSAtom* atom = Atomize(cx, name, strlen(name));
  372. if (!atom)
  373. return nullptr;
  374. Rooted<jsid> id(cx, AtomToId(atom));
  375. return DefineFunction(cx, obj, id, call, nargs, attrs, gc::AllocKind::FUNCTION_EXTENDED);
  376. }
  377. JS_FRIEND_API(JSFunction*)
  378. js::NewFunctionWithReserved(JSContext* cx, JSNative native, unsigned nargs, unsigned flags,
  379. const char* name)
  380. {
  381. MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
  382. CHECK_REQUEST(cx);
  383. RootedAtom atom(cx);
  384. if (name) {
  385. atom = Atomize(cx, name, strlen(name));
  386. if (!atom)
  387. return nullptr;
  388. }
  389. return (flags & JSFUN_CONSTRUCTOR) ?
  390. NewNativeConstructor(cx, native, nargs, atom, gc::AllocKind::FUNCTION_EXTENDED) :
  391. NewNativeFunction(cx, native, nargs, atom, gc::AllocKind::FUNCTION_EXTENDED);
  392. }
  393. JS_FRIEND_API(JSFunction*)
  394. js::NewFunctionByIdWithReserved(JSContext* cx, JSNative native, unsigned nargs, unsigned flags,
  395. jsid id)
  396. {
  397. MOZ_ASSERT(JSID_IS_STRING(id));
  398. MOZ_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
  399. CHECK_REQUEST(cx);
  400. RootedAtom atom(cx, JSID_TO_ATOM(id));
  401. return (flags & JSFUN_CONSTRUCTOR) ?
  402. NewNativeConstructor(cx, native, nargs, atom, gc::AllocKind::FUNCTION_EXTENDED) :
  403. NewNativeFunction(cx, native, nargs, atom, gc::AllocKind::FUNCTION_EXTENDED);
  404. }
  405. JS_FRIEND_API(const Value&)
  406. js::GetFunctionNativeReserved(JSObject* fun, size_t which)
  407. {
  408. MOZ_ASSERT(fun->as<JSFunction>().isNative());
  409. return fun->as<JSFunction>().getExtendedSlot(which);
  410. }
  411. JS_FRIEND_API(void)
  412. js::SetFunctionNativeReserved(JSObject* fun, size_t which, const Value& val)
  413. {
  414. MOZ_ASSERT(fun->as<JSFunction>().isNative());
  415. MOZ_ASSERT_IF(val.isObject(), val.toObject().compartment() == fun->compartment());
  416. fun->as<JSFunction>().setExtendedSlot(which, val);
  417. }
  418. JS_FRIEND_API(bool)
  419. js::FunctionHasNativeReserved(JSObject* fun)
  420. {
  421. MOZ_ASSERT(fun->as<JSFunction>().isNative());
  422. return fun->as<JSFunction>().isExtended();
  423. }
  424. JS_FRIEND_API(bool)
  425. js::GetObjectProto(JSContext* cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JSObject*> proto)
  426. {
  427. if (IsProxy(obj))
  428. return JS_GetPrototype(cx, obj, proto);
  429. proto.set(reinterpret_cast<const shadow::Object*>(obj.get())->group->proto);
  430. return true;
  431. }
  432. JS_FRIEND_API(JSObject*)
  433. js::GetStaticPrototype(JSObject* obj)
  434. {
  435. MOZ_ASSERT(obj->hasStaticPrototype());
  436. return obj->staticPrototype();
  437. }
  438. JS_FRIEND_API(bool)
  439. js::GetOriginalEval(JSContext* cx, HandleObject scope, MutableHandleObject eval)
  440. {
  441. assertSameCompartment(cx, scope);
  442. Rooted<GlobalObject*> global(cx, &scope->global());
  443. return GlobalObject::getOrCreateEval(cx, global, eval);
  444. }
  445. JS_FRIEND_API(void)
  446. js::SetReservedOrProxyPrivateSlotWithBarrier(JSObject* obj, size_t slot, const js::Value& value)
  447. {
  448. if (IsProxy(obj)) {
  449. MOZ_ASSERT(slot == 0);
  450. obj->as<ProxyObject>().setSameCompartmentPrivate(value);
  451. } else {
  452. obj->as<NativeObject>().setSlot(slot, value);
  453. }
  454. }
  455. void
  456. js::SetPreserveWrapperCallback(JSContext* cx, PreserveWrapperCallback callback)
  457. {
  458. cx->preserveWrapperCallback = callback;
  459. }
  460. namespace js {
  461. // Defined in vm/GlobalObject.cpp.
  462. extern size_t sSetProtoCalled;
  463. } // namespace js
  464. JS_FRIEND_API(size_t)
  465. JS_SetProtoCalled(JSContext*)
  466. {
  467. return sSetProtoCalled;
  468. }
  469. // Defined in jsiter.cpp.
  470. extern size_t sCustomIteratorCount;
  471. JS_FRIEND_API(size_t)
  472. JS_GetCustomIteratorCount(JSContext* cx)
  473. {
  474. return sCustomIteratorCount;
  475. }
  476. JS_FRIEND_API(unsigned)
  477. JS_PCToLineNumber(JSScript* script, jsbytecode* pc, unsigned* columnp)
  478. {
  479. return PCToLineNumber(script, pc, columnp);
  480. }
  481. JS_FRIEND_API(bool)
  482. JS_IsDeadWrapper(JSObject* obj)
  483. {
  484. return IsDeadProxyObject(obj);
  485. }
  486. void
  487. js::TraceWeakMaps(WeakMapTracer* trc)
  488. {
  489. WeakMapBase::traceAllMappings(trc);
  490. }
  491. extern JS_FRIEND_API(bool)
  492. js::AreGCGrayBitsValid(JSContext* cx)
  493. {
  494. return cx->areGCGrayBitsValid();
  495. }
  496. JS_FRIEND_API(bool)
  497. js::ZoneGlobalsAreAllGray(JS::Zone* zone)
  498. {
  499. for (CompartmentsInZoneIter comp(zone); !comp.done(); comp.next()) {
  500. JSObject* obj = comp->unsafeUnbarrieredMaybeGlobal();
  501. if (!obj || !JS::ObjectIsMarkedGray(obj))
  502. return false;
  503. }
  504. return true;
  505. }
  506. namespace {
  507. struct VisitGrayCallbackFunctor {
  508. GCThingCallback callback_;
  509. void* closure_;
  510. VisitGrayCallbackFunctor(GCThingCallback callback, void* closure)
  511. : callback_(callback), closure_(closure)
  512. {}
  513. template <class T>
  514. void operator()(T tp) const {
  515. if ((*tp)->isTenured() && (*tp)->asTenured().isMarked(gc::GRAY))
  516. callback_(closure_, JS::GCCellPtr(*tp));
  517. }
  518. };
  519. } // namespace (anonymous)
  520. JS_FRIEND_API(void)
  521. js::VisitGrayWrapperTargets(Zone* zone, GCThingCallback callback, void* closure)
  522. {
  523. for (CompartmentsInZoneIter comp(zone); !comp.done(); comp.next()) {
  524. for (JSCompartment::WrapperEnum e(comp); !e.empty(); e.popFront())
  525. e.front().mutableKey().applyToWrapped(VisitGrayCallbackFunctor(callback, closure));
  526. }
  527. }
  528. JS_FRIEND_API(JSObject*)
  529. js::GetWeakmapKeyDelegate(JSObject* key)
  530. {
  531. if (JSWeakmapKeyDelegateOp op = key->getClass()->extWeakmapKeyDelegateOp())
  532. return op(key);
  533. return nullptr;
  534. }
  535. JS_FRIEND_API(JSLinearString*)
  536. js::StringToLinearStringSlow(JSContext* cx, JSString* str)
  537. {
  538. return str->ensureLinear(cx);
  539. }
  540. JS_FRIEND_API(JSObject*)
  541. JS_CloneObject(JSContext* cx, HandleObject obj, HandleObject protoArg)
  542. {
  543. Rooted<TaggedProto> proto(cx, TaggedProto(protoArg.get()));
  544. return CloneObject(cx, obj, proto);
  545. }
  546. #ifdef DEBUG
  547. JS_FRIEND_API(void)
  548. js::DumpString(JSString* str, FILE* fp)
  549. {
  550. str->dump(fp);
  551. }
  552. JS_FRIEND_API(void)
  553. js::DumpAtom(JSAtom* atom, FILE* fp)
  554. {
  555. atom->dump(fp);
  556. }
  557. JS_FRIEND_API(void)
  558. js::DumpChars(const char16_t* s, size_t n, FILE* fp)
  559. {
  560. fprintf(fp, "char16_t * (%p) = ", (void*) s);
  561. JSString::dumpChars(s, n, fp);
  562. fputc('\n', fp);
  563. }
  564. JS_FRIEND_API(void)
  565. js::DumpObject(JSObject* obj, FILE* fp)
  566. {
  567. if (!obj) {
  568. fprintf(fp, "NULL\n");
  569. return;
  570. }
  571. obj->dump(fp);
  572. }
  573. JS_FRIEND_API(void)
  574. js::DumpString(JSString* str) {
  575. DumpString(str, stderr);
  576. }
  577. JS_FRIEND_API(void)
  578. js::DumpAtom(JSAtom* atom) {
  579. DumpAtom(atom, stderr);
  580. }
  581. JS_FRIEND_API(void)
  582. js::DumpObject(JSObject* obj) {
  583. DumpObject(obj, stderr);
  584. }
  585. JS_FRIEND_API(void)
  586. js::DumpChars(const char16_t* s, size_t n) {
  587. DumpChars(s, n, stderr);
  588. }
  589. JS_FRIEND_API(void)
  590. js::DumpValue(const JS::Value& val) {
  591. DumpValue(val, stderr);
  592. }
  593. JS_FRIEND_API(void)
  594. js::DumpId(jsid id) {
  595. DumpId(id, stderr);
  596. }
  597. JS_FRIEND_API(void)
  598. js::DumpInterpreterFrame(JSContext* cx, InterpreterFrame* start)
  599. {
  600. DumpInterpreterFrame(cx, stderr, start);
  601. }
  602. JS_FRIEND_API(bool)
  603. js::DumpPC(JSContext* cx) {
  604. return DumpPC(cx, stdout);
  605. }
  606. JS_FRIEND_API(bool)
  607. js::DumpScript(JSContext* cx, JSScript* scriptArg)
  608. {
  609. return DumpScript(cx, scriptArg, stdout);
  610. }
  611. #endif
  612. static const char*
  613. FormatValue(JSContext* cx, const Value& vArg, JSAutoByteString& bytes)
  614. {
  615. RootedValue v(cx, vArg);
  616. if (v.isMagic(JS_OPTIMIZED_OUT))
  617. return "[unavailable]";
  618. /*
  619. * We could use Maybe<AutoCompartment> here, but G++ can't quite follow
  620. * that, and warns about uninitialized members being used in the
  621. * destructor.
  622. */
  623. RootedString str(cx);
  624. if (v.isObject()) {
  625. AutoCompartment ac(cx, &v.toObject());
  626. str = ToString<CanGC>(cx, v);
  627. } else {
  628. str = ToString<CanGC>(cx, v);
  629. }
  630. if (!str)
  631. return nullptr;
  632. const char* buf = bytes.encodeLatin1(cx, str);
  633. if (!buf)
  634. return nullptr;
  635. const char* found = strstr(buf, "function ");
  636. if (found && (found - buf <= 2))
  637. return "[function]";
  638. return buf;
  639. }
  640. // Wrapper for JS_sprintf_append() that reports allocation failure to the
  641. // context.
  642. static char*
  643. MOZ_FORMAT_PRINTF(3, 4)
  644. sprintf_append(JSContext* cx, char* buf, const char* fmt, ...)
  645. {
  646. va_list ap;
  647. va_start(ap, fmt);
  648. char* result = JS_vsprintf_append(buf, fmt, ap);
  649. va_end(ap);
  650. if (!result) {
  651. ReportOutOfMemory(cx);
  652. return nullptr;
  653. }
  654. return result;
  655. }
  656. static char*
  657. FormatFrame(JSContext* cx, const FrameIter& iter, char* buf, int num,
  658. bool showArgs, bool showLocals, bool showThisProps)
  659. {
  660. MOZ_ASSERT(!cx->isExceptionPending());
  661. RootedScript script(cx, iter.script());
  662. jsbytecode* pc = iter.pc();
  663. RootedObject envChain(cx, iter.environmentChain(cx));
  664. JSAutoCompartment ac(cx, envChain);
  665. const char* filename = script->filename();
  666. unsigned lineno = PCToLineNumber(script, pc);
  667. RootedFunction fun(cx, iter.maybeCallee(cx));
  668. RootedString funname(cx);
  669. if (fun)
  670. funname = fun->displayAtom();
  671. RootedValue thisVal(cx);
  672. if (iter.hasUsableAbstractFramePtr() &&
  673. iter.isFunctionFrame() &&
  674. fun && !fun->isArrow() && !fun->isDerivedClassConstructor() &&
  675. !(fun->isBoundFunction() && iter.isConstructing()))
  676. {
  677. if (!GetFunctionThis(cx, iter.abstractFramePtr(), &thisVal))
  678. return nullptr;
  679. }
  680. // print the frame number and function name
  681. if (funname) {
  682. JSAutoByteString funbytes;
  683. char* str = funbytes.encodeLatin1(cx, funname);
  684. if (!str)
  685. return nullptr;
  686. buf = sprintf_append(cx, buf, "%d %s(", num, str);
  687. } else if (fun) {
  688. buf = sprintf_append(cx, buf, "%d anonymous(", num);
  689. } else {
  690. buf = sprintf_append(cx, buf, "%d <TOP LEVEL>", num);
  691. }
  692. if (!buf)
  693. return nullptr;
  694. if (showArgs && iter.hasArgs()) {
  695. PositionalFormalParameterIter fi(script);
  696. bool first = true;
  697. for (unsigned i = 0; i < iter.numActualArgs(); i++) {
  698. RootedValue arg(cx);
  699. if (i < iter.numFormalArgs() && fi.closedOver()) {
  700. arg = iter.callObj(cx).aliasedBinding(fi);
  701. } else if (iter.hasUsableAbstractFramePtr()) {
  702. if (script->analyzedArgsUsage() &&
  703. script->argsObjAliasesFormals() &&
  704. iter.hasArgsObj())
  705. {
  706. arg = iter.argsObj().arg(i);
  707. } else {
  708. arg = iter.unaliasedActual(i, DONT_CHECK_ALIASING);
  709. }
  710. } else {
  711. arg = MagicValue(JS_OPTIMIZED_OUT);
  712. }
  713. JSAutoByteString valueBytes;
  714. const char* value = FormatValue(cx, arg, valueBytes);
  715. if (!value) {
  716. if (cx->isThrowingOutOfMemory())
  717. return nullptr;
  718. cx->clearPendingException();
  719. }
  720. JSAutoByteString nameBytes;
  721. const char* name = nullptr;
  722. if (i < iter.numFormalArgs()) {
  723. MOZ_ASSERT(fi.argumentSlot() == i);
  724. if (!fi.isDestructured()) {
  725. name = nameBytes.encodeLatin1(cx, fi.name());
  726. if (!name)
  727. return nullptr;
  728. } else {
  729. name = "(destructured parameter)";
  730. }
  731. fi++;
  732. }
  733. if (value) {
  734. buf = sprintf_append(cx, buf, "%s%s%s%s%s%s",
  735. !first ? ", " : "",
  736. name ? name :"",
  737. name ? " = " : "",
  738. arg.isString() ? "\"" : "",
  739. value,
  740. arg.isString() ? "\"" : "");
  741. if (!buf)
  742. return nullptr;
  743. first = false;
  744. } else {
  745. buf = sprintf_append(cx, buf,
  746. " <Failed to get argument while inspecting stack frame>\n");
  747. if (!buf)
  748. return nullptr;
  749. }
  750. }
  751. }
  752. // print filename and line number
  753. buf = sprintf_append(cx, buf, "%s [\"%s\":%d]\n",
  754. fun ? ")" : "",
  755. filename ? filename : "<unknown>",
  756. lineno);
  757. if (!buf)
  758. return nullptr;
  759. // Note: Right now we don't dump the local variables anymore, because
  760. // that is hard to support across all the JITs etc.
  761. // print the value of 'this'
  762. if (showLocals) {
  763. if (!thisVal.isUndefined()) {
  764. JSAutoByteString thisValBytes;
  765. RootedString thisValStr(cx, ToString<CanGC>(cx, thisVal));
  766. if (!thisValStr) {
  767. if (cx->isThrowingOutOfMemory())
  768. return nullptr;
  769. cx->clearPendingException();
  770. }
  771. if (thisValStr) {
  772. const char* str = thisValBytes.encodeLatin1(cx, thisValStr);
  773. if (!str)
  774. return nullptr;
  775. buf = sprintf_append(cx, buf, " this = %s\n", str);
  776. } else {
  777. buf = sprintf_append(cx, buf, " <failed to get 'this' value>\n");
  778. }
  779. if (!buf)
  780. return nullptr;
  781. }
  782. }
  783. if (showThisProps && thisVal.isObject()) {
  784. RootedObject obj(cx, &thisVal.toObject());
  785. AutoIdVector keys(cx);
  786. if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY, &keys)) {
  787. if (cx->isThrowingOutOfMemory())
  788. return nullptr;
  789. cx->clearPendingException();
  790. }
  791. RootedId id(cx);
  792. for (size_t i = 0; i < keys.length(); i++) {
  793. RootedId id(cx, keys[i]);
  794. RootedValue key(cx, IdToValue(id));
  795. RootedValue v(cx);
  796. if (!GetProperty(cx, obj, obj, id, &v)) {
  797. if (cx->isThrowingOutOfMemory())
  798. return nullptr;
  799. cx->clearPendingException();
  800. buf = sprintf_append(cx, buf,
  801. " <Failed to fetch property while inspecting stack frame>\n");
  802. if (!buf)
  803. return nullptr;
  804. continue;
  805. }
  806. JSAutoByteString nameBytes;
  807. const char* name = FormatValue(cx, key, nameBytes);
  808. if (!name) {
  809. if (cx->isThrowingOutOfMemory())
  810. return nullptr;
  811. cx->clearPendingException();
  812. }
  813. JSAutoByteString valueBytes;
  814. const char* value = FormatValue(cx, v, valueBytes);
  815. if (!value) {
  816. if (cx->isThrowingOutOfMemory())
  817. return nullptr;
  818. cx->clearPendingException();
  819. }
  820. if (name && value) {
  821. buf = sprintf_append(cx, buf, " this.%s = %s%s%s\n",
  822. name,
  823. v.isString() ? "\"" : "",
  824. value,
  825. v.isString() ? "\"" : "");
  826. } else {
  827. buf = sprintf_append(cx, buf,
  828. " <Failed to format values while inspecting stack frame>\n");
  829. }
  830. if (!buf)
  831. return nullptr;
  832. }
  833. }
  834. MOZ_ASSERT(!cx->isExceptionPending());
  835. return buf;
  836. }
  837. static char*
  838. FormatWasmFrame(JSContext* cx, const FrameIter& iter, char* buf, int num, bool showArgs)
  839. {
  840. JSAtom* functionDisplayAtom = iter.functionDisplayAtom();
  841. UniqueChars nameStr;
  842. if (functionDisplayAtom)
  843. nameStr = StringToNewUTF8CharsZ(cx, *functionDisplayAtom);
  844. buf = sprintf_append(cx, buf, "%d %s()",
  845. num,
  846. nameStr ? nameStr.get() : "<wasm-function>");
  847. if (!buf)
  848. return nullptr;
  849. const char* filename = iter.filename();
  850. uint32_t lineno = iter.computeLine();
  851. buf = sprintf_append(cx, buf, " [\"%s\":%d]\n",
  852. filename ? filename : "<unknown>",
  853. lineno);
  854. MOZ_ASSERT(!cx->isExceptionPending());
  855. return buf;
  856. }
  857. JS_FRIEND_API(char*)
  858. JS::FormatStackDump(JSContext* cx, char* buf, bool showArgs, bool showLocals, bool showThisProps)
  859. {
  860. int num = 0;
  861. for (AllFramesIter i(cx); !i.done(); ++i) {
  862. if (i.hasScript())
  863. buf = FormatFrame(cx, i, buf, num, showArgs, showLocals, showThisProps);
  864. else
  865. buf = FormatWasmFrame(cx, i, buf, num, showArgs);
  866. if (!buf)
  867. return nullptr;
  868. num++;
  869. }
  870. if (!num)
  871. buf = JS_sprintf_append(buf, "JavaScript stack is empty\n");
  872. return buf;
  873. }
  874. extern JS_FRIEND_API(bool)
  875. JS::ForceLexicalInitialization(JSContext *cx, HandleObject obj)
  876. {
  877. AssertHeapIsIdle(cx);
  878. CHECK_REQUEST(cx);
  879. assertSameCompartment(cx, obj);
  880. bool initializedAny = false;
  881. NativeObject* nobj = &obj->as<NativeObject>();
  882. for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
  883. Shape* s = &r.front();
  884. Value v = nobj->getSlot(s->slot());
  885. if (s->hasSlot() && v.isMagic() && v.whyMagic() == JS_UNINITIALIZED_LEXICAL) {
  886. nobj->setSlot(s->slot(), UndefinedValue());
  887. initializedAny = true;
  888. }
  889. }
  890. return initializedAny;
  891. }
  892. struct DumpHeapTracer : public JS::CallbackTracer, public WeakMapTracer
  893. {
  894. const char* prefix;
  895. FILE* output;
  896. DumpHeapTracer(FILE* fp, JSContext* cx)
  897. : JS::CallbackTracer(cx, DoNotTraceWeakMaps),
  898. js::WeakMapTracer(cx), prefix(""), output(fp)
  899. {}
  900. private:
  901. void trace(JSObject* map, JS::GCCellPtr key, JS::GCCellPtr value) override {
  902. JSObject* kdelegate = nullptr;
  903. if (key.is<JSObject>())
  904. kdelegate = js::GetWeakmapKeyDelegate(&key.as<JSObject>());
  905. fprintf(output, "WeakMapEntry map=%p key=%p keyDelegate=%p value=%p\n",
  906. map, key.asCell(), kdelegate, value.asCell());
  907. }
  908. void onChild(const JS::GCCellPtr& thing) override;
  909. };
  910. static char
  911. MarkDescriptor(void* thing)
  912. {
  913. gc::TenuredCell* cell = gc::TenuredCell::fromPointer(thing);
  914. if (cell->isMarked(gc::BLACK))
  915. return cell->isMarked(gc::GRAY) ? 'G' : 'B';
  916. else
  917. return cell->isMarked(gc::GRAY) ? 'X' : 'W';
  918. }
  919. static void
  920. DumpHeapVisitZone(JSRuntime* rt, void* data, Zone* zone)
  921. {
  922. DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
  923. fprintf(dtrc->output, "# zone %p\n", (void*)zone);
  924. }
  925. static void
  926. DumpHeapVisitCompartment(JSContext* cx, void* data, JSCompartment* comp)
  927. {
  928. char name[1024];
  929. if (cx->compartmentNameCallback)
  930. (*cx->compartmentNameCallback)(cx, comp, name, sizeof(name));
  931. else
  932. strcpy(name, "<unknown>");
  933. DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
  934. fprintf(dtrc->output, "# compartment %s [in zone %p]\n", name, (void*)comp->zone());
  935. }
  936. static void
  937. DumpHeapVisitArena(JSRuntime* rt, void* data, gc::Arena* arena,
  938. JS::TraceKind traceKind, size_t thingSize)
  939. {
  940. DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
  941. fprintf(dtrc->output, "# arena allockind=%u size=%u\n",
  942. unsigned(arena->getAllocKind()), unsigned(thingSize));
  943. }
  944. static void
  945. DumpHeapVisitCell(JSRuntime* rt, void* data, void* thing,
  946. JS::TraceKind traceKind, size_t thingSize)
  947. {
  948. DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
  949. char cellDesc[1024 * 32];
  950. JS_GetTraceThingInfo(cellDesc, sizeof(cellDesc), dtrc, thing, traceKind, true);
  951. fprintf(dtrc->output, "%p %c %s\n", thing, MarkDescriptor(thing), cellDesc);
  952. js::TraceChildren(dtrc, thing, traceKind);
  953. }
  954. void
  955. DumpHeapTracer::onChild(const JS::GCCellPtr& thing)
  956. {
  957. if (gc::IsInsideNursery(thing.asCell()))
  958. return;
  959. char buffer[1024];
  960. getTracingEdgeName(buffer, sizeof(buffer));
  961. fprintf(output, "%s%p %c %s\n", prefix, thing.asCell(), MarkDescriptor(thing.asCell()), buffer);
  962. }
  963. void
  964. js::DumpHeap(JSContext* cx, FILE* fp, js::DumpHeapNurseryBehaviour nurseryBehaviour)
  965. {
  966. if (nurseryBehaviour == js::CollectNurseryBeforeDump)
  967. cx->gc.evictNursery(JS::gcreason::API);
  968. DumpHeapTracer dtrc(fp, cx);
  969. fprintf(dtrc.output, "# Roots.\n");
  970. TraceRuntime(&dtrc);
  971. fprintf(dtrc.output, "# Weak maps.\n");
  972. WeakMapBase::traceAllMappings(&dtrc);
  973. fprintf(dtrc.output, "==========\n");
  974. dtrc.prefix = "> ";
  975. IterateZonesCompartmentsArenasCells(cx, &dtrc,
  976. DumpHeapVisitZone,
  977. DumpHeapVisitCompartment,
  978. DumpHeapVisitArena,
  979. DumpHeapVisitCell);
  980. fflush(dtrc.output);
  981. }
  982. JS_FRIEND_API(void)
  983. js::SetActivityCallback(JSContext* cx, ActivityCallback cb, void* arg)
  984. {
  985. cx->activityCallback = cb;
  986. cx->activityCallbackArg = arg;
  987. }
  988. JS_FRIEND_API(void)
  989. JS::NotifyDidPaint(JSContext* cx)
  990. {
  991. cx->gc.notifyDidPaint();
  992. }
  993. JS_FRIEND_API(void)
  994. JS::PokeGC(JSContext* cx)
  995. {
  996. cx->gc.poke();
  997. }
  998. JS_FRIEND_API(JSCompartment*)
  999. js::GetAnyCompartmentInZone(JS::Zone* zone)
  1000. {
  1001. CompartmentsInZoneIter comp(zone);
  1002. MOZ_ASSERT(!comp.done());
  1003. return comp.get();
  1004. }
  1005. void
  1006. JS::ObjectPtr::finalize(JSRuntime* rt)
  1007. {
  1008. if (IsIncrementalBarrierNeeded(rt->contextFromMainThread()))
  1009. IncrementalObjectBarrier(value);
  1010. value = nullptr;
  1011. }
  1012. void
  1013. JS::ObjectPtr::finalize(JSContext* cx)
  1014. {
  1015. finalize(cx->runtime());
  1016. }
  1017. void
  1018. JS::ObjectPtr::updateWeakPointerAfterGC()
  1019. {
  1020. if (js::gc::IsAboutToBeFinalizedUnbarriered(value.unsafeGet()))
  1021. value = nullptr;
  1022. }
  1023. void
  1024. JS::ObjectPtr::trace(JSTracer* trc, const char* name)
  1025. {
  1026. JS::TraceEdge(trc, &value, name);
  1027. }
  1028. JS_FRIEND_API(JSObject*)
  1029. js::GetTestingFunctions(JSContext* cx)
  1030. {
  1031. RootedObject obj(cx, JS_NewPlainObject(cx));
  1032. if (!obj)
  1033. return nullptr;
  1034. if (!DefineTestingFunctions(cx, obj, false, false))
  1035. return nullptr;
  1036. return obj;
  1037. }
  1038. #ifdef DEBUG
  1039. JS_FRIEND_API(unsigned)
  1040. js::GetEnterCompartmentDepth(JSContext* cx)
  1041. {
  1042. return cx->getEnterCompartmentDepth();
  1043. }
  1044. #endif
  1045. JS_FRIEND_API(void)
  1046. js::SetDOMCallbacks(JSContext* cx, const DOMCallbacks* callbacks)
  1047. {
  1048. cx->DOMcallbacks = callbacks;
  1049. }
  1050. JS_FRIEND_API(const DOMCallbacks*)
  1051. js::GetDOMCallbacks(JSContext* cx)
  1052. {
  1053. return cx->DOMcallbacks;
  1054. }
  1055. static const void* gDOMProxyHandlerFamily = nullptr;
  1056. static uint32_t gDOMProxyExpandoSlot = 0;
  1057. static DOMProxyShadowsCheck gDOMProxyShadowsCheck;
  1058. JS_FRIEND_API(void)
  1059. js::SetDOMProxyInformation(const void* domProxyHandlerFamily, uint32_t domProxyExpandoSlot,
  1060. DOMProxyShadowsCheck domProxyShadowsCheck)
  1061. {
  1062. gDOMProxyHandlerFamily = domProxyHandlerFamily;
  1063. gDOMProxyExpandoSlot = domProxyExpandoSlot;
  1064. gDOMProxyShadowsCheck = domProxyShadowsCheck;
  1065. }
  1066. const void*
  1067. js::GetDOMProxyHandlerFamily()
  1068. {
  1069. return gDOMProxyHandlerFamily;
  1070. }
  1071. uint32_t
  1072. js::GetDOMProxyExpandoSlot()
  1073. {
  1074. return gDOMProxyExpandoSlot;
  1075. }
  1076. DOMProxyShadowsCheck
  1077. js::GetDOMProxyShadowsCheck()
  1078. {
  1079. return gDOMProxyShadowsCheck;
  1080. }
  1081. bool
  1082. js::detail::IdMatchesAtom(jsid id, JSAtom* atom)
  1083. {
  1084. return id == INTERNED_STRING_TO_JSID(nullptr, atom);
  1085. }
  1086. JS_FRIEND_API(void)
  1087. js::PrepareScriptEnvironmentAndInvoke(JSContext* cx, HandleObject scope, ScriptEnvironmentPreparer::Closure& closure)
  1088. {
  1089. MOZ_ASSERT(!cx->isExceptionPending());
  1090. MOZ_RELEASE_ASSERT(cx->runtime()->scriptEnvironmentPreparer,
  1091. "Embedding needs to set a scriptEnvironmentPreparer callback");
  1092. cx->runtime()->scriptEnvironmentPreparer->invoke(scope, closure);
  1093. }
  1094. JS_FRIEND_API(void)
  1095. js::SetScriptEnvironmentPreparer(JSContext* cx, ScriptEnvironmentPreparer* preparer)
  1096. {
  1097. cx->scriptEnvironmentPreparer = preparer;
  1098. }
  1099. JS_FRIEND_API(void)
  1100. js::SetCTypesActivityCallback(JSContext* cx, CTypesActivityCallback cb)
  1101. {
  1102. cx->ctypesActivityCallback = cb;
  1103. }
  1104. js::AutoCTypesActivityCallback::AutoCTypesActivityCallback(JSContext* cx,
  1105. js::CTypesActivityType beginType,
  1106. js::CTypesActivityType endType
  1107. MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
  1108. : cx(cx), callback(cx->runtime()->ctypesActivityCallback), endType(endType)
  1109. {
  1110. MOZ_GUARD_OBJECT_NOTIFIER_INIT;
  1111. if (callback)
  1112. callback(cx, beginType);
  1113. }
  1114. JS_FRIEND_API(void)
  1115. js::SetAllocationMetadataBuilder(JSContext* cx, const AllocationMetadataBuilder *callback)
  1116. {
  1117. cx->compartment()->setAllocationMetadataBuilder(callback);
  1118. }
  1119. JS_FRIEND_API(JSObject*)
  1120. js::GetAllocationMetadata(JSObject* obj)
  1121. {
  1122. ObjectWeakMap* map = obj->compartment()->objectMetadataTable;
  1123. if (map)
  1124. return map->lookup(obj);
  1125. return nullptr;
  1126. }
  1127. JS_FRIEND_API(bool)
  1128. js::ReportIsNotFunction(JSContext* cx, HandleValue v)
  1129. {
  1130. return ReportIsNotFunction(cx, v, -1);
  1131. }
  1132. JS_FRIEND_API(void)
  1133. js::ReportASCIIErrorWithId(JSContext* cx, const char* msg, HandleId id)
  1134. {
  1135. RootedValue idv(cx);
  1136. if (!JS_IdToValue(cx, id, &idv))
  1137. return;
  1138. RootedString idstr(cx, JS::ToString(cx, idv));
  1139. if (!idstr)
  1140. return;
  1141. JSAutoByteString bytes;
  1142. if (!bytes.encodeUtf8(cx, idstr))
  1143. return;
  1144. JS_ReportErrorUTF8(cx, msg, bytes.ptr());
  1145. }
  1146. #ifdef DEBUG
  1147. bool
  1148. js::HasObjectMovedOp(JSObject* obj) {
  1149. return !!GetObjectClass(obj)->extObjectMovedOp();
  1150. }
  1151. #endif
  1152. JS_FRIEND_API(bool)
  1153. js::ForwardToNative(JSContext* cx, JSNative native, const CallArgs& args)
  1154. {
  1155. return native(cx, args.length(), args.base());
  1156. }
  1157. JS_FRIEND_API(JSObject*)
  1158. js::ConvertArgsToArray(JSContext* cx, const CallArgs& args)
  1159. {
  1160. RootedObject argsArray(cx, NewDenseCopiedArray(cx, args.length(), args.array()));
  1161. return argsArray;
  1162. }
  1163. JS_FRIEND_API(JSAtom*)
  1164. js::GetPropertyNameFromPC(JSScript* script, jsbytecode* pc)
  1165. {
  1166. if (!IsGetPropPC(pc) && !IsSetPropPC(pc))
  1167. return nullptr;
  1168. return script->getName(pc);
  1169. }
  1170. JS_FRIEND_API(void)
  1171. js::SetWindowProxyClass(JSContext* cx, const js::Class* clasp)
  1172. {
  1173. MOZ_ASSERT(!cx->maybeWindowProxyClass());
  1174. cx->setWindowProxyClass(clasp);
  1175. }
  1176. JS_FRIEND_API(void)
  1177. js::SetWindowProxy(JSContext* cx, HandleObject global, HandleObject windowProxy)
  1178. {
  1179. AssertHeapIsIdle(cx);
  1180. CHECK_REQUEST(cx);
  1181. assertSameCompartment(cx, global, windowProxy);
  1182. MOZ_ASSERT(IsWindowProxy(windowProxy));
  1183. global->as<GlobalObject>().setWindowProxy(windowProxy);
  1184. }
  1185. JS_FRIEND_API(JSObject*)
  1186. js::ToWindowIfWindowProxy(JSObject* obj)
  1187. {
  1188. if (IsWindowProxy(obj))
  1189. return &obj->global();
  1190. return obj;
  1191. }
  1192. JS_FRIEND_API(JSObject*)
  1193. js::ToWindowProxyIfWindow(JSObject* obj)
  1194. {
  1195. if (IsWindow(obj))
  1196. return obj->as<GlobalObject>().windowProxy();
  1197. return obj;
  1198. }
  1199. JS_FRIEND_API(bool)
  1200. js::IsWindowProxy(JSObject* obj)
  1201. {
  1202. // Note: simply checking `obj == obj->global().windowProxy()` is not
  1203. // sufficient: we may have transplanted the window proxy with a CCW.
  1204. // Check the Class to ensure we really have a window proxy.
  1205. return obj->getClass() == obj->runtimeFromAnyThread()->maybeWindowProxyClass();
  1206. }
  1207. JS_FRIEND_API(bool)
  1208. js::detail::IsWindowSlow(JSObject* obj)
  1209. {
  1210. return obj->as<GlobalObject>().maybeWindowProxy();
  1211. }