Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / shell / SystemClass.cpp
blob9eebc1142829977a80615cc8ee6e34ef8077f84d
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 ***** */
41 #include "avmshell.h"
43 namespace avmshell
45 SystemClass::SystemClass(VTable *cvtable)
46 : ClassClosure(cvtable)
48 ShellCore* core = (ShellCore*)this->core();
49 if (core->systemClass == NULL) {
50 core->systemClass = this;
53 createVanillaPrototype();
55 // initialTime: support for getTimer
56 // todo note this is currently routed to the performance counter
57 // for benchmark purposes.
58 #ifdef PERFORMANCE_GETTIMER
59 initialTime = VMPI_getPerformanceCounter();
60 #else
61 initialTime = VMPI_getTime();
62 #endif // PERFORMANCE_GETTIMER
66 SystemClass::~SystemClass()
68 initialTime = 0;
71 void SystemClass::exit(int status)
73 Platform::GetInstance()->exit(status);
76 int SystemClass::exec(Stringp command)
78 if (!command) {
79 toplevel()->throwArgumentError(kNullArgumentError, "command");
81 #ifdef UNDER_CE
82 AvmAssert(0);
83 return 0;
84 #else
85 StUTF8String commandUTF8(command);
86 return system(commandUTF8.c_str());
87 #endif
90 Stringp SystemClass::getAvmplusVersion()
92 return core()->newConstantStringLatin1(AVMPLUS_VERSION_USER " " AVMPLUS_BUILD_CODE);
95 Stringp SystemClass::getFeatures()
97 return core()->newConstantStringLatin1(avmfeatures);
100 Stringp SystemClass::getRunmode()
102 ShellCore* core = (ShellCore*)this->core();
103 if (core->config.runmode == RM_mixed)
104 return core->newConstantStringLatin1("mixed");
105 if (core->config.runmode == RM_jit_all)
107 if (core->config.jitordie)
108 return core->newConstantStringLatin1("jitordie");
109 return core->newConstantStringLatin1("jit");
111 if (core->config.runmode == RM_interp_all)
112 return core->newConstantStringLatin1("interp");
113 return core->newConstantStringLatin1("unknown");
116 void SystemClass::write(Stringp s)
118 if (!s)
119 toplevel()->throwArgumentError(kNullArgumentError, "string");
120 core()->console << s;
123 void SystemClass::trace(ArrayObject* a)
125 if (!a)
126 toplevel()->throwArgumentError(kNullArgumentError, "array");
127 AvmCore* core = this->core();
128 PrintWriter& console = core->console;
129 for (int i=0, n = a->getLength(); i < n; i++)
131 if (i > 0)
132 console << ' ';
133 StringIndexer s(core->string(a->getUintProperty(i)));
134 for (int j = 0; j < s->length(); j++)
136 wchar c = s[j];
137 // '\r' gets converted into '\n'
138 // '\n' is left alone
139 // '\r\n' is left alone
140 if (c == '\r')
142 if (((j+1) < s->length()) && s[j+1] == '\n')
144 console << '\r';
145 j++;
148 console << '\n';
150 else
152 console << c;
156 console << '\n';
159 void SystemClass::debugger()
161 #ifdef DEBUGGER
162 if (core()->debugger())
163 core()->debugger()->enterDebugger();
164 #endif
167 bool SystemClass::isDebugger()
169 #ifdef DEBUGGER
170 return core()->debugger() != NULL;
171 #else
172 return false;
173 #endif
176 unsigned SystemClass::getTimer()
178 #ifdef PERFORMANCE_GETTIMER
179 double time = ((double) (VMPI_getPerformanceCounter() - initialTime) * 1000.0 /
180 (double)VMPI_getPerformanceFrequency());
181 return (uint32_t)time;
182 #else
183 return (uint32_t)(VMPI_getTime() - initialTime);
184 #endif /* PERFORMANCE_GETTIMER */
188 int SystemClass::user_argc;
189 char **SystemClass::user_argv;
191 ArrayObject * SystemClass::getArgv()
193 // get VTable for avmplus.System
194 Toplevel *toplevel = this->toplevel();
195 AvmCore *core = this->core();
197 ArrayObject *array = toplevel->arrayClass()->newArray();
198 for(int i=0; i<user_argc;i++)
199 array->setUintProperty(i, core->newStringUTF8(user_argv[i])->atom());
201 return array;
204 Stringp SystemClass::readLine()
206 AvmCore* core = this->core();
207 Stringp s = core->kEmptyString;
208 wchar wc[64];
209 int i=0;
210 for (int c = getchar(); c != '\n' && c != EOF; c = getchar())
212 wc[i++] = (wchar)c;
213 if (i == 63) {
214 wc[i] = 0;
215 s = s->append16(wc);
216 i = 0;
219 if (i > 0) {
220 wc[i] = 0;
221 s = s->append16(wc);
223 return s;
226 double SystemClass::get_totalMemory()
228 MMgc::GCHeap* gcheap = MMgc::GCHeap::GetGCHeap();
229 return double(gcheap->GetTotalHeapSize() * MMgc::GCHeap::kBlockSize);
232 double SystemClass::get_freeMemory()
234 MMgc::GCHeap* gcheap = MMgc::GCHeap::GetGCHeap();
235 return double(gcheap->GetFreeHeapSize() * MMgc::GCHeap::kBlockSize);
238 double SystemClass::get_privateMemory()
240 return double(VMPI_getPrivateResidentPageCount() * VMPI_getVMPageSize());
243 int32_t SystemClass::get_swfVersion()
245 ShellCore* core = (ShellCore*)this->core();
246 BugCompatibility::Version v = core->getDefaultBugCompatibilityVersion();
247 AvmAssert(v >= 0 && v < BugCompatibility::VersionCount);
248 return BugCompatibility::kNames[v];
251 int32_t SystemClass::get_apiVersion()
253 ShellCore* core = (ShellCore*)this->core();
254 return core->defaultAPIVersion;
257 void SystemClass::forceFullCollection()
259 core()->GetGC()->Collect();
262 void SystemClass::queueCollection()
264 core()->GetGC()->QueueCollection();
267 bool SystemClass::isGlobal(Atom o)
269 return AvmCore::isObject(o) ? AvmCore::atomToScriptObject(o)->isGlobalObject() : false;
272 void SystemClass::disposeXML(XMLObject *xmlObject)
274 if(xmlObject)
275 xmlObject->dispose();
278 /*static*/ void FASTCALL CheckBaseClass::preCreateInstanceCheck(ClassClosure* cls)
280 Multiname qname(cls->traits()->ns(), cls->traits()->name());
281 // Deliberately throw a weird, non-sequitur error here so that we
282 // can distinguish this from construct="none" in the acceptance tests.
283 cls->toplevel()->argumentErrorClass()->throwError(kNotImplementedError, cls->core()->toErrorString(&qname));