PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsfun.cpp

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ | 2101 lines | 1530 code | 230 blank | 341 comment | 355 complexity | f75c575ae46a95c89a215ee036a1f7df MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=99:
  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. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either of the GNU General Public License Version 2 or later (the "GPL"),
  29. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. /*
  41. * JS function support.
  42. */
  43. #include "jsstddef.h"
  44. #include <string.h>
  45. #include "jstypes.h"
  46. #include "jsbit.h"
  47. #include "jsutil.h" /* Added by JSIFY */
  48. #include "jsapi.h"
  49. #include "jsarray.h"
  50. #include "jsatom.h"
  51. #include "jsbuiltins.h"
  52. #include "jscntxt.h"
  53. #include "jsversion.h"
  54. #include "jsdbgapi.h"
  55. #include "jsemit.h"
  56. #include "jsfun.h"
  57. #include "jsgc.h"
  58. #include "jsinterp.h"
  59. #include "jslock.h"
  60. #include "jsnum.h"
  61. #include "jsobj.h"
  62. #include "jsopcode.h"
  63. #include "jsparse.h"
  64. #include "jsscan.h"
  65. #include "jsscope.h"
  66. #include "jsscript.h"
  67. #include "jsstr.h"
  68. #include "jsexn.h"
  69. #include "jsstaticcheck.h"
  70. #if JS_HAS_GENERATORS
  71. # include "jsiter.h"
  72. #endif
  73. #if JS_HAS_XDR
  74. # include "jsxdrapi.h"
  75. #endif
  76. /* Generic function/call/arguments tinyids -- also reflected bit numbers. */
  77. enum {
  78. CALL_ARGUMENTS = -1, /* predefined arguments local variable */
  79. ARGS_LENGTH = -2, /* number of actual args, arity if inactive */
  80. ARGS_CALLEE = -3, /* reference from arguments to active funobj */
  81. FUN_ARITY = -4, /* number of formal parameters; desired argc */
  82. FUN_NAME = -5, /* function name, "" if anonymous */
  83. FUN_CALLER = -6 /* Function.prototype.caller, backward compat */
  84. };
  85. #if JSFRAME_OVERRIDE_BITS < 8
  86. # error "not enough override bits in JSStackFrame.flags!"
  87. #endif
  88. #define TEST_OVERRIDE_BIT(fp, tinyid) \
  89. ((fp)->flags & JS_BIT(JSFRAME_OVERRIDE_SHIFT - ((tinyid) + 1)))
  90. #define SET_OVERRIDE_BIT(fp, tinyid) \
  91. ((fp)->flags |= JS_BIT(JSFRAME_OVERRIDE_SHIFT - ((tinyid) + 1)))
  92. JSBool
  93. js_GetArgsValue(JSContext *cx, JSStackFrame *fp, jsval *vp)
  94. {
  95. JSObject *argsobj;
  96. if (TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
  97. JS_ASSERT(fp->callobj);
  98. return OBJ_GET_PROPERTY(cx, fp->callobj,
  99. ATOM_TO_JSID(cx->runtime->atomState
  100. .argumentsAtom),
  101. vp);
  102. }
  103. argsobj = js_GetArgsObject(cx, fp);
  104. if (!argsobj)
  105. return JS_FALSE;
  106. *vp = OBJECT_TO_JSVAL(argsobj);
  107. return JS_TRUE;
  108. }
  109. static JSBool
  110. MarkArgDeleted(JSContext *cx, JSStackFrame *fp, uintN slot)
  111. {
  112. JSObject *argsobj;
  113. jsval bmapval, bmapint;
  114. size_t nbits, nbytes;
  115. jsbitmap *bitmap;
  116. argsobj = fp->argsobj;
  117. (void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
  118. nbits = fp->argc;
  119. JS_ASSERT(slot < nbits);
  120. if (JSVAL_IS_VOID(bmapval)) {
  121. if (nbits <= JSVAL_INT_BITS) {
  122. bmapint = 0;
  123. bitmap = (jsbitmap *) &bmapint;
  124. } else {
  125. nbytes = JS_HOWMANY(nbits, JS_BITS_PER_WORD) * sizeof(jsbitmap);
  126. bitmap = (jsbitmap *) JS_malloc(cx, nbytes);
  127. if (!bitmap)
  128. return JS_FALSE;
  129. memset(bitmap, 0, nbytes);
  130. bmapval = PRIVATE_TO_JSVAL(bitmap);
  131. JS_SetReservedSlot(cx, argsobj, 0, bmapval);
  132. }
  133. } else {
  134. if (nbits <= JSVAL_INT_BITS) {
  135. bmapint = JSVAL_TO_INT(bmapval);
  136. bitmap = (jsbitmap *) &bmapint;
  137. } else {
  138. bitmap = (jsbitmap *) JSVAL_TO_PRIVATE(bmapval);
  139. }
  140. }
  141. JS_SET_BIT(bitmap, slot);
  142. if (bitmap == (jsbitmap *) &bmapint) {
  143. bmapval = INT_TO_JSVAL(bmapint);
  144. JS_SetReservedSlot(cx, argsobj, 0, bmapval);
  145. }
  146. return JS_TRUE;
  147. }
  148. /* NB: Infallible predicate, false does not mean error/exception. */
  149. static JSBool
  150. ArgWasDeleted(JSContext *cx, JSStackFrame *fp, uintN slot)
  151. {
  152. JSObject *argsobj;
  153. jsval bmapval, bmapint;
  154. jsbitmap *bitmap;
  155. argsobj = fp->argsobj;
  156. (void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
  157. if (JSVAL_IS_VOID(bmapval))
  158. return JS_FALSE;
  159. if (fp->argc <= JSVAL_INT_BITS) {
  160. bmapint = JSVAL_TO_INT(bmapval);
  161. bitmap = (jsbitmap *) &bmapint;
  162. } else {
  163. bitmap = (jsbitmap *) JSVAL_TO_PRIVATE(bmapval);
  164. }
  165. return JS_TEST_BIT(bitmap, slot) != 0;
  166. }
  167. JSBool
  168. js_GetArgsProperty(JSContext *cx, JSStackFrame *fp, jsid id, jsval *vp)
  169. {
  170. jsval val;
  171. JSObject *obj;
  172. uintN slot;
  173. if (TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
  174. JS_ASSERT(fp->callobj);
  175. if (!OBJ_GET_PROPERTY(cx, fp->callobj,
  176. ATOM_TO_JSID(cx->runtime->atomState
  177. .argumentsAtom),
  178. &val)) {
  179. return JS_FALSE;
  180. }
  181. if (JSVAL_IS_PRIMITIVE(val)) {
  182. obj = js_ValueToNonNullObject(cx, val);
  183. if (!obj)
  184. return JS_FALSE;
  185. } else {
  186. obj = JSVAL_TO_OBJECT(val);
  187. }
  188. return OBJ_GET_PROPERTY(cx, obj, id, vp);
  189. }
  190. *vp = JSVAL_VOID;
  191. if (JSID_IS_INT(id)) {
  192. slot = (uintN) JSID_TO_INT(id);
  193. if (slot < fp->argc) {
  194. if (fp->argsobj && ArgWasDeleted(cx, fp, slot))
  195. return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
  196. *vp = fp->argv[slot];
  197. } else {
  198. /*
  199. * Per ECMA-262 Ed. 3, 10.1.8, last bulleted item, do not share
  200. * storage between the formal parameter and arguments[k] for all
  201. * fp->argc <= k && k < fp->fun->nargs. For example, in
  202. *
  203. * function f(x) { x = 42; return arguments[0]; }
  204. * f();
  205. *
  206. * the call to f should return undefined, not 42. If fp->argsobj
  207. * is null at this point, as it would be in the example, return
  208. * undefined in *vp.
  209. */
  210. if (fp->argsobj)
  211. return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
  212. }
  213. } else {
  214. if (id == ATOM_TO_JSID(cx->runtime->atomState.lengthAtom)) {
  215. if (fp->argsobj && TEST_OVERRIDE_BIT(fp, ARGS_LENGTH))
  216. return OBJ_GET_PROPERTY(cx, fp->argsobj, id, vp);
  217. *vp = INT_TO_JSVAL((jsint) fp->argc);
  218. }
  219. }
  220. return JS_TRUE;
  221. }
  222. JSObject *
  223. js_GetArgsObject(JSContext *cx, JSStackFrame *fp)
  224. {
  225. JSObject *argsobj, *global, *parent;
  226. /*
  227. * We must be in a function activation; the function must be lightweight
  228. * or else fp must have a variable object.
  229. */
  230. JS_ASSERT(fp->fun && (!(fp->fun->flags & JSFUN_HEAVYWEIGHT) || fp->varobj));
  231. /* Skip eval and debugger frames. */
  232. while (fp->flags & JSFRAME_SPECIAL)
  233. fp = fp->down;
  234. /* Create an arguments object for fp only if it lacks one. */
  235. argsobj = fp->argsobj;
  236. if (argsobj)
  237. return argsobj;
  238. /* Link the new object to fp so it can get actual argument values. */
  239. argsobj = js_NewObject(cx, &js_ArgumentsClass, NULL, NULL, 0);
  240. if (!argsobj || !JS_SetPrivate(cx, argsobj, fp)) {
  241. cx->weakRoots.newborn[GCX_OBJECT] = NULL;
  242. return NULL;
  243. }
  244. /*
  245. * Give arguments an intrinsic scope chain link to fp's global object.
  246. * Since the arguments object lacks a prototype because js_ArgumentsClass
  247. * is not initialized, js_NewObject won't assign a default parent to it.
  248. *
  249. * Therefore if arguments is used as the head of an eval scope chain (via
  250. * a direct or indirect call to eval(program, arguments)), any reference
  251. * to a standard class object in the program will fail to resolve due to
  252. * js_GetClassPrototype not being able to find a global object containing
  253. * the standard prototype by starting from arguments and following parent.
  254. */
  255. global = fp->scopeChain;
  256. while ((parent = OBJ_GET_PARENT(cx, global)) != NULL)
  257. global = parent;
  258. STOBJ_SET_PARENT(argsobj, global);
  259. fp->argsobj = argsobj;
  260. return argsobj;
  261. }
  262. static JSBool
  263. args_enumerate(JSContext *cx, JSObject *obj);
  264. JS_FRIEND_API(JSBool)
  265. js_PutArgsObject(JSContext *cx, JSStackFrame *fp)
  266. {
  267. JSObject *argsobj;
  268. jsval bmapval, rval;
  269. JSBool ok;
  270. JSRuntime *rt;
  271. /*
  272. * Reuse args_enumerate here to reflect fp's actual arguments as indexed
  273. * elements of argsobj. Do this first, before clearing and freeing the
  274. * deleted argument slot bitmap, because args_enumerate depends on that.
  275. */
  276. argsobj = fp->argsobj;
  277. ok = args_enumerate(cx, argsobj);
  278. /*
  279. * Now clear the deleted argument number bitmap slot and free the bitmap,
  280. * if one was actually created due to 'delete arguments[0]' or similar.
  281. */
  282. (void) JS_GetReservedSlot(cx, argsobj, 0, &bmapval);
  283. if (!JSVAL_IS_VOID(bmapval)) {
  284. JS_SetReservedSlot(cx, argsobj, 0, JSVAL_VOID);
  285. if (fp->argc > JSVAL_INT_BITS)
  286. JS_free(cx, JSVAL_TO_PRIVATE(bmapval));
  287. }
  288. /*
  289. * Now get the prototype properties so we snapshot fp->fun and fp->argc
  290. * before fp goes away.
  291. */
  292. rt = cx->runtime;
  293. ok &= js_GetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.calleeAtom),
  294. &rval);
  295. ok &= js_SetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.calleeAtom),
  296. &rval);
  297. ok &= js_GetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.lengthAtom),
  298. &rval);
  299. ok &= js_SetProperty(cx, argsobj, ATOM_TO_JSID(rt->atomState.lengthAtom),
  300. &rval);
  301. /*
  302. * Clear the private pointer to fp, which is about to go away (js_Invoke).
  303. * Do this last because the args_enumerate and js_GetProperty calls above
  304. * need to follow the private slot to find fp.
  305. */
  306. ok &= JS_SetPrivate(cx, argsobj, NULL);
  307. fp->argsobj = NULL;
  308. return ok;
  309. }
  310. static JSBool
  311. args_delProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  312. {
  313. jsint slot;
  314. JSStackFrame *fp;
  315. if (!JSVAL_IS_INT(id))
  316. return JS_TRUE;
  317. fp = (JSStackFrame *)
  318. JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
  319. if (!fp)
  320. return JS_TRUE;
  321. JS_ASSERT(fp->argsobj);
  322. slot = JSVAL_TO_INT(id);
  323. switch (slot) {
  324. case ARGS_CALLEE:
  325. case ARGS_LENGTH:
  326. SET_OVERRIDE_BIT(fp, slot);
  327. break;
  328. default:
  329. if ((uintN)slot < fp->argc && !MarkArgDeleted(cx, fp, slot))
  330. return JS_FALSE;
  331. break;
  332. }
  333. return JS_TRUE;
  334. }
  335. static JSBool
  336. args_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  337. {
  338. jsint slot;
  339. JSStackFrame *fp;
  340. if (!JSVAL_IS_INT(id))
  341. return JS_TRUE;
  342. fp = (JSStackFrame *)
  343. JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
  344. if (!fp)
  345. return JS_TRUE;
  346. JS_ASSERT(fp->argsobj);
  347. slot = JSVAL_TO_INT(id);
  348. switch (slot) {
  349. case ARGS_CALLEE:
  350. if (!TEST_OVERRIDE_BIT(fp, slot))
  351. *vp = OBJECT_TO_JSVAL(fp->callee);
  352. break;
  353. case ARGS_LENGTH:
  354. if (!TEST_OVERRIDE_BIT(fp, slot))
  355. *vp = INT_TO_JSVAL((jsint)fp->argc);
  356. break;
  357. default:
  358. if ((uintN)slot < fp->argc && !ArgWasDeleted(cx, fp, slot))
  359. *vp = fp->argv[slot];
  360. break;
  361. }
  362. return JS_TRUE;
  363. }
  364. static JSBool
  365. args_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  366. {
  367. JSStackFrame *fp;
  368. jsint slot;
  369. if (!JSVAL_IS_INT(id))
  370. return JS_TRUE;
  371. fp = (JSStackFrame *)
  372. JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
  373. if (!fp)
  374. return JS_TRUE;
  375. JS_ASSERT(fp->argsobj);
  376. slot = JSVAL_TO_INT(id);
  377. switch (slot) {
  378. case ARGS_CALLEE:
  379. case ARGS_LENGTH:
  380. SET_OVERRIDE_BIT(fp, slot);
  381. break;
  382. default:
  383. if (FUN_INTERPRETED(fp->fun) &&
  384. (uintN)slot < fp->argc &&
  385. !ArgWasDeleted(cx, fp, slot)) {
  386. fp->argv[slot] = *vp;
  387. }
  388. break;
  389. }
  390. return JS_TRUE;
  391. }
  392. static JSBool
  393. args_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
  394. JSObject **objp)
  395. {
  396. JSStackFrame *fp;
  397. uintN slot;
  398. JSString *str;
  399. JSAtom *atom;
  400. intN tinyid;
  401. jsval value;
  402. *objp = NULL;
  403. fp = (JSStackFrame *)
  404. JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
  405. if (!fp)
  406. return JS_TRUE;
  407. JS_ASSERT(fp->argsobj);
  408. if (JSVAL_IS_INT(id)) {
  409. slot = JSVAL_TO_INT(id);
  410. if (slot < fp->argc && !ArgWasDeleted(cx, fp, slot)) {
  411. /* XXX ECMA specs DontEnum, contrary to other array-like objects */
  412. if (!js_DefineProperty(cx, obj, INT_JSVAL_TO_JSID(id),
  413. fp->argv[slot],
  414. args_getProperty, args_setProperty,
  415. 0, NULL)) {
  416. return JS_FALSE;
  417. }
  418. *objp = obj;
  419. }
  420. } else {
  421. str = JSVAL_TO_STRING(id);
  422. atom = cx->runtime->atomState.lengthAtom;
  423. if (str == ATOM_TO_STRING(atom)) {
  424. tinyid = ARGS_LENGTH;
  425. value = INT_TO_JSVAL(fp->argc);
  426. } else {
  427. atom = cx->runtime->atomState.calleeAtom;
  428. if (str == ATOM_TO_STRING(atom)) {
  429. tinyid = ARGS_CALLEE;
  430. value = OBJECT_TO_JSVAL(fp->callee);
  431. } else {
  432. atom = NULL;
  433. /* Quell GCC overwarnings. */
  434. tinyid = 0;
  435. value = JSVAL_NULL;
  436. }
  437. }
  438. if (atom && !TEST_OVERRIDE_BIT(fp, tinyid)) {
  439. if (!js_DefineNativeProperty(cx, obj, ATOM_TO_JSID(atom), value,
  440. args_getProperty, args_setProperty, 0,
  441. SPROP_HAS_SHORTID, tinyid, NULL)) {
  442. return JS_FALSE;
  443. }
  444. *objp = obj;
  445. }
  446. }
  447. return JS_TRUE;
  448. }
  449. static JSBool
  450. args_enumerate(JSContext *cx, JSObject *obj)
  451. {
  452. JSStackFrame *fp;
  453. JSObject *pobj;
  454. JSProperty *prop;
  455. uintN slot, argc;
  456. fp = (JSStackFrame *)
  457. JS_GetInstancePrivate(cx, obj, &js_ArgumentsClass, NULL);
  458. if (!fp)
  459. return JS_TRUE;
  460. JS_ASSERT(fp->argsobj);
  461. /*
  462. * Trigger reflection with value snapshot in args_resolve using a series
  463. * of js_LookupProperty calls. We handle length, callee, and the indexed
  464. * argument properties. We know that args_resolve covers all these cases
  465. * and creates direct properties of obj, but that it may fail to resolve
  466. * length or callee if overridden.
  467. */
  468. if (!js_LookupProperty(cx, obj,
  469. ATOM_TO_JSID(cx->runtime->atomState.lengthAtom),
  470. &pobj, &prop)) {
  471. return JS_FALSE;
  472. }
  473. if (prop)
  474. OBJ_DROP_PROPERTY(cx, pobj, prop);
  475. if (!js_LookupProperty(cx, obj,
  476. ATOM_TO_JSID(cx->runtime->atomState.calleeAtom),
  477. &pobj, &prop)) {
  478. return JS_FALSE;
  479. }
  480. if (prop)
  481. OBJ_DROP_PROPERTY(cx, pobj, prop);
  482. argc = fp->argc;
  483. for (slot = 0; slot < argc; slot++) {
  484. if (!js_LookupProperty(cx, obj, INT_TO_JSID((jsint)slot), &pobj, &prop))
  485. return JS_FALSE;
  486. if (prop)
  487. OBJ_DROP_PROPERTY(cx, pobj, prop);
  488. }
  489. return JS_TRUE;
  490. }
  491. #if JS_HAS_GENERATORS
  492. /*
  493. * If a generator-iterator's arguments or call object escapes, it needs to
  494. * mark its generator object.
  495. */
  496. static void
  497. args_or_call_trace(JSTracer *trc, JSObject *obj)
  498. {
  499. JSStackFrame *fp;
  500. fp = (JSStackFrame *) JS_GetPrivate(trc->context, obj);
  501. if (fp && (fp->flags & JSFRAME_GENERATOR)) {
  502. JS_CALL_OBJECT_TRACER(trc, FRAME_TO_GENERATOR(fp)->obj,
  503. "FRAME_TO_GENERATOR(fp)->obj");
  504. }
  505. }
  506. #else
  507. # define args_or_call_trace NULL
  508. #endif
  509. /*
  510. * The Arguments class is not initialized via JS_InitClass, and must not be,
  511. * because its name is "Object". Per ECMA, that causes instances of it to
  512. * delegate to the object named by Object.prototype. It also ensures that
  513. * arguments.toString() returns "[object Object]".
  514. *
  515. * The JSClass functions below collaborate to lazily reflect and synchronize
  516. * actual argument values, argument count, and callee function object stored
  517. * in a JSStackFrame with their corresponding property values in the frame's
  518. * arguments object.
  519. */
  520. JSClass js_ArgumentsClass = {
  521. js_Object_str,
  522. JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_HAS_RESERVED_SLOTS(1) |
  523. JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
  524. JS_PropertyStub, args_delProperty,
  525. args_getProperty, args_setProperty,
  526. args_enumerate, (JSResolveOp) args_resolve,
  527. JS_ConvertStub, JS_FinalizeStub,
  528. NULL, NULL,
  529. NULL, NULL,
  530. NULL, NULL,
  531. JS_CLASS_TRACE(args_or_call_trace), NULL
  532. };
  533. #define JSSLOT_SCRIPTED_FUNCTION (JSSLOT_PRIVATE + 1)
  534. #define JSSLOT_CALL_ARGUMENTS (JSSLOT_PRIVATE + 2)
  535. #define CALL_CLASS_FIXED_RESERVED_SLOTS 2
  536. JSObject *
  537. js_GetCallObject(JSContext *cx, JSStackFrame *fp, JSObject *parent)
  538. {
  539. JSObject *callobj, *funobj;
  540. /* Create a call object for fp only if it lacks one. */
  541. JS_ASSERT(fp->fun);
  542. callobj = fp->callobj;
  543. if (callobj)
  544. return callobj;
  545. /* The default call parent is its function's parent (static link). */
  546. if (!parent) {
  547. funobj = fp->callee;
  548. if (funobj)
  549. parent = OBJ_GET_PARENT(cx, funobj);
  550. }
  551. /* Create the call object and link it to its stack frame. */
  552. callobj = js_NewObject(cx, &js_CallClass, NULL, parent, 0);
  553. if (!callobj)
  554. return NULL;
  555. JS_SetPrivate(cx, callobj, fp);
  556. STOBJ_SET_SLOT(callobj, JSSLOT_SCRIPTED_FUNCTION,
  557. OBJECT_TO_JSVAL(FUN_OBJECT(fp->fun)));
  558. fp->callobj = callobj;
  559. /* Make callobj be the scope chain and the variables object. */
  560. JS_ASSERT(fp->scopeChain == parent);
  561. fp->scopeChain = callobj;
  562. fp->varobj = callobj;
  563. return callobj;
  564. }
  565. JSFunction *
  566. js_GetCallObjectFunction(JSObject *obj)
  567. {
  568. jsval v;
  569. JS_ASSERT(STOBJ_GET_CLASS(obj) == &js_CallClass);
  570. v = STOBJ_GET_SLOT(obj, JSSLOT_SCRIPTED_FUNCTION);
  571. if (JSVAL_IS_VOID(v)) {
  572. /* Newborn or prototype object. */
  573. return NULL;
  574. }
  575. JS_ASSERT(!JSVAL_IS_PRIMITIVE(v));
  576. return (JSFunction *) JSVAL_TO_OBJECT(v);
  577. }
  578. JS_FRIEND_API(JSBool)
  579. js_PutCallObject(JSContext *cx, JSStackFrame *fp)
  580. {
  581. JSObject *callobj;
  582. JSBool ok;
  583. JSFunction *fun;
  584. uintN n;
  585. JSScope *scope;
  586. /*
  587. * Since for a call object all fixed slots happen to be taken, we can copy
  588. * arguments and variables straight into JSObject.dslots.
  589. */
  590. JS_STATIC_ASSERT(JS_INITIAL_NSLOTS - JSSLOT_PRIVATE ==
  591. 1 + CALL_CLASS_FIXED_RESERVED_SLOTS);
  592. callobj = fp->callobj;
  593. if (!callobj)
  594. return JS_TRUE;
  595. /*
  596. * Get the arguments object to snapshot fp's actual argument values.
  597. */
  598. ok = JS_TRUE;
  599. if (fp->argsobj) {
  600. if (!TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
  601. STOBJ_SET_SLOT(callobj, JSSLOT_CALL_ARGUMENTS,
  602. OBJECT_TO_JSVAL(fp->argsobj));
  603. }
  604. ok &= js_PutArgsObject(cx, fp);
  605. }
  606. fun = fp->fun;
  607. JS_ASSERT(fun == js_GetCallObjectFunction(callobj));
  608. n = JS_GET_LOCAL_NAME_COUNT(fun);
  609. if (n != 0) {
  610. JS_LOCK_OBJ(cx, callobj);
  611. n += JS_INITIAL_NSLOTS;
  612. if (n > STOBJ_NSLOTS(callobj))
  613. ok &= js_ReallocSlots(cx, callobj, n, JS_TRUE);
  614. scope = OBJ_SCOPE(callobj);
  615. if (ok) {
  616. memcpy(callobj->dslots, fp->argv, fun->nargs * sizeof(jsval));
  617. memcpy(callobj->dslots + fun->nargs, fp->slots,
  618. fun->u.i.nvars * sizeof(jsval));
  619. if (scope->object == callobj && n > scope->map.freeslot)
  620. scope->map.freeslot = n;
  621. }
  622. JS_UNLOCK_SCOPE(cx, scope);
  623. }
  624. /*
  625. * Clear the private pointer to fp, which is about to go away (js_Invoke).
  626. * Do this last because js_GetProperty calls above need to follow the
  627. * private slot to find fp.
  628. */
  629. JS_SetPrivate(cx, callobj, NULL);
  630. fp->callobj = NULL;
  631. return ok;
  632. }
  633. static JSBool
  634. call_enumerate(JSContext *cx, JSObject *obj)
  635. {
  636. JSFunction *fun;
  637. uintN n, i;
  638. void *mark;
  639. jsuword *names;
  640. JSBool ok;
  641. JSAtom *name;
  642. JSObject *pobj;
  643. JSProperty *prop;
  644. fun = js_GetCallObjectFunction(obj);
  645. n = JS_GET_LOCAL_NAME_COUNT(fun);
  646. if (n == 0)
  647. return JS_TRUE;
  648. mark = JS_ARENA_MARK(&cx->tempPool);
  649. MUST_FLOW_THROUGH("out");
  650. names = js_GetLocalNameArray(cx, fun, &cx->tempPool);
  651. if (!names) {
  652. ok = JS_FALSE;
  653. goto out;
  654. }
  655. for (i = 0; i != n; ++i) {
  656. name = JS_LOCAL_NAME_TO_ATOM(names[i]);
  657. if (!name)
  658. continue;
  659. /*
  660. * Trigger reflection by looking up the name of the argument or
  661. * variable.
  662. */
  663. ok = js_LookupProperty(cx, obj, ATOM_TO_JSID(name), &pobj, &prop);
  664. if (!ok)
  665. goto out;
  666. /*
  667. * At this point the call object always has a property corresponding
  668. * to the local name because call_resolve creates the property using
  669. * JSPROP_PERMANENT.
  670. */
  671. JS_ASSERT(prop && pobj == obj);
  672. OBJ_DROP_PROPERTY(cx, pobj, prop);
  673. }
  674. ok = JS_TRUE;
  675. out:
  676. JS_ARENA_RELEASE(&cx->tempPool, mark);
  677. return ok;
  678. }
  679. typedef enum JSCallPropertyKind {
  680. JSCPK_ARGUMENTS,
  681. JSCPK_ARG,
  682. JSCPK_VAR
  683. } JSCallPropertyKind;
  684. static JSBool
  685. CallPropertyOp(JSContext *cx, JSObject *obj, jsid id, jsval *vp,
  686. JSCallPropertyKind kind, JSBool setter)
  687. {
  688. JSFunction *fun;
  689. JSStackFrame *fp;
  690. uintN i;
  691. jsval *array;
  692. if (STOBJ_GET_CLASS(obj) != &js_CallClass)
  693. return JS_TRUE;
  694. fun = js_GetCallObjectFunction(obj);
  695. fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
  696. if (kind == JSCPK_ARGUMENTS) {
  697. if (setter) {
  698. if (fp)
  699. SET_OVERRIDE_BIT(fp, CALL_ARGUMENTS);
  700. STOBJ_SET_SLOT(obj, JSSLOT_CALL_ARGUMENTS, *vp);
  701. } else {
  702. if (fp && !TEST_OVERRIDE_BIT(fp, CALL_ARGUMENTS)) {
  703. JSObject *argsobj;
  704. argsobj = js_GetArgsObject(cx, fp);
  705. if (!argsobj)
  706. return JS_FALSE;
  707. *vp = OBJECT_TO_JSVAL(argsobj);
  708. } else {
  709. *vp = STOBJ_GET_SLOT(obj, JSSLOT_CALL_ARGUMENTS);
  710. }
  711. }
  712. return JS_TRUE;
  713. }
  714. JS_ASSERT((int16) JSVAL_TO_INT(id) == JSVAL_TO_INT(id));
  715. i = (uint16) JSVAL_TO_INT(id);
  716. JS_ASSERT_IF(kind == JSCPK_ARG, i < fun->nargs);
  717. JS_ASSERT_IF(kind == JSCPK_VAR, i < fun->u.i.nvars);
  718. if (!fp) {
  719. i += CALL_CLASS_FIXED_RESERVED_SLOTS;
  720. if (kind == JSCPK_VAR)
  721. i += fun->nargs;
  722. else
  723. JS_ASSERT(kind == JSCPK_ARG);
  724. return setter
  725. ? JS_SetReservedSlot(cx, obj, i, *vp)
  726. : JS_GetReservedSlot(cx, obj, i, vp);
  727. }
  728. if (kind == JSCPK_ARG) {
  729. array = fp->argv;
  730. } else {
  731. JS_ASSERT(kind == JSCPK_VAR);
  732. array = fp->slots;
  733. }
  734. if (setter)
  735. array[i] = *vp;
  736. else
  737. *vp = array[i];
  738. return JS_TRUE;
  739. }
  740. static JSBool
  741. GetCallArguments(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
  742. {
  743. return CallPropertyOp(cx, obj, id, vp, JSCPK_ARGUMENTS, JS_FALSE);
  744. }
  745. static JSBool
  746. SetCallArguments(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
  747. {
  748. return CallPropertyOp(cx, obj, id, vp, JSCPK_ARGUMENTS, JS_TRUE);
  749. }
  750. JSBool
  751. js_GetCallArg(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
  752. {
  753. return CallPropertyOp(cx, obj, id, vp, JSCPK_ARG, JS_FALSE);
  754. }
  755. static JSBool
  756. SetCallArg(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
  757. {
  758. return CallPropertyOp(cx, obj, id, vp, JSCPK_ARG, JS_TRUE);
  759. }
  760. JSBool
  761. js_GetCallVar(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
  762. {
  763. return CallPropertyOp(cx, obj, id, vp, JSCPK_VAR, JS_FALSE);
  764. }
  765. static JSBool
  766. SetCallVar(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
  767. {
  768. return CallPropertyOp(cx, obj, id, vp, JSCPK_VAR, JS_TRUE);
  769. }
  770. static JSBool
  771. call_resolve(JSContext *cx, JSObject *obj, jsval idval, uintN flags,
  772. JSObject **objp)
  773. {
  774. JSFunction *fun;
  775. jsid id;
  776. JSLocalKind localKind;
  777. JSPropertyOp getter, setter;
  778. uintN slot, attrs;
  779. if (!JSVAL_IS_STRING(idval))
  780. return JS_TRUE;
  781. fun = js_GetCallObjectFunction(obj);
  782. if (!fun)
  783. return JS_TRUE;
  784. if (!js_ValueToStringId(cx, idval, &id))
  785. return JS_FALSE;
  786. localKind = js_LookupLocal(cx, fun, JSID_TO_ATOM(id), &slot);
  787. if (localKind != JSLOCAL_NONE) {
  788. JS_ASSERT((uint16) slot == slot);
  789. attrs = JSPROP_PERMANENT | JSPROP_SHARED;
  790. if (localKind == JSLOCAL_ARG) {
  791. JS_ASSERT(slot < fun->nargs);
  792. getter = js_GetCallArg;
  793. setter = SetCallArg;
  794. } else {
  795. JS_ASSERT(localKind == JSLOCAL_VAR || localKind == JSLOCAL_CONST);
  796. JS_ASSERT(slot < fun->u.i.nvars);
  797. getter = js_GetCallVar;
  798. setter = SetCallVar;
  799. if (localKind == JSLOCAL_CONST)
  800. attrs |= JSPROP_READONLY;
  801. }
  802. if (!js_DefineNativeProperty(cx, obj, id, JSVAL_VOID, getter, setter,
  803. attrs, SPROP_HAS_SHORTID, (int16) slot,
  804. NULL)) {
  805. return JS_FALSE;
  806. }
  807. *objp = obj;
  808. return JS_TRUE;
  809. }
  810. /*
  811. * Resolve arguments so that we never store a particular Call object's
  812. * arguments object reference in a Call prototype's |arguments| slot.
  813. */
  814. if (id == ATOM_TO_JSID(cx->runtime->atomState.argumentsAtom)) {
  815. if (!js_DefineNativeProperty(cx, obj, id, JSVAL_VOID,
  816. GetCallArguments, SetCallArguments,
  817. JSPROP_PERMANENT | JSPROP_SHARED,
  818. 0, 0, NULL)) {
  819. return JS_FALSE;
  820. }
  821. *objp = obj;
  822. return JS_TRUE;
  823. }
  824. return JS_TRUE;
  825. }
  826. static JSBool
  827. call_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
  828. {
  829. JSStackFrame *fp;
  830. if (type == JSTYPE_FUNCTION) {
  831. fp = (JSStackFrame *) JS_GetPrivate(cx, obj);
  832. if (fp) {
  833. JS_ASSERT(fp->fun);
  834. *vp = OBJECT_TO_JSVAL(fp->callee);
  835. }
  836. }
  837. return JS_TRUE;
  838. }
  839. static uint32
  840. call_reserveSlots(JSContext *cx, JSObject *obj)
  841. {
  842. JSFunction *fun;
  843. fun = js_GetCallObjectFunction(obj);
  844. return JS_GET_LOCAL_NAME_COUNT(fun);
  845. }
  846. JS_FRIEND_DATA(JSClass) js_CallClass = {
  847. js_Call_str,
  848. JSCLASS_HAS_PRIVATE |
  849. JSCLASS_HAS_RESERVED_SLOTS(CALL_CLASS_FIXED_RESERVED_SLOTS) |
  850. JSCLASS_NEW_RESOLVE | JSCLASS_IS_ANONYMOUS |
  851. JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Call),
  852. JS_PropertyStub, JS_PropertyStub,
  853. JS_PropertyStub, JS_PropertyStub,
  854. call_enumerate, (JSResolveOp)call_resolve,
  855. call_convert, JS_FinalizeStub,
  856. NULL, NULL,
  857. NULL, NULL,
  858. NULL, NULL,
  859. JS_CLASS_TRACE(args_or_call_trace), call_reserveSlots
  860. };
  861. static JSBool
  862. fun_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  863. {
  864. jsint slot;
  865. JSFunction *fun;
  866. JSStackFrame *fp;
  867. JSSecurityCallbacks *callbacks;
  868. if (!JSVAL_IS_INT(id))
  869. return JS_TRUE;
  870. slot = JSVAL_TO_INT(id);
  871. /*
  872. * Loop because getter and setter can be delegated from another class,
  873. * but loop only for ARGS_LENGTH because we must pretend that f.length
  874. * is in each function instance f, per ECMA-262, instead of only in the
  875. * Function.prototype object (we use JSPROP_PERMANENT with JSPROP_SHARED
  876. * to make it appear so).
  877. *
  878. * This code couples tightly to the attributes for the function_props[]
  879. * initializers above, and to js_SetProperty and js_HasOwnProperty.
  880. *
  881. * It's important to allow delegating objects, even though they inherit
  882. * this getter (fun_getProperty), to override arguments, arity, caller,
  883. * and name. If we didn't return early for slot != ARGS_LENGTH, we would
  884. * clobber *vp with the native property value, instead of letting script
  885. * override that value in delegating objects.
  886. *
  887. * Note how that clobbering is what simulates JSPROP_READONLY for all of
  888. * the non-standard properties when the directly addressed object (obj)
  889. * is a function object (i.e., when this loop does not iterate).
  890. */
  891. while (!(fun = (JSFunction *)
  892. JS_GetInstancePrivate(cx, obj, &js_FunctionClass, NULL))) {
  893. if (slot != ARGS_LENGTH)
  894. return JS_TRUE;
  895. obj = OBJ_GET_PROTO(cx, obj);
  896. if (!obj)
  897. return JS_TRUE;
  898. }
  899. /* Find fun's top-most activation record. */
  900. for (fp = cx->fp; fp && (fp->fun != fun || (fp->flags & JSFRAME_SPECIAL));
  901. fp = fp->down) {
  902. continue;
  903. }
  904. switch (slot) {
  905. case CALL_ARGUMENTS:
  906. /* Warn if strict about f.arguments or equivalent unqualified uses. */
  907. if (!JS_ReportErrorFlagsAndNumber(cx,
  908. JSREPORT_WARNING | JSREPORT_STRICT,
  909. js_GetErrorMessage, NULL,
  910. JSMSG_DEPRECATED_USAGE,
  911. js_arguments_str)) {
  912. return JS_FALSE;
  913. }
  914. if (fp) {
  915. if (!js_GetArgsValue(cx, fp, vp))
  916. return JS_FALSE;
  917. } else {
  918. *vp = JSVAL_NULL;
  919. }
  920. break;
  921. case ARGS_LENGTH:
  922. case FUN_ARITY:
  923. *vp = INT_TO_JSVAL((jsint)fun->nargs);
  924. break;
  925. case FUN_NAME:
  926. *vp = fun->atom
  927. ? ATOM_KEY(fun->atom)
  928. : STRING_TO_JSVAL(cx->runtime->emptyString);
  929. break;
  930. case FUN_CALLER:
  931. if (fp && fp->down && fp->down->fun)
  932. *vp = OBJECT_TO_JSVAL(fp->down->callee);
  933. else
  934. *vp = JSVAL_NULL;
  935. if (!JSVAL_IS_PRIMITIVE(*vp)) {
  936. callbacks = JS_GetSecurityCallbacks(cx);
  937. if (callbacks && callbacks->checkObjectAccess) {
  938. id = ATOM_KEY(cx->runtime->atomState.callerAtom);
  939. if (!callbacks->checkObjectAccess(cx, obj, id, JSACC_READ, vp))
  940. return JS_FALSE;
  941. }
  942. }
  943. break;
  944. default:
  945. /* XXX fun[0] and fun.arguments[0] are equivalent. */
  946. if (fp && fp->fun && (uintN)slot < fp->fun->nargs)
  947. *vp = fp->argv[slot];
  948. break;
  949. }
  950. return JS_TRUE;
  951. }
  952. /*
  953. * ECMA-262 specifies that length is a property of function object instances,
  954. * but we can avoid that space cost by delegating to a prototype property that
  955. * is JSPROP_PERMANENT and JSPROP_SHARED. Each fun_getProperty call computes
  956. * a fresh length value based on the arity of the individual function object's
  957. * private data.
  958. *
  959. * The extensions below other than length, i.e., the ones not in ECMA-262,
  960. * are neither JSPROP_READONLY nor JSPROP_SHARED, because for compatibility
  961. * with ECMA we must allow a delegating object to override them. Therefore to
  962. * avoid entraining garbage in Function.prototype slots, they must be resolved
  963. * in non-prototype function objects, wherefore the lazy_function_props table
  964. * and fun_resolve's use of it.
  965. */
  966. #define LENGTH_PROP_ATTRS (JSPROP_READONLY|JSPROP_PERMANENT|JSPROP_SHARED)
  967. static JSPropertySpec function_props[] = {
  968. {js_length_str, ARGS_LENGTH, LENGTH_PROP_ATTRS, fun_getProperty, JS_PropertyStub},
  969. {0,0,0,0,0}
  970. };
  971. typedef struct LazyFunctionProp {
  972. uint16 atomOffset;
  973. int8 tinyid;
  974. uint8 attrs;
  975. } LazyFunctionProp;
  976. /* NB: no sentinel at the end -- use JS_ARRAY_LENGTH to bound loops. */
  977. static LazyFunctionProp lazy_function_props[] = {
  978. {ATOM_OFFSET(arguments), CALL_ARGUMENTS, JSPROP_PERMANENT},
  979. {ATOM_OFFSET(arity), FUN_ARITY, JSPROP_PERMANENT},
  980. {ATOM_OFFSET(caller), FUN_CALLER, JSPROP_PERMANENT},
  981. {ATOM_OFFSET(name), FUN_NAME, JSPROP_PERMANENT},
  982. };
  983. static JSBool
  984. fun_enumerate(JSContext *cx, JSObject *obj)
  985. {
  986. jsid prototypeId;
  987. JSObject *pobj;
  988. JSProperty *prop;
  989. prototypeId = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
  990. if (!OBJ_LOOKUP_PROPERTY(cx, obj, prototypeId, &pobj, &prop))
  991. return JS_FALSE;
  992. if (prop)
  993. OBJ_DROP_PROPERTY(cx, pobj, prop);
  994. return JS_TRUE;
  995. }
  996. static JSBool
  997. fun_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
  998. JSObject **objp)
  999. {
  1000. JSFunction *fun;
  1001. JSAtom *atom;
  1002. uintN i;
  1003. if (!JSVAL_IS_STRING(id))
  1004. return JS_TRUE;
  1005. fun = GET_FUNCTION_PRIVATE(cx, obj);
  1006. /*
  1007. * No need to reflect fun.prototype in 'fun.prototype = ... '.
  1008. *
  1009. * This is not just an optimization, because we must not resolve when
  1010. * defining hidden properties during compilation. The setup code for the
  1011. * prototype and the lazy properties below eventually calls the property
  1012. * hooks for the function object. That in turn calls fun_reserveSlots to
  1013. * get the number of the reserved slots which is just the number of
  1014. * regular expressions literals in the function. When compiling, that
  1015. * number is not yet ready so we must make sure that fun_resolve does
  1016. * nothing until the code for the function is generated.
  1017. */
  1018. if (flags & JSRESOLVE_ASSIGNING)
  1019. return JS_TRUE;
  1020. /*
  1021. * Ok, check whether id is 'prototype' and bootstrap the function object's
  1022. * prototype property.
  1023. */
  1024. atom = cx->runtime->atomState.classPrototypeAtom;
  1025. if (id == ATOM_KEY(atom)) {
  1026. JSObject *proto;
  1027. /*
  1028. * Beware of the wacky case of a user function named Object -- trying
  1029. * to find a prototype for that will recur back here _ad perniciem_.
  1030. */
  1031. if (fun->atom == CLASS_ATOM(cx, Object))
  1032. return JS_TRUE;
  1033. /*
  1034. * Make the prototype object to have the same parent as the function
  1035. * object itself.
  1036. */
  1037. proto = js_NewObject(cx, &js_ObjectClass, NULL, OBJ_GET_PARENT(cx, obj),
  1038. 0);
  1039. if (!proto)
  1040. return JS_FALSE;
  1041. /*
  1042. * ECMA (15.3.5.2) says that constructor.prototype is DontDelete for
  1043. * user-defined functions, but DontEnum | ReadOnly | DontDelete for
  1044. * native "system" constructors such as Object or Function. So lazily
  1045. * set the former here in fun_resolve, but eagerly define the latter
  1046. * in JS_InitClass, with the right attributes.
  1047. */
  1048. if (!js_SetClassPrototype(cx, obj, proto,
  1049. JSPROP_ENUMERATE | JSPROP_PERMANENT)) {
  1050. cx->weakRoots.newborn[GCX_OBJECT] = NULL;
  1051. return JS_FALSE;
  1052. }
  1053. *objp = obj;
  1054. return JS_TRUE;
  1055. }
  1056. for (i = 0; i < JS_ARRAY_LENGTH(lazy_function_props); i++) {
  1057. LazyFunctionProp *lfp = &lazy_function_props[i];
  1058. atom = OFFSET_TO_ATOM(cx->runtime, lfp->atomOffset);
  1059. if (id == ATOM_KEY(atom)) {
  1060. if (!js_DefineNativeProperty(cx, obj,
  1061. ATOM_TO_JSID(atom), JSVAL_VOID,
  1062. fun_getProperty, JS_PropertyStub,
  1063. lfp->attrs, SPROP_HAS_SHORTID,
  1064. lfp->tinyid, NULL)) {
  1065. return JS_FALSE;
  1066. }
  1067. *objp = obj;
  1068. return JS_TRUE;
  1069. }
  1070. }
  1071. return JS_TRUE;
  1072. }
  1073. static JSBool
  1074. fun_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
  1075. {
  1076. switch (type) {
  1077. case JSTYPE_FUNCTION:
  1078. *vp = OBJECT_TO_JSVAL(obj);
  1079. return JS_TRUE;
  1080. default:
  1081. return js_TryValueOf(cx, obj, type, vp);
  1082. }
  1083. }
  1084. #if JS_HAS_XDR
  1085. /* XXX store parent and proto, if defined */
  1086. static JSBool
  1087. fun_xdrObject(JSXDRState *xdr, JSObject **objp)
  1088. {
  1089. JSContext *cx;
  1090. JSFunction *fun;
  1091. uint32 nullAtom; /* flag to indicate if fun->atom is NULL */
  1092. uintN nargs, nvars, n;
  1093. uint32 localsword; /* word to xdr argument and variable counts */
  1094. uint32 flagsword; /* originally only flags was JS_XDRUint8'd */
  1095. JSTempValueRooter tvr;
  1096. JSBool ok;
  1097. cx = xdr->cx;
  1098. if (xdr->mode == JSXDR_ENCODE) {
  1099. fun = GET_FUNCTION_PRIVATE(cx, *objp);
  1100. if (!FUN_INTERPRETED(fun)) {
  1101. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1102. JSMSG_NOT_SCRIPTED_FUNCTION,
  1103. JS_GetFunctionName(fun));
  1104. return JS_FALSE;
  1105. }
  1106. nullAtom = !fun->atom;
  1107. nargs = fun->nargs;
  1108. nvars = fun->u.i.nvars;
  1109. localsword = (nargs << 16) | nvars;
  1110. flagsword = fun->flags;
  1111. } else {
  1112. fun = js_NewFunction(cx, NULL, NULL, 0, JSFUN_INTERPRETED, NULL, NULL);
  1113. if (!fun)
  1114. return JS_FALSE;
  1115. STOBJ_CLEAR_PARENT(FUN_OBJECT(fun));
  1116. STOBJ_CLEAR_PROTO(FUN_OBJECT(fun));
  1117. #ifdef __GNUC__
  1118. nvars = nargs = 0; /* quell GCC uninitialized warning */
  1119. #endif
  1120. }
  1121. /* From here on, control flow must flow through label out. */
  1122. JS_PUSH_TEMP_ROOT_OBJECT(cx, FUN_OBJECT(fun), &tvr);
  1123. ok = JS_TRUE;
  1124. if (!JS_XDRUint32(xdr, &nullAtom))
  1125. goto bad;
  1126. if (!nullAtom && !js_XDRStringAtom(xdr, &fun->atom))
  1127. goto bad;
  1128. if (!JS_XDRUint32(xdr, &localsword) ||
  1129. !JS_XDRUint32(xdr, &flagsword)) {
  1130. goto bad;
  1131. }
  1132. if (xdr->mode == JSXDR_DECODE) {
  1133. nargs = localsword >> 16;
  1134. nvars = localsword & JS_BITMASK(16);
  1135. JS_ASSERT(flagsword | JSFUN_INTERPRETED);
  1136. fun->flags = (uint16) flagsword;
  1137. }
  1138. /* do arguments and local vars */
  1139. n = nargs + nvars;
  1140. if (n != 0) {
  1141. void *mark;
  1142. uintN i;
  1143. uintN bitmapLength;
  1144. uint32 *bitmap;
  1145. jsuword *names;
  1146. JSAtom *name;
  1147. JSLocalKind localKind;
  1148. mark = JS_ARENA_MARK(&xdr->cx->tempPool);
  1149. /*
  1150. * From this point the control must flow via the label release_mark.
  1151. *
  1152. * To xdr the names we prefix the names with a bitmap descriptor and
  1153. * then xdr the names as strings. For argument names (indexes below
  1154. * nargs) the corresponding bit in the bitmap is unset when the name
  1155. * is null. Such null names are not encoded or decoded. For variable
  1156. * names (indexes starting from nargs) bitmap's bit is set when the
  1157. * name is declared as const, not as ordinary var.
  1158. * */
  1159. bitmapLength = JS_HOWMANY(n, JS_BITS_PER_UINT32);
  1160. JS_ARENA_ALLOCATE_CAST(bitmap, uint32 *, &xdr->cx->tempPool,
  1161. bitmapLength * sizeof *bitmap);
  1162. if (!bitmap) {
  1163. js_ReportOutOfScriptQuota(xdr->cx);
  1164. ok = JS_FALSE;
  1165. goto release_mark;
  1166. }
  1167. if (xdr->mode == JSXDR_ENCODE) {
  1168. names = js_GetLocalNameArray(xdr->cx, fun, &xdr->cx->tempPool);
  1169. if (!names) {
  1170. ok = JS_FALSE;
  1171. goto release_mark;
  1172. }
  1173. memset(bitmap, 0, bitmapLength * sizeof *bitmap);
  1174. for (i = 0; i != n; ++i) {
  1175. if (i < fun->nargs
  1176. ? JS_LOCAL_NAME_TO_ATOM(names[i]) != NULL
  1177. : JS_LOCAL_NAME_IS_CONST(names[i])) {
  1178. bitmap[i >> JS_BITS_PER_UINT32_LOG2] |=
  1179. JS_BIT(i & (JS_BITS_PER_UINT32 - 1));
  1180. }
  1181. }
  1182. }
  1183. #ifdef __GNUC__
  1184. else {
  1185. names = NULL; /* quell GCC uninitialized warning */
  1186. }
  1187. #endif
  1188. for (i = 0; i != bitmapLength; ++i) {
  1189. ok = JS_XDRUint32(xdr, &bitmap[i]);
  1190. if (!ok)
  1191. goto release_mark;
  1192. }
  1193. for (i = 0; i != n; ++i) {
  1194. if (i < nargs &&
  1195. !(bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
  1196. JS_BIT(i & (JS_BITS_PER_UINT32 - 1)))) {
  1197. if (xdr->mode == JSXDR_DECODE) {
  1198. ok = js_AddLocal(xdr->cx, fun, NULL, JSLOCAL_ARG);
  1199. if (!ok)
  1200. goto release_mark;
  1201. } else {
  1202. JS_ASSERT(!JS_LOCAL_NAME_TO_ATOM(names[i]));
  1203. }
  1204. continue;
  1205. }
  1206. if (xdr->mode == JSXDR_ENCODE)
  1207. name = JS_LOCAL_NAME_TO_ATOM(names[i]);
  1208. ok = js_XDRStringAtom(xdr, &name);
  1209. if (!ok)
  1210. goto release_mark;
  1211. if (xdr->mode == JSXDR_DECODE) {
  1212. localKind = (i < nargs)
  1213. ? JSLOCAL_ARG
  1214. : bitmap[i >> JS_BITS_PER_UINT32_LOG2] &
  1215. JS_BIT(i & (JS_BITS_PER_UINT32 - 1))
  1216. ? JSLOCAL_CONST
  1217. : JSLOCAL_VAR;
  1218. ok = js_AddLocal(xdr->cx, fun, name, localKind);
  1219. if (!ok)
  1220. goto release_mark;
  1221. }
  1222. }
  1223. ok = JS_TRUE;
  1224. release_mark:
  1225. JS_ARENA_RELEASE(&xdr->cx->tempPool, mark);
  1226. if (!ok)
  1227. goto out;
  1228. if (xdr->mode == JSXDR_DECODE)
  1229. js_FreezeLocalNames(cx, fun);
  1230. }
  1231. if (!js_XDRScript(xdr, &fun->u.i.script, NULL))
  1232. goto bad;
  1233. if (xdr->mode == JSXDR_DECODE) {
  1234. *objp = FUN_OBJECT(fun);
  1235. #ifdef CHECK_SCRIPT_OWNER
  1236. fun->u.i.script->owner = NULL;
  1237. #endif
  1238. js_CallNewScriptHook(cx, fun->u.i.script, fun);
  1239. }
  1240. out:
  1241. JS_POP_TEMP_ROOT(cx, &tvr);
  1242. return ok;
  1243. bad:
  1244. ok = JS_FALSE;
  1245. goto out;
  1246. }
  1247. #else /* !JS_HAS_XDR */
  1248. #define fun_xdrObject NULL
  1249. #endif /* !JS_HAS_XDR */
  1250. /*
  1251. * [[HasInstance]] internal method for Function objects: fetch the .prototype
  1252. * property of its 'this' parameter, and walks the prototype chain of v (only
  1253. * if v is an object) returning true if .prototype is found.
  1254. */
  1255. static JSBool
  1256. fun_hasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp)
  1257. {
  1258. jsval pval;
  1259. if (!OBJ_GET_PROPERTY(cx, obj,
  1260. ATOM_TO_JSID(cx->runtime->atomState
  1261. .classPrototypeAtom),
  1262. &pval)) {
  1263. return JS_FALSE;
  1264. }
  1265. if (JSVAL_IS_PRIMITIVE(pval)) {
  1266. /*
  1267. * Throw a runtime error if instanceof is called on a function that
  1268. * has a non-object as its .prototype value.
  1269. */
  1270. js_ReportValueError(cx, JSMSG_BAD_PROTOTYPE,
  1271. -1, OBJECT_TO_JSVAL(obj), NULL);
  1272. return JS_FALSE;
  1273. }
  1274. return js_IsDelegate(cx, JSVAL_TO_OBJECT(pval), v, bp);
  1275. }
  1276. static void
  1277. TraceLocalNames(JSTracer *trc, JSFunction *fun);
  1278. static void
  1279. DestroyLocalNames(JSContext *cx, JSFunction *fun);
  1280. static void
  1281. fun_trace(JSTracer *trc, JSObject *obj)
  1282. {
  1283. JSFunction *fun;
  1284. /* A newborn function object may have a not yet initialized private slot. */
  1285. fun = (JSFunction *) JS_GetPrivate(trc->context, obj);
  1286. if (!fun)
  1287. return;
  1288. if (FUN_OBJECT(fun) != obj) {
  1289. /* obj is cloned function object, trace the original. */
  1290. JS_CALL_TRACER(trc, FUN_OBJECT(fun), JSTRACE_OBJECT, "private");
  1291. return;
  1292. }
  1293. if (fun->atom)
  1294. JS_CALL_STRING_TRACER(trc, ATOM_TO_STRING(fun->atom), "atom");
  1295. if (FUN_INTERPRETED(fun)) {
  1296. if (fun->u.i.script)
  1297. js_TraceScript(trc, fun->u.i.script);
  1298. TraceLocalNames(trc, fun);
  1299. }
  1300. }
  1301. static void
  1302. fun_finalize(JSContext *cx, JSObject *obj)
  1303. {
  1304. JSFunction *fun;
  1305. /* Ignore newborn and cloned function objects. */
  1306. fun = (JSFunction *) JS_GetPrivate(cx, obj);
  1307. if (!fun || FUN_OBJECT(fun) != obj)
  1308. return;
  1309. /*
  1310. * Null-check of u.i.script is required since the parser sets interpreted
  1311. * very early.
  1312. */
  1313. if (FUN_INTERPRETED(fun)) {
  1314. if (fun->u.i.script)
  1315. js_DestroyScript(cx, fun->u.i.script);
  1316. DestroyLocalNames(cx, fun);
  1317. }
  1318. }
  1319. static uint32
  1320. fun_reserveSlots(JSContext *cx, JSObject *obj)
  1321. {
  1322. JSFunction *fun;
  1323. uint32 nslots;
  1324. /*
  1325. * We use JS_GetPrivate and not GET_FUNCTION_PRIVATE because during
  1326. * js_InitFunctionClass invocation the function is called before the
  1327. * private slot of the function object is set.
  1328. */
  1329. fun = (JSFunction *) JS_GetPrivate(cx, obj);
  1330. nslots = 0;
  1331. if (fun && FUN_INTERPRETED(fun) && fun->u.i.script) {
  1332. if (fun->u.i.script->upvarsOffset != 0)
  1333. nslots = JS_SCRIPT_UPVARS(fun->u.i.script)->length;
  1334. if (fun->u.i.script->regexpsOffset != 0)
  1335. nslots += JS_SCRIPT_REGEXPS(fun->u.i.script)->length;
  1336. }
  1337. return nslots;
  1338. }
  1339. /*
  1340. * Reserve two slots in all function objects for XPConnect. Note that this
  1341. * does not bloat every instance, only those on which reserved slots are set,
  1342. * and those on which ad-hoc properties are defined.
  1343. */
  1344. JS_FRIEND_DATA(JSClass) js_FunctionClass = {
  1345. js_Function_str,
  1346. JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_HAS_RESERVED_SLOTS(2) |
  1347. JSCLASS_MARK_IS_TRACE | JSCLASS_HAS_CACHED_PROTO(JSProto_Function),
  1348. JS_PropertyStub, JS_PropertyStub,
  1349. JS_PropertyStub, JS_PropertyStub,
  1350. fun_enumerate, (JSResolveOp)fun_resolve,
  1351. fun_convert, fun_finalize,
  1352. NULL, NULL,
  1353. NULL, NULL,
  1354. fun_xdrObject, fun_hasInstance,
  1355. JS_CLASS_TRACE(fun_trace), fun_reserveSlots
  1356. };
  1357. static JSBool
  1358. fun_toStringHelper(JSContext *cx, uint32 indent, uintN argc, jsval *vp)
  1359. {
  1360. jsval fval;
  1361. JSObject *obj;
  1362. JSFunction *fun;
  1363. JSString *str;
  1364. fval = JS_THIS(cx, vp);
  1365. if (JSVAL_IS_NULL(fval))
  1366. return JS_FALSE;
  1367. if (!VALUE_IS_FUNCTION(cx, fval)) {
  1368. /*
  1369. * If we don't have a function to start off with, try converting the
  1370. * object to a function. If that doesn't work, complain.
  1371. */
  1372. if (!JSVAL_IS_PRIMITIVE(fval)) {
  1373. obj = JSVAL_TO_OBJECT(fval);
  1374. if (!OBJ_GET_CLASS(cx, obj)->convert(cx, obj, JSTYPE_FUNCTION,
  1375. &fval)) {
  1376. return JS_FALSE;
  1377. }
  1378. vp[1] = fval;
  1379. }
  1380. if (!VALUE_IS_FUNCTION(cx, fval)) {
  1381. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1382. JSMSG_INCOMPATIBLE_PROTO,
  1383. js_Function_str, js_toString_str,
  1384. JS_GetTypeName(cx, JS_TypeOfValue(cx, fval)));
  1385. return JS_FALSE;
  1386. }
  1387. }
  1388. obj = JSVAL_TO_OBJECT(fval);
  1389. if (argc != 0) {
  1390. indent = js_ValueToECMAUint32(cx, &vp[2]);
  1391. if (JSVAL_IS_NULL(vp[2]))
  1392. return JS_FALSE;
  1393. }
  1394. JS_ASSERT(JS_ObjectIsFunction(cx, obj));
  1395. fun = GET_FUNCTION_PRIVATE(cx, obj);
  1396. if (!fun)
  1397. return JS_TRUE;
  1398. str = JS_DecompileFunction(cx, fun, (uintN)indent);
  1399. if (!str)
  1400. return JS_FALSE;
  1401. *vp = STRING_TO_JSVAL(str);
  1402. return JS_TRUE;
  1403. }
  1404. static JSBool
  1405. fun_toString(JSContext *cx, uintN argc, jsval *vp)
  1406. {
  1407. return fun_toStringHelper(cx, 0, argc, vp);
  1408. }
  1409. #if JS_HAS_TOSOURCE
  1410. static JSBool
  1411. fun_toSource(JSContext *cx, uintN argc, jsval *vp)
  1412. {
  1413. return fun_toStringHelper(cx, JS_DONT_PRETTY_PRINT, argc, vp);
  1414. }
  1415. #endif
  1416. JSBool
  1417. js_fun_call(JSContext *cx, uintN argc, jsval *vp)
  1418. {
  1419. JSObject *obj;
  1420. jsval fval, *argv, *invokevp;
  1421. JSString *str;
  1422. void *mark;
  1423. JSBool ok;
  1424. obj = JS_THIS_OBJECT(cx, vp);
  1425. if (!obj || !OBJ_DEFAULT_VALUE(cx, obj, JSTYPE_FUNCTION, &vp[1]))
  1426. return JS_FALSE;
  1427. fval = vp[1];
  1428. if (!VALUE_IS_FUNCTION(cx, fval)) {
  1429. str = JS_ValueToString(cx, fval);
  1430. if (str) {
  1431. const char *bytes = js_GetStringBytes(cx, str);
  1432. if (bytes) {
  1433. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1434. JSMSG_INCOMPATIBLE_PROTO,
  1435. js_Function_str, js_call_str,
  1436. bytes);
  1437. }
  1438. }
  1439. return JS_FALSE;
  1440. }
  1441. argv = vp + 2;
  1442. if (argc == 0) {
  1443. /* Call fun with its global object as the 'this' param if no args. */
  1444. obj = NULL;
  1445. } else {
  1446. /* Otherwise convert the first arg to 'this' and skip over it. */
  1447. if (!JSVAL_IS_PRIMITIVE(argv[0]))
  1448. obj = JSVAL_TO_OBJECT(argv[0]);
  1449. else if (!js_ValueToObject(cx, argv[0], &obj))
  1450. return JS_FALSE;
  1451. argc--;
  1452. argv++;
  1453. }
  1454. /* Allocate stack space for fval, obj, and the args. */
  1455. invokevp = js_AllocStack(cx, 2 + argc, &mark);
  1456. if (!invokevp)
  1457. return JS_FALSE;
  1458. /* Push fval, obj, and the args. */
  1459. invokevp[0] = fval;
  1460. invokevp[1] = OBJECT_TO_JSVAL(obj);
  1461. memcpy(invokevp + 2, argv, argc * sizeof *argv);
  1462. ok = js_Invoke(cx, argc, invokevp, 0);
  1463. *vp = *invokevp;
  1464. js_FreeStack(cx, mark);
  1465. return ok;
  1466. }
  1467. JSBool
  1468. js_fun_apply(JSContext *cx, uintN argc, jsval *vp)
  1469. {
  1470. JSObject *obj, *aobj;
  1471. jsval fval, *invokevp, *sp;
  1472. JSString *str;
  1473. jsuint length;
  1474. JSBool arraylike, ok;
  1475. void *mark;
  1476. uintN i;
  1477. if (argc == 0) {
  1478. /* Will get globalObject as 'this' and no other arguments. */
  1479. return js_fun_call(cx, argc, vp);
  1480. }
  1481. obj = JS_THIS_OBJECT(cx, vp);
  1482. if (!obj || !OBJ_DEFAULT_VALUE(cx, obj, JSTYPE_FUNCTION, &vp[1]))
  1483. return JS_FALSE;
  1484. fval = vp[1];
  1485. if (!VALUE_IS_FUNCTION(cx, fval)) {
  1486. str = JS_ValueToString(cx, fval);
  1487. if (str) {
  1488. const char *bytes = js_GetStringBytes(cx, str);
  1489. if (bytes) {
  1490. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1491. JSMSG_INCOMPATIBLE_PROTO,
  1492. js_Function_str, js_apply_str,
  1493. bytes);
  1494. }
  1495. }
  1496. return JS_FALSE;
  1497. }
  1498. /* Quell GCC overwarnings. */
  1499. aobj = NULL;
  1500. length = 0;
  1501. if (argc >= 2) {
  1502. /* If the 2nd arg is null or void, call the function with 0 args. */
  1503. if (JSVAL_IS_NULL(vp[3]) || JSVAL_IS_VOID(vp[3])) {
  1504. argc = 0;
  1505. } else {
  1506. /* The second arg must be an array (or arguments object). */
  1507. arraylike = JS_FALSE;
  1508. if (!JSVAL_IS_PRIMITIVE(vp[3])) {
  1509. aobj = JSVAL_TO_OBJECT(vp[3]);
  1510. if (!js_IsArrayLike(cx, aobj, &arraylike, &length))
  1511. return JS_FALSE;
  1512. }
  1513. if (!arraylike) {
  1514. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1515. JSMSG_BAD_APPLY_ARGS, js_apply_str);
  1516. return JS_FALSE;
  1517. }
  1518. }
  1519. }
  1520. /* Convert the first arg to 'this' and skip over it. */
  1521. if (!JSVAL_IS_PRIMITIVE(vp[2]))
  1522. obj = JSVAL_TO_OBJECT(vp[2]);
  1523. else if (!js_ValueToObject(cx, vp[2], &obj))
  1524. return JS_FALSE;
  1525. /* Allocate stack space for fval, obj, and the args. */
  1526. argc = (uintN)JS_MIN(length, ARRAY_INIT_LIMIT - 1);
  1527. invokevp = js_AllocStack(cx, 2 + argc, &mark);
  1528. if (!invokevp)
  1529. return JS_FALSE;
  1530. /* Push fval, obj, and aobj's elements as args. */
  1531. sp = invokevp;
  1532. *sp++ = fval;
  1533. *sp++ = OBJECT_TO_JSVAL(obj);
  1534. for (i = 0; i < argc; i++) {
  1535. ok = JS_GetElement(cx, aobj, (jsint)i, sp);
  1536. if (!ok)
  1537. goto out;
  1538. sp++;
  1539. }
  1540. ok = js_Invoke(cx, argc, invokevp, 0);
  1541. *vp = *invokevp;
  1542. out:
  1543. js_FreeStack(cx, mark);
  1544. return ok;
  1545. }
  1546. #ifdef NARCISSUS
  1547. static JSBool
  1548. fun_applyConstructor(JSContext *cx, uintN argc, jsval *vp)
  1549. {
  1550. JSObject *aobj;
  1551. uintN length, i;
  1552. void *mark;
  1553. jsval *invokevp, *sp;
  1554. JSBool ok;
  1555. if (JSVAL_IS_PRIMITIVE(vp[2]) ||
  1556. (aobj = JSVAL_TO_OBJECT(vp[2]),
  1557. OBJ_GET_CLASS(cx, aobj) != &js_ArrayClass &&
  1558. OBJ_GET_CLASS(cx, aobj) != &js_ArgumentsClass)) {
  1559. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1560. JSMSG_BAD_APPLY_ARGS, "__applyConstruct__");
  1561. return JS_FALSE;
  1562. }
  1563. if (!js_GetLengthProperty(cx, aobj, &length))
  1564. return JS_FALSE;
  1565. if (length >= ARRAY_INIT_LIMIT)
  1566. length = ARRAY_INIT_LIMIT - 1;
  1567. invokevp = js_AllocStack(cx, 2 + length, &mark);
  1568. if (!invokevp)
  1569. return JS_FALSE;
  1570. sp = invokevp;
  1571. *sp++ = vp[1];
  1572. *sp++ = JSVAL_NULL; /* this is filled automagically */
  1573. for (i = 0; i < length; i++) {
  1574. ok = JS_GetElement(cx, aobj, (jsint)i, sp);
  1575. if (!ok)
  1576. goto out;
  1577. sp++;
  1578. }
  1579. ok = js_InvokeConstructor(cx, length, JS_TRUE, invokevp);
  1580. *vp = *invokevp;
  1581. out:
  1582. js_FreeStack(cx, mark);
  1583. return ok;
  1584. }
  1585. #endif
  1586. static JSFunctionSpec function_methods[] = {
  1587. #if JS_HAS_TOSOURCE
  1588. JS_FN(js_toSource_str, fun_toSource, 0,0),
  1589. #endif
  1590. JS_FN(js_toString_str, fun_toString, 0,0),
  1591. JS_FN(js_apply_str, js_fun_apply, 2,0),
  1592. JS_FN(js_call_str, js_fun_call, 1,0),
  1593. #ifdef NARCISSUS
  1594. JS_FN("__applyConstructor__", fun_applyConstructor, 1,0),
  1595. #endif
  1596. JS_FS_END
  1597. };
  1598. static JSBool
  1599. Function(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
  1600. {
  1601. JSStackFrame *fp, *caller;
  1602. JSFunction *fun;
  1603. JSObject *parent;
  1604. uintN i, n, lineno;
  1605. JSAtom *atom;
  1606. const char *filename;
  1607. JSBool ok;
  1608. JSString *str, *arg;
  1609. JSTokenStream ts;
  1610. JSPrincipals *principals;
  1611. jschar *collected_args, *cp;
  1612. void *mark;
  1613. size_t arg_length, args_length, old_args_length;
  1614. JSTokenType tt;
  1615. fp = cx->fp;
  1616. if (!(fp->flags & JSFRAME_CONSTRUCTING)) {
  1617. obj = js_NewObject(cx, &js_FunctionClass, NULL, NULL, 0);
  1618. if (!obj)
  1619. return JS_FALSE;
  1620. *rval = OBJECT_TO_JSVAL(obj);
  1621. } else {
  1622. /*
  1623. * The constructor is called before the private slot is initialized so
  1624. * we must use JS_GetPrivate, not GET_FUNCTION_PRIVATE here.
  1625. */
  1626. if (JS_GetPrivate(cx, obj))
  1627. return JS_TRUE;
  1628. }
  1629. /*
  1630. * NB: (new Function) is not lexically closed by its caller, it's just an
  1631. * anonymous function in the top-level scope that its constructor inhabits.
  1632. * Thus 'var x = 42; f = new Function("return x"); print(f())' prints 42,
  1633. * and so would a call to f from another top-level's script or function.
  1634. *
  1635. * In older versions, before call objects, a new Function was adopted by
  1636. * its running context's globalObject, which might be different from the
  1637. * top-level reachable from scopeChain (in HTML frames, e.g.).
  1638. */
  1639. parent = OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(argv[-2]));
  1640. fun = js_NewFunction(cx, obj, NULL, 0, JSFUN_LAMBDA | JSFUN_INTERPRETED,
  1641. parent, cx->runtime->atomState.anonymousAtom);
  1642. if (!fun)
  1643. return JS_FALSE;
  1644. /*
  1645. * Function is static and not called directly by other functions in this
  1646. * file, therefore it is callable only as a native function by js_Invoke.
  1647. * Find the scripted caller, possibly skipping other native frames such as
  1648. * are built for Function.prototype.call or .apply activations that invoke
  1649. * Function indirectly from a script.
  1650. */
  1651. JS_ASSERT(!fp->script && fp->fun && fp->fun->u.n.native == Function);
  1652. caller = JS_GetScriptedCaller(cx, fp);
  1653. if (caller) {
  1654. principals = JS_EvalFramePrincipals(cx, fp, caller);
  1655. filename = js_ComputeFilename(cx, caller, principals, &lineno);
  1656. } else {
  1657. filename = NULL;
  1658. lineno = 0;
  1659. principals = NULL;
  1660. }
  1661. /* Belt-and-braces: check that the caller has access to parent. */
  1662. if (!js_CheckPrincipalsAccess(cx, parent, principals,
  1663. CLASS_ATOM(cx, Function))) {
  1664. return JS_FALSE;
  1665. }
  1666. n = argc ? argc - 1 : 0;
  1667. if (n > 0) {
  1668. enum { OK, BAD, BAD_FORMAL } state;
  1669. /*
  1670. * Collect the function-argument arguments into one string, separated
  1671. * by commas, then make a tokenstream from that string, and scan it to
  1672. * get the arguments. We need to throw the full scanner at the
  1673. * problem, because the argument string can legitimately contain
  1674. * comments and linefeeds. XXX It might be better to concatenate
  1675. * everything up into a function definition and pass it to the
  1676. * compiler, but doing it this way is less of a delta from the old
  1677. * code. See ECMA 15.3.2.1.
  1678. */
  1679. state = BAD_FORMAL;
  1680. args_length = 0;
  1681. for (i = 0; i < n; i++) {
  1682. /* Collect the lengths for all the function-argument arguments. */
  1683. arg = js_ValueToString(cx, argv[i]);
  1684. if (!arg)
  1685. return JS_FALSE;
  1686. argv[i] = STRING_TO_JSVAL(arg);
  1687. /*
  1688. * Check for overflow. The < test works because the maximum
  1689. * JSString length fits in 2 fewer bits than size_t has.
  1690. */
  1691. old_args_length = args_length;
  1692. args_length = old_args_length + JSSTRING_LENGTH(arg);
  1693. if (args_length < old_args_length) {
  1694. js_ReportAllocationOverflow(cx);
  1695. return JS_FALSE;
  1696. }
  1697. }
  1698. /* Add 1 for each joining comma and check for overflow (two ways). */
  1699. old_args_length = args_length;
  1700. args_length = old_args_length + n - 1;
  1701. if (args_length < old_args_length ||
  1702. args_length >= ~(size_t)0 / sizeof(jschar)) {
  1703. js_ReportAllocationOverflow(cx);
  1704. return JS_FALSE;
  1705. }
  1706. /*
  1707. * Allocate a string to hold the concatenated arguments, including room
  1708. * for a terminating 0. Mark cx->tempPool for later release, to free
  1709. * collected_args and its tokenstream in one swoop.
  1710. */
  1711. mark = JS_ARENA_MARK(&cx->tempPool);
  1712. JS_ARENA_ALLOCATE_CAST(cp, jschar *, &cx->tempPool,
  1713. (args_length+1) * sizeof(jschar));
  1714. if (!cp) {
  1715. js_ReportOutOfScriptQuota(cx);
  1716. return JS_FALSE;
  1717. }
  1718. collected_args = cp;
  1719. /*
  1720. * Concatenate the arguments into the new string, separated by commas.
  1721. */
  1722. for (i = 0; i < n; i++) {
  1723. arg = JSVAL_TO_STRING(argv[i]);
  1724. arg_length = JSSTRING_LENGTH(arg);
  1725. (void) js_strncpy(cp, JSSTRING_CHARS(arg), arg_length);
  1726. cp += arg_length;
  1727. /* Add separating comma or terminating 0. */
  1728. *cp++ = (i + 1 < n) ? ',' : 0;
  1729. }
  1730. /* Initialize a tokenstream that reads from the given string. */
  1731. if (!js_InitTokenStream(cx, &ts, collected_args, args_length,
  1732. NULL, filename, lineno)) {
  1733. JS_ARENA_RELEASE(&cx->tempPool, mark);
  1734. return JS_FALSE;
  1735. }
  1736. /* The argument string may be empty or contain no tokens. */
  1737. tt = js_GetToken(cx, &ts);
  1738. if (tt != TOK_EOF) {
  1739. for (;;) {
  1740. /*
  1741. * Check that it's a name. This also implicitly guards against
  1742. * TOK_ERROR, which was already reported.
  1743. */
  1744. if (tt != TOK_NAME)
  1745. goto after_args;
  1746. /*
  1747. * Get the atom corresponding to the name from the token
  1748. * stream; we're assured at this point that it's a valid
  1749. * identifier.
  1750. */
  1751. atom = CURRENT_TOKEN(&ts).t_atom;
  1752. /* Check for a duplicate parameter name. */
  1753. if (js_LookupLocal(cx, fun, atom, NULL) != JSLOCAL_NONE) {
  1754. const char *name;
  1755. name = js_AtomToPrintableString(cx, atom);
  1756. ok = name &&
  1757. js_ReportCompileErrorNumber(cx, &ts, NULL,
  1758. JSREPORT_WARNING |
  1759. JSREPORT_STRICT,
  1760. JSMSG_DUPLICATE_FORMAL,
  1761. name);
  1762. if (!ok)
  1763. goto after_args;
  1764. }
  1765. if (!js_AddLocal(cx, fun, atom, JSLOCAL_ARG))
  1766. goto after_args;
  1767. /*
  1768. * Get the next token. Stop on end of stream. Otherwise
  1769. * insist on a comma, get another name, and iterate.
  1770. */
  1771. tt = js_GetToken(cx, &ts);
  1772. if (tt == TOK_EOF)
  1773. break;
  1774. if (tt != TOK_COMMA)
  1775. goto after_args;
  1776. tt = js_GetToken(cx, &ts);
  1777. }
  1778. }
  1779. state = OK;
  1780. after_args:
  1781. if (state == BAD_FORMAL && !(ts.flags & TSF_ERROR)) {
  1782. /*
  1783. * Report "malformed formal parameter" iff no illegal char or
  1784. * similar scanner error was already reported.
  1785. */
  1786. JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
  1787. JSMSG_BAD_FORMAL);
  1788. }
  1789. js_CloseTokenStream(cx, &ts);
  1790. JS_ARENA_RELEASE(&cx->tempPool, mark);
  1791. if (state != OK)
  1792. return JS_FALSE;
  1793. }
  1794. if (argc) {
  1795. str = js_ValueToString(cx, argv[argc-1]);
  1796. if (!str)
  1797. return JS_FALSE;
  1798. argv[argc-1] = STRING_TO_JSVAL(str);
  1799. } else {
  1800. str = cx->runtime->emptyString;
  1801. }
  1802. return js_CompileFunctionBody(cx, fun, principals,
  1803. JSSTRING_CHARS(str), JSSTRING_LENGTH(str),
  1804. filename, lineno);
  1805. }
  1806. JSObject *
  1807. js_InitFunctionClass(JSContext *cx, JSObject *obj)
  1808. {
  1809. JSObject *proto;
  1810. JSFunction *fun;
  1811. proto = JS_InitClass(cx, obj, NULL, &js_FunctionClass, Function, 1,
  1812. function_props, function_methods, NULL, NULL);
  1813. if (!proto)
  1814. return NULL;
  1815. fun = js_NewFunction(cx, proto, NULL, 0, JSFUN_INTERPRETED, obj, NULL);
  1816. if (!fun)
  1817. goto bad;
  1818. fun->u.i.script = js_NewScript(cx, 1, 1, 0, 0, 0, 0, 0);
  1819. if (!fun->u.i.script)
  1820. goto bad;
  1821. fun->u.i.script->code[0] = JSOP_STOP;
  1822. *SCRIPT_NOTES(fun->u.i.script) = SRC_NULL;
  1823. #ifdef CHECK_SCRIPT_OWNER
  1824. fun->u.i.script->owner = NULL;
  1825. #endif
  1826. return proto;
  1827. bad:
  1828. cx->weakRoots.newborn[GCX_OBJECT] = NULL;
  1829. return NULL;
  1830. }
  1831. JSObject *
  1832. js_InitCallClass(JSContext *cx, JSObject *obj)
  1833. {
  1834. JSObject *proto;
  1835. proto = JS_InitClass(cx, obj, NULL, &js_CallClass, NULL, 0,
  1836. NULL, NULL, NULL, NULL);
  1837. if (!proto)
  1838. return NULL;
  1839. /*
  1840. * Null Call.prototype's proto slot so that Object.prototype.* does not
  1841. * pollute the scope of heavyweight functions.
  1842. */
  1843. OBJ_CLEAR_PROTO(cx, proto);
  1844. return proto;
  1845. }
  1846. JSFunction *
  1847. js_NewFunction(JSContext *cx, JSObject *funobj, JSNative native, uintN nargs,
  1848. uintN flags, JSObject *parent, JSAtom *atom)
  1849. {
  1850. JSFunction *fun;
  1851. if (funobj) {
  1852. JS_ASSERT(HAS_FUNCTION_CLASS(funobj));
  1853. OBJ_SET_PARENT(cx, funobj, parent);
  1854. } else {
  1855. funobj = js_NewObject(cx, &js_FunctionClass, NULL, parent, 0);
  1856. if (!funobj)
  1857. return NULL;
  1858. }
  1859. JS_ASSERT(JSVAL_IS_VOID(funobj->fslots[JSSLOT_PRIVATE]));
  1860. fun = (JSFunction *) funobj;
  1861. /* Initialize all function members. */
  1862. fun->nargs = nargs;
  1863. fun->flags = flags & (JSFUN_FLAGS_MASK | JSFUN_INTERPRETED | JSFUN_TRACEABLE);
  1864. if (flags & JSFUN_INTERPRETED) {
  1865. JS_ASSERT(!native);
  1866. JS_ASSERT(nargs == 0);
  1867. fun->u.i.nvars = 0;
  1868. fun->u.i.nupvars = 0;
  1869. fun->u.i.script = NULL;
  1870. #ifdef DEBUG
  1871. fun