forked from adobe-flash/avmplus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDomainClass.cpp
180 lines (146 loc) · 6.42 KB
/
DomainClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "avmshell.h"
#ifndef AVMSHELL_BUILD
#error "This file is only for use with avmshell"
#endif
namespace avmplus
{
using namespace avmshell;
DomainObject::DomainObject(VTable *vtable, ScriptObject *delegate)
: ScriptObject(vtable, delegate)
, domainEnv(delegate->core()->codeContext()->domainEnv())
, domainToplevel(delegate->toplevel())
{
}
void DomainObject::init(DomainObject* parentDomain)
{
ShellCore* core = (ShellCore*) this->core();
DomainEnv* baseDomainEnv = parentDomain ?
(DomainEnv*)parentDomain->domainEnv :
(DomainEnv*)NULL;
Domain* baseDomain = baseDomainEnv ?
baseDomainEnv->domain() :
NULL;
domainToplevel = parentDomain ?
(Toplevel*)parentDomain->domainToplevel :
core->createShellToplevel();
Domain* domain = Domain::newDomain(core, baseDomain);
domainEnv = DomainEnv::newDomainEnv(core, domain, parentDomain ? (DomainEnv*)parentDomain->domainEnv : (DomainEnv*)NULL);
}
Atom DomainObject::loadBytes(ByteArrayObject* b, uint32_t swfVersion)
{
AvmCore* core = this->core();
if (!b)
toplevel()->throwTypeError(kNullArgumentError, core->toErrorString("bytes"));
// parse new bytecode
size_t len = b->get_length();
ScriptBuffer code = core->newScriptBuffer(len);
VMPI_memcpy(code.getBuffer(), &b->GetByteArray()[0], len);
Toplevel* toplevel = domainToplevel;
ApiVersion apiVersion = core->getApiVersionFromCallStack();
// parse constants and attributes.
PoolObject* pool = core->parseActionBlock(code,
/*start*/0,
toplevel,
domainEnv->domain(),
/*ninit*/NULL,
apiVersion);
// by default, use the same bugCompatibility as the builtins use
const BugCompatibility* bugCompatibility = toplevel->abcEnv()->codeContext()->bugCompatibility();
if (swfVersion != 0)
{
// ...unless specified otherwise.
for (int j = 0; j < BugCompatibility::VersionCount; ++j)
{
if (BugCompatibility::kNames[j] == swfVersion)
{
bugCompatibility = core->createBugCompatibility((BugCompatibility::Version)j);
goto done;
}
}
// if we get here, didn't find a valid name
toplevel->throwTypeError(kInvalidArgumentError, core->toErrorString("swfVersion"));
}
done:
ShellCodeContext* codeContext = new (core->GetGC()) ShellCodeContext(domainEnv, bugCompatibility);
return core->handleActionPool(pool, toplevel, codeContext);
}
ScriptObject* DomainObject::finddef(const Multiname& multiname,
DomainEnv* domainEnv)
{
Toplevel* toplevel = this->toplevel();
ScriptEnv* script = core()->domainMgr()->findScriptEnvInDomainEnvByMultiname(domainEnv, multiname);
if (script == (ScriptEnv*)BIND_AMBIGUOUS)
toplevel->throwReferenceError(kAmbiguousBindingError, multiname);
if (script == (ScriptEnv*)BIND_NONE)
toplevel->throwReferenceError(kUndefinedVarError, multiname);
if (script->global == NULL)
{
script->initGlobal();
script->coerceEnter(script->global->atom());
}
return script->global;
}
ClassClosure* DomainObject::getClass(Stringp name)
{
Toplevel* toplevel = this->toplevel();
AvmCore* core = toplevel->core();
if (name == NULL) {
toplevel->throwArgumentError(kNullArgumentError, core->toErrorString("name"));
}
// Search for a dot from the end.
int dot = name->lastIndexOf(core->cachedChars[(int)'.']);
// If there is a '.', this is a fully-qualified
// class name in a package. Must turn it into
// a namespace-qualified multiname.
Namespace* ns;
Stringp className;
if (dot >= 0) {
Stringp uri = core->internString(name->substring(0, dot));
ns = core->internNamespace(core->newNamespace(uri, Namespace::NS_Public, core->getApiVersionFromCallStack()));
className = core->internString(name->substring(dot+1, name->length()));
} else {
ns = core->findPublicNamespace();
className = core->internString(name);
}
Multiname multiname(ns, className);
ScriptObject *container = finddef(multiname, domainEnv);
if (!container) {
toplevel->throwTypeError(kClassNotFoundError, core->toErrorString(&multiname));
}
Atom atom = toplevel->getproperty(container->atom(),
&multiname,
container->vtable);
// Note: this used to throw "kClassNotFoundError" if the class wasn't
// found; now it throws "kCheckTypeFailedError" (by way of coerceToType).
// This probably doesn't matter since this is a shell-only class, and it
// gives us a testcase for coerceToType.
return toplevel->builtinClasses()->get_ClassClass()->coerceToType(atom);
}
DomainClass::DomainClass(VTable *cvtable)
: ClassClosure(cvtable)
{
createVanillaPrototype();
}
DomainObject* DomainClass::get_currentDomain()
{
return (DomainObject*) newInstance();
}
int DomainClass::get_MIN_DOMAIN_MEMORY_LENGTH()
{
return DomainEnv::GLOBAL_MEMORY_MIN_SIZE;
}
ByteArrayObject* DomainObject::get_domainMemory() const
{
return (ByteArrayObject*)domainEnv->get_globalMemory();
}
void DomainObject::set_domainMemory(ByteArrayObject* mem)
{
if(!domainEnv->set_globalMemory(mem))
toplevel()->throwError(kEndOfFileError);
}
}