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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* Copyright (C) 2006, 2007, 2013, 2016 Apple Inc. All rights reserved.
*
* 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 "JSBase.h"
#include "JSBaseInternal.h"
#include "JSBasePrivate.h"
#include "APICast.h"
#include "Completion.h"
#include "GCActivityCallback.h"
#include "JSCInlines.h"
#include "JSLock.h"
#include "ObjectConstructor.h"
#include "OpaqueJSString.h"
#include "SourceCode.h"
#if ENABLE(REMOTE_INSPECTOR)
#include "JSGlobalObjectInspectorController.h"
#endif
using namespace JSC;
JSValueRef JSEvaluateScriptInternal(const JSLockHolder&, JSContextRef ctx, JSObjectRef thisObject, const SourceCode& source, JSValueRef* exception)
{
JSObject* jsThisObject = toJS(thisObject);
// evaluate sets "this" to the global object if it is NULL
JSGlobalObject* globalObject = toJS(ctx);
NakedPtr<Exception> evaluationException;
JSValue returnValue = profiledEvaluate(globalObject, ProfilingReason::API, source, jsThisObject, evaluationException);
if (evaluationException) {
if (exception)
*exception = toRef(globalObject, evaluationException->value());
#if ENABLE(REMOTE_INSPECTOR)
// FIXME: If we have a debugger attached we could learn about ParseError exceptions through
// ScriptDebugServer::sourceParsed and this path could produce a duplicate warning. The
// Debugger path is currently ignored by inspector.
// NOTE: If we don't have a debugger, this SourceCode will be forever lost to the inspector.
// We could stash it in the inspector in case an inspector is ever opened.
globalObject->inspectorController().reportAPIException(globalObject, evaluationException);
#endif
return nullptr;
}
if (returnValue)
return toRef(globalObject, returnValue);
// happens, for example, when the only statement is an empty (';') statement
return toRef(globalObject, jsUndefined());
}
JSValueRef JSEvaluateScript(JSContextRef ctx, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURLString, int startingLineNumber, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
startingLineNumber = std::max(1, startingLineNumber);
auto sourceURL = sourceURLString ? URL({ }, sourceURLString->string()) : URL();
SourceCode source = makeSource(script->string(), SourceOrigin { sourceURL }, SourceTaintedOrigin::Untainted, sourceURL.string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber()));
return JSEvaluateScriptInternal(locker, ctx, thisObject, source, exception);
}
bool JSCheckScriptSyntax(JSContextRef ctx, JSStringRef script, JSStringRef sourceURLString, int startingLineNumber, JSValueRef* exception)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return false;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
startingLineNumber = std::max(1, startingLineNumber);
auto sourceURL = sourceURLString ? URL({ }, sourceURLString->string()) : URL();
SourceCode source = makeSource(script->string(), SourceOrigin { sourceURL }, SourceTaintedOrigin::Untainted, sourceURL.string(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber()));
JSValue syntaxException;
bool isValidSyntax = checkSyntax(globalObject, source, &syntaxException);
if (!isValidSyntax) {
if (exception)
*exception = toRef(globalObject, syntaxException);
#if ENABLE(REMOTE_INSPECTOR)
Exception* exception = Exception::create(vm, syntaxException);
globalObject->inspectorController().reportAPIException(globalObject, exception);
#endif
return false;
}
return true;
}
void JSGarbageCollect(JSContextRef ctx)
{
// We used to recommend passing NULL as an argument here, which caused the only heap to be collected.
// As there is no longer a shared heap, the previously recommended usage became a no-op (but the GC
// will happen when the context group is destroyed).
// Because the function argument was originally ignored, some clients may pass their released context here,
// in which case there is a risk of crashing if another thread performs GC on the same heap in between.
if (!ctx)
return;
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
vm.heap.reportAbandonedObjectGraph();
}
void JSReportExtraMemoryCost(JSContextRef ctx, size_t size)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
vm.heap.deprecatedReportExtraMemory(size);
}
extern "C" JS_EXPORT void JSSynchronousGarbageCollectForDebugging(JSContextRef);
extern "C" JS_EXPORT void JSSynchronousEdenCollectForDebugging(JSContextRef);
void JSSynchronousGarbageCollectForDebugging(JSContextRef ctx)
{
if (!ctx)
return;
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
vm.heap.collectNow(Sync, CollectionScope::Full);
}
void JSSynchronousEdenCollectForDebugging(JSContextRef ctx)
{
if (!ctx)
return;
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
vm.heap.collectSync(CollectionScope::Eden);
}
void JSDisableGCTimer(void)
{
GCActivityCallback::s_shouldCreateGCTimer = false;
}
#if !OS(DARWIN) && !OS(WINDOWS)
bool JSConfigureSignalForGC(int signal)
{
if (g_wtfConfig.isThreadSuspendResumeSignalConfigured)
return false;
g_wtfConfig.sigThreadSuspendResume = signal;
g_wtfConfig.isUserSpecifiedThreadSuspendResumeSignalConfigured = true;
return true;
}
#endif
JSObjectRef JSGetMemoryUsageStatistics(JSContextRef ctx)
{
if (!ctx) {
ASSERT_NOT_REACHED();
return nullptr;
}
JSGlobalObject* globalObject = toJS(ctx);
VM& vm = globalObject->vm();
JSLockHolder locker(vm);
auto typeCounts = vm.heap.objectTypeCounts();
JSObject* objectTypeCounts = constructEmptyObject(globalObject);
for (auto& it : *typeCounts)
objectTypeCounts->putDirect(vm, Identifier::fromString(vm, it.key), jsNumber(it.value));
JSObject* object = constructEmptyObject(globalObject);
object->putDirect(vm, Identifier::fromString(vm, "heapSize"_s), jsNumber(vm.heap.size()));
object->putDirect(vm, Identifier::fromString(vm, "heapCapacity"_s), jsNumber(vm.heap.capacity()));
object->putDirect(vm, Identifier::fromString(vm, "extraMemorySize"_s), jsNumber(vm.heap.extraMemorySize()));
object->putDirect(vm, Identifier::fromString(vm, "objectCount"_s), jsNumber(vm.heap.objectCount()));
object->putDirect(vm, Identifier::fromString(vm, "protectedObjectCount"_s), jsNumber(vm.heap.protectedObjectCount()));
object->putDirect(vm, Identifier::fromString(vm, "globalObjectCount"_s), jsNumber(vm.heap.globalObjectCount()));
object->putDirect(vm, Identifier::fromString(vm, "protectedGlobalObjectCount"_s), jsNumber(vm.heap.protectedGlobalObjectCount()));
object->putDirect(vm, Identifier::fromString(vm, "objectTypeCounts"_s), objectTypeCounts);
return toRef(object);
}
|