JSModuleNamespaceObject.cpp [plain text]
#include "config.h"
#include "JSModuleNamespaceObject.h"
#include "AbstractModuleRecord.h"
#include "Error.h"
#include "JSCInlines.h"
#include "JSModuleEnvironment.h"
#include "JSPropertyNameIterator.h"
namespace JSC {
static EncodedJSValue JSC_HOST_CALL moduleNamespaceObjectSymbolIterator(ExecState*);
const ClassInfo JSModuleNamespaceObject::s_info = { "ModuleNamespaceObject", &Base::s_info, nullptr, CREATE_METHOD_TABLE(JSModuleNamespaceObject) };
JSModuleNamespaceObject::JSModuleNamespaceObject(VM& vm, Structure* structure)
: Base(vm, structure)
, m_exports()
{
}
void JSModuleNamespaceObject::finishCreation(ExecState* exec, JSGlobalObject* globalObject, AbstractModuleRecord* moduleRecord, const IdentifierSet& exports)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Base::finishCreation(vm);
ASSERT(inherits(info()));
Vector<UniquedStringImpl*> temporaryVector(exports.size(), nullptr);
std::transform(exports.begin(), exports.end(), temporaryVector.begin(), [](const RefPtr<WTF::UniquedStringImpl>& ref) {
return ref.get();
});
std::sort(temporaryVector.begin(), temporaryVector.end(), [] (UniquedStringImpl* lhs, UniquedStringImpl* rhs) {
return codePointCompare(lhs, rhs) < 0;
});
for (auto* identifier : temporaryVector)
m_exports.add(identifier);
m_moduleRecord.set(vm, this, moduleRecord);
JSFunction* iteratorFunction = JSFunction::create(vm, globalObject, 0, ASCIILiteral("[Symbol.iterator]"), moduleNamespaceObjectSymbolIterator, NoIntrinsic);
putDirect(vm, vm.propertyNames->iteratorSymbol, iteratorFunction, DontEnum);
putDirect(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Module"), DontEnum | ReadOnly);
methodTable(vm)->preventExtensions(this, exec);
ASSERT_UNUSED(scope, !scope.exception());
}
void JSModuleNamespaceObject::destroy(JSCell* cell)
{
JSModuleNamespaceObject* thisObject = static_cast<JSModuleNamespaceObject*>(cell);
thisObject->JSModuleNamespaceObject::~JSModuleNamespaceObject();
}
void JSModuleNamespaceObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
JSModuleNamespaceObject* thisObject = jsCast<JSModuleNamespaceObject*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(thisObject->m_moduleRecord);
}
bool JSModuleNamespaceObject::getOwnPropertySlot(JSObject* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSModuleNamespaceObject* thisObject = jsCast<JSModuleNamespaceObject*>(cell);
if (propertyName.isSymbol())
return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
slot.disableCaching();
slot.setIsTaintedByOpaqueObject();
if (!thisObject->m_exports.contains(propertyName.uid()))
return false;
switch (slot.internalMethodType()) {
case PropertySlot::InternalMethodType::Get:
case PropertySlot::InternalMethodType::GetOwnProperty: {
AbstractModuleRecord* moduleRecord = thisObject->moduleRecord();
AbstractModuleRecord::Resolution resolution = moduleRecord->resolveExport(exec, Identifier::fromUid(exec, propertyName.uid()));
ASSERT(resolution.type != AbstractModuleRecord::Resolution::Type::NotFound && resolution.type != AbstractModuleRecord::Resolution::Type::Ambiguous);
AbstractModuleRecord* targetModule = resolution.moduleRecord;
JSModuleEnvironment* targetEnvironment = targetModule->moduleEnvironment();
PropertySlot trampolineSlot(targetEnvironment, PropertySlot::InternalMethodType::Get);
bool found = targetEnvironment->methodTable(vm)->getOwnPropertySlot(targetEnvironment, exec, resolution.localName, trampolineSlot);
ASSERT_UNUSED(found, found);
JSValue value = trampolineSlot.getValue(exec, propertyName);
ASSERT(!scope.exception());
if (!value) {
throwVMError(exec, scope, createTDZError(exec));
return false;
}
slot.setValue(thisObject, DontDelete, value);
return true;
}
case PropertySlot::InternalMethodType::HasProperty: {
slot.setValue(thisObject, DontDelete, jsUndefined());
return true;
}
case PropertySlot::InternalMethodType::VMInquiry:
return false;
}
RELEASE_ASSERT_NOT_REACHED();
return false;
}
bool JSModuleNamespaceObject::put(JSCell*, ExecState* exec, PropertyName, JSValue, PutPropertySlot& slot)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (slot.isStrictMode())
throwTypeError(exec, scope, ASCIILiteral(ReadonlyPropertyWriteError));
return false;
}
bool JSModuleNamespaceObject::putByIndex(JSCell*, ExecState* exec, unsigned, JSValue, bool shouldThrow)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (shouldThrow)
throwTypeError(exec, scope, ASCIILiteral(ReadonlyPropertyWriteError));
return false;
}
bool JSModuleNamespaceObject::deleteProperty(JSCell* cell, ExecState*, PropertyName propertyName)
{
JSModuleNamespaceObject* thisObject = jsCast<JSModuleNamespaceObject*>(cell);
return !thisObject->m_exports.contains(propertyName.uid());
}
void JSModuleNamespaceObject::getOwnPropertyNames(JSObject* cell, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
{
JSModuleNamespaceObject* thisObject = jsCast<JSModuleNamespaceObject*>(cell);
for (const auto& name : thisObject->m_exports)
propertyNames.add(name.get());
return JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
}
bool JSModuleNamespaceObject::defineOwnProperty(JSObject*, ExecState* exec, PropertyName, const PropertyDescriptor&, bool shouldThrow)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (shouldThrow)
throwTypeError(exec, scope, ASCIILiteral(NonExtensibleObjectPropertyDefineError));
return false;
}
EncodedJSValue JSC_HOST_CALL moduleNamespaceObjectSymbolIterator(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSModuleNamespaceObject* object = jsDynamicCast<JSModuleNamespaceObject*>(exec->thisValue());
if (!object)
return throwVMTypeError(exec, scope, ASCIILiteral("|this| should be a module namespace object"));
return JSValue::encode(JSPropertyNameIterator::create(exec, exec->lexicalGlobalObject()->propertyNameIteratorStructure(), object));
}
}