PageRenderTime 60ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ | 497 lines | 385 code | 58 blank | 54 comment | 76 complexity | 4967552c23efce3e90821ec1020bc059 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 SpiderMonkey JavaScript 1.9 code, released
  18. * May 28, 2008.
  19. *
  20. * The Initial Developer of the Original Code is
  21. * Andreas Gal <gal@mozilla.com>
  22. *
  23. * Contributor(s):
  24. * Brendan Eich <brendan@mozilla.org>
  25. * Mike Shaver <shaver@mozilla.org>
  26. * David Anderson <danderson@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. #include "jsstddef.h"
  42. #include <math.h>
  43. #include "jsapi.h"
  44. #include "jsarray.h"
  45. #include "jsbool.h"
  46. #include "jscntxt.h"
  47. #include "jsgc.h"
  48. #include "jsiter.h"
  49. #include "jslibmath.h"
  50. #include "jsmath.h"
  51. #include "jsnum.h"
  52. #include "prmjtime.h"
  53. #include "jsdate.h"
  54. #include "jsscope.h"
  55. #include "jsstr.h"
  56. #include "jsbuiltins.h"
  57. #include "jstracer.h"
  58. using namespace avmplus;
  59. using namespace nanojit;
  60. extern jsdouble js_NaN;
  61. /*
  62. * NB: bool FASTCALL is not compatible with Nanojit's calling convention usage.
  63. * Do not use bool FASTCALL, use JSBool only!
  64. */
  65. jsdouble FASTCALL
  66. js_dmod(jsdouble a, jsdouble b)
  67. {
  68. if (b == 0.0) {
  69. jsdpun u;
  70. u.s.hi = JSDOUBLE_HI32_EXPMASK | JSDOUBLE_HI32_MANTMASK;
  71. u.s.lo = 0xffffffff;
  72. return u.d;
  73. }
  74. jsdouble r;
  75. #ifdef XP_WIN
  76. /* Workaround MS fmod bug where 42 % (1/0) => NaN, not 42. */
  77. if (JSDOUBLE_IS_FINITE(a) && JSDOUBLE_IS_INFINITE(b))
  78. r = a;
  79. else
  80. #endif
  81. r = fmod(a, b);
  82. return r;
  83. }
  84. int32 FASTCALL
  85. js_imod(int32 a, int32 b)
  86. {
  87. if (a < 0 || b <= 0)
  88. return -1;
  89. int r = a % b;
  90. return r;
  91. }
  92. /* The following boxing/unboxing primitives we can't emit inline because
  93. they either interact with the GC and depend on Spidermonkey's 32-bit
  94. integer representation. */
  95. jsval FASTCALL
  96. js_BoxDouble(JSContext* cx, jsdouble d)
  97. {
  98. int32 i;
  99. if (JSDOUBLE_IS_INT(d, i) && INT_FITS_IN_JSVAL(i))
  100. return INT_TO_JSVAL(i);
  101. JS_ASSERT(JS_ON_TRACE(cx));
  102. jsval v; /* not rooted but ok here because we know GC won't run */
  103. if (!js_NewDoubleInRootedValue(cx, d, &v))
  104. return JSVAL_ERROR_COOKIE;
  105. return v;
  106. }
  107. jsval FASTCALL
  108. js_BoxInt32(JSContext* cx, int32 i)
  109. {
  110. if (JS_LIKELY(INT_FITS_IN_JSVAL(i)))
  111. return INT_TO_JSVAL(i);
  112. JS_ASSERT(JS_ON_TRACE(cx));
  113. jsval v; /* not rooted but ok here because we know GC won't run */
  114. jsdouble d = (jsdouble)i;
  115. if (!js_NewDoubleInRootedValue(cx, d, &v))
  116. return JSVAL_ERROR_COOKIE;
  117. return v;
  118. }
  119. jsdouble FASTCALL
  120. js_UnboxDouble(jsval v)
  121. {
  122. if (JS_LIKELY(JSVAL_IS_INT(v)))
  123. return (jsdouble)JSVAL_TO_INT(v);
  124. return *JSVAL_TO_DOUBLE(v);
  125. }
  126. int32 FASTCALL
  127. js_UnboxInt32(jsval v)
  128. {
  129. if (JS_LIKELY(JSVAL_IS_INT(v)))
  130. return JSVAL_TO_INT(v);
  131. return js_DoubleToECMAInt32(*JSVAL_TO_DOUBLE(v));
  132. }
  133. int32 FASTCALL
  134. js_DoubleToInt32(jsdouble d)
  135. {
  136. return js_DoubleToECMAInt32(d);
  137. }
  138. uint32 FASTCALL
  139. js_DoubleToUint32(jsdouble d)
  140. {
  141. return js_DoubleToECMAUint32(d);
  142. }
  143. jsdouble FASTCALL
  144. js_StringToNumber(JSContext* cx, JSString* str)
  145. {
  146. const jschar* bp;
  147. const jschar* end;
  148. const jschar* ep;
  149. jsdouble d;
  150. JSSTRING_CHARS_AND_END(str, bp, end);
  151. if ((!js_strtod(cx, bp, end, &ep, &d) ||
  152. js_SkipWhiteSpace(ep, end) != end) &&
  153. (!js_strtointeger(cx, bp, end, &ep, 0, &d) ||
  154. js_SkipWhiteSpace(ep, end) != end)) {
  155. return js_NaN;
  156. }
  157. return d;
  158. }
  159. int32 FASTCALL
  160. js_StringToInt32(JSContext* cx, JSString* str)
  161. {
  162. const jschar* bp;
  163. const jschar* end;
  164. const jschar* ep;
  165. jsdouble d;
  166. JSSTRING_CHARS_AND_END(str, bp, end);
  167. if (!js_strtod(cx, bp, end, &ep, &d) || js_SkipWhiteSpace(ep, end) != end)
  168. return 0;
  169. return js_DoubleToECMAInt32(d);
  170. }
  171. static inline JSBool
  172. js_Int32ToId(JSContext* cx, int32 index, jsid* id)
  173. {
  174. if (index <= JSVAL_INT_MAX) {
  175. *id = INT_TO_JSID(index);
  176. return JS_TRUE;
  177. }
  178. JSString* str = js_NumberToString(cx, index);
  179. if (!str)
  180. return JS_FALSE;
  181. return js_ValueToStringId(cx, STRING_TO_JSVAL(str), id);
  182. }
  183. jsval FASTCALL
  184. js_Any_getprop(JSContext* cx, JSObject* obj, JSString* idstr)
  185. {
  186. jsval v;
  187. jsid id;
  188. if (!js_ValueToStringId(cx, STRING_TO_JSVAL(idstr), &id))
  189. return JSVAL_ERROR_COOKIE;
  190. if (!OBJ_GET_PROPERTY(cx, obj, id, &v))
  191. return JSVAL_ERROR_COOKIE;
  192. return v;
  193. }
  194. JSBool FASTCALL
  195. js_Any_setprop(JSContext* cx, JSObject* obj, JSString* idstr, jsval v)
  196. {
  197. jsid id;
  198. if (!js_ValueToStringId(cx, STRING_TO_JSVAL(idstr), &id))
  199. return JS_FALSE;
  200. return OBJ_SET_PROPERTY(cx, obj, id, &v);
  201. }
  202. jsval FASTCALL
  203. js_Any_getelem(JSContext* cx, JSObject* obj, int32 index)
  204. {
  205. jsval v;
  206. jsid id;
  207. if (!js_Int32ToId(cx, index, &id))
  208. return JSVAL_ERROR_COOKIE;
  209. if (!OBJ_GET_PROPERTY(cx, obj, id, &v))
  210. return JSVAL_ERROR_COOKIE;
  211. return v;
  212. }
  213. JSBool FASTCALL
  214. js_Any_setelem(JSContext* cx, JSObject* obj, int32 index, jsval v)
  215. {
  216. jsid id;
  217. if (!js_Int32ToId(cx, index, &id))
  218. return JSVAL_ERROR_COOKIE;
  219. return OBJ_SET_PROPERTY(cx, obj, id, &v);
  220. }
  221. JSObject* FASTCALL
  222. js_FastValueToIterator(JSContext* cx, jsuint flags, jsval v)
  223. {
  224. if (!js_ValueToIterator(cx, flags, &v))
  225. return NULL;
  226. return JSVAL_TO_OBJECT(v);
  227. }
  228. jsval FASTCALL
  229. js_FastCallIteratorNext(JSContext* cx, JSObject* iterobj)
  230. {
  231. jsval v;
  232. if (!js_CallIteratorNext(cx, iterobj, &v))
  233. return JSVAL_ERROR_COOKIE;
  234. return v;
  235. }
  236. SideExit* FASTCALL
  237. js_CallTree(InterpState* state, Fragment* f)
  238. {
  239. union { NIns *code; GuardRecord* (FASTCALL *func)(InterpState*, Fragment*); } u;
  240. u.code = f->code();
  241. JS_ASSERT(u.code);
  242. GuardRecord* rec;
  243. #if defined(JS_NO_FASTCALL) && defined(NANOJIT_IA32)
  244. SIMULATE_FASTCALL(rec, state, NULL, u.func);
  245. #else
  246. rec = u.func(state, NULL);
  247. #endif
  248. VMSideExit* lr = (VMSideExit*)rec->exit;
  249. if (lr->exitType == NESTED_EXIT) {
  250. /* This only occurs once a tree call guard mismatches and we unwind the tree call stack.
  251. We store the first (innermost) tree call guard in state and we will try to grow
  252. the outer tree the failing call was in starting at that guard. */
  253. if (!state->lastTreeCallGuard) {
  254. state->lastTreeCallGuard = lr;
  255. FrameInfo* rp = (FrameInfo*)state->rp;
  256. state->rpAtLastTreeCall = rp + lr->calldepth;
  257. }
  258. } else {
  259. /* If the tree exits on a regular (non-nested) guard, keep updating lastTreeExitGuard
  260. with that guard. If we mismatch on a tree call guard, this will contain the last
  261. non-nested guard we encountered, which is the innermost loop or branch guard. */
  262. state->lastTreeExitGuard = lr;
  263. }
  264. return lr;
  265. }
  266. JSObject* FASTCALL
  267. js_FastNewObject(JSContext* cx, JSObject* ctor)
  268. {
  269. JS_ASSERT(HAS_FUNCTION_CLASS(ctor));
  270. JSFunction* fun = GET_FUNCTION_PRIVATE(cx, ctor);
  271. JSClass* clasp = (FUN_INTERPRETED(fun) || (fun->flags & JSFUN_TRACEABLE))
  272. ? &js_ObjectClass
  273. : FUN_CLASP(fun);
  274. JS_ASSERT(clasp != &js_ArrayClass);
  275. JS_LOCK_OBJ(cx, ctor);
  276. JSScope *scope = OBJ_SCOPE(ctor);
  277. JS_ASSERT(scope->object == ctor);
  278. JSAtom* atom = cx->runtime->atomState.classPrototypeAtom;
  279. JSScopeProperty *sprop = SCOPE_GET_PROPERTY(scope, ATOM_TO_JSID(atom));
  280. JS_ASSERT(SPROP_HAS_VALID_SLOT(sprop, scope));
  281. jsval v = LOCKED_OBJ_GET_SLOT(ctor, sprop->slot);
  282. JS_UNLOCK_SCOPE(cx, scope);
  283. JSObject* proto;
  284. if (JSVAL_IS_PRIMITIVE(v)) {
  285. if (!js_GetClassPrototype(cx, JSVAL_TO_OBJECT(ctor->fslots[JSSLOT_PARENT]),
  286. INT_TO_JSID(JSProto_Object), &proto)) {
  287. return NULL;
  288. }
  289. } else {
  290. proto = JSVAL_TO_OBJECT(v);
  291. }
  292. JS_ASSERT(JS_ON_TRACE(cx));
  293. JSObject* obj = (JSObject*) js_NewGCThing(cx, GCX_OBJECT, sizeof(JSObject));
  294. if (!obj)
  295. return NULL;
  296. obj->classword = jsuword(clasp);
  297. obj->fslots[JSSLOT_PROTO] = OBJECT_TO_JSVAL(proto);
  298. obj->fslots[JSSLOT_PARENT] = ctor->fslots[JSSLOT_PARENT];
  299. for (unsigned i = JSSLOT_PRIVATE; i != JS_INITIAL_NSLOTS; ++i)
  300. obj->fslots[i] = JSVAL_VOID;
  301. obj->map = js_HoldObjectMap(cx, proto->map);
  302. obj->dslots = NULL;
  303. return obj;
  304. }
  305. JSBool FASTCALL
  306. js_AddProperty(JSContext* cx, JSObject* obj, JSScopeProperty* sprop)
  307. {
  308. JSScopeProperty* sprop2 = NULL; // initialize early to make MSVC happy
  309. JS_ASSERT(OBJ_IS_NATIVE(obj));
  310. JS_ASSERT(SPROP_HAS_STUB_SETTER(sprop));
  311. JS_LOCK_OBJ(cx, obj);
  312. JSScope* scope = OBJ_SCOPE(obj);
  313. if (scope->object == obj) {
  314. JS_ASSERT(!SCOPE_HAS_PROPERTY(scope, sprop));
  315. } else {
  316. scope = js_GetMutableScope(cx, obj);
  317. if (!scope) {
  318. JS_UNLOCK_OBJ(cx, obj);
  319. return JS_FALSE;
  320. }
  321. }
  322. uint32 slot = sprop->slot;
  323. if (!scope->table && sprop->parent == scope->lastProp && slot == scope->map.freeslot) {
  324. if (slot < STOBJ_NSLOTS(obj) && !OBJ_GET_CLASS(cx, obj)->reserveSlots) {
  325. JS_ASSERT(JSVAL_IS_VOID(STOBJ_GET_SLOT(obj, scope->map.freeslot)));
  326. ++scope->map.freeslot;
  327. } else {
  328. if (!js_AllocSlot(cx, obj, &slot)) {
  329. JS_UNLOCK_SCOPE(cx, scope);
  330. return JS_FALSE;
  331. }
  332. if (slot != sprop->slot)
  333. goto slot_changed;
  334. }
  335. SCOPE_EXTEND_SHAPE(cx, scope, sprop);
  336. ++scope->entryCount;
  337. scope->lastProp = sprop;
  338. JS_UNLOCK_SCOPE(cx, scope);
  339. return JS_TRUE;
  340. }
  341. sprop2 = js_AddScopeProperty(cx, scope, sprop->id,
  342. sprop->getter, sprop->setter, SPROP_INVALID_SLOT,
  343. sprop->attrs, sprop->flags, sprop->shortid);
  344. if (sprop2 == sprop) {
  345. JS_UNLOCK_SCOPE(cx, scope);
  346. return JS_TRUE;
  347. }
  348. slot = sprop2->slot;
  349. slot_changed:
  350. js_FreeSlot(cx, obj, slot);
  351. JS_UNLOCK_SCOPE(cx, scope);
  352. return JS_FALSE;
  353. }
  354. JSBool FASTCALL
  355. js_HasNamedProperty(JSContext* cx, JSObject* obj, JSString* idstr)
  356. {
  357. jsid id;
  358. if (!obj || !js_ValueToStringId(cx, STRING_TO_JSVAL(idstr), &id))
  359. return JSVAL_TO_BOOLEAN(JSVAL_VOID);
  360. JSObject* obj2;
  361. JSProperty* prop;
  362. if (!OBJ_LOOKUP_PROPERTY(cx, obj, id, &obj2, &prop))
  363. return JSVAL_TO_BOOLEAN(JSVAL_VOID);
  364. if (prop)
  365. OBJ_DROP_PROPERTY(cx, obj2, prop);
  366. return prop != NULL;
  367. }
  368. JSBool FASTCALL
  369. js_HasNamedPropertyInt32(JSContext* cx, JSObject* obj, int32 index)
  370. {
  371. jsid id;
  372. if (!obj || !js_Int32ToId(cx, index, &id))
  373. return JSVAL_TO_BOOLEAN(JSVAL_VOID);
  374. JSObject* obj2;
  375. JSProperty* prop;
  376. if (!OBJ_LOOKUP_PROPERTY(cx, obj, id, &obj2, &prop))
  377. return JSVAL_TO_BOOLEAN(JSVAL_VOID);
  378. if (prop)
  379. OBJ_DROP_PROPERTY(cx, obj2, prop);
  380. return prop != NULL;
  381. }
  382. jsval FASTCALL
  383. js_CallGetter(JSContext* cx, JSObject* obj, JSScopeProperty* sprop)
  384. {
  385. JS_ASSERT(!SPROP_HAS_STUB_GETTER(sprop));
  386. jsval v;
  387. if (!SPROP_GET(cx, sprop, obj, obj, &v))
  388. return JSVAL_ERROR_COOKIE;
  389. return v;
  390. }
  391. JSString* FASTCALL
  392. js_TypeOfObject(JSContext* cx, JSObject* obj)
  393. {
  394. JSType type = JS_TypeOfValue(cx, OBJECT_TO_JSVAL(obj));
  395. return ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[type]);
  396. }
  397. JSString* FASTCALL
  398. js_TypeOfBoolean(JSContext* cx, int32 unboxed)
  399. {
  400. jsval boxed = BOOLEAN_TO_JSVAL(unboxed);
  401. JS_ASSERT(JSVAL_IS_VOID(boxed) || JSVAL_IS_BOOLEAN(boxed));
  402. JSType type = JS_TypeOfValue(cx, boxed);
  403. return ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[type]);
  404. }
  405. jsdouble FASTCALL
  406. js_BooleanOrUndefinedToNumber(JSContext* cx, int32 unboxed)
  407. {
  408. if (unboxed == JSVAL_TO_BOOLEAN(JSVAL_VOID))
  409. return js_NaN;
  410. return unboxed;
  411. }
  412. JSString* FASTCALL
  413. js_BooleanOrUndefinedToString(JSContext *cx, int32 unboxed)
  414. {
  415. JS_ASSERT(uint32(unboxed) <= 2);
  416. return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[unboxed]);
  417. }
  418. JSString* FASTCALL
  419. js_ObjectToString(JSContext* cx, JSObject* obj)
  420. {
  421. if (!obj)
  422. return ATOM_TO_STRING(cx->runtime->atomState.nullAtom);
  423. jsval v;
  424. if (!OBJ_DEFAULT_VALUE(cx, obj, JSTYPE_STRING, &v))
  425. return NULL;
  426. JS_ASSERT(JSVAL_IS_STRING(v));
  427. return JSVAL_TO_STRING(v);
  428. }
  429. JSObject* FASTCALL
  430. js_Arguments(JSContext* cx)
  431. {
  432. return NULL;
  433. }
  434. #define BUILTIN1 JS_DEFINE_CALLINFO_1
  435. #define BUILTIN2 JS_DEFINE_CALLINFO_2
  436. #define BUILTIN3 JS_DEFINE_CALLINFO_3
  437. #define BUILTIN4 JS_DEFINE_CALLINFO_4
  438. #define BUILTIN5 JS_DEFINE_CALLINFO_5
  439. #include "builtins.tbl"