Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / shell / ShellCore.h
blob553fdcf97478161151ab2074e6b860a600691981
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * 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.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef __avmshell_core__
41 #define __avmshell_core__
43 using namespace avmplus;
45 namespace avmshell
47 /**
48 * Settings for ShellCore. The command line parser uses this, but the initial state
49 * is set in ShellCore.cpp, and it's propagated throughout various parts of the
50 * system. Partly duplicates the functionality in AvmCore::config, and some settings
51 * are copied from ShellCoreSettings into that structure when a new core is created.
53 * We try to avoid conditional compilation here, as there's only ever one of these
54 * structures floating around and the savings would be minuscule.
56 * FIXME: the naming of many fields could be much better, and they should be documented.
58 class ShellCoreSettings
60 public:
61 ShellCoreSettings();
63 char** arguments; // non-terminated array of argument values, never NULL
64 int numargs; // number of entries in 'arguments'
65 bool nodebugger;
66 int astrace_console;
67 uint32_t do_verbose; // copy to config
68 bool enter_debugger_on_launch;
69 bool interrupts; // copy to config
70 bool verifyall; // copy to config
71 bool verifyonly; // copy to config
72 bool greedy; // copy to each GC
73 bool nogc; // copy to each GC
74 bool incremental; // copy to each GC
75 bool exactgc; // copy to each GC
76 int32_t markstackAllowance; // copy to each GC;
77 bool fixedcheck; // copy to each GC
78 int gcthreshold; // copy to each GC
79 int langID; // copy to ShellCore?
80 bool jitordie; // copy to config
81 bool do_testSWFHasAS3;
82 Runmode runmode; // copy to config
83 #ifdef VMCFG_NANOJIT
84 nanojit::Config njconfig; // copy to config
85 #endif
86 AvmCore::CacheSizes cacheSizes; // Default to unlimited
87 const char* st_component;
88 const char* st_category;
89 const char* st_name;
90 ApiVersionSeries apiVersionSeries;
91 ApiVersion apiVersion;
92 BugCompatibility::Version swfVersion;
94 MMgc::GC::GCMode gcMode()
96 if (nogc) return MMgc::GC::kDisableGC;
97 else if (greedy) return MMgc::GC::kGreedyGC;
98 else if (incremental) return MMgc::GC::kIncrementalGC;
99 else return MMgc::GC::kNonincrementalGC;
103 class ShellCodeContext : public CodeContext
105 public:
106 inline ShellCodeContext(DomainEnv* env, const BugCompatibility* bugCompatibility)
107 : CodeContext(env, bugCompatibility) { }
111 * ShellCore provides typical housekeeping tasks around an AvmCore, lightweight
112 * embedders may prefer to use this rather than to subclass AvmCore themselves
113 * and do all the housekeeping that results. The shell uses this; see the
114 * shell code for typical usage patterns.
116 class ShellCore : public AvmCore
118 friend class SystemClass;
119 friend class avmplus::DomainObject;
120 public:
122 * Create a new core with the given GC (one gc per core).
124 * Requires: MMGC_ENTER and MMGC_GCENTER(gc) on the stack.
126 ShellCore(MMgc::GC *gc, ApiVersionSeries apiVersionSeries);
129 * Initialize the new core from the settings. This creates a top-level
130 * environment and performs other housekeeping.
132 * Requires: MMGC_ENTER and MMGC_GCENTER(gc) on the stack.
134 bool setup(const ShellCoreSettings& settings);
137 * Load the contents from the file and run them in the context of this core's
138 * top level. The file may contain ABC bytecode, a SWF containing a single DoABC
139 * tag, or (if the run-time compiler is present) ActionScript source code.
141 * Requires: MMGC_ENTER and MMGC_GCENTER(gc) on the stack.
143 int evaluateFile(ShellCoreSettings& settings, const char* filename);
145 #ifdef VMCFG_EVAL
147 * Load ActionScript source code from the string and run it in the
148 * context of this core's top level. Note the string must have been
149 * created in the context of the GC in this ShellCore instance.
151 * Requires: MMGC_ENTER and MMGC_GCENTER(gc) on the stack.
153 void evaluateString(String* input, bool record_time=false);
154 #endif
156 #ifdef AVMSHELL_PROJECTOR_SUPPORT
157 int executeProjector(char *executablePath);
158 #endif
160 #ifdef VMCFG_SELFTEST
161 void executeSelftest(ShellCoreSettings& settings);
162 #endif
164 PoolObject* getShellPool() { return shellPool; }
166 SystemClass* getSystemClass() { return systemClass; }
168 #ifdef AVMSHELL_PROJECTOR_SUPPORT
169 static bool isValidProjectorFile(const char* filename);
170 #endif
172 /*virtual*/ inline ApiVersion getDefaultAPI() { return this->defaultAPIVersion; }
174 inline BugCompatibility::Version getDefaultBugCompatibilityVersion() const { return defaultBugCompatibilityVersion; }
176 protected:
177 virtual void setStackLimit() = 0;
179 virtual Toplevel* createToplevel(AbcEnv* abcEnv);
180 #ifdef DEBUGGER
181 virtual avmplus::Debugger* createDebugger(int tracelevel)
183 AvmAssert(allowDebugger >= 0);
184 return allowDebugger ? DebugCLI::create(GetGC(), this, (avmplus::Debugger::TraceLevel)tracelevel) : NULL;
187 #endif
188 #ifdef VMCFG_EVAL
189 virtual String* readFileForEval(String* referencing_filename, String* filename);
190 #endif
192 private:
193 static void interruptTimerCallback(void* data);
195 ShellToplevel* createShellToplevel();
197 void interrupt(Toplevel*, InterruptReason);
198 void stackOverflow(Toplevel *toplevel);
199 void setEnv(Toplevel *toplevel, int argc, char *argv[]);
200 void initShellPool();
201 int handleArbitraryExecutableContent(ShellCoreSettings& settings, ScriptBuffer& code, const char * filename);
202 #ifdef VMCFG_EVAL
203 String* decodeBytesAsUTF16String(uint8_t* bytes, uint32_t nbytes, bool terminate=false);
204 #endif // VMCFG_EVAL
205 #ifdef DEBUGGER
206 DebugCLI* debugCLI() { return (DebugCLI*)debugger(); }
207 #endif
209 #ifdef AVMPLUS_VERBOSE
210 virtual const char* identifyDomain(Domain* domain);
211 #endif
212 SystemClass* systemClass;
213 PoolObject* shellPool;
214 OutputStream *consoleOutputStream;
215 bool gracePeriod;
216 bool inStackOverflow;
217 int allowDebugger;
218 ShellToplevel* shell_toplevel;
219 DomainEnv* shell_domainEnv;
220 Domain* shell_domain;
221 // Note that this has been renamed to emphasize the fact that it is
222 // the CodeContext/DomainEnv that user code will run in (as opposed
223 // to the Shell's builtin classes, eg System, File, Domain).
224 ShellCodeContext* user_codeContext;
225 ApiVersion defaultAPIVersion;
226 BugCompatibility::Version defaultBugCompatibilityVersion;
229 class GC_CPP_EXACT(ShellToplevel, avmplus::Toplevel)
231 friend class ShellCore;
233 private:
234 ShellToplevel(AbcEnv* abcEnv);
236 public:
237 REALLY_INLINE static ShellToplevel* create(MMgc::GC* gc, AbcEnv* abcEnv)
239 return new (gc, MMgc::kExact) ShellToplevel(abcEnv);
242 ShellCore* core() const {
243 return (ShellCore*)Toplevel::core();
246 private:
247 GC_DATA_BEGIN(ShellToplevel)
249 DWB(shell_toplevelClassManifest*) GC_POINTER(shellClasses);
251 GC_DATA_END(ShellToplevel)
255 #endif /* __avmshell_core__ */