merge from argo
[tamarin-stm.git] / shell / SystemClass.cpp
blobc0398c3390e7a2dea17cd83e8f068153bad0895e
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 void SystemClass::write(Stringp s)
97 if (!s)
98 toplevel()->throwArgumentError(kNullArgumentError, "string");
99 core()->console << s;
102 void SystemClass::trace(ArrayObject* a)
104 if (!a)
105 toplevel()->throwArgumentError(kNullArgumentError, "array");
106 AvmCore* core = this->core();
107 PrintWriter& console = core->console;
108 for (int i=0, n = a->getLength(); i < n; i++)
110 if (i > 0)
111 console << ' ';
112 StringIndexer s(core->string(a->getUintProperty(i)));
113 for (int j = 0; j < s->length(); j++)
115 wchar c = s[j];
116 // '\r' gets converted into '\n'
117 // '\n' is left alone
118 // '\r\n' is left alone
119 if (c == '\r')
121 if (((j+1) < s->length()) && s[j+1] == '\n')
123 console << '\r';
124 j++;
127 console << '\n';
129 else
131 console << c;
135 console << '\n';
138 void SystemClass::debugger()
140 #ifdef DEBUGGER
141 if (core()->debugger())
142 core()->debugger()->enterDebugger();
143 #endif
146 bool SystemClass::isDebugger()
148 #ifdef DEBUGGER
149 return core()->debugger() != NULL;
150 #else
151 return false;
152 #endif
155 unsigned SystemClass::getTimer()
157 #ifdef PERFORMANCE_GETTIMER
158 double time = ((double) (VMPI_getPerformanceCounter() - initialTime) * 1000.0 /
159 (double)VMPI_getPerformanceFrequency());
160 return (uint32_t)time;
161 #else
162 return (uint32_t)(VMPI_getTime() - initialTime);
163 #endif /* PERFORMANCE_GETTIMER */
167 int SystemClass::user_argc;
168 char **SystemClass::user_argv;
170 ArrayObject * SystemClass::getArgv()
172 // get VTable for avmplus.System
173 Toplevel *toplevel = this->toplevel();
174 AvmCore *core = this->core();
176 ArrayObject *array = toplevel->arrayClass->newArray();
177 for(int i=0; i<user_argc;i++)
178 array->setUintProperty(i, core->newStringUTF8(user_argv[i])->atom());
180 return array;
183 Stringp SystemClass::readLine()
185 AvmCore* core = this->core();
186 Stringp s = core->kEmptyString;
187 wchar wc[64];
188 int i=0;
189 for (int c = getchar(); c != '\n' && c != EOF; c = getchar())
191 wc[i++] = (wchar)c;
192 if (i == 63) {
193 wc[i] = 0;
194 s = s->append16(wc);
195 i = 0;
198 if (i > 0) {
199 wc[i] = 0;
200 s = s->append16(wc);
202 return s;
205 double SystemClass::get_totalMemory()
207 MMgc::GCHeap* gcheap = MMgc::GCHeap::GetGCHeap();
208 return double(gcheap->GetTotalHeapSize() * MMgc::GCHeap::kBlockSize);
211 double SystemClass::get_freeMemory()
213 MMgc::GCHeap* gcheap = MMgc::GCHeap::GetGCHeap();
214 return double(gcheap->GetFreeHeapSize() * MMgc::GCHeap::kBlockSize);
217 double SystemClass::get_privateMemory()
219 return double(VMPI_getPrivateResidentPageCount() * VMPI_getVMPageSize());
222 void SystemClass::forceFullCollection()
224 core()->GetGC()->Collect();
227 void SystemClass::queueCollection()
229 core()->GetGC()->QueueCollection();
232 bool SystemClass::isGlobal(Atom o)
234 return AvmCore::isObject(o) ? AvmCore::atomToScriptObject(o)->isGlobalObject() : false;