darling-JavaScriptCore/inspector/JSInjectedScriptHost.cpp

871 lines
34 KiB
C++
Raw Normal View History

2017-08-12 16:48:01 +00:00
/*
* Copyright (C) 2013-2020 Apple Inc. All rights reserved.
2017-08-12 16:48:01 +00:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "JSInjectedScriptHost.h"
2020-08-29 13:27:11 +00:00
#include "ArrayPrototype.h"
2017-08-12 16:48:01 +00:00
#include "Completion.h"
#include "DateInstance.h"
#include "DeferGC.h"
2017-08-12 16:48:01 +00:00
#include "DirectArguments.h"
2020-08-29 13:27:11 +00:00
#include "FunctionPrototype.h"
#include "HeapAnalyzer.h"
2020-08-29 13:27:11 +00:00
#include "HeapIterationScope.h"
#include "HeapProfiler.h"
2017-08-12 16:48:01 +00:00
#include "InjectedScriptHost.h"
2020-08-29 13:27:11 +00:00
#include "IterationKind.h"
2017-08-12 16:48:01 +00:00
#include "IteratorOperations.h"
#include "JSArray.h"
#include "JSArrayIterator.h"
2017-08-12 16:48:01 +00:00
#include "JSBoundFunction.h"
#include "JSCInlines.h"
#include "JSInjectedScriptHostPrototype.h"
#include "JSMap.h"
#include "JSMapIterator.h"
2017-08-12 16:48:01 +00:00
#include "JSPromise.h"
2020-08-29 13:27:11 +00:00
#include "JSPromisePrototype.h"
2017-08-12 16:48:01 +00:00
#include "JSSet.h"
#include "JSSetIterator.h"
2017-08-12 16:48:01 +00:00
#include "JSStringIterator.h"
#include "JSTypedArrays.h"
#include "JSWeakMap.h"
#include "JSWeakSet.h"
2020-08-29 13:27:11 +00:00
#include "MarkedSpaceInlines.h"
2017-08-12 16:48:01 +00:00
#include "ObjectConstructor.h"
#include "PreventCollectionScope.h"
2017-08-12 16:48:01 +00:00
#include "ProxyObject.h"
#include "RegExpObject.h"
#include "ScopedArguments.h"
#include "SourceCode.h"
#include <wtf/Function.h>
#include <wtf/HashFunctions.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/Lock.h>
#include <wtf/PrintStream.h>
#include <wtf/text/StringConcatenate.h>
2017-08-12 16:48:01 +00:00
namespace Inspector {
using namespace JSC;
2018-01-03 05:16:05 +00:00
const ClassInfo JSInjectedScriptHost::s_info = { "InjectedScriptHost", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSInjectedScriptHost) };
2017-08-12 16:48:01 +00:00
JSInjectedScriptHost::JSInjectedScriptHost(VM& vm, Structure* structure, Ref<InjectedScriptHost>&& impl)
: Base(vm, structure)
2017-08-12 16:48:01 +00:00
, m_wrapped(WTFMove(impl))
{
}
void JSInjectedScriptHost::finishCreation(VM& vm)
{
Base::finishCreation(vm);
2018-01-03 05:16:05 +00:00
ASSERT(inherits(vm, info()));
2017-08-12 16:48:01 +00:00
}
JSObject* JSInjectedScriptHost::createPrototype(VM& vm, JSGlobalObject* globalObject)
{
return JSInjectedScriptHostPrototype::create(vm, globalObject, JSInjectedScriptHostPrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
}
void JSInjectedScriptHost::destroy(JSC::JSCell* cell)
{
JSInjectedScriptHost* thisObject = static_cast<JSInjectedScriptHost*>(cell);
thisObject->JSInjectedScriptHost::~JSInjectedScriptHost();
}
JSValue JSInjectedScriptHost::evaluate(JSGlobalObject* globalObject) const
2017-08-12 16:48:01 +00:00
{
return globalObject->evalFunction();
}
JSValue JSInjectedScriptHost::savedResultAlias(JSGlobalObject* globalObject) const
{
auto savedResultAlias = impl().savedResultAlias();
if (!savedResultAlias)
return jsUndefined();
return jsString(globalObject->vm(), savedResultAlias.value());
}
JSValue JSInjectedScriptHost::evaluateWithScopeExtension(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue scriptValue = callFrame->argument(0);
2017-08-12 16:48:01 +00:00
if (!scriptValue.isString())
return throwTypeError(globalObject, scope, "InjectedScriptHost.evaluateWithScopeExtension first argument must be a string."_s);
2017-08-12 16:48:01 +00:00
String program = asString(scriptValue)->value(globalObject);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
NakedPtr<Exception> exception;
JSObject* scopeExtension = callFrame->argument(1).getObject();
JSValue result = JSC::evaluateWithScopeExtension(globalObject, makeSource(program, callFrame->callerSourceOrigin(vm)), scopeExtension, exception);
2017-08-12 16:48:01 +00:00
if (exception)
throwException(globalObject, scope, exception);
2017-08-12 16:48:01 +00:00
return result;
}
JSValue JSInjectedScriptHost::internalConstructorName(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
JSObject* object = jsCast<JSObject*>(callFrame->uncheckedArgument(0).toThis(globalObject, ECMAMode::sloppy()));
return jsString(vm, JSObject::calculatedClassName(object));
2017-08-12 16:48:01 +00:00
}
JSValue JSInjectedScriptHost::isHTMLAllCollection(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
JSValue value = callFrame->uncheckedArgument(0);
2018-01-03 05:16:05 +00:00
return jsBoolean(impl().isHTMLAllCollection(vm, value));
2017-08-12 16:48:01 +00:00
}
JSValue JSInjectedScriptHost::isPromiseRejectedWithNativeGetterTypeError(JSGlobalObject* globalObject, CallFrame* callFrame)
2020-08-29 13:27:11 +00:00
{
VM& vm = globalObject->vm();
2020-08-29 13:27:11 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
auto* promise = jsDynamicCast<JSPromise*>(vm, callFrame->argument(0));
if (!promise)
return throwTypeError(globalObject, scope, "InjectedScriptHost.isPromiseRejectedWithNativeGetterTypeError first argument must be a Promise."_s);
2020-08-29 13:27:11 +00:00
bool result = false;
if (auto* errorInstance = jsDynamicCast<ErrorInstance*>(vm, promise->result(vm)))
result = errorInstance->isNativeGetterTypeError();
return jsBoolean(result);
}
JSValue JSInjectedScriptHost::subtype(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
JSValue value = callFrame->uncheckedArgument(0);
2017-08-12 16:48:01 +00:00
if (value.isString())
2020-08-29 13:27:11 +00:00
return vm.smallStrings.stringString();
2017-08-12 16:48:01 +00:00
if (value.isBoolean())
2020-08-29 13:27:11 +00:00
return vm.smallStrings.booleanString();
2017-08-12 16:48:01 +00:00
if (value.isNumber())
2020-08-29 13:27:11 +00:00
return vm.smallStrings.numberString();
2017-08-12 16:48:01 +00:00
if (value.isSymbol())
2020-08-29 13:27:11 +00:00
return vm.smallStrings.symbolString();
2017-08-12 16:48:01 +00:00
2020-08-29 13:27:11 +00:00
if (auto* object = jsDynamicCast<JSObject*>(vm, value)) {
2017-08-12 16:48:01 +00:00
if (object->isErrorInstance())
return jsNontrivialString(vm, "error"_s);
2017-08-12 16:48:01 +00:00
// Consider class constructor functions class objects.
2018-01-03 05:16:05 +00:00
JSFunction* function = jsDynamicCast<JSFunction*>(vm, value);
2017-08-12 16:48:01 +00:00
if (function && function->isClassConstructorFunction())
return jsNontrivialString(vm, "class"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSArray>(vm))
return jsNontrivialString(vm, "array"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<DirectArguments>(vm) || object->inherits<ScopedArguments>(vm))
return jsNontrivialString(vm, "array"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<DateInstance>(vm))
return jsNontrivialString(vm, "date"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<RegExpObject>(vm))
return jsNontrivialString(vm, "regexp"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<ProxyObject>(vm))
return jsNontrivialString(vm, "proxy"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSMap>(vm))
return jsNontrivialString(vm, "map"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSSet>(vm))
return jsNontrivialString(vm, "set"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSWeakMap>(vm))
return jsNontrivialString(vm, "weakmap"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSWeakSet>(vm))
return jsNontrivialString(vm, "weakset"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSStringIterator>(vm))
return jsNontrivialString(vm, "iterator"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSArrayIterator>(vm)
|| object->inherits<JSMapIterator>(vm)
|| object->inherits<JSSetIterator>(vm))
return jsNontrivialString(vm, "iterator"_s);
2020-08-29 13:27:11 +00:00
if (object->inherits<JSInt8Array>(vm)
|| object->inherits<JSInt16Array>(vm)
|| object->inherits<JSInt32Array>(vm)
|| object->inherits<JSUint8Array>(vm)
|| object->inherits<JSUint8ClampedArray>(vm)
|| object->inherits<JSUint16Array>(vm)
|| object->inherits<JSUint32Array>(vm)
|| object->inherits<JSFloat32Array>(vm)
|| object->inherits<JSFloat64Array>(vm))
return jsNontrivialString(vm, "array"_s);
2017-08-12 16:48:01 +00:00
}
return impl().subtype(globalObject, value);
2017-08-12 16:48:01 +00:00
}
JSValue JSInjectedScriptHost::functionDetails(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
JSValue value = callFrame->uncheckedArgument(0);
2020-08-29 13:27:11 +00:00
auto* function = jsDynamicCast<JSFunction*>(vm, value);
if (!function)
2017-08-12 16:48:01 +00:00
return jsUndefined();
2018-01-03 05:16:05 +00:00
// FIXME: <https://webkit.org/b/87192> Web Inspector: Expose function scope / closure data
2017-08-12 16:48:01 +00:00
// FIXME: This should provide better details for JSBoundFunctions.
const SourceCode* sourceCode = function->sourceCode();
if (!sourceCode)
return jsUndefined();
// In the inspector protocol all positions are 0-based while in SourceCode they are 1-based
int lineNumber = sourceCode->firstLine().oneBasedInt();
if (lineNumber)
lineNumber -= 1;
int columnNumber = sourceCode->startColumn().oneBasedInt();
if (columnNumber)
columnNumber -= 1;
String scriptID = String::number(sourceCode->provider()->asID());
JSObject* location = constructEmptyObject(globalObject);
location->putDirect(vm, Identifier::fromString(vm, "scriptId"), jsString(vm, scriptID));
location->putDirect(vm, Identifier::fromString(vm, "lineNumber"), jsNumber(lineNumber));
location->putDirect(vm, Identifier::fromString(vm, "columnNumber"), jsNumber(columnNumber));
2017-08-12 16:48:01 +00:00
JSObject* result = constructEmptyObject(globalObject);
result->putDirect(vm, Identifier::fromString(vm, "location"), location);
2017-08-12 16:48:01 +00:00
String name = function->name(vm);
if (!name.isEmpty())
result->putDirect(vm, Identifier::fromString(vm, "name"), jsString(vm, name));
2017-08-12 16:48:01 +00:00
String displayName = function->displayName(vm);
if (!displayName.isEmpty())
result->putDirect(vm, Identifier::fromString(vm, "displayName"), jsString(vm, displayName));
2017-08-12 16:48:01 +00:00
return result;
}
static JSObject* constructInternalProperty(JSGlobalObject* globalObject, const String& name, JSValue value)
2017-08-12 16:48:01 +00:00
{
VM& vm = globalObject->vm();
JSObject* result = constructEmptyObject(globalObject);
result->putDirect(vm, Identifier::fromString(vm, "name"), jsString(vm, name));
result->putDirect(vm, Identifier::fromString(vm, "value"), value);
2017-08-12 16:48:01 +00:00
return result;
}
JSValue JSInjectedScriptHost::getInternalProperties(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue value = callFrame->uncheckedArgument(0);
2017-08-12 16:48:01 +00:00
JSValue internalProperties = impl().getInternalProperties(vm, globalObject, value);
2020-08-29 13:27:11 +00:00
if (internalProperties)
return internalProperties;
2018-01-03 05:16:05 +00:00
if (JSPromise* promise = jsDynamicCast<JSPromise*>(vm, value)) {
2017-08-12 16:48:01 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
2020-08-29 13:27:11 +00:00
switch (promise->status(vm)) {
2017-08-12 16:48:01 +00:00
case JSPromise::Status::Pending:
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "status"_s, jsNontrivialString(vm, "pending"_s)));
2017-08-12 16:48:01 +00:00
return array;
case JSPromise::Status::Fulfilled:
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "status"_s, jsNontrivialString(vm, "resolved"_s)));
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "result"_s, promise->result(vm)));
2017-08-12 16:48:01 +00:00
return array;
case JSPromise::Status::Rejected:
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "status"_s, jsNontrivialString(vm, "rejected"_s)));
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "result"_s, promise->result(vm)));
2017-08-12 16:48:01 +00:00
return array;
}
// FIXME: <https://webkit.org/b/141664> Web Inspector: ES6: Improved Support for Promises - Promise Reactions
RELEASE_ASSERT_NOT_REACHED();
}
2018-01-03 05:16:05 +00:00
if (JSBoundFunction* boundFunction = jsDynamicCast<JSBoundFunction*>(vm, value)) {
2017-08-12 16:48:01 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "targetFunction", boundFunction->targetFunction()));
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "boundThis", boundFunction->boundThis()));
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
if (boundFunction->boundArgs()) {
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "boundArgs", boundFunction->boundArgsCopy(globalObject)));
2017-08-12 16:48:01 +00:00
return array;
}
return array;
}
2018-01-03 05:16:05 +00:00
if (ProxyObject* proxy = jsDynamicCast<ProxyObject*>(vm, value)) {
2017-08-12 16:48:01 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr, 2);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "target"_s, proxy->target()));
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "handler"_s, proxy->handler()));
2017-08-12 16:48:01 +00:00
return array;
}
2018-01-03 05:16:05 +00:00
if (JSObject* iteratorObject = jsDynamicCast<JSObject*>(vm, value)) {
auto toString = [&] (IterationKind kind) {
switch (kind) {
case IterationKind::Keys:
return jsNontrivialString(vm, "keys"_s);
case IterationKind::Values:
return jsNontrivialString(vm, "values"_s);
case IterationKind::Entries:
return jsNontrivialString(vm, "entries"_s);
}
return jsNontrivialString(vm, ""_s);
};
if (auto* arrayIterator = jsDynamicCast<JSArrayIterator*>(vm, iteratorObject)) {
JSValue iteratedValue = arrayIterator->iteratedObject();
IterationKind kind = arrayIterator->kind();
2017-08-12 16:48:01 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr, 2);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "array", iteratedValue));
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "kind", toString(kind)));
2017-08-12 16:48:01 +00:00
return array;
}
if (auto* mapIterator = jsDynamicCast<JSMapIterator*>(vm, iteratorObject)) {
JSValue iteratedValue = mapIterator->iteratedObject();
IterationKind kind = mapIterator->kind();
2020-08-29 13:27:11 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr, 2);
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "map", iteratedValue));
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "kind", toString(kind)));
2020-08-29 13:27:11 +00:00
return array;
2017-08-12 16:48:01 +00:00
}
2018-01-03 05:16:05 +00:00
if (auto* setIterator = jsDynamicCast<JSSetIterator*>(vm, iteratorObject)) {
JSValue iteratedValue = setIterator->iteratedObject();
IterationKind kind = setIterator->kind();
2020-08-29 13:27:11 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr, 2);
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "set", iteratedValue));
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "kind", toString(kind)));
2020-08-29 13:27:11 +00:00
return array;
2017-08-12 16:48:01 +00:00
}
}
2018-01-03 05:16:05 +00:00
if (JSStringIterator* stringIterator = jsDynamicCast<JSStringIterator*>(vm, value)) {
2017-08-12 16:48:01 +00:00
unsigned index = 0;
JSArray* array = constructEmptyArray(globalObject, nullptr, 1);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
scope.release();
array->putDirectIndex(globalObject, index++, constructInternalProperty(globalObject, "string", stringIterator->iteratedString()));
2017-08-12 16:48:01 +00:00
return array;
}
return jsUndefined();
}
JSValue JSInjectedScriptHost::proxyTargetValue(VM& vm, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
JSValue value = callFrame->uncheckedArgument(0);
2018-01-03 05:16:05 +00:00
ProxyObject* proxy = jsDynamicCast<ProxyObject*>(vm, value);
2017-08-12 16:48:01 +00:00
if (!proxy)
return jsUndefined();
JSObject* target = proxy->target();
2018-01-03 05:16:05 +00:00
while (ProxyObject* proxy = jsDynamicCast<ProxyObject*>(vm, target))
2017-08-12 16:48:01 +00:00
target = proxy->target();
return target;
}
JSValue JSInjectedScriptHost::weakMapSize(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
JSValue value = callFrame->uncheckedArgument(0);
2018-01-03 05:16:05 +00:00
JSWeakMap* weakMap = jsDynamicCast<JSWeakMap*>(vm, value);
2017-08-12 16:48:01 +00:00
if (!weakMap)
return jsUndefined();
2020-08-29 13:27:11 +00:00
return jsNumber(weakMap->size());
2017-08-12 16:48:01 +00:00
}
JSValue JSInjectedScriptHost::weakMapEntries(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
auto* weakMap = jsDynamicCast<JSWeakMap*>(vm, callFrame->uncheckedArgument(0));
2017-08-12 16:48:01 +00:00
if (!weakMap)
return jsUndefined();
MarkedArgumentBuffer buffer;
auto fetchCount = callFrame->argument(1).toInteger(globalObject);
weakMap->takeSnapshot(buffer, fetchCount >= 0 ? static_cast<unsigned>(fetchCount) : 0);
ASSERT(!buffer.hasOverflowed());
2017-08-12 16:48:01 +00:00
JSArray* array = constructEmptyArray(globalObject, nullptr);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
2020-08-29 13:27:11 +00:00
for (unsigned index = 0; index < buffer.size(); index += 2) {
JSObject* entry = constructEmptyObject(globalObject);
entry->putDirect(vm, Identifier::fromString(vm, "key"), buffer.at(index));
entry->putDirect(vm, Identifier::fromString(vm, "value"), buffer.at(index + 1));
array->putDirectIndex(globalObject, index / 2, entry);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
}
return array;
}
JSValue JSInjectedScriptHost::weakSetSize(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
JSValue value = callFrame->uncheckedArgument(0);
2018-01-03 05:16:05 +00:00
JSWeakSet* weakSet = jsDynamicCast<JSWeakSet*>(vm, value);
2017-08-12 16:48:01 +00:00
if (!weakSet)
return jsUndefined();
2020-08-29 13:27:11 +00:00
return jsNumber(weakSet->size());
2017-08-12 16:48:01 +00:00
}
JSValue JSInjectedScriptHost::weakSetEntries(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
auto* weakSet = jsDynamicCast<JSWeakSet*>(vm, callFrame->uncheckedArgument(0));
2017-08-12 16:48:01 +00:00
if (!weakSet)
return jsUndefined();
MarkedArgumentBuffer buffer;
auto fetchCount = callFrame->argument(1).toInteger(globalObject);
weakSet->takeSnapshot(buffer, fetchCount >= 0 ? static_cast<unsigned>(fetchCount) : 0);
ASSERT(!buffer.hasOverflowed());
2017-08-12 16:48:01 +00:00
JSArray* array = constructEmptyArray(globalObject, nullptr);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
2020-08-29 13:27:11 +00:00
for (unsigned index = 0; index < buffer.size(); ++index) {
JSObject* entry = constructEmptyObject(globalObject);
entry->putDirect(vm, Identifier::fromString(vm, "value"), buffer.at(index));
array->putDirectIndex(globalObject, index, entry);
2017-08-12 16:48:01 +00:00
RETURN_IF_EXCEPTION(scope, JSValue());
}
return array;
}
static JSObject* cloneArrayIteratorObject(JSGlobalObject* globalObject, VM& vm, JSArrayIterator* iteratorObject)
2017-08-12 16:48:01 +00:00
{
JSArrayIterator* clone = JSArrayIterator::create(vm, globalObject->arrayIteratorStructure(), iteratorObject->iteratedObject(), iteratorObject->internalField(JSArrayIterator::Field::Kind).get());
clone->internalField(JSArrayIterator::Field::Index).set(vm, clone, iteratorObject->internalField(JSArrayIterator::Field::Index).get());
2017-08-12 16:48:01 +00:00
return clone;
}
static JSObject* cloneMapIteratorObject(JSGlobalObject* globalObject, VM& vm, JSMapIterator* iteratorObject)
2020-08-29 13:27:11 +00:00
{
JSMapIterator* clone = JSMapIterator::create(vm, globalObject->mapIteratorStructure(), jsCast<JSMap*>(iteratorObject->iteratedObject()), iteratorObject->kind());
clone->internalField(JSMapIterator::Field::MapBucket).set(vm, clone, iteratorObject->internalField(JSMapIterator::Field::MapBucket).get());
2020-08-29 13:27:11 +00:00
return clone;
}
static JSObject* cloneSetIteratorObject(JSGlobalObject* globalObject, VM& vm, JSSetIterator* iteratorObject)
2020-08-29 13:27:11 +00:00
{
JSSetIterator* clone = JSSetIterator::create(vm, globalObject->setIteratorStructure(), jsCast<JSSet*>(iteratorObject->iteratedObject()), iteratorObject->kind());
clone->internalField(JSSetIterator::Field::SetBucket).set(vm, clone, iteratorObject->internalField(JSSetIterator::Field::SetBucket).get());
2020-08-29 13:27:11 +00:00
return clone;
}
JSValue JSInjectedScriptHost::iteratorEntries(JSGlobalObject* globalObject, CallFrame* callFrame)
2017-08-12 16:48:01 +00:00
{
if (callFrame->argumentCount() < 1)
2017-08-12 16:48:01 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
2017-08-12 16:48:01 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
2018-01-03 05:16:05 +00:00
2017-08-12 16:48:01 +00:00
JSValue iterator;
JSValue value = callFrame->uncheckedArgument(0);
2020-08-29 13:27:11 +00:00
if (JSStringIterator* stringIterator = jsDynamicCast<JSStringIterator*>(vm, value)) {
2018-01-03 05:16:05 +00:00
if (globalObject->isStringPrototypeIteratorProtocolFastAndNonObservable())
iterator = stringIterator->clone(globalObject);
2018-01-03 05:16:05 +00:00
} else if (JSObject* iteratorObject = jsDynamicCast<JSObject*>(vm, value)) {
if (auto* arrayIterator = jsDynamicCast<JSArrayIterator*>(vm, iteratorObject)) {
JSObject* iteratedObject = arrayIterator->iteratedObject();
2018-01-03 05:16:05 +00:00
if (isJSArray(iteratedObject)) {
JSArray* array = jsCast<JSArray*>(iteratedObject);
if (array->isIteratorProtocolFastAndNonObservable())
iterator = cloneArrayIteratorObject(globalObject, vm, arrayIterator);
} else if (TypeInfo::isArgumentsType(iteratedObject->type())) {
2018-01-03 05:16:05 +00:00
if (globalObject->isArrayPrototypeIteratorProtocolFastAndNonObservable())
iterator = cloneArrayIteratorObject(globalObject, vm, arrayIterator);
2018-01-03 05:16:05 +00:00
}
} else if (auto* mapIterator = jsDynamicCast<JSMapIterator*>(vm, iteratorObject)) {
if (jsCast<JSMap*>(mapIterator->iteratedObject())->isIteratorProtocolFastAndNonObservable())
iterator = cloneMapIteratorObject(globalObject, vm, mapIterator);
} else if (auto* setIterator = jsDynamicCast<JSSetIterator*>(vm, iteratorObject)) {
if (jsCast<JSSet*>(setIterator->iteratedObject())->isIteratorProtocolFastAndNonObservable())
iterator = cloneSetIteratorObject(globalObject, vm, setIterator);
2017-08-12 16:48:01 +00:00
}
}
2018-01-03 05:16:05 +00:00
RETURN_IF_EXCEPTION(scope, { });
2017-08-12 16:48:01 +00:00
if (!iterator)
return jsUndefined();
IterationRecord iterationRecord = { iterator, iterator.get(globalObject, vm.propertyNames->next) };
2020-08-29 13:27:11 +00:00
2017-08-12 16:48:01 +00:00
unsigned numberToFetch = 5;
JSValue numberToFetchArg = callFrame->argument(1);
double fetchDouble = numberToFetchArg.toInteger(globalObject);
2018-01-03 05:16:05 +00:00
RETURN_IF_EXCEPTION(scope, { });
2017-08-12 16:48:01 +00:00
if (fetchDouble >= 0)
numberToFetch = static_cast<unsigned>(fetchDouble);
JSArray* array = constructEmptyArray(globalObject, nullptr);
2018-01-03 05:16:05 +00:00
RETURN_IF_EXCEPTION(scope, { });
2017-08-12 16:48:01 +00:00
for (unsigned i = 0; i < numberToFetch; ++i) {
JSValue next = iteratorStep(globalObject, iterationRecord);
2018-01-03 05:16:05 +00:00
if (UNLIKELY(scope.exception()) || next.isFalse())
2017-08-12 16:48:01 +00:00
break;
JSValue nextValue = iteratorValue(globalObject, next);
2018-01-03 05:16:05 +00:00
RETURN_IF_EXCEPTION(scope, { });
2017-08-12 16:48:01 +00:00
JSObject* entry = constructEmptyObject(globalObject);
entry->putDirect(vm, Identifier::fromString(vm, "value"), nextValue);
array->putDirectIndex(globalObject, i, entry);
2018-01-03 05:16:05 +00:00
if (UNLIKELY(scope.exception())) {
scope.release();
iteratorClose(globalObject, iterationRecord);
2017-08-12 16:48:01 +00:00
break;
2018-01-03 05:16:05 +00:00
}
2017-08-12 16:48:01 +00:00
}
return array;
}
static bool checkForbiddenPrototype(JSGlobalObject* globalObject, JSValue value, JSValue proto)
2020-08-29 13:27:11 +00:00
{
if (value == proto)
return true;
// Check that the prototype chain of proto hasn't been modified to include value.
return JSObject::defaultHasInstance(globalObject, proto, value);
2020-08-29 13:27:11 +00:00
}
JSValue JSInjectedScriptHost::queryInstances(JSGlobalObject* globalObject, CallFrame* callFrame)
2020-08-29 13:27:11 +00:00
{
if (callFrame->argumentCount() < 1)
2020-08-29 13:27:11 +00:00
return jsUndefined();
VM& vm = globalObject->vm();
2020-08-29 13:27:11 +00:00
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue prototypeOrConstructor = callFrame->uncheckedArgument(0);
2020-08-29 13:27:11 +00:00
if (!prototypeOrConstructor.isObject())
return throwTypeError(globalObject, scope, "queryInstances first argument must be an object."_s);
2020-08-29 13:27:11 +00:00
JSObject* object = asObject(prototypeOrConstructor);
if (object->inherits<ProxyObject>(vm))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with a Proxy."_s);
2020-08-29 13:27:11 +00:00
JSValue prototype = object;
PropertySlot prototypeSlot(object, PropertySlot::InternalMethodType::VMInquiry, &vm);
if (object->getPropertySlot(globalObject, vm.propertyNames->prototype, prototypeSlot)) {
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, { });
if (prototypeSlot.isValue()) {
JSValue prototypeValue = prototypeSlot.getValue(globalObject, vm.propertyNames->prototype);
2020-08-29 13:27:11 +00:00
if (prototypeValue.isObject()) {
prototype = prototypeValue;
object = asObject(prototype);
}
}
}
prototypeSlot.disallowVMEntry.reset();
2020-08-29 13:27:11 +00:00
if (object->inherits<ProxyObject>(vm) || prototype.inherits<ProxyObject>(vm))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with a Proxy."_s);
2020-08-29 13:27:11 +00:00
// FIXME: implement a way of distinguishing between internal and user-created objects.
if (checkForbiddenPrototype(globalObject, object, globalObject->objectPrototype()))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with Object."_s);
if (checkForbiddenPrototype(globalObject, object, globalObject->functionPrototype()))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with Function."_s);
if (checkForbiddenPrototype(globalObject, object, globalObject->arrayPrototype()))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with Array."_s);
if (checkForbiddenPrototype(globalObject, object, globalObject->mapPrototype()))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with Map."_s);
if (checkForbiddenPrototype(globalObject, object, globalObject->jsSetPrototype()))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with Set."_s);
if (checkForbiddenPrototype(globalObject, object, globalObject->promisePrototype()))
return throwTypeError(globalObject, scope, "queryInstances cannot be called with Promise."_s);
sanitizeStackForVM(vm);
2020-08-29 13:27:11 +00:00
vm.heap.collectNow(Sync, CollectionScope::Full);
JSArray* array = constructEmptyArray(globalObject, nullptr);
2020-08-29 13:27:11 +00:00
RETURN_IF_EXCEPTION(scope, { });
{
HeapIterationScope iterationScope(vm.heap);
vm.heap.objectSpace().forEachLiveCell(iterationScope, [&] (HeapCell* cell, HeapCell::Kind kind) {
if (!isJSCellKind(kind))
return IterationStatus::Continue;
JSValue value(static_cast<JSCell*>(cell));
if (value.inherits<ProxyObject>(vm))
return IterationStatus::Continue;
if (JSObject::defaultHasInstance(globalObject, value, prototype))
array->putDirectIndex(globalObject, array->length(), value);
2020-08-29 13:27:11 +00:00
return IterationStatus::Continue;
});
}
return array;
}
class HeapHolderFinder final : public HeapAnalyzer {
WTF_MAKE_FAST_ALLOCATED;
public:
HeapHolderFinder(HeapProfiler& profiler, JSCell* target)
: HeapAnalyzer()
, m_target(target)
{
ASSERT(!profiler.activeHeapAnalyzer());
profiler.setActiveHeapAnalyzer(this);
profiler.vm().heap.collectNow(Sync, CollectionScope::Full);
profiler.setActiveHeapAnalyzer(nullptr);
HashSet<JSCell*> queue;
// Filter `m_holders` based on whether they're reachable from a non-Debugger root.
HashSet<JSCell*> visited;
for (auto* root : m_rootsToInclude)
queue.add(root);
while (auto* from = queue.takeAny()) {
if (m_rootsToIgnore.contains(from))
continue;
if (!visited.add(from).isNewEntry)
continue;
for (auto* to : m_successors.get(from))
queue.add(to);
}
// If a known holder is not an object, also consider all of the holder's holders.
for (auto* holder : m_holders)
queue.add(holder);
while (auto* holder = queue.takeAny()) {
if (holder->isObject())
continue;
for (auto* from : m_predecessors.get(holder)) {
if (!m_holders.contains(from)) {
m_holders.add(from);
queue.add(from);
}
}
}
m_holders.removeIf([&] (auto* holder) {
return !holder->isObject() || !visited.contains(holder);
});
}
HashSet<JSCell*>& holders() { return m_holders; }
void analyzeEdge(JSCell* from, JSCell* to, SlotVisitor::RootMarkReason reason) final
{
ASSERT(to);
ASSERT(to->vm().heapProfiler()->activeHeapAnalyzer() == this);
auto locker = holdLock(m_mutex);
if (from && from != to) {
m_successors.ensure(from, [] {
return HashSet<JSCell*>();
}).iterator->value.add(to);
m_predecessors.ensure(to, [] {
return HashSet<JSCell*>();
}).iterator->value.add(from);
if (to == m_target)
m_holders.add(from);
}
if (reason == SlotVisitor::RootMarkReason::Debugger)
m_rootsToIgnore.add(to);
else if (!from || reason != SlotVisitor::RootMarkReason::None)
m_rootsToInclude.add(to);
}
void analyzePropertyNameEdge(JSCell* from, JSCell* to, UniquedStringImpl*) final { analyzeEdge(from, to, SlotVisitor::RootMarkReason::None); }
void analyzeVariableNameEdge(JSCell* from, JSCell* to, UniquedStringImpl*) final { analyzeEdge(from, to, SlotVisitor::RootMarkReason::None); }
void analyzeIndexEdge(JSCell* from, JSCell* to, uint32_t) final { analyzeEdge(from, to, SlotVisitor::RootMarkReason::None); }
void analyzeNode(JSCell*) final { }
void setOpaqueRootReachabilityReasonForCell(JSCell*, const char*) final { }
void setWrappedObjectForCell(JSCell*, void*) final { }
void setLabelForCell(JSCell*, const String&) final { }
#ifndef NDEBUG
void dump(PrintStream& out) const
{
Indentation<4> indent;
HashSet<JSCell*> visited;
Function<void(JSCell*)> visit = [&] (auto* from) {
auto isFirstVisit = visited.add(from).isNewEntry;
out.print(makeString(indent));
out.print("[ "_s);
if (from == m_target)
out.print("T "_s);
if (m_holders.contains(from))
out.print("H "_s);
if (m_rootsToIgnore.contains(from))
out.print("- "_s);
else if (m_rootsToInclude.contains(from))
out.print("+ "_s);
if (!isFirstVisit)
out.print("V "_s);
out.print("] "_s);
from->dump(out);
out.println();
if (isFirstVisit) {
IndentationScope<4> scope(indent);
for (auto* to : m_successors.get(from))
visit(to);
}
};
for (auto* from : m_rootsToInclude)
visit(from);
}
#endif
private:
Lock m_mutex;
HashMap<JSCell*, HashSet<JSCell*>> m_predecessors;
HashMap<JSCell*, HashSet<JSCell*>> m_successors;
HashSet<JSCell*> m_rootsToInclude;
HashSet<JSCell*> m_rootsToIgnore;
HashSet<JSCell*> m_holders;
const JSCell* m_target;
};
JSValue JSInjectedScriptHost::queryHolders(JSGlobalObject* globalObject, CallFrame* callFrame)
{
if (callFrame->argumentCount() < 1)
return jsUndefined();
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue target = callFrame->uncheckedArgument(0);
if (!target.isObject())
return throwTypeError(globalObject, scope, "queryHolders first argument must be an object."_s);
JSArray* result = constructEmptyArray(globalObject, nullptr);
RETURN_IF_EXCEPTION(scope, { });
{
DeferGC deferGC(vm.heap);
PreventCollectionScope preventCollectionScope(vm.heap);
sanitizeStackForVM(vm);
HeapHolderFinder holderFinder(vm.ensureHeapProfiler(), target.asCell());
auto holders = copyToVector(holderFinder.holders());
std::sort(holders.begin(), holders.end());
for (auto* holder : holders)
result->putDirectIndex(globalObject, result->length(), holder);
}
return result;
}
2017-08-12 16:48:01 +00:00
} // namespace Inspector