Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / core / ScriptObject.h
blob5bc9120f20ca6432d3a94975f267fe70dff2b768
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 __avmplus_ScriptObject__
41 #define __avmplus_ScriptObject__
44 namespace avmplus
46 #define avmplus_ScriptObject_isExactInterlock 1
48 /**
49 * one actionscript object. might or might not be a function.
50 * Base class for all objects visible to script code.
52 class ScriptObject : public AvmPlusScriptableObject
54 protected:
55 ScriptObject(VTable* vtable, ScriptObject* delegate);
56 ScriptObject(VTable* vtable, ScriptObject* delegate, int htCapacity);
58 public:
59 REALLY_INLINE static ScriptObject* create(MMgc::GC* gc, VTable* vtable, ScriptObject* delegate);
60 REALLY_INLINE static ScriptObject* create(MMgc::GC* gc, VTable* vtable, ScriptObject* delegate, int htCapacity);
62 ~ScriptObject();
64 virtual bool gcTrace(MMgc::GC* gc, size_t cursor);
66 ScriptObject* getDelegate() const;
67 void setDelegate(ScriptObject *d);
68 Atom atom() const;
69 virtual Atom toAtom() const;
70 Traits* traits() const;
71 AvmCore* core() const;
72 MMgc::GC* gc() const;
73 Toplevel* toplevel() const;
74 InlineHashtable* getTable() const;
75 InlineHashtable* getTableNoInit() const;
77 Atom getSlotAtom(uint32_t slot);
79 // like getSlotAtom, but assume the resulting Atom is a ScriptObject...
80 // if it is not, return NULL. useful for callers that expect only
81 // a ScriptObject and reject other types (eg callproperty). If the
82 // slot in question is usually a ScriptObject, it's substantially faster
83 // to call this (vs getSlotAtom).
84 ScriptObject* getSlotObject(uint32_t slot);
86 // NOTE, this now does the equivalent of Toplevel::coerce() internally;
87 // it is not necessary to call coerce() prior to calling this!
88 void coerceAndSetSlotAtom(uint32_t slot, Atom atom);
90 virtual Atom getDescendants(const Multiname* name) const;
92 // argv[0] = receiver
93 virtual Atom callProperty(const Multiname* name, int argc, Atom* argv);
94 virtual Atom constructProperty(const Multiname* name, int argc, Atom* argv);
96 // common set/set/has/delete/etc virtual methods renamed to explicitly name the expected arg types,
97 // to avoid potentially hidden virtual functions
98 virtual Atom getAtomProperty(Atom name) const;
99 virtual void setAtomProperty(Atom name, Atom value);
100 virtual bool deleteAtomProperty(Atom name);
101 virtual bool hasAtomProperty(Atom name) const;
102 virtual bool getAtomPropertyIsEnumerable(Atom name) const;
103 virtual void setAtomPropertyIsEnumerable(Atom name, bool enumerable);
105 virtual Atom getMultinameProperty(const Multiname* name) const;
106 virtual void setMultinameProperty(const Multiname* name, Atom value);
107 virtual bool deleteMultinameProperty(const Multiname* name);
108 virtual bool hasMultinameProperty(const Multiname* name) const;
110 virtual Atom getUintProperty(uint32_t i) const;
111 virtual void setUintProperty(uint32_t i, Atom value);
112 virtual bool delUintProperty(uint32_t i);
113 virtual bool hasUintProperty(uint32_t i) const;
115 // convenience wrappers for passing Stringp instead of Atom
116 // inline, not virtual (should never need overriding)
117 Atom getStringProperty(Stringp name) const;
118 Atom getStringPropertyFromProtoChain(Stringp name, ScriptObject* protochain, Traits *origObjTraits) const;
119 void setStringProperty(Stringp name, Atom value);
120 bool deleteStringProperty(Stringp name);
121 bool hasStringProperty(Stringp name) const;
122 bool getStringPropertyIsEnumerable(Stringp name) const;
123 void setStringPropertyIsEnumerable(Stringp name, bool enumerable);
125 virtual Atom defaultValue(); // ECMA [[DefaultValue]]
126 virtual Stringp toString();
128 // argv[0] = receiver
129 virtual Atom call(int argc, Atom* argv);
132 * argv[0] = receiver(ignored)
133 * arg1 = argv[1]
134 * argN = argv[argc]
136 * called as constructor, as in new C(). for user classes this
137 * invokes the implicit constructor followed by the user's constructor
138 * if any.
140 * NOTE: The contents of the given argument list can be modified during
141 * invocation of the constructor without further warning.
142 * Do not reuse an argument list AS-IS for multiple constructor calls,
143 * unless you make sure to reinitialize the contents of the argument list after each call.
145 * NOTE: subclasses should never need to declare this method in their class;
146 * an override declaration will be provided for them iff
147 * customconstruct="some-value" is specified in the AS3 file.
149 virtual Atom construct(int argc, Atom* argv);
151 // TODO make this const
152 virtual Atom nextName(int index);
154 // TODO make this const
155 virtual Atom nextValue(int index);
157 // TODO make this const
158 virtual int nextNameIndex(int index);
160 virtual Atom applyTypeArgs(int argc, Atom* argv);
162 // This is a fast way to implement "istype(MethodClosure)"; it returns non-null if
163 // this is an MethodClosure (or subclass thereof)
164 virtual MethodClosure* toMethodClosure() const;
165 bool isMethodClosure() const;
167 // The maximum integer key we can use with our ScriptObject
168 // HashTable must fit within 28 bits. Any integer larger
169 // than 28 bits will use a string key.
170 static const uint32_t MAX_INTEGER_MASK = 0xF0000000;
172 // return true iff the object is a toplevel script init object.
173 bool isGlobalObject() const;
175 virtual Stringp implToString() const;
177 // this really shouldn't exist here, but AIR currently plays some games with subclassing
178 // "Function" that use C++ classes that don't descend from FunctionObject. Easier to fix
179 // by rooting this method here than by fixing AIR at this time.
180 virtual CodeContext* getFunctionCodeContext() const;
182 // This is a fast way to implement "istype(ARRAY)"; it returns non-null if
183 // this is an ArrayObject (or subclass thereof)
184 virtual ArrayObject* toArrayObject();
186 // This gets the property named "length" (if any) on the object, coerces it to a uint,
187 // and returns the value. (Since uint(undefined) == 0, undefined length is zero.)
188 virtual uint32_t getLengthProperty();
190 // This sets the property named "length" to the given uint value. If this
191 // is a nondynamic object, or there is a read-only length property, or uint
192 // can't be coerced to the type of "length", an exception will be thrown.
193 virtual void setLengthProperty(uint32_t newLen);
195 virtual ClassClosure* toClassClosure();
197 #ifdef AVMPLUS_VERBOSE
198 public:
199 virtual PrintWriter& print(PrintWriter& prw) const;
200 #endif
202 #ifdef DEBUGGER
203 public:
204 virtual uint64_t bytesUsed() const;
205 #endif
206 #if defined(DEBUGGER) || defined(VMCFG_AOT)
207 public:
208 virtual MethodEnv* getCallMethodEnv();
209 #endif
211 protected:
213 Atom getAtomPropertyFromProtoChain(Atom name, ScriptObject* protochain, Traits *origObjTraits) const;
214 void throwWriteSealedError(Atom name);
215 void throwWriteSealedError(const Multiname& name);
217 public:
218 void throwCantInstantiateError();
220 private:
221 void initHashtable(int capacity = InlineHashtable::kDefaultCapacity);
223 // ------------------------ DATA SECTION BEGIN
224 public: VTable* const vtable;
225 private: DRCWB(ScriptObject*) delegate; // __proto__ in AS2, archetype in semantics
226 // ------------------------ DATA SECTION END
230 #endif /* __avmplus_ScriptObject__ */