1 /* Copyright (c) 2003, David Leonard. All rights reserved. */
2 
3 #ifndef _SEE_h_interpreter_
4 #define _SEE_h_interpreter_
5 
6 #include <see/type.h>
7 
8 struct SEE_object;
9 struct SEE_try_context;
10 struct SEE_throw_location;
11 struct SEE_context;
12 struct SEE_scope;
13 struct SEE_traceback;
14 struct SEE_regex_engine;
15 struct SEE_interpreter_state;
16 
17 enum SEE_trace_event {
18 	SEE_TRACE_CALL,
19 	SEE_TRACE_RETURN,
20 	SEE_TRACE_STATEMENT,
21 	SEE_TRACE_THROW
22 };
23 
24 /*
25  * This is the place to stick things that, once created
26  * for an interpreter instance, should be kept around
27  * for easy access. (And cannot be replaced by scripts)
28  */
29 
30 struct SEE_interpreter {
31 	void *	host_data;		/* reserved for host app's use */
32 	int	compatibility;		/* compatibility flags (read-only?) */
33 
34 	/* Built-in objects newly created for each interpreter instance */
35 	struct SEE_object *Global;
36 	struct SEE_object *Object;
37 	struct SEE_object *Object_prototype;
38 	struct SEE_object *Error;
39 	struct SEE_object *EvalError;
40 	struct SEE_object *RangeError;
41 	struct SEE_object *ReferenceError;
42 	struct SEE_object *SyntaxError;
43 	struct SEE_object *TypeError;
44 	struct SEE_object *URIError;
45 	struct SEE_object *String;
46 	struct SEE_object *String_prototype;
47 	struct SEE_object *Function;
48 	struct SEE_object *Function_prototype;
49 	struct SEE_object *Array;
50 	struct SEE_object *Array_prototype;
51 	struct SEE_object *Number;
52 	struct SEE_object *Number_prototype;
53 	struct SEE_object *Boolean;
54 	struct SEE_object *Boolean_prototype;
55 	struct SEE_object *Math;
56 	struct SEE_object *RegExp;
57 	struct SEE_object *RegExp_prototype;
58 	struct SEE_object *Date;
59 	struct SEE_object *Date_prototype;
60 	struct SEE_object *Global_eval;
61 	struct SEE_scope  *Global_scope;
62 
63 	/* Current 'try-catch' context */
64 	volatile struct SEE_try_context * try_context;
65 	struct SEE_throw_location * try_location;
66 
67 	struct SEE_traceback *traceback;/* call chain for traceback */
68 	void **module_private;		/* private pointers for each module */
69 	void *intern_tab;		/* interned string table */
70 	unsigned int random_seed;	/* used by Math.random() */
71 	const char *locale;		/* current locale (may be NULL) */
72 	int recursion_limit;		/* -1 means don't care */
73 	void *sec_domain;		/* security domain for new code */
74 
75 	/* Trace hook, called by interpreter at each step if not NULL */
76 	void (*trace)(struct SEE_interpreter *, struct SEE_throw_location *,
77 			struct SEE_context *, enum SEE_trace_event);
78 
79 	/* Regex implementation used by Regex object (experimental) */
80 	const struct SEE_regex_engine *regex_engine;
81 };
82 
83 /* Compatibility flags */
84 #define SEE_COMPAT_STRICT       0x0000  /* Strict ECMA-262 3rd ed. */
85 #define SEE_COMPAT_262_3B       (1<< 1) /* ECMA-262 3rd ed. Annex B */
86 #define SEE_COMPAT_UTF_UNSAFE   (1<< 2) /* accept 'valid but insecure' UTF */
87 #define SEE_COMPAT_SGMLCOM      (1<< 3) /* treat '<!--' as a '//' comment */
88 /* SEE_COMPAT_EXT1 deprecated; use SEE_COMPAT_JS11 instead */
89 #define SEE_COMPAT_SEE          (1<< 4) /* SEE-specific extensions */
90 #define SEE_COMPAT_JS_MASK      (7<< 5) /* mask for JS compat values  */
91 #define SEE_COMPAT_JS_NONE         0      /* no JS compat */
92 #define SEE_COMPAT_JS11           (1<< 5) /* JavaScript1.1 */
93 #define SEE_COMPAT_JS12           (2<< 5) /* JavaScript1.2 */
94 #define SEE_COMPAT_JS13           (3<< 5) /* JavaScript1.3 */
95 #define SEE_COMPAT_JS14           (4<< 5) /* JavaScript1.4 */
96 #define SEE_COMPAT_JS15           (5<< 5) /* JavaScript1.5 */
97 #define SEE_COMPAT_ERRATA       (1<< 8) /* ECMA-262 3rd ed errata */
98 
99 /* This macro is used to see if an ECMA deviation is required */
100 #define SEE_COMPAT_JS(i,cmp,jsnn) \
101 	((SEE_GET_JS_COMPAT(i) != SEE_COMPAT_JS_NONE) && \
102 	 (SEE_GET_JS_COMPAT(i) cmp SEE_COMPAT_##jsnn))
103 #define SEE_GET_JS_COMPAT(i) ((i)->compatibility & SEE_COMPAT_JS_MASK)
104 #define SEE_SET_JS_COMPAT(i,c) \
105 	(i)->compatibility = ((i)->compatibility & ~SEE_COMPAT_JS_MASK)|(c)
106 
107 /* Call traceback */
108 struct SEE_traceback {
109 	struct SEE_throw_location *call_location;
110 	struct SEE_object *callee;
111 	int call_type;		/* SEE_CALLTYPE_* */
112 	struct SEE_traceback *prev;
113 };
114 #define SEE_CALLTYPE_CALL	1
115 #define SEE_CALLTYPE_CONSTRUCT	2
116 #define SEE_CALLTYPE_THROW	3
117 
118 /* Initialises an interpreter with default behaviour */
119 void SEE_interpreter_init(struct SEE_interpreter *i);
120 
121 /* Initialises an interpreter with specific behaviour */
122 void SEE_interpreter_init_compat(struct SEE_interpreter *i, int compat_flags);
123 
124 /* Saves interpreter state for concurrent access */
125 struct SEE_interpreter_state *SEE_interpreter_save_state(
126 	struct SEE_interpreter *i);
127 /* Restores interpreter state; destroys state */
128 void SEE_interpreter_restore_state(struct SEE_interpreter *i,
129 	struct SEE_interpreter_state *state);
130 
131 #endif /* _SEE_h_interpreter_ */
132