PageRenderTime 306ms CodeModel.GetById 162ms app.highlight 13ms RepoModel.GetById 127ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ Header | 296 lines | 150 code | 50 blank | 96 comment | 3 complexity | 58edbe1f7a5df9208436b4271a438ae9 MD5 | raw file
  1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2 *
  3 * ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is Mozilla Communicator client code, released
 17 * March 31, 1998.
 18 *
 19 * The Initial Developer of the Original Code is
 20 * Netscape Communications Corporation.
 21 * Portions created by the Initial Developer are Copyright (C) 1998
 22 * the Initial Developer. All Rights Reserved.
 23 *
 24 * Contributor(s):
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either of the GNU General Public License Version 2 or later (the "GPL"),
 28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40#ifndef jsfun_h___
 41#define jsfun_h___
 42/*
 43 * JS function definitions.
 44 */
 45#include "jsprvtd.h"
 46#include "jspubtd.h"
 47#include "jsobj.h"
 48
 49JS_BEGIN_EXTERN_C
 50
 51typedef struct JSLocalNameMap JSLocalNameMap;
 52
 53/*
 54 * Depending on the number of arguments and variables in the function their
 55 * names and attributes are stored either as a single atom or as an array of
 56 * tagged atoms (when there are few locals) or as a hash-based map (when there
 57 * are many locals). In the first 2 cases the lowest bit of the atom is used
 58 * as a tag to distinguish const from var. See jsfun.c for details.
 59 */
 60typedef union JSLocalNames {
 61    jsuword         taggedAtom;
 62    jsuword         *array;
 63    JSLocalNameMap  *map;
 64} JSLocalNames;
 65
 66struct JSFunction {
 67    JSObject        object;       /* GC'ed object header */
 68    uint16          nargs;        /* maximum number of specified arguments,
 69                                     reflected as f.length/f.arity */
 70    uint16          flags;        /* flags, see JSFUN_* below and in jsapi.h */
 71    union {
 72        struct {
 73            uint16      extra;    /* number of arg slots for local GC roots */
 74            uint16      spare;    /* reserved for future use */
 75            JSNative    native;   /* native method pointer or null */
 76            union {
 77                JSClass             *clasp;    /* class of objects constructed
 78                                                  by this function */
 79                JSTraceableNative   *trcinfo;  /* tracer metadata; can be first
 80                                                  element of array */
 81            } u;
 82        } n;
 83        struct {
 84            uint16      nvars;    /* number of local variables */
 85            uint16      nupvars;  /* number of upvars (computable from script
 86                                     but here for faster access) */
 87            JSScript    *script;  /* interpreted bytecode descriptor or null */
 88            JSLocalNames names;   /* argument and variable names */
 89        } i;
 90    } u;
 91    JSAtom          *atom;        /* name for diagnostics and decompiling */
 92};
 93
 94#define JSFUN_TRACEABLE      0x2000 /* can trace across calls to this native
 95                                       function; use FUN_TRCINFO if set,
 96                                       FUN_CLASP if unset */
 97#define JSFUN_EXPR_CLOSURE   0x4000 /* expression closure: function(x)x*x */
 98#define JSFUN_INTERPRETED    0x8000 /* use u.i if set, u.n if unset */
 99
100#define JSFUN_SCRIPT_OR_FAST_NATIVE (JSFUN_INTERPRETED | JSFUN_FAST_NATIVE)
101
102#define FUN_OBJECT(fun)      (&(fun)->object)
103#define FUN_INTERPRETED(fun) ((fun)->flags & JSFUN_INTERPRETED)
104#define FUN_SLOW_NATIVE(fun) (!((fun)->flags & JSFUN_SCRIPT_OR_FAST_NATIVE))
105#define FUN_SCRIPT(fun)      (FUN_INTERPRETED(fun) ? (fun)->u.i.script : NULL)
106#define FUN_NATIVE(fun)      (FUN_SLOW_NATIVE(fun) ? (fun)->u.n.native : NULL)
107#define FUN_FAST_NATIVE(fun) (((fun)->flags & JSFUN_FAST_NATIVE)              \
108                              ? (JSFastNative) (fun)->u.n.native              \
109                              : NULL)
110#define FUN_MINARGS(fun)     (((fun)->flags & JSFUN_FAST_NATIVE)              \
111                              ? 0                                             \
112                              : (fun)->nargs)
113#define FUN_CLASP(fun)       (JS_ASSERT(!FUN_INTERPRETED(fun)),               \
114                              JS_ASSERT(!((fun)->flags & JSFUN_TRACEABLE)),   \
115                              fun->u.n.u.clasp)
116#define FUN_TRCINFO(fun)     (JS_ASSERT(!FUN_INTERPRETED(fun)),               \
117                              JS_ASSERT((fun)->flags & JSFUN_TRACEABLE),      \
118                              fun->u.n.u.trcinfo)
119
120/*
121 * Traceable native.  This expands to a JSFunctionSpec initializer (like JS_FN
122 * in jsapi.h).  fastcall is a JSFastNative; trcinfo is a JSTraceableNative *.
123 */
124#ifdef JS_TRACER
125/* MSVC demands the intermediate (void *) cast here. */
126# define JS_TN(name,fastcall,nargs,flags,trcinfo)                             \
127    {name, (JSNative)(void *)(trcinfo), nargs,                                \
128     (flags) | JSFUN_FAST_NATIVE | JSFUN_STUB_GSOPS | JSFUN_TRACEABLE, 0}
129#else
130# define JS_TN(name,fastcall,nargs,flags,trcinfo)                             \
131    JS_FN(name, fastcall, nargs, flags)
132#endif
133
134extern JSClass js_ArgumentsClass;
135extern JS_FRIEND_DATA(JSClass) js_CallClass;
136
137/* JS_FRIEND_DATA so that VALUE_IS_FUNCTION is callable from the shell. */
138extern JS_FRIEND_DATA(JSClass) js_FunctionClass;
139
140#define HAS_FUNCTION_CLASS(obj) (STOBJ_GET_CLASS(obj) == &js_FunctionClass)
141
142/*
143 * NB: jsapi.h and jsobj.h must be included before any call to this macro.
144 */
145#define VALUE_IS_FUNCTION(cx, v)                                              \
146    (!JSVAL_IS_PRIMITIVE(v) && HAS_FUNCTION_CLASS(JSVAL_TO_OBJECT(v)))
147
148/*
149 * Macro to access the private slot of the function object after the slot is
150 * initialized.
151 */
152#define GET_FUNCTION_PRIVATE(cx, funobj)                                      \
153    (JS_ASSERT(HAS_FUNCTION_CLASS(funobj)),                                   \
154     (JSFunction *) OBJ_GET_PRIVATE(cx, funobj))
155
156extern JSObject *
157js_InitFunctionClass(JSContext *cx, JSObject *obj);
158
159extern JSObject *
160js_InitArgumentsClass(JSContext *cx, JSObject *obj);
161
162extern JSObject *
163js_InitCallClass(JSContext *cx, JSObject *obj);
164
165extern JSFunction *
166js_NewFunction(JSContext *cx, JSObject *funobj, JSNative native, uintN nargs,
167               uintN flags, JSObject *parent, JSAtom *atom);
168
169extern void
170js_TraceFunction(JSTracer *trc, JSFunction *fun);
171
172extern void
173js_FinalizeFunction(JSContext *cx, JSFunction *fun);
174
175extern JSObject *
176js_CloneFunctionObject(JSContext *cx, JSFunction *fun, JSObject *parent);
177
178extern JSBool
179js_LinkFunctionObject(JSContext *cx, JSFunction *fun, JSObject *object);
180
181extern JSFunction *
182js_DefineFunction(JSContext *cx, JSObject *obj, JSAtom *atom, JSNative native,
183                  uintN nargs, uintN flags);
184
185/*
186 * Flags for js_ValueToFunction and js_ReportIsNotFunction.  We depend on the
187 * fact that JSINVOKE_CONSTRUCT (aka JSFRAME_CONSTRUCTING) is 1, and test that
188 * with #if/#error in jsfun.c.
189 */
190#define JSV2F_CONSTRUCT         JSINVOKE_CONSTRUCT
191#define JSV2F_ITERATOR          JSINVOKE_ITERATOR
192#define JSV2F_SEARCH_STACK      0x10000
193
194extern JSFunction *
195js_ValueToFunction(JSContext *cx, jsval *vp, uintN flags);
196
197extern JSObject *
198js_ValueToFunctionObject(JSContext *cx, jsval *vp, uintN flags);
199
200extern JSObject *
201js_ValueToCallableObject(JSContext *cx, jsval *vp, uintN flags);
202
203extern void
204js_ReportIsNotFunction(JSContext *cx, jsval *vp, uintN flags);
205
206extern JSObject *
207js_GetCallObject(JSContext *cx, JSStackFrame *fp, JSObject *parent);
208
209extern JS_FRIEND_API(JSBool)
210js_PutCallObject(JSContext *cx, JSStackFrame *fp);
211
212extern JSBool
213js_GetCallArg(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
214
215extern JSBool
216js_GetCallVar(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
217
218extern JSBool
219js_GetArgsValue(JSContext *cx, JSStackFrame *fp, jsval *vp);
220
221extern JSBool
222js_GetArgsProperty(JSContext *cx, JSStackFrame *fp, jsid id, jsval *vp);
223
224extern JSObject *
225js_GetArgsObject(JSContext *cx, JSStackFrame *fp);
226
227extern JS_FRIEND_API(JSBool)
228js_PutArgsObject(JSContext *cx, JSStackFrame *fp);
229
230extern JSBool
231js_XDRFunction(JSXDRState *xdr, JSObject **objp);
232
233typedef enum JSLocalKind {
234    JSLOCAL_NONE,
235    JSLOCAL_ARG,
236    JSLOCAL_VAR,
237    JSLOCAL_CONST,
238    JSLOCAL_UPVAR
239} JSLocalKind;
240
241#define JS_UPVAR_LOCAL_NAME_START(fun)  ((fun)->nargs + (fun)->u.i.nvars)
242#define JS_GET_LOCAL_NAME_COUNT(fun)    (JS_UPVAR_LOCAL_NAME_START(fun) +     \
243                                         (fun)->u.i.nupvars)
244
245extern JSBool
246js_AddLocal(JSContext *cx, JSFunction *fun, JSAtom *atom, JSLocalKind kind);
247
248/*
249 * Look up an argument or variable name returning its kind when found or
250 * JSLOCAL_NONE when no such name exists. When indexp is not null and the name
251 * exists, *indexp will receive the index of the corresponding argument or
252 * variable.
253 */
254extern JSLocalKind
255js_LookupLocal(JSContext *cx, JSFunction *fun, JSAtom *atom, uintN *indexp);
256
257/*
258 * Functions to work with local names as an array of words.
259 *
260 * js_GetLocalNameArray returns the array, or null if we are out of memory.
261 * This function must not be called when JS_GET_LOCAL_NAME_COUNT(fun) is zero.
262 *
263 * The supplied pool is used to allocate the returned array, so the caller is
264 * obligated to mark and release to free it.
265 *
266 * The elements of the array with index less than fun->nargs correspond to the
267 * names of function formal parameters. An index >= fun->nargs addresses a var
268 * binding. Use JS_LOCAL_NAME_TO_ATOM to convert array's element to an atom
269 * pointer. This pointer can be null when the element is for a formal parameter
270 * corresponding to a destructuring pattern.
271 *
272 * If nameWord does not name a formal parameter, use JS_LOCAL_NAME_IS_CONST to
273 * check if nameWord corresponds to the const declaration.
274 */
275extern jsuword *
276js_GetLocalNameArray(JSContext *cx, JSFunction *fun, struct JSArenaPool *pool);
277
278#define JS_LOCAL_NAME_TO_ATOM(nameWord)                                       \
279    ((JSAtom *) ((nameWord) & ~(jsuword) 1))
280
281#define JS_LOCAL_NAME_IS_CONST(nameWord)                                      \
282    ((((nameWord) & (jsuword) 1)) != 0)
283
284extern void
285js_FreezeLocalNames(JSContext *cx, JSFunction *fun);
286
287extern JSBool
288js_fun_apply(JSContext *cx, uintN argc, jsval *vp);
289
290extern JSBool
291js_fun_call(JSContext *cx, uintN argc, jsval *vp);
292
293
294JS_END_EXTERN_C
295
296#endif /* jsfun_h___ */