JSModuleNamespaceObject.cpp [plain text]
#include "config.h"
#include "JSModuleNamespaceObject.h"
#include "Error.h"
#include "IdentifierInlines.h"
#include "JSCJSValueInlines.h"
#include "JSCellInlines.h"
#include "JSModuleEnvironment.h"
#include "JSModuleRecord.h"
#include "JSPropertyNameIterator.h"
#include "SlotVisitorInlines.h"
#include "StructureInlines.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, JSModuleRecord* moduleRecord, const IdentifierSet& exports)
{
VM& vm = exec->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);
JSC_NATIVE_FUNCTION(vm.propertyNames->iteratorSymbol, moduleNamespaceObjectSymbolIterator, DontEnum, 0);
putDirect(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Module"), DontEnum | ReadOnly);
methodTable(vm)->preventExtensions(this, exec);
ASSERT(!exec->hadException());
}
void JSModuleNamespaceObject::destroy(JSCell* cell)
{
JSModuleNamespaceObject* thisObject = jsCast<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);
}
static EncodedJSValue callbackGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName)
{
JSModuleNamespaceObject* thisObject = jsCast<JSModuleNamespaceObject*>(JSValue::decode(thisValue));
JSModuleRecord* moduleRecord = thisObject->moduleRecord();
JSModuleRecord::Resolution resolution = moduleRecord->resolveExport(exec, Identifier::fromUid(exec, propertyName.uid()));
ASSERT(resolution.type != JSModuleRecord::Resolution::Type::NotFound && resolution.type != JSModuleRecord::Resolution::Type::Ambiguous);
JSModuleRecord* targetModule = resolution.moduleRecord;
JSModuleEnvironment* targetEnvironment = targetModule->moduleEnvironment();
PropertySlot trampolineSlot(targetEnvironment, PropertySlot::InternalMethodType::Get);
if (!targetEnvironment->methodTable(exec->vm())->getOwnPropertySlot(targetEnvironment, exec, resolution.localName, trampolineSlot))
return JSValue::encode(jsUndefined());
JSValue value = trampolineSlot.getValue(exec, propertyName);
if (exec->hadException())
return JSValue::encode(jsUndefined());
if (!value)
return throwVMError(exec, createTDZError(exec));
return JSValue::encode(value);
}
bool JSModuleNamespaceObject::getOwnPropertySlot(JSObject* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
JSModuleNamespaceObject* thisObject = jsCast<JSModuleNamespaceObject*>(cell);
if (propertyName.isSymbol())
return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
if (!thisObject->m_exports.contains(propertyName.uid()))
return false;
slot.setCustom(thisObject, DontDelete, callbackGetter);
return true;
}
bool JSModuleNamespaceObject::put(JSCell*, ExecState* exec, PropertyName, JSValue, PutPropertySlot& slot)
{
if (slot.isStrictMode())
throwTypeError(exec, ASCIILiteral(StrictModeReadonlyPropertyWriteError));
return false;
}
bool JSModuleNamespaceObject::putByIndex(JSCell*, ExecState* exec, unsigned, JSValue, bool shouldThrow)
{
if (shouldThrow)
throwTypeError(exec, ASCIILiteral(StrictModeReadonlyPropertyWriteError));
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)
{
if (shouldThrow)
throwTypeError(exec, ASCIILiteral("Attempting to define property on object that is not extensible."));
return false;
}
EncodedJSValue JSC_HOST_CALL moduleNamespaceObjectSymbolIterator(ExecState* exec)
{
JSValue thisValue = exec->thisValue();
if (!thisValue.isObject())
return JSValue::encode(throwTypeError(exec, ASCIILiteral("|this| should be an object")));
return JSValue::encode(JSPropertyNameIterator::create(exec, exec->lexicalGlobalObject()->propertyNameIteratorStructure(), asObject(thisValue)));
}
}