InternalFunction.cpp [plain text]
#include "config.h"
#include "InternalFunction.h"
#include "JSBoundFunction.h"
#include "JSCInlines.h"
#include "ProxyObject.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(InternalFunction);
const ClassInfo InternalFunction::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(InternalFunction) };
InternalFunction::InternalFunction(VM& vm, Structure* structure, NativeFunction functionForCall, NativeFunction functionForConstruct)
: Base(vm, structure)
, m_functionForCall(functionForCall)
, m_functionForConstruct(functionForConstruct ? functionForConstruct : callHostFunctionAsConstructor)
, m_globalObject(vm, this, structure->globalObject())
{
ASSERT_WITH_MESSAGE(m_functionForCall, "[[Call]] must be implemented");
ASSERT(m_functionForConstruct);
}
void InternalFunction::finishCreation(VM& vm, const String& name, NameAdditionMode nameAdditionMode)
{
Base::finishCreation(vm);
ASSERT(jsDynamicCast<InternalFunction*>(vm, this));
ASSERT(methodTable(vm)->getCallData == InternalFunction::info()->methodTable.getCallData);
ASSERT(methodTable(vm)->getConstructData == InternalFunction::info()->methodTable.getConstructData);
ASSERT(type() == InternalFunctionType || type() == NullSetterFunctionType);
JSString* nameString = jsString(vm, name);
m_originalName.set(vm, this, nameString);
if (nameAdditionMode == NameAdditionMode::WithStructureTransition)
putDirect(vm, vm.propertyNames->name, nameString, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
else
putDirectWithoutTransition(vm, vm.propertyNames->name, nameString, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
}
void InternalFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
InternalFunction* thisObject = jsCast<InternalFunction*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
visitor.append(thisObject->m_originalName);
}
const String& InternalFunction::name()
{
const String& name = m_originalName->tryGetValue();
ASSERT(name); return name;
}
const String InternalFunction::displayName(VM& vm)
{
JSValue displayName = getDirect(vm, vm.propertyNames->displayName);
if (displayName && isJSString(displayName))
return asString(displayName)->tryGetValue();
return String();
}
CallData InternalFunction::getCallData(JSCell* cell)
{
auto* function = jsCast<InternalFunction*>(cell);
ASSERT(function->m_functionForCall);
CallData callData;
callData.type = CallData::Type::Native;
callData.native.function = function->m_functionForCall;
return callData;
}
CallData InternalFunction::getConstructData(JSCell* cell)
{
CallData constructData;
auto* function = jsCast<InternalFunction*>(cell);
if (function->m_functionForConstruct != callHostFunctionAsConstructor) {
constructData.type = CallData::Type::Native;
constructData.native.function = function->m_functionForConstruct;
}
return constructData;
}
const String InternalFunction::calculatedDisplayName(VM& vm)
{
const String explicitName = displayName(vm);
if (!explicitName.isEmpty())
return explicitName;
return name();
}
Structure* InternalFunction::createSubclassStructure(JSGlobalObject* globalObject, JSObject* newTarget, Structure* baseClass)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(baseClass->hasMonoProto());
JSFunction* targetFunction = jsDynamicCast<JSFunction*>(vm, newTarget);
JSGlobalObject* baseGlobalObject = baseClass->globalObject();
if (LIKELY(targetFunction)) {
FunctionRareData* rareData = targetFunction->ensureRareData(vm);
Structure* structure = rareData->internalFunctionAllocationStructure();
if (LIKELY(structure && structure->classInfo() == baseClass->classInfo() && structure->globalObject() == baseGlobalObject))
return structure;
JSValue prototypeValue = targetFunction->get(globalObject, vm.propertyNames->prototype);
RETURN_IF_EXCEPTION(scope, nullptr);
if (JSObject* prototype = jsDynamicCast<JSObject*>(vm, prototypeValue))
return rareData->createInternalFunctionAllocationStructureFromBase(vm, baseGlobalObject, prototype, baseClass);
} else {
JSValue prototypeValue = newTarget->get(globalObject, vm.propertyNames->prototype);
RETURN_IF_EXCEPTION(scope, nullptr);
if (JSObject* prototype = jsDynamicCast<JSObject*>(vm, prototypeValue)) {
return vm.structureCache.emptyStructureForPrototypeFromBaseStructure(baseGlobalObject, prototype, baseClass);
}
}
return baseClass;
}
InternalFunction* InternalFunction::createFunctionThatMasqueradesAsUndefined(VM& vm, JSGlobalObject* globalObject, int length, const String& name, NativeFunction nativeFunction)
{
Structure* structure = Structure::create(vm, globalObject, globalObject->objectPrototype(), TypeInfo(InternalFunctionType, InternalFunction::StructureFlags | MasqueradesAsUndefined), InternalFunction::info());
globalObject->masqueradesAsUndefinedWatchpoint()->fireAll(globalObject->vm(), "Allocated masquerading object");
InternalFunction* function = new (NotNull, allocateCell<InternalFunction>(vm.heap)) InternalFunction(vm, structure, nativeFunction);
function->finishCreation(vm, name, NameAdditionMode::WithoutStructureTransition);
function->putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(length), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
return function;
}
JSGlobalObject* getFunctionRealm(VM& vm, JSObject* object)
{
ASSERT(object->isCallable(vm));
while (true) {
if (object->inherits<JSBoundFunction>(vm)) {
object = jsCast<JSBoundFunction*>(object)->targetFunction();
continue;
}
if (object->type() == ProxyObjectType) {
auto* proxy = jsCast<ProxyObject*>(object);
if (!proxy->isRevoked()) {
object = proxy->target();
continue;
}
}
return object->globalObject(vm);
}
}
}