PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/src/jsscript.cpp

http://github.com/zpao/v8monkey
C++ | 1908 lines | 1458 code | 254 blank | 196 comment | 463 complexity | 1fc336b206cecbd3c5ca178c1c20d694 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=78:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is Mozilla Communicator client code, released
  18. * March 31, 1998.
  19. *
  20. * The Initial Developer of the Original Code is
  21. * Netscape Communications Corporation.
  22. * Portions created by the Initial Developer are Copyright (C) 1998
  23. * the Initial Developer. All Rights Reserved.
  24. *
  25. * Contributor(s):
  26. * Nick Fitzgerald <nfitzgerald@mozilla.com>
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either of the GNU General Public License Version 2 or later (the "GPL"),
  30. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. /*
  42. * JS script operations.
  43. */
  44. #include <string.h>
  45. #include "jstypes.h"
  46. #include "jsutil.h"
  47. #include "jscrashreport.h"
  48. #include "jsprf.h"
  49. #include "jsapi.h"
  50. #include "jsatom.h"
  51. #include "jscntxt.h"
  52. #include "jsversion.h"
  53. #include "jsdbgapi.h"
  54. #include "jsfun.h"
  55. #include "jsgc.h"
  56. #include "jsgcmark.h"
  57. #include "jsinterp.h"
  58. #include "jslock.h"
  59. #include "jsnum.h"
  60. #include "jsopcode.h"
  61. #include "jsscope.h"
  62. #include "jsscript.h"
  63. #if JS_HAS_XDR
  64. #include "jsxdrapi.h"
  65. #endif
  66. #include "frontend/BytecodeEmitter.h"
  67. #include "frontend/Parser.h"
  68. #include "js/MemoryMetrics.h"
  69. #include "methodjit/MethodJIT.h"
  70. #include "methodjit/Retcon.h"
  71. #include "vm/Debugger.h"
  72. #include "jsinferinlines.h"
  73. #include "jsinterpinlines.h"
  74. #include "jsobjinlines.h"
  75. #include "jsscriptinlines.h"
  76. using namespace js;
  77. using namespace js::gc;
  78. using namespace js::frontend;
  79. namespace js {
  80. BindingKind
  81. Bindings::lookup(JSContext *cx, JSAtom *name, uintN *indexp) const
  82. {
  83. if (!lastBinding)
  84. return NONE;
  85. Shape **spp;
  86. Shape *shape = Shape::search(cx, lastBinding, ATOM_TO_JSID(name), &spp);
  87. if (!shape)
  88. return NONE;
  89. if (indexp)
  90. *indexp = shape->shortid();
  91. if (shape->getter() == GetCallArg)
  92. return ARGUMENT;
  93. if (shape->getter() == GetCallUpvar)
  94. return UPVAR;
  95. return shape->writable() ? VARIABLE : CONSTANT;
  96. }
  97. bool
  98. Bindings::add(JSContext *cx, JSAtom *name, BindingKind kind)
  99. {
  100. if (!ensureShape(cx))
  101. return false;
  102. /*
  103. * We still follow 10.2.3 of ES3 and make argument and variable properties
  104. * of the Call objects enumerable. ES5 reformulated all of its Clause 10 to
  105. * avoid objects as activations, something we should do too.
  106. */
  107. uintN attrs = JSPROP_ENUMERATE | JSPROP_PERMANENT;
  108. uint16_t *indexp;
  109. PropertyOp getter;
  110. StrictPropertyOp setter;
  111. uint32_t slot = CallObject::RESERVED_SLOTS;
  112. if (kind == ARGUMENT) {
  113. JS_ASSERT(nvars == 0);
  114. JS_ASSERT(nupvars == 0);
  115. indexp = &nargs;
  116. getter = GetCallArg;
  117. setter = SetCallArg;
  118. slot += nargs;
  119. } else if (kind == UPVAR) {
  120. indexp = &nupvars;
  121. getter = GetCallUpvar;
  122. setter = SetCallUpvar;
  123. slot = lastBinding->maybeSlot();
  124. attrs |= JSPROP_SHARED;
  125. } else {
  126. JS_ASSERT(kind == VARIABLE || kind == CONSTANT);
  127. JS_ASSERT(nupvars == 0);
  128. indexp = &nvars;
  129. getter = GetCallVar;
  130. setter = SetCallVar;
  131. if (kind == CONSTANT)
  132. attrs |= JSPROP_READONLY;
  133. slot += nargs + nvars;
  134. }
  135. if (*indexp == BINDING_COUNT_LIMIT) {
  136. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  137. (kind == ARGUMENT)
  138. ? JSMSG_TOO_MANY_FUN_ARGS
  139. : JSMSG_TOO_MANY_LOCALS);
  140. return false;
  141. }
  142. jsid id;
  143. if (!name) {
  144. JS_ASSERT(kind == ARGUMENT); /* destructuring */
  145. id = INT_TO_JSID(nargs);
  146. } else {
  147. id = ATOM_TO_JSID(name);
  148. }
  149. StackBaseShape base(&CallClass, NULL, BaseShape::VAROBJ);
  150. base.updateGetterSetter(attrs, getter, setter);
  151. UnownedBaseShape *nbase = BaseShape::getUnowned(cx, base);
  152. if (!nbase)
  153. return NULL;
  154. StackShape child(nbase, id, slot, 0, attrs, Shape::HAS_SHORTID, *indexp);
  155. /* Shapes in bindings cannot be dictionaries. */
  156. Shape *shape = lastBinding->getChildBinding(cx, child);
  157. if (!shape)
  158. return false;
  159. lastBinding = shape;
  160. ++*indexp;
  161. return true;
  162. }
  163. Shape *
  164. Bindings::callObjectShape(JSContext *cx) const
  165. {
  166. if (!hasDup())
  167. return lastShape();
  168. /*
  169. * Build a vector of non-duplicate properties in order from last added
  170. * to first (i.e., the order we normally have iterate over Shapes). Choose
  171. * the last added property in each set of dups.
  172. */
  173. Vector<const Shape *> shapes(cx);
  174. HashSet<jsid> seen(cx);
  175. if (!seen.init())
  176. return NULL;
  177. for (Shape::Range r = lastShape()->all(); !r.empty(); r.popFront()) {
  178. const Shape &s = r.front();
  179. HashSet<jsid>::AddPtr p = seen.lookupForAdd(s.propid());
  180. if (!p) {
  181. if (!seen.add(p, s.propid()))
  182. return NULL;
  183. if (!shapes.append(&s))
  184. return NULL;
  185. }
  186. }
  187. /*
  188. * Now build the Shape without duplicate properties.
  189. */
  190. RootedVarShape shape(cx);
  191. shape = initialShape(cx);
  192. for (int i = shapes.length() - 1; i >= 0; --i) {
  193. shape = shape->getChildBinding(cx, shapes[i]);
  194. if (!shape)
  195. return NULL;
  196. }
  197. return shape;
  198. }
  199. bool
  200. Bindings::getLocalNameArray(JSContext *cx, Vector<JSAtom *> *namesp)
  201. {
  202. JS_ASSERT(lastBinding);
  203. JS_ASSERT(hasLocalNames());
  204. Vector<JSAtom *> &names = *namesp;
  205. JS_ASSERT(names.empty());
  206. uintN n = countLocalNames();
  207. if (!names.growByUninitialized(n))
  208. return false;
  209. #ifdef DEBUG
  210. JSAtom * const POISON = reinterpret_cast<JSAtom *>(0xdeadbeef);
  211. for (uintN i = 0; i < n; i++)
  212. names[i] = POISON;
  213. #endif
  214. for (Shape::Range r = lastBinding->all(); !r.empty(); r.popFront()) {
  215. const Shape &shape = r.front();
  216. uintN index = uint16_t(shape.shortid());
  217. if (shape.getter() == GetCallArg) {
  218. JS_ASSERT(index < nargs);
  219. } else if (shape.getter() == GetCallUpvar) {
  220. JS_ASSERT(index < nupvars);
  221. index += nargs + nvars;
  222. } else {
  223. JS_ASSERT(index < nvars);
  224. index += nargs;
  225. }
  226. if (JSID_IS_ATOM(shape.propid())) {
  227. names[index] = JSID_TO_ATOM(shape.propid());
  228. } else {
  229. JS_ASSERT(JSID_IS_INT(shape.propid()));
  230. JS_ASSERT(shape.getter() == GetCallArg);
  231. names[index] = NULL;
  232. }
  233. }
  234. #ifdef DEBUG
  235. for (uintN i = 0; i < n; i++)
  236. JS_ASSERT(names[i] != POISON);
  237. #endif
  238. return true;
  239. }
  240. const Shape *
  241. Bindings::lastArgument() const
  242. {
  243. JS_ASSERT(lastBinding);
  244. const js::Shape *shape = lastVariable();
  245. if (nvars > 0) {
  246. while (shape->previous() && shape->getter() != GetCallArg)
  247. shape = shape->previous();
  248. }
  249. return shape;
  250. }
  251. const Shape *
  252. Bindings::lastVariable() const
  253. {
  254. JS_ASSERT(lastBinding);
  255. const js::Shape *shape = lastUpvar();
  256. if (nupvars > 0) {
  257. while (shape->getter() == GetCallUpvar)
  258. shape = shape->previous();
  259. }
  260. return shape;
  261. }
  262. const Shape *
  263. Bindings::lastUpvar() const
  264. {
  265. JS_ASSERT(lastBinding);
  266. return lastBinding;
  267. }
  268. void
  269. Bindings::makeImmutable()
  270. {
  271. JS_ASSERT(lastBinding);
  272. JS_ASSERT(!lastBinding->inDictionary());
  273. }
  274. void
  275. Bindings::trace(JSTracer *trc)
  276. {
  277. if (lastBinding)
  278. MarkShape(trc, lastBinding, "shape");
  279. }
  280. #ifdef JS_CRASH_DIAGNOSTICS
  281. void
  282. CheckScript(JSScript *script, JSScript *prev)
  283. {
  284. if (script->cookie1[0] != JS_SCRIPT_COOKIE || script->cookie2[0] != JS_SCRIPT_COOKIE) {
  285. crash::StackBuffer<sizeof(JSScript), 0x87> buf1(script);
  286. crash::StackBuffer<sizeof(JSScript), 0x88> buf2(prev);
  287. JS_OPT_ASSERT(false);
  288. }
  289. }
  290. #endif /* JS_CRASH_DIAGNOSTICS */
  291. } /* namespace js */
  292. #if JS_HAS_XDR
  293. enum ScriptBits {
  294. NoScriptRval,
  295. SavedCallerFun,
  296. StrictModeCode,
  297. UsesEval,
  298. UsesArguments
  299. };
  300. static const char *
  301. SaveScriptFilename(JSContext *cx, const char *filename);
  302. JSBool
  303. js_XDRScript(JSXDRState *xdr, JSScript **scriptp)
  304. {
  305. JSScript *oldscript;
  306. JSBool ok;
  307. uint32_t length, lineno, nslots;
  308. uint32_t natoms, nsrcnotes, ntrynotes, nobjects, nregexps, nconsts, i;
  309. uint32_t prologLength, version, encodedClosedCount;
  310. uint16_t nClosedArgs = 0, nClosedVars = 0;
  311. uint32_t nTypeSets = 0;
  312. uint32_t encodeable, sameOriginPrincipals;
  313. JSSecurityCallbacks *callbacks;
  314. uint32_t scriptBits = 0;
  315. JSContext *cx = xdr->cx;
  316. JSScript *script = *scriptp;
  317. nsrcnotes = ntrynotes = natoms = nobjects = nregexps = nconsts = 0;
  318. jssrcnote *notes = NULL;
  319. XDRScriptState *state = xdr->state;
  320. JS_ASSERT(state);
  321. /* Should not XDR scripts optimized for a single global object. */
  322. JS_ASSERT_IF(script, !JSScript::isValidOffset(script->globalsOffset));
  323. /* XDR arguments, local vars, and upvars. */
  324. uint16_t nargs, nvars, nupvars;
  325. #if defined(DEBUG) || defined(__GNUC__) /* quell GCC overwarning */
  326. nargs = nvars = nupvars = Bindings::BINDING_COUNT_LIMIT;
  327. #endif
  328. uint32_t argsVars, paddingUpvars;
  329. if (xdr->mode == JSXDR_ENCODE) {
  330. nargs = script->bindings.countArgs();
  331. nvars = script->bindings.countVars();
  332. nupvars = script->bindings.countUpvars();
  333. argsVars = (nargs << 16) | nvars;
  334. paddingUpvars = nupvars;
  335. }
  336. if (!JS_XDRUint32(xdr, &argsVars) || !JS_XDRUint32(xdr, &paddingUpvars))
  337. return false;
  338. if (xdr->mode == JSXDR_DECODE) {
  339. nargs = argsVars >> 16;
  340. nvars = argsVars & 0xFFFF;
  341. JS_ASSERT((paddingUpvars >> 16) == 0);
  342. nupvars = paddingUpvars & 0xFFFF;
  343. }
  344. JS_ASSERT(nargs != Bindings::BINDING_COUNT_LIMIT);
  345. JS_ASSERT(nvars != Bindings::BINDING_COUNT_LIMIT);
  346. JS_ASSERT(nupvars != Bindings::BINDING_COUNT_LIMIT);
  347. Bindings bindings(cx);
  348. uint32_t nameCount = nargs + nvars + nupvars;
  349. if (nameCount > 0) {
  350. LifoAllocScope las(&cx->tempLifoAlloc());
  351. /*
  352. * To xdr the names we prefix the names with a bitmap descriptor and
  353. * then xdr the names as strings. For argument names (indexes below
  354. * nargs) the corresponding bit in the bitmap is unset when the name
  355. * is null. Such null names are not encoded or decoded. For variable
  356. * names (indexes starting from nargs) bitmap's bit is set when the
  357. * name is declared as const, not as ordinary var.
  358. * */
  359. uintN bitmapLength = JS_HOWMANY(nameCount, JS_BITS_PER_UINT32);
  360. uint32_t *bitmap = cx->tempLifoAlloc().newArray<uint32_t>(bitmapLength);
  361. if (!bitmap) {
  362. js_ReportOutOfMemory(cx);
  363. return false;
  364. }
  365. Vector<JSAtom *> names(cx);
  366. if (xdr->mode == JSXDR_ENCODE) {
  367. if (!script->bindings.getLocalNameArray(cx, &names))
  368. return false;
  369. PodZero(bitmap, bitmapLength);
  370. for (uintN i = 0; i < nameCount; i++) {
  371. if (i < nargs && names[i])
  372. bitmap[i >> JS_BITS_PER_UINT32_LOG2] |= JS_BIT(i & (JS_BITS_PER_UINT32 - 1));
  373. }
  374. }
  375. for (uintN i = 0; i < bitmapLength; ++i) {
  376. if (!JS_XDRUint32(xdr, &bitmap[i]))
  377. return false;
  378. }
  379. for (uintN i = 0; i < nameCount; i++) {
  380. if (i < nargs &&
  381. !(bitmap[i >> JS_BITS_PER_UINT32_LOG2] & JS_BIT(i & (JS_BITS_PER_UINT32 - 1))))
  382. {
  383. if (xdr->mode == JSXDR_DECODE) {
  384. uint16_t dummy;
  385. if (!bindings.addDestructuring(cx, &dummy))
  386. return false;
  387. } else {
  388. JS_ASSERT(!names[i]);
  389. }
  390. continue;
  391. }
  392. JSAtom *name;
  393. if (xdr->mode == JSXDR_ENCODE)
  394. name = names[i];
  395. if (!js_XDRAtom(xdr, &name))
  396. return false;
  397. if (xdr->mode == JSXDR_DECODE) {
  398. BindingKind kind = (i < nargs)
  399. ? ARGUMENT
  400. : (i < uintN(nargs + nvars))
  401. ? (bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
  402. JS_BIT(i & (JS_BITS_PER_UINT32 - 1))
  403. ? CONSTANT
  404. : VARIABLE)
  405. : UPVAR;
  406. if (!bindings.add(cx, name, kind))
  407. return false;
  408. }
  409. }
  410. }
  411. if (xdr->mode == JSXDR_DECODE) {
  412. if (!bindings.ensureShape(cx))
  413. return false;
  414. bindings.makeImmutable();
  415. }
  416. if (xdr->mode == JSXDR_ENCODE)
  417. length = script->length;
  418. if (!JS_XDRUint32(xdr, &length))
  419. return JS_FALSE;
  420. if (xdr->mode == JSXDR_ENCODE) {
  421. prologLength = script->mainOffset;
  422. JS_ASSERT(script->getVersion() != JSVERSION_UNKNOWN);
  423. version = (uint32_t)script->getVersion() | (script->nfixed << 16);
  424. lineno = (uint32_t)script->lineno;
  425. nslots = (uint32_t)script->nslots;
  426. nslots = (uint32_t)((script->staticLevel << 16) | script->nslots);
  427. natoms = script->natoms;
  428. notes = script->notes();
  429. nsrcnotes = script->numNotes();
  430. if (JSScript::isValidOffset(script->objectsOffset))
  431. nobjects = script->objects()->length;
  432. if (JSScript::isValidOffset(script->upvarsOffset))
  433. JS_ASSERT(script->bindings.countUpvars() == script->upvars()->length);
  434. if (JSScript::isValidOffset(script->regexpsOffset))
  435. nregexps = script->regexps()->length;
  436. if (JSScript::isValidOffset(script->trynotesOffset))
  437. ntrynotes = script->trynotes()->length;
  438. if (JSScript::isValidOffset(script->constOffset))
  439. nconsts = script->consts()->length;
  440. nClosedArgs = script->nClosedArgs;
  441. nClosedVars = script->nClosedVars;
  442. encodedClosedCount = (nClosedArgs << 16) | nClosedVars;
  443. nTypeSets = script->nTypeSets;
  444. if (script->noScriptRval)
  445. scriptBits |= (1 << NoScriptRval);
  446. if (script->savedCallerFun)
  447. scriptBits |= (1 << SavedCallerFun);
  448. if (script->strictModeCode)
  449. scriptBits |= (1 << StrictModeCode);
  450. if (script->usesEval)
  451. scriptBits |= (1 << UsesEval);
  452. if (script->usesArguments)
  453. scriptBits |= (1 << UsesArguments);
  454. JS_ASSERT(!script->compileAndGo);
  455. JS_ASSERT(!script->hasSingletons);
  456. }
  457. if (!JS_XDRUint32(xdr, &prologLength))
  458. return JS_FALSE;
  459. if (!JS_XDRUint32(xdr, &version))
  460. return JS_FALSE;
  461. /*
  462. * To fuse allocations, we need srcnote, atom, objects, regexp, and trynote
  463. * counts early.
  464. */
  465. if (!JS_XDRUint32(xdr, &natoms))
  466. return JS_FALSE;
  467. if (!JS_XDRUint32(xdr, &nsrcnotes))
  468. return JS_FALSE;
  469. if (!JS_XDRUint32(xdr, &ntrynotes))
  470. return JS_FALSE;
  471. if (!JS_XDRUint32(xdr, &nobjects))
  472. return JS_FALSE;
  473. if (!JS_XDRUint32(xdr, &nregexps))
  474. return JS_FALSE;
  475. if (!JS_XDRUint32(xdr, &nconsts))
  476. return JS_FALSE;
  477. if (!JS_XDRUint32(xdr, &encodedClosedCount))
  478. return JS_FALSE;
  479. if (!JS_XDRUint32(xdr, &nTypeSets))
  480. return JS_FALSE;
  481. if (!JS_XDRUint32(xdr, &scriptBits))
  482. return JS_FALSE;
  483. if (xdr->mode == JSXDR_DECODE) {
  484. nClosedArgs = encodedClosedCount >> 16;
  485. nClosedVars = encodedClosedCount & 0xFFFF;
  486. /* Note: version is packed into the 32b space with another 16b value. */
  487. JSVersion version_ = JSVersion(version & JS_BITMASK(16));
  488. JS_ASSERT((version_ & VersionFlags::FULL_MASK) == uintN(version_));
  489. script = JSScript::NewScript(cx, length, nsrcnotes, natoms, nobjects, nupvars,
  490. nregexps, ntrynotes, nconsts, 0, nClosedArgs,
  491. nClosedVars, nTypeSets, version_);
  492. if (!script)
  493. return JS_FALSE;
  494. script->bindings.transfer(cx, &bindings);
  495. JS_ASSERT(!script->mainOffset);
  496. script->mainOffset = prologLength;
  497. script->nfixed = uint16_t(version >> 16);
  498. /* If we know nsrcnotes, we allocated space for notes in script. */
  499. notes = script->notes();
  500. *scriptp = script;
  501. if (scriptBits & (1 << NoScriptRval))
  502. script->noScriptRval = true;
  503. if (scriptBits & (1 << SavedCallerFun))
  504. script->savedCallerFun = true;
  505. if (scriptBits & (1 << StrictModeCode))
  506. script->strictModeCode = true;
  507. if (scriptBits & (1 << UsesEval))
  508. script->usesEval = true;
  509. if (scriptBits & (1 << UsesArguments))
  510. script->usesArguments = true;
  511. }
  512. /*
  513. * Control hereafter must goto error on failure, in order for the
  514. * DECODE case to destroy script.
  515. */
  516. oldscript = xdr->script;
  517. xdr->script = script;
  518. ok = JS_XDRBytes(xdr, (char *)script->code, length * sizeof(jsbytecode));
  519. if (!ok)
  520. goto error;
  521. if (!JS_XDRBytes(xdr, (char *)notes, nsrcnotes * sizeof(jssrcnote)) ||
  522. !JS_XDRUint32(xdr, &lineno) ||
  523. !JS_XDRUint32(xdr, &nslots)) {
  524. goto error;
  525. }
  526. if (xdr->mode == JSXDR_DECODE && state->filename) {
  527. if (!state->filenameSaved) {
  528. const char *filename = state->filename;
  529. filename = SaveScriptFilename(xdr->cx, filename);
  530. xdr->cx->free_((void *) state->filename);
  531. state->filename = filename;
  532. state->filenameSaved = true;
  533. if (!filename)
  534. goto error;
  535. }
  536. script->filename = state->filename;
  537. }
  538. JS_ASSERT_IF(xdr->mode == JSXDR_ENCODE, state->filename == script->filename);
  539. callbacks = JS_GetSecurityCallbacks(cx);
  540. if (xdr->mode == JSXDR_ENCODE)
  541. encodeable = script->principals && callbacks && callbacks->principalsTranscoder;
  542. if (!JS_XDRUint32(xdr, &encodeable))
  543. goto error;
  544. if (encodeable) {
  545. if (!callbacks || !callbacks->principalsTranscoder) {
  546. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  547. JSMSG_CANT_DECODE_PRINCIPALS);
  548. goto error;
  549. }
  550. if (!callbacks->principalsTranscoder(xdr, &script->principals))
  551. goto error;
  552. if (xdr->mode == JSXDR_ENCODE)
  553. sameOriginPrincipals = script->principals == script->originPrincipals;
  554. if (!JS_XDRUint32(xdr, &sameOriginPrincipals))
  555. goto error;
  556. if (sameOriginPrincipals) {
  557. if (xdr->mode == JSXDR_DECODE) {
  558. script->originPrincipals = script->principals;
  559. JSPRINCIPALS_HOLD(cx, script->originPrincipals);
  560. }
  561. } else {
  562. if (!callbacks->principalsTranscoder(xdr, &script->originPrincipals))
  563. goto error;
  564. }
  565. }
  566. if (xdr->mode == JSXDR_DECODE) {
  567. script->lineno = (uintN)lineno;
  568. script->nslots = uint16_t(nslots);
  569. script->staticLevel = uint16_t(nslots >> 16);
  570. }
  571. for (i = 0; i != natoms; ++i) {
  572. if (!js_XDRAtom(xdr, &script->atoms[i]))
  573. goto error;
  574. }
  575. /*
  576. * Here looping from 0-to-length to xdr objects is essential. It ensures
  577. * that block objects from the script->objects array will be written and
  578. * restored in the outer-to-inner order. js_XDRBlockObject relies on this
  579. * to restore the parent chain.
  580. */
  581. for (i = 0; i != nobjects; ++i) {
  582. HeapPtr<JSObject> *objp = &script->objects()->vector[i];
  583. uint32_t isBlock;
  584. if (xdr->mode == JSXDR_ENCODE) {
  585. JSObject *obj = *objp;
  586. JS_ASSERT(obj->isFunction() || obj->isStaticBlock());
  587. isBlock = obj->isBlock() ? 1 : 0;
  588. }
  589. if (!JS_XDRUint32(xdr, &isBlock))
  590. goto error;
  591. if (isBlock == 0) {
  592. JSObject *tmp = *objp;
  593. if (!js_XDRFunctionObject(xdr, &tmp))
  594. goto error;
  595. *objp = tmp;
  596. } else {
  597. JS_ASSERT(isBlock == 1);
  598. StaticBlockObject *tmp = static_cast<StaticBlockObject *>(objp->get());
  599. if (!js_XDRStaticBlockObject(xdr, &tmp))
  600. goto error;
  601. *objp = tmp;
  602. }
  603. }
  604. for (i = 0; i != nupvars; ++i) {
  605. if (!JS_XDRUint32(xdr, reinterpret_cast<uint32_t *>(&script->upvars()->vector[i])))
  606. goto error;
  607. }
  608. for (i = 0; i != nregexps; ++i) {
  609. JSObject *tmp = script->regexps()->vector[i];
  610. if (!js_XDRRegExpObject(xdr, &tmp))
  611. goto error;
  612. script->regexps()->vector[i] = tmp;
  613. }
  614. for (i = 0; i != nClosedArgs; ++i) {
  615. if (!JS_XDRUint32(xdr, &script->closedSlots[i]))
  616. goto error;
  617. }
  618. for (i = 0; i != nClosedVars; ++i) {
  619. if (!JS_XDRUint32(xdr, &script->closedSlots[nClosedArgs + i]))
  620. goto error;
  621. }
  622. if (ntrynotes != 0) {
  623. /*
  624. * We combine tn->kind and tn->stackDepth when serializing as XDR is not
  625. * efficient when serializing small integer types.
  626. */
  627. JSTryNote *tn, *tnfirst;
  628. uint32_t kindAndDepth;
  629. JS_STATIC_ASSERT(sizeof(tn->kind) == sizeof(uint8_t));
  630. JS_STATIC_ASSERT(sizeof(tn->stackDepth) == sizeof(uint16_t));
  631. tnfirst = script->trynotes()->vector;
  632. JS_ASSERT(script->trynotes()->length == ntrynotes);
  633. tn = tnfirst + ntrynotes;
  634. do {
  635. --tn;
  636. if (xdr->mode == JSXDR_ENCODE) {
  637. kindAndDepth = (uint32_t(tn->kind) << 16)
  638. | uint32_t(tn->stackDepth);
  639. }
  640. if (!JS_XDRUint32(xdr, &kindAndDepth) ||
  641. !JS_XDRUint32(xdr, &tn->start) ||
  642. !JS_XDRUint32(xdr, &tn->length)) {
  643. goto error;
  644. }
  645. if (xdr->mode == JSXDR_DECODE) {
  646. tn->kind = uint8_t(kindAndDepth >> 16);
  647. tn->stackDepth = uint16_t(kindAndDepth);
  648. }
  649. } while (tn != tnfirst);
  650. }
  651. for (i = 0; i != nconsts; ++i) {
  652. Value tmp = script->consts()->vector[i];
  653. if (!JS_XDRValue(xdr, &tmp))
  654. goto error;
  655. script->consts()->vector[i] = tmp;
  656. }
  657. if (xdr->mode == JSXDR_DECODE && cx->hasRunOption(JSOPTION_PCCOUNT))
  658. (void) script->initCounts(cx);
  659. xdr->script = oldscript;
  660. return JS_TRUE;
  661. error:
  662. if (xdr->mode == JSXDR_DECODE)
  663. *scriptp = NULL;
  664. xdr->script = oldscript;
  665. return JS_FALSE;
  666. }
  667. #endif /* JS_HAS_XDR */
  668. bool
  669. JSScript::initCounts(JSContext *cx)
  670. {
  671. JS_ASSERT(!pcCounters);
  672. size_t count = 0;
  673. jsbytecode *pc, *next;
  674. for (pc = code; pc < code + length; pc = next) {
  675. count += OpcodeCounts::numCounts(JSOp(*pc));
  676. next = pc + GetBytecodeLength(pc);
  677. }
  678. size_t bytes = (length * sizeof(OpcodeCounts)) + (count * sizeof(double));
  679. char *cursor = (char *) cx->calloc_(bytes);
  680. if (!cursor)
  681. return false;
  682. DebugOnly<char *> base = cursor;
  683. pcCounters.counts = (OpcodeCounts *) cursor;
  684. cursor += length * sizeof(OpcodeCounts);
  685. for (pc = code; pc < code + length; pc = next) {
  686. pcCounters.counts[pc - code].counts = (double *) cursor;
  687. size_t capacity = OpcodeCounts::numCounts(JSOp(*pc));
  688. #ifdef DEBUG
  689. pcCounters.counts[pc - code].capacity = capacity;
  690. #endif
  691. cursor += capacity * sizeof(double);
  692. next = pc + GetBytecodeLength(pc);
  693. }
  694. JS_ASSERT(size_t(cursor - base) == bytes);
  695. /* Enable interrupts in any interpreter frames running on this script. */
  696. InterpreterFrames *frames;
  697. for (frames = cx->runtime->interpreterFrames; frames; frames = frames->older)
  698. frames->enableInterruptsIfRunning(this);
  699. return true;
  700. }
  701. void
  702. JSScript::destroyCounts(JSContext *cx)
  703. {
  704. if (pcCounters) {
  705. cx->free_(pcCounters.counts);
  706. pcCounters.counts = NULL;
  707. }
  708. }
  709. /*
  710. * Shared script filename management.
  711. */
  712. static const char *
  713. SaveScriptFilename(JSContext *cx, const char *filename)
  714. {
  715. JSCompartment *comp = cx->compartment;
  716. ScriptFilenameTable::AddPtr p = comp->scriptFilenameTable.lookupForAdd(filename);
  717. if (!p) {
  718. size_t size = offsetof(ScriptFilenameEntry, filename) + strlen(filename) + 1;
  719. ScriptFilenameEntry *entry = (ScriptFilenameEntry *) cx->malloc_(size);
  720. if (!entry)
  721. return NULL;
  722. entry->marked = false;
  723. strcpy(entry->filename, filename);
  724. if (!comp->scriptFilenameTable.add(p, entry)) {
  725. Foreground::free_(entry);
  726. JS_ReportOutOfMemory(cx);
  727. return NULL;
  728. }
  729. }
  730. return (*p)->filename;
  731. }
  732. /*
  733. * Back up from a saved filename by its offset within its hash table entry.
  734. */
  735. #define FILENAME_TO_SFE(fn) \
  736. ((ScriptFilenameEntry *) ((fn) - offsetof(ScriptFilenameEntry, filename)))
  737. void
  738. js_MarkScriptFilename(const char *filename)
  739. {
  740. ScriptFilenameEntry *sfe = FILENAME_TO_SFE(filename);
  741. sfe->marked = true;
  742. }
  743. void
  744. js_SweepScriptFilenames(JSCompartment *comp)
  745. {
  746. ScriptFilenameTable &table = comp->scriptFilenameTable;
  747. for (ScriptFilenameTable::Enum e(table); !e.empty(); e.popFront()) {
  748. ScriptFilenameEntry *entry = e.front();
  749. if (entry->marked) {
  750. entry->marked = false;
  751. } else if (!comp->rt->gcKeepAtoms) {
  752. Foreground::free_(entry);
  753. e.removeFront();
  754. }
  755. }
  756. }
  757. /*
  758. * JSScript data structures memory alignment:
  759. *
  760. * JSScript
  761. * JSObjectArray script objects' descriptor if JSScript.objectsOffset != 0,
  762. * use script->objects() to access it.
  763. * JSObjectArray script regexps' descriptor if JSScript.regexpsOffset != 0,
  764. * use script->regexps() to access it.
  765. * JSTryNoteArray script try notes' descriptor if JSScript.tryNotesOffset
  766. * != 0, use script->trynotes() to access it.
  767. * JSAtom *a[] array of JSScript.natoms atoms pointed by
  768. * JSScript.atoms if any.
  769. * JSObject *o[] array of script->objects()->length objects if any
  770. * pointed by script->objects()->vector.
  771. * JSObject *r[] array of script->regexps()->length regexps if any
  772. * pointed by script->regexps()->vector.
  773. * JSTryNote t[] array of script->trynotes()->length try notes if any
  774. * pointed by script->trynotes()->vector.
  775. * jsbytecode b[] script bytecode pointed by JSScript.code.
  776. * jssrcnote s[] script source notes, use script->notes() to access it
  777. *
  778. * The alignment avoids gaps between entries as alignment requirement for each
  779. * subsequent structure or array is the same or divides the alignment
  780. * requirement for the previous one.
  781. *
  782. * The followings asserts checks that assuming that the alignment requirement
  783. * for JSObjectArray and JSTryNoteArray are sizeof(void *) and for JSTryNote
  784. * it is sizeof(uint32_t) as the structure consists of 3 uint32_t fields.
  785. */
  786. JS_STATIC_ASSERT(sizeof(JSScript) % sizeof(void *) == 0);
  787. JS_STATIC_ASSERT(sizeof(JSObjectArray) % sizeof(void *) == 0);
  788. JS_STATIC_ASSERT(sizeof(JSTryNoteArray) == sizeof(JSObjectArray));
  789. JS_STATIC_ASSERT(sizeof(JSAtom *) == sizeof(JSObject *));
  790. JS_STATIC_ASSERT(sizeof(JSObject *) % sizeof(uint32_t) == 0);
  791. JS_STATIC_ASSERT(sizeof(JSTryNote) == 3 * sizeof(uint32_t));
  792. JS_STATIC_ASSERT(sizeof(uint32_t) % sizeof(jsbytecode) == 0);
  793. JS_STATIC_ASSERT(sizeof(jsbytecode) % sizeof(jssrcnote) == 0);
  794. /*
  795. * Check that uint8_t offsets is enough to reach any optional array allocated
  796. * after JSScript. For that we check that the maximum possible offset for
  797. * JSConstArray, that last optional array, still fits 1 byte and do not
  798. * coincide with INVALID_OFFSET.
  799. */
  800. JS_STATIC_ASSERT(sizeof(JSObjectArray) +
  801. sizeof(JSUpvarArray) +
  802. sizeof(JSObjectArray) +
  803. sizeof(JSTryNoteArray) +
  804. sizeof(js::GlobalSlotArray)
  805. < JSScript::INVALID_OFFSET);
  806. JS_STATIC_ASSERT(JSScript::INVALID_OFFSET <= 255);
  807. JSScript *
  808. JSScript::NewScript(JSContext *cx, uint32_t length, uint32_t nsrcnotes, uint32_t natoms,
  809. uint32_t nobjects, uint32_t nupvars, uint32_t nregexps,
  810. uint32_t ntrynotes, uint32_t nconsts, uint32_t nglobals,
  811. uint16_t nClosedArgs, uint16_t nClosedVars, uint32_t nTypeSets, JSVersion version)
  812. {
  813. size_t size = sizeof(JSAtom *) * natoms;
  814. if (nobjects != 0)
  815. size += sizeof(JSObjectArray) + nobjects * sizeof(JSObject *);
  816. if (nupvars != 0)
  817. size += sizeof(JSUpvarArray) + nupvars * sizeof(uint32_t);
  818. if (nregexps != 0)
  819. size += sizeof(JSObjectArray) + nregexps * sizeof(JSObject *);
  820. if (ntrynotes != 0)
  821. size += sizeof(JSTryNoteArray) + ntrynotes * sizeof(JSTryNote);
  822. if (nglobals != 0)
  823. size += sizeof(GlobalSlotArray) + nglobals * sizeof(GlobalSlotArray::Entry);
  824. uint32_t totalClosed = nClosedArgs + nClosedVars;
  825. if (totalClosed != 0)
  826. size += totalClosed * sizeof(uint32_t);
  827. /*
  828. * To esnure jsval alignment for the const array we place it immediately
  829. * after JSSomethingArray structs as their sizes all divide sizeof(jsval).
  830. * This works as long as the data itself is allocated with proper
  831. * alignment which we ensure below.
  832. */
  833. JS_STATIC_ASSERT(sizeof(JSObjectArray) % sizeof(jsval) == 0);
  834. JS_STATIC_ASSERT(sizeof(JSUpvarArray) % sizeof(jsval) == 0);
  835. JS_STATIC_ASSERT(sizeof(JSTryNoteArray) % sizeof(jsval) == 0);
  836. JS_STATIC_ASSERT(sizeof(GlobalSlotArray) % sizeof(jsval) == 0);
  837. JS_STATIC_ASSERT(sizeof(JSConstArray) % sizeof(jsval) == 0);
  838. if (nconsts != 0)
  839. size += sizeof(JSConstArray) + nconsts * sizeof(Value);
  840. size += length * sizeof(jsbytecode) + nsrcnotes * sizeof(jssrcnote);
  841. uint8_t *data = NULL;
  842. #if JS_SCRIPT_INLINE_DATA_LIMIT
  843. if (size <= JS_SCRIPT_INLINE_DATA_LIMIT) {
  844. /*
  845. * Check that if inlineData is big enough to store const values, we
  846. * can do that without any special alignment requirements given that
  847. * the script as a GC thing is always aligned on Cell::CellSize.
  848. */
  849. JS_STATIC_ASSERT(Cell::CellSize % sizeof(Value) == 0);
  850. JS_STATIC_ASSERT(JS_SCRIPT_INLINE_DATA_LIMIT < sizeof(Value) ||
  851. offsetof(JSScript, inlineData) % sizeof(Value) == 0);
  852. } else
  853. #endif
  854. {
  855. /*
  856. * We assume that calloc aligns on sizeof(Value) if the size we ask to
  857. * allocate divides sizeof(Value).
  858. */
  859. JS_STATIC_ASSERT(sizeof(Value) == sizeof(jsdouble));
  860. data = static_cast<uint8_t *>(cx->calloc_(JS_ROUNDUP(size, sizeof(Value))));
  861. if (!data)
  862. return NULL;
  863. }
  864. JSScript *script = js_NewGCScript(cx);
  865. if (!script) {
  866. Foreground::free_(data);
  867. return NULL;
  868. }
  869. PodZero(script);
  870. #ifdef JS_CRASH_DIAGNOSTICS
  871. script->cookie1[0] = script->cookie2[0] = JS_SCRIPT_COOKIE;
  872. #endif
  873. #if JS_SCRIPT_INLINE_DATA_LIMIT
  874. if (!data)
  875. data = script->inlineData;
  876. #endif
  877. script->data = data;
  878. script->length = length;
  879. script->version = version;
  880. new (&script->bindings) Bindings(cx);
  881. uint8_t *cursor = data;
  882. if (nobjects != 0) {
  883. script->objectsOffset = uint8_t(cursor - data);
  884. cursor += sizeof(JSObjectArray);
  885. } else {
  886. script->objectsOffset = JSScript::INVALID_OFFSET;
  887. }
  888. if (nupvars != 0) {
  889. script->upvarsOffset = uint8_t(cursor - data);
  890. cursor += sizeof(JSUpvarArray);
  891. } else {
  892. script->upvarsOffset = JSScript::INVALID_OFFSET;
  893. }
  894. if (nregexps != 0) {
  895. script->regexpsOffset = uint8_t(cursor - data);
  896. cursor += sizeof(JSObjectArray);
  897. } else {
  898. script->regexpsOffset = JSScript::INVALID_OFFSET;
  899. }
  900. if (ntrynotes != 0) {
  901. script->trynotesOffset = uint8_t(cursor - data);
  902. cursor += sizeof(JSTryNoteArray);
  903. } else {
  904. script->trynotesOffset = JSScript::INVALID_OFFSET;
  905. }
  906. if (nglobals != 0) {
  907. script->globalsOffset = uint8_t(cursor - data);
  908. cursor += sizeof(GlobalSlotArray);
  909. } else {
  910. script->globalsOffset = JSScript::INVALID_OFFSET;
  911. }
  912. JS_ASSERT(cursor - data < 0xFF);
  913. if (nconsts != 0) {
  914. script->constOffset = uint8_t(cursor - data);
  915. cursor += sizeof(JSConstArray);
  916. } else {
  917. script->constOffset = JSScript::INVALID_OFFSET;
  918. }
  919. JS_STATIC_ASSERT(sizeof(JSObjectArray) +
  920. sizeof(JSUpvarArray) +
  921. sizeof(JSObjectArray) +
  922. sizeof(JSTryNoteArray) +
  923. sizeof(GlobalSlotArray) < 0xFF);
  924. if (nconsts != 0) {
  925. JS_ASSERT(reinterpret_cast<uintptr_t>(cursor) % sizeof(jsval) == 0);
  926. script->consts()->length = nconsts;
  927. script->consts()->vector = (HeapValue *)cursor;
  928. cursor += nconsts * sizeof(script->consts()->vector[0]);
  929. }
  930. if (natoms != 0) {
  931. script->natoms = natoms;
  932. script->atoms = reinterpret_cast<JSAtom **>(cursor);
  933. cursor += natoms * sizeof(script->atoms[0]);
  934. }
  935. if (nobjects != 0) {
  936. script->objects()->length = nobjects;
  937. script->objects()->vector = (HeapPtr<JSObject> *)cursor;
  938. cursor += nobjects * sizeof(script->objects()->vector[0]);
  939. }
  940. if (nregexps != 0) {
  941. script->regexps()->length = nregexps;
  942. script->regexps()->vector = (HeapPtr<JSObject> *)cursor;
  943. cursor += nregexps * sizeof(script->regexps()->vector[0]);
  944. }
  945. if (ntrynotes != 0) {
  946. script->trynotes()->length = ntrynotes;
  947. script->trynotes()->vector = reinterpret_cast<JSTryNote *>(cursor);
  948. size_t vectorSize = ntrynotes * sizeof(script->trynotes()->vector[0]);
  949. #ifdef DEBUG
  950. memset(cursor, 0, vectorSize);
  951. #endif
  952. cursor += vectorSize;
  953. }
  954. if (nglobals != 0) {
  955. script->globals()->length = nglobals;
  956. script->globals()->vector = reinterpret_cast<GlobalSlotArray::Entry *>(cursor);
  957. cursor += nglobals * sizeof(script->globals()->vector[0]);
  958. }
  959. if (totalClosed != 0) {
  960. script->nClosedArgs = nClosedArgs;
  961. script->nClosedVars = nClosedVars;
  962. script->closedSlots = reinterpret_cast<uint32_t *>(cursor);
  963. cursor += totalClosed * sizeof(uint32_t);
  964. }
  965. JS_ASSERT(nTypeSets <= UINT16_MAX);
  966. script->nTypeSets = uint16_t(nTypeSets);
  967. /*
  968. * NB: We allocate the vector of uint32_t upvar cookies after all vectors of
  969. * pointers, to avoid misalignment on 64-bit platforms. See bug 514645.
  970. */
  971. if (nupvars != 0) {
  972. script->upvars()->length = nupvars;
  973. script->upvars()->vector = reinterpret_cast<UpvarCookie *>(cursor);
  974. cursor += nupvars * sizeof(script->upvars()->vector[0]);
  975. }
  976. script->code = (jsbytecode *)cursor;
  977. JS_ASSERT(cursor + length * sizeof(jsbytecode) + nsrcnotes * sizeof(jssrcnote) == data + size);
  978. #ifdef DEBUG
  979. script->id_ = 0;
  980. #endif
  981. JS_ASSERT(script->getVersion() == version);
  982. return script;
  983. }
  984. JSScript *
  985. JSScript::NewScriptFromEmitter(JSContext *cx, BytecodeEmitter *bce)
  986. {
  987. uint32_t mainLength, prologLength, nfixed;
  988. JSScript *script;
  989. const char *filename;
  990. JSFunction *fun;
  991. /* The counts of indexed things must be checked during code generation. */
  992. JS_ASSERT(bce->atomIndices->count() <= INDEX_LIMIT);
  993. JS_ASSERT(bce->objectList.length <= INDEX_LIMIT);
  994. JS_ASSERT(bce->regexpList.length <= INDEX_LIMIT);
  995. mainLength = bce->offset();
  996. prologLength = bce->prologOffset();
  997. if (!bce->bindings.ensureShape(cx))
  998. return NULL;
  999. uint32_t nsrcnotes = uint32_t(bce->countFinalSourceNotes());
  1000. uint16_t nClosedArgs = uint16_t(bce->closedArgs.length());
  1001. JS_ASSERT(nClosedArgs == bce->closedArgs.length());
  1002. uint16_t nClosedVars = uint16_t(bce->closedVars.length());
  1003. JS_ASSERT(nClosedVars == bce->closedVars.length());
  1004. size_t upvarIndexCount = bce->upvarIndices.hasMap() ? bce->upvarIndices->count() : 0;
  1005. script = NewScript(cx, prologLength + mainLength, nsrcnotes,
  1006. bce->atomIndices->count(), bce->objectList.length,
  1007. upvarIndexCount, bce->regexpList.length,
  1008. bce->ntrynotes, bce->constList.length(),
  1009. bce->globalUses.length(), nClosedArgs, nClosedVars,
  1010. bce->typesetCount, bce->version());
  1011. if (!script)
  1012. return NULL;
  1013. bce->bindings.makeImmutable();
  1014. JS_ASSERT(script->mainOffset == 0);
  1015. script->mainOffset = prologLength;
  1016. PodCopy<jsbytecode>(script->code, bce->prologBase(), prologLength);
  1017. PodCopy<jsbytecode>(script->main(), bce->base(), mainLength);
  1018. nfixed = bce->inFunction() ? bce->bindings.countVars() : 0;
  1019. JS_ASSERT(nfixed < SLOTNO_LIMIT);
  1020. script->nfixed = uint16_t(nfixed);
  1021. js_InitAtomMap(cx, bce->atomIndices.getMap(), script->atoms);
  1022. filename = bce->parser->tokenStream.getFilename();
  1023. if (filename) {
  1024. script->filename = SaveScriptFilename(cx, filename);
  1025. if (!script->filename)
  1026. return NULL;
  1027. }
  1028. script->lineno = bce->firstLine;
  1029. if (script->nfixed + bce->maxStackDepth >= JS_BIT(16)) {
  1030. ReportCompileErrorNumber(cx, bce->tokenStream(), NULL, JSREPORT_ERROR, JSMSG_NEED_DIET,
  1031. "script");
  1032. return NULL;
  1033. }
  1034. script->nslots = script->nfixed + bce->maxStackDepth;
  1035. script->staticLevel = uint16_t(bce->staticLevel);
  1036. script->principals = bce->parser->principals;
  1037. if (script->principals)
  1038. JSPRINCIPALS_HOLD(cx, script->principals);
  1039. /* Establish invariant: principals implies originPrincipals. */
  1040. script->originPrincipals = bce->parser->originPrincipals;
  1041. if (!script->originPrincipals)
  1042. script->originPrincipals = script->principals;
  1043. if (script->originPrincipals)
  1044. JSPRINCIPALS_HOLD(cx, script->originPrincipals);
  1045. script->sourceMap = (jschar *) bce->parser->tokenStream.releaseSourceMap();
  1046. if (!FinishTakingSrcNotes(cx, bce, script->notes()))
  1047. return NULL;
  1048. if (bce->ntrynotes != 0)
  1049. FinishTakingTryNotes(bce, script->trynotes());
  1050. if (bce->objectList.length != 0)
  1051. bce->objectList.finish(script->objects());
  1052. if (bce->regexpList.length != 0)
  1053. bce->regexpList.finish(script->regexps());
  1054. if (bce->constList.length() != 0)
  1055. bce->constList.finish(script->consts());
  1056. if (bce->flags & TCF_NO_SCRIPT_RVAL)
  1057. script->noScriptRval = true;
  1058. if (bce->flags & TCF_STRICT_MODE_CODE)
  1059. script->strictModeCode = true;
  1060. if (bce->flags & TCF_COMPILE_N_GO) {
  1061. script->compileAndGo = true;
  1062. const StackFrame *fp = bce->parser->callerFrame;
  1063. if (fp && fp->isFunctionFrame())
  1064. script->savedCallerFun = true;
  1065. }
  1066. if (bce->callsEval())
  1067. script->usesEval = true;
  1068. if (bce->flags & TCF_FUN_USES_ARGUMENTS)
  1069. script->usesArguments = true;
  1070. if (bce->flags & TCF_HAS_SINGLETONS)
  1071. script->hasSingletons = true;
  1072. if (bce->hasUpvarIndices()) {
  1073. JS_ASSERT(bce->upvarIndices->count() <= bce->upvarMap.length());
  1074. PodCopy<UpvarCookie>(script->upvars()->vector, bce->upvarMap.begin(),
  1075. bce->upvarIndices->count());
  1076. bce->upvarIndices->clear();
  1077. bce->upvarMap.clear();
  1078. }
  1079. if (bce->globalUses.length()) {
  1080. PodCopy<GlobalSlotArray::Entry>(script->globals()->vector, &bce->globalUses[0],
  1081. bce->globalUses.length());
  1082. }
  1083. if (script->nClosedArgs)
  1084. PodCopy<uint32_t>(script->closedSlots, &bce->closedArgs[0], script->nClosedArgs);
  1085. if (script->nClosedVars) {
  1086. PodCopy<uint32_t>(&script->closedSlots[script->nClosedArgs], &bce->closedVars[0],
  1087. script->nClosedVars);
  1088. }
  1089. script->bindings.transfer(cx, &bce->bindings);
  1090. fun = NULL;
  1091. if (bce->inFunction()) {
  1092. /*
  1093. * We initialize fun->script() to be the script constructed above
  1094. * so that the debugger has a valid fun->script().
  1095. */
  1096. fun = bce->fun();
  1097. JS_ASSERT(fun->isInterpreted());
  1098. JS_ASSERT(!fun->script());
  1099. #ifdef DEBUG
  1100. if (JSScript::isValidOffset(script->upvarsOffset))
  1101. JS_ASSERT(script->upvars()->length == script->bindings.countUpvars());
  1102. else
  1103. JS_ASSERT(script->bindings.countUpvars() == 0);
  1104. #endif
  1105. if (bce->flags & TCF_FUN_HEAVYWEIGHT)
  1106. fun->flags |= JSFUN_HEAVYWEIGHT;
  1107. /*
  1108. * Mark functions which will only be executed once as singletons.
  1109. * Skip this for flat closures, which must be copied on executing.
  1110. */
  1111. bool singleton =
  1112. cx->typeInferenceEnabled() &&
  1113. bce->parent &&
  1114. bce->parent->compiling() &&
  1115. bce->parent->asBytecodeEmitter()->checkSingletonContext() &&
  1116. !fun->isFlatClosure();
  1117. if (!script->typeSetFunction(cx, fun, singleton))
  1118. return NULL;
  1119. fun->setScript(script);
  1120. script->globalObject = fun->getParent() ? &fun->getParent()->global() : NULL;
  1121. } else {
  1122. /*
  1123. * Initialize script->object, if necessary, so that the debugger has a
  1124. * valid holder object.
  1125. */
  1126. if (bce->flags & TCF_NEED_SCRIPT_GLOBAL)
  1127. script->globalObject = GetCurrentGlobal(cx);
  1128. }
  1129. /* Tell the debugger about this compiled script. */
  1130. js_CallNewScriptHook(cx, script, fun);
  1131. if (!bce->parent) {
  1132. GlobalObject *compileAndGoGlobal = NULL;
  1133. if (script->compileAndGo) {
  1134. compileAndGoGlobal = script->globalObject;
  1135. if (!compileAndGoGlobal)
  1136. compileAndGoGlobal = &bce->scopeChain()->global();
  1137. }
  1138. Debugger::onNewScript(cx, script, compileAndGoGlobal);
  1139. }
  1140. if (cx->hasRunOption(JSOPTION_PCCOUNT))
  1141. (void) script->initCounts(cx);
  1142. return script;
  1143. }
  1144. size_t
  1145. JSScript::computedSizeOfData()
  1146. {
  1147. #if JS_SCRIPT_INLINE_DATA_LIMIT
  1148. if (data == inlineData)
  1149. return 0;
  1150. #endif
  1151. uint8_t *dataEnd = code + length * sizeof(jsbytecode) + numNotes() * sizeof(jssrcnote);
  1152. JS_ASSERT(dataEnd >= data);
  1153. return dataEnd - data;
  1154. }
  1155. size_t
  1156. JSScript::sizeOfData(JSMallocSizeOfFun mallocSizeOf)
  1157. {
  1158. #if JS_SCRIPT_INLINE_DATA_LIMIT
  1159. if (data == inlineData)
  1160. return 0;
  1161. #endif
  1162. return mallocSizeOf(data);
  1163. }
  1164. /*
  1165. * Nb: srcnotes are variable-length. This function computes the number of
  1166. * srcnote *slots*, which may be greater than the number of srcnotes.
  1167. */
  1168. uint32_t
  1169. JSScript::numNotes()
  1170. {
  1171. jssrcnote *sn;
  1172. jssrcnote *notes_ = notes();
  1173. for (sn = notes_; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn))
  1174. continue;
  1175. return sn - notes_ + 1; /* +1 for the terminator */
  1176. }
  1177. JS_FRIEND_API(void)
  1178. js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun)
  1179. {
  1180. JS_ASSERT(!script->callDestroyHook);
  1181. if (JSNewScriptHook hook = cx->debugHooks->newScriptHook) {
  1182. AutoKeepAtoms keep(cx->runtime);
  1183. hook(cx, script->filename, script->lineno, script, fun,
  1184. cx->debugHooks->newScriptHookData);
  1185. }
  1186. script->callDestroyHook = true;
  1187. }
  1188. void
  1189. js_CallDestroyScriptHook(JSContext *cx, JSScript *script)
  1190. {
  1191. if (!script->callDestroyHook)
  1192. return;
  1193. if (JSDestroyScriptHook hook = cx->debugHooks->destroyScriptHook)
  1194. hook(cx, script, cx->debugHooks->destroyScriptHookData);
  1195. script->callDestroyHook = false;
  1196. JS_ClearScriptTraps(cx, script);
  1197. }
  1198. void
  1199. JSScript::finalize(JSContext *cx, bool background)
  1200. {
  1201. CheckScript(this, NULL);
  1202. js_CallDestroyScriptHook(cx, this);
  1203. JS_ASSERT_IF(principals, originPrincipals);
  1204. if (principals)
  1205. JSPRINCIPALS_DROP(cx, principals);
  1206. if (originPrincipals)
  1207. JSPRINCIPALS_DROP(cx, originPrincipals);
  1208. if (types)
  1209. types->destroy();
  1210. #ifdef JS_METHODJIT
  1211. mjit::ReleaseScriptCode(cx, this);
  1212. #endif
  1213. destroyCounts(cx);
  1214. if (sourceMap)
  1215. cx->free_(sourceMap);
  1216. if (debug) {
  1217. jsbytecode *end = code + length;
  1218. for (jsbytecode *pc = code; pc < end; pc++) {
  1219. if (BreakpointSite *site = getBreakpointSite(pc)) {
  1220. /* Breakpoints are swept before finalization. */
  1221. JS_ASSERT(site->firstBreakpoint() == NULL);
  1222. site->clearTrap(cx, NULL, NULL);
  1223. JS_ASSERT(getBreakpointSite(pc) == NULL);
  1224. }
  1225. }
  1226. cx->free_(debug);
  1227. }
  1228. #if JS_SCRIPT_INLINE_DATA_LIMIT
  1229. if (data != inlineData)
  1230. #endif
  1231. {
  1232. JS_POISON(data, 0xdb, computedSizeOfData());
  1233. cx->free_(data);
  1234. }
  1235. }
  1236. namespace js {
  1237. static const uint32_t GSN_CACHE_THRESHOLD = 100;
  1238. static const uint32_t GSN_CACHE_MAP_INIT_SIZE = 20;
  1239. void
  1240. GSNCache::purge()
  1241. {
  1242. code = NULL;
  1243. if (map.initialized())
  1244. map.finish();
  1245. }
  1246. } /* namespace js */
  1247. jssrcnote *
  1248. js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc)
  1249. {
  1250. size_t target = pc - script->code;
  1251. if (target >= size_t(script->length))
  1252. return NULL;
  1253. GSNCache *cache = GetGSNCache(cx);
  1254. if (cache->code == script->code) {
  1255. JS_ASSERT(cache->map.initialized());
  1256. GSNCache::Map::Ptr p = cache->map.lookup(pc);
  1257. return p ? p->value : NULL;
  1258. }
  1259. size_t offset = 0;
  1260. jssrcnote *result;
  1261. for (jssrcnote *sn = script->notes(); ; sn = SN_NEXT(sn)) {
  1262. if (SN_IS_TERMINATOR(sn)) {
  1263. result = NULL;
  1264. break;
  1265. }
  1266. offset += SN_DELTA(sn);
  1267. if (offset == target && SN_IS_GETTABLE(sn)) {
  1268. result = sn;
  1269. break;
  1270. }
  1271. }
  1272. if (cache->code != script->code && script->length >= GSN_CACHE_THRESHOLD) {
  1273. uintN nsrcnotes = 0;
  1274. for (jssrcnote *sn = script->notes(); !SN_IS_TERMINATOR(sn);
  1275. sn = SN_NEXT(sn)) {
  1276. if (SN_IS_GETTABLE(sn))
  1277. ++nsrcnotes;
  1278. }
  1279. if (cache->code) {
  1280. JS_ASSERT(cache->map.initialized());
  1281. cache->map.finish();
  1282. cache->code = NULL;
  1283. }
  1284. if (cache->map.init(nsrcnotes)) {
  1285. pc = script->code;
  1286. for (jssrcnote *sn = script->notes(); !SN_IS_TERMINATOR(sn);
  1287. sn = SN_NEXT(sn)) {
  1288. pc += SN_DELTA(sn);
  1289. if (SN_IS_GETTABLE(sn))
  1290. JS_ALWAYS_TRUE(cache->map.put(pc, sn));
  1291. }
  1292. cache->code = script->code;
  1293. }
  1294. }
  1295. return result;
  1296. }
  1297. uintN
  1298. js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc)
  1299. {
  1300. /* Cope with StackFrame.pc value prior to entering js_Interpret. */
  1301. if (!pc)
  1302. return 0;
  1303. /*
  1304. * Special case: function definition needs no line number note because
  1305. * the function's script contains its starting line number.
  1306. */
  1307. JSOp op = JSOp(*pc);
  1308. if (js_CodeSpec[op].format & JOF_INDEXBASE)
  1309. pc += js_CodeSpec[op].length;
  1310. if (*pc == JSOP_DEFFUN)
  1311. return script->getFunction(GET_UINT32_INDEX(pc))->script()->lineno;
  1312. /*
  1313. * General case: walk through source notes accumulating their deltas,
  1314. * keeping track of line-number notes, until we pass the note for pc's
  1315. * offset within script->code.
  1316. */
  1317. uintN lineno = script->lineno;
  1318. ptrdiff_t offset = 0;
  1319. ptrdiff_t target = pc - script->code;
  1320. for (jssrcnote *sn = script->notes(); !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  1321. offset += SN_DELTA(sn);
  1322. SrcNoteType type = (SrcNoteType) SN_TYPE(sn);
  1323. if (type == SRC_SETLINE) {
  1324. if (offset <= target)
  1325. lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
  1326. } else if (type == SRC_NEWLINE) {
  1327. if (offset <= target)
  1328. lineno++;
  1329. }
  1330. if (offset > target)
  1331. break;
  1332. }
  1333. return lineno;
  1334. }
  1335. /* The line number limit is the same as the jssrcnote offset limit. */
  1336. #define SN_LINE_LIMIT (SN_3BYTE_OFFSET_FLAG << 16)
  1337. jsbytecode *
  1338. js_LineNumberToPC(JSScript *script, uintN target)
  1339. {
  1340. ptrdiff_t offset = 0;
  1341. ptrdiff_t best = -1;
  1342. uintN lineno = script->lineno;
  1343. uintN bestdiff = SN_LINE_LIMIT;
  1344. for (jssrcnote *sn = script->notes(); !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  1345. /*
  1346. * Exact-match only if offset is not in the prolog; otherwise use
  1347. * nearest greater-or-equal line number match.
  1348. */
  1349. if (lineno == target && offset >= ptrdiff_t(script->mainOffset))
  1350. goto out;
  1351. if (lineno >= target) {
  1352. uintN diff = lineno - target;
  1353. if (diff < bestdiff) {
  1354. bestdiff = diff;
  1355. best = offset;
  1356. }
  1357. }
  1358. offset += SN_DELTA(sn);
  1359. SrcNoteType type = (SrcNoteType) SN_TYPE(sn);
  1360. if (type == SRC_SETLINE) {
  1361. lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
  1362. } else if (type == SRC_NEWLINE) {
  1363. lineno++;
  1364. }
  1365. }
  1366. if (best >= 0)
  1367. offset = best;
  1368. out:
  1369. return script->code + offset;
  1370. }
  1371. JS_FRIEND_API(uintN)
  1372. js_GetScriptLineExtent(JSScript *script)
  1373. {
  1374. uintN lineno = script->lineno;
  1375. uintN maxLineNo = 0;
  1376. bool counting = true;
  1377. for (jssrcnote *sn = script->notes(); !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  1378. SrcNoteType type = (SrcNoteType) SN_TYPE(sn);
  1379. if (type == SRC_SETLINE) {
  1380. if (maxLineNo < lineno)
  1381. maxLineNo = lineno;
  1382. lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
  1383. counting = true;
  1384. if (maxLineNo < lineno)
  1385. maxLineNo = lineno;
  1386. else
  1387. counting = false;
  1388. } else if (type == SRC_NEWLINE) {
  1389. if (counting)
  1390. lineno++;
  1391. }
  1392. }
  1393. if (maxLineNo > lineno)
  1394. lineno = maxLineNo;
  1395. return 1 + lineno - script->lineno;
  1396. }
  1397. namespace js {
  1398. uintN
  1399. CurrentLine(JSContext *cx)
  1400. {
  1401. return js_PCToLineNumber(cx, cx->fp()->script(), cx->regs().pc);
  1402. }
  1403. void
  1404. CurrentScriptFileLineOriginSlow(JSContext *cx, const char **file, uintN *linenop,
  1405. JSPrincipals **origin)
  1406. {
  1407. FrameRegsIter iter(cx);
  1408. while (!iter.done() && !iter.fp()->isScriptFrame())
  1409. ++iter;
  1410. if (iter.done()) {
  1411. *file = NULL;
  1412. *linenop = 0;
  1413. *origin = NULL;
  1414. return;
  1415. }
  1416. JSScript *script = iter.fp()->script();
  1417. *file = script->filename;
  1418. *linenop = js_PCToLineNumber(cx, iter.fp()->script(), iter.pc());
  1419. *origin = script->originPrincipals;
  1420. }
  1421. } /* namespace js */
  1422. class DisablePrincipalsTranscoding {
  1423. JSSecurityCallbacks *callbacks;
  1424. JSPrincipalsTranscoder temp;
  1425. public:
  1426. DisablePrincipalsTranscoding(JSContext *cx)
  1427. : callbacks(JS_GetRuntimeSecurityCallbacks(cx->runtime)),
  1428. temp(NULL)
  1429. {
  1430. if (callbacks) {
  1431. temp = callbacks->principalsTranscoder;
  1432. callbacks->principalsTranscoder = NULL;
  1433. }
  1434. }
  1435. ~DisablePrincipalsTranscoding() {
  1436. if (callbacks)
  1437. callbacks->principalsTranscoder = temp;
  1438. }
  1439. };
  1440. class AutoJSXDRState {
  1441. public:
  1442. AutoJSXDRState(JSXDRState *x
  1443. JS_GUARD_OBJECT_NOTIFIER_PARAM)
  1444. : xdr(x)
  1445. {
  1446. JS_GUARD_OBJECT_NOTIFIER_INIT;
  1447. }
  1448. ~AutoJSXDRState()
  1449. {
  1450. JS_XDRDestroy(xdr);
  1451. }
  1452. operator JSXDRState*() const
  1453. {
  1454. return xdr;
  1455. }
  1456. private:
  1457. JSXDRState *const xdr;
  1458. JS_DECL_USE_GUARD_OBJECT_NOTIFIER
  1459. };
  1460. JSScript *
  1461. js_CloneScript(JSContext *cx, JSScript *script)
  1462. {
  1463. JS_ASSERT(cx->compartment != script->compartment());
  1464. // serialize script
  1465. AutoJSXDRState w(JS_XDRNewMem(cx, JSXDR_ENCODE));
  1466. if (!w)
  1467. return NULL;
  1468. // we don't want gecko to transcribe our principals for us
  1469. DisablePrincipalsTranscoding disable(cx);
  1470. XDRScriptState wstate(w);
  1471. #ifdef DEBUG
  1472. wstate.filename = script->filename;
  1473. #endif
  1474. if (!js_XDRScript(w, &script))
  1475. return NULL;
  1476. uint32_t nbytes;
  1477. void *p = JS_XDRMemGetData(w, &nbytes);
  1478. if (!p)
  1479. return NULL;
  1480. // de-serialize script
  1481. AutoJSXDRState r(JS_XDRNewMem(cx, JSXDR_DECODE));
  1482. if (!r)
  1483. return NULL;
  1484. // Hand p off from w to r. Don't want them to share the data
  1485. // mem, lest they both try to free it in JS_XDRDestroy
  1486. JS_XDRMemSetData(r, p, nbytes);
  1487. JS_XDRMemSetData(w, NULL, 0);
  1488. XDRScriptState rstate(r);
  1489. rstate.filename = script->filename;
  1490. rstate.filenameSaved = true;
  1491. JSScript *newScript = NULL;
  1492. if (!js_XDRScript(r, &newScript))
  1493. return NULL;
  1494. // set the proper principals for the script's new compartment
  1495. // the originPrincipals are not related to compartment, so just copy
  1496. newScript->principals = newScript->compartment()->principals;
  1497. newScript->originPrincipals = script->originPrincipals;
  1498. if (!newScript->originPrincipals)
  1499. newScript->originPrincipals = newScript->principals;
  1500. if (newScript->principals) {
  1501. JSPRINCIPALS_HOLD(cx, newScript->principals);
  1502. JSPRINCIPALS_HOLD(cx, newScript->originPrincipals);
  1503. }
  1504. return newScript;
  1505. }
  1506. void
  1507. JSScript::copyClosedSlotsTo(JSScript *other)
  1508. {
  1509. js_memcpy(other->closedSlots, closedSlots, nClosedArgs + nClosedVars);
  1510. }
  1511. bool
  1512. JSScript::ensureHasDebug(JSContext *cx)
  1513. {
  1514. if (debug)
  1515. return true;
  1516. size_t nbytes = offsetof(DebugScript, breakpoints) + length * sizeof(BreakpointSite*);
  1517. debug = (DebugScript *) cx->calloc_(nbytes);
  1518. if (!debug)
  1519. return false;
  1520. /*
  1521. * Ensure that any Interpret() instances running on this script have
  1522. * interrupts enabled. The interrupts must stay enabled until the
  1523. * debug state is destroyed.
  1524. */
  1525. InterpreterFrames *frames;
  1526. for (frames = cx->runtime->interpreterFrames; frames; frames = frames->older)
  1527. frames->enableInterruptsIfRunning(this);
  1528. return true;
  1529. }
  1530. bool
  1531. JSScript::recompileForStepMode(JSContext *cx)
  1532. {
  1533. #ifdef JS_METHODJIT
  1534. if (jitNormal || jitCtor) {
  1535. mjit::Recompiler::clearStackReferences(cx, this);
  1536. mjit::ReleaseScriptCode(cx, this);
  1537. }
  1538. #endif
  1539. return true;
  1540. }
  1541. bool
  1542. JSScript::tryNewStepMode(JSContext *cx, uint32_t newValue)
  1543. {
  1544. JS_ASSERT(debug);
  1545. uint32_t prior = debug->stepMode;
  1546. debug->stepMode = newValue;
  1547. if (!prior != !newValue) {
  1548. /* Step mode has been enabled or disabled. Alert the methodjit. */
  1549. if (!recompileForStepMode(cx)) {
  1550. debug->stepMode = prior;
  1551. return false;
  1552. }
  1553. if (!stepModeEnabled() && !debug->numSites) {
  1554. cx->free_(debug);
  1555. debug = NULL;
  1556. }
  1557. }
  1558. return true;
  1559. }
  1560. bool
  1561. JSScript::setStepModeFlag(JSContext *cx, bool step)
  1562. {
  1563. if (!ensureHasDebug(cx))
  1564. return false;
  1565. return tryNewStepMode(cx, (debug->stepMode & stepCountMask) | (step ? stepFlagMask : 0));
  1566. }
  1567. bool
  1568. JSScript::changeStepModeCount(JSContext *cx, int delta)
  1569. {
  1570. if (!ensureHasDebug(cx))
  1571. return false;
  1572. assertSameCompartment(cx, this);
  1573. JS_ASSERT_IF(delta > 0, cx->compartment->debugMode());
  1574. uint32_t count = debug->stepMode & stepCountMask;
  1575. JS_ASSERT(((count + delta) & stepCountMask) == count + delta);
  1576. return tryNewStepMode(cx,
  1577. (debug->stepMode & stepFlagMask) |
  1578. ((count + delta) & stepCountMask));
  1579. }
  1580. BreakpointSite *
  1581. JSScript::getOrCreateBreakpointSite(JSContext *cx, jsbytecode *pc,
  1582. GlobalObject *scriptGlobal)
  1583. {
  1584. JS_ASSERT(size_t(pc - code) < length);
  1585. if (!ensureHasDebug(cx))
  1586. return NULL;
  1587. BreakpointSite *&site = debug->breakpoints[pc - code];
  1588. if (!site) {
  1589. site = cx->runtime->new_<BreakpointSite>(this, pc);
  1590. if (!site) {
  1591. js_ReportOutOfMemory(cx);
  1592. return NULL;
  1593. }
  1594. debug->numSites++;
  1595. }
  1596. if (site->scriptGlobal)
  1597. JS_ASSERT_IF(scriptGlobal, site->scriptGlobal == scriptGlobal);
  1598. else
  1599. site->scriptGlobal = scriptGlobal;
  1600. return site;
  1601. }
  1602. void
  1603. JSScript::destroyBreakpointSite(JSRuntime *rt, jsbytecode *pc)
  1604. {
  1605. JS_ASSERT(unsigned(pc - code) < length);
  1606. BreakpointSite *&site = debug->breakpoints[pc - code];
  1607. JS_ASSERT(site);
  1608. rt->delete_(site);
  1609. site = NULL;
  1610. if (--debug->numSites == 0 && !stepModeEnabled()) {
  1611. rt->free_(debug);
  1612. debug = NULL;
  1613. }
  1614. }
  1615. void
  1616. JSScript::clearBreakpointsIn(JSContext *cx, js::Debugger *dbg, JSObject *handler)
  1617. {
  1618. if (!hasAnyBreakpointsOrStepMode())
  1619. return;
  1620. jsbytecode *end = code + length;
  1621. for (jsbytecode *pc = code; pc < end; pc++) {
  1622. BreakpointSite *site = getBreakpointSite(pc);
  1623. if (site) {
  1624. Breakpoint *nextbp;
  1625. for (Breakpoint *bp = site->firstBreakpoint(); bp; bp = nextbp) {
  1626. nextbp = bp->nextInSite();
  1627. if ((!dbg || bp->debugger == dbg) && (!handler || bp->getHandler() == handler))
  1628. bp->destroy(cx);
  1629. }
  1630. }
  1631. }
  1632. }
  1633. void
  1634. JSScript::clearTraps(JSContext *cx)
  1635. {
  1636. if (!hasAnyBreakpointsOrStepMode())
  1637. return;
  1638. jsbytecode *end = code + length;
  1639. for (jsbytecode *pc = code; pc < end; pc++) {
  1640. BreakpointSite *site = getBreakpointSite(pc);
  1641. if (site)
  1642. site->clearTrap(cx);
  1643. }
  1644. }
  1645. void
  1646. JSScript::markTrapClosures(JSTracer *trc)
  1647. {
  1648. JS_ASSERT(hasAnyBreakpointsOrStepMode());
  1649. for (unsigned i = 0; i < length; i++) {
  1650. BreakpointSite *site = debug->breakpoints[i];
  1651. if (site && site->trapHandler)
  1652. MarkValue(trc, site->trapClosure, "trap closure");
  1653. }
  1654. }