X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..12899fa232562c774004a3a9d7d3149944dec712:/API/JSContextRef.cpp diff --git a/API/JSContextRef.cpp b/API/JSContextRef.cpp index c331179..3869e87 100644 --- a/API/JSContextRef.cpp +++ b/API/JSContextRef.cpp @@ -25,16 +25,22 @@ #include "config.h" #include "JSContextRef.h" +#include "JSContextRefPrivate.h" #include "APICast.h" #include "InitializeThreading.h" +#include +#include #include "JSCallbackObject.h" #include "JSClassRef.h" #include "JSGlobalObject.h" #include "JSObject.h" -#include +#include "Operations.h" +#include "SourceProvider.h" +#include +#include -#if PLATFORM(DARWIN) +#if OS(DARWIN) #include static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0 @@ -42,10 +48,15 @@ static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; using namespace JSC; +// From the API's perspective, a context group remains alive iff +// (a) it has been JSContextGroupRetained +// OR +// (b) one of its contexts has been JSContextRetained + JSContextGroupRef JSContextGroupCreate() { initializeThreading(); - return toRef(JSGlobalData::create().releaseRef()); + return toRef(VM::createContextGroup().leakRef()); } JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group) @@ -56,24 +67,59 @@ JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group) void JSContextGroupRelease(JSContextGroupRef group) { - toJS(group)->deref(); + IdentifierTable* savedIdentifierTable; + VM& vm = *toJS(group); + + { + JSLockHolder lock(vm); + savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(vm.identifierTable); + vm.deref(); + } + + wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable); } +static bool internalScriptTimeoutCallback(ExecState* exec, void* callbackPtr, void* callbackData) +{ + JSShouldTerminateCallback callback = reinterpret_cast(callbackPtr); + JSContextRef contextRef = toRef(exec); + ASSERT(callback); + return callback(contextRef, callbackData); +} + +void JSContextGroupSetExecutionTimeLimit(JSContextGroupRef group, double limit, JSShouldTerminateCallback callback, void* callbackData) +{ + VM& vm = *toJS(group); + APIEntryShim entryShim(&vm); + Watchdog& watchdog = vm.watchdog; + if (callback) { + void* callbackPtr = reinterpret_cast(callback); + watchdog.setTimeLimit(vm, limit, internalScriptTimeoutCallback, callbackPtr, callbackData); + } else + watchdog.setTimeLimit(vm, limit); +} + +void JSContextGroupClearExecutionTimeLimit(JSContextGroupRef group) +{ + VM& vm = *toJS(group); + APIEntryShim entryShim(&vm); + Watchdog& watchdog = vm.watchdog; + watchdog.setTimeLimit(vm, std::numeric_limits::infinity()); +} + +// From the API's perspective, a global context remains alive iff it has been JSGlobalContextRetained. + JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass) { initializeThreading(); -#if PLATFORM(DARWIN) - // When running on Tiger or Leopard, or if the application was linked before JSGlobalContextCreate was changed - // to use a unique JSGlobalData, we use a shared one for compatibility. -#if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) + +#if OS(DARWIN) + // If the application was linked before JSGlobalContextCreate was changed to use a unique VM, + // we use a shared one for backwards compatibility. if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts) { -#else - { -#endif - JSLock lock(true); - return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass); + return JSGlobalContextCreateInGroup(toRef(&VM::sharedInstance()), globalObjectClass); } -#endif // PLATFORM(DARWIN) +#endif // OS(DARWIN) return JSGlobalContextCreateInGroup(0, globalObjectClass); } @@ -82,73 +128,134 @@ JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClass { initializeThreading(); - JSLock lock(true); + RefPtr vm = group ? PassRefPtr(toJS(group)) : VM::createContextGroup(); - RefPtr globalData = group ? PassRefPtr(toJS(group)) : JSGlobalData::create(); - -#if ENABLE(JSC_MULTIPLE_THREADS) - globalData->makeUsableFromMultipleThreads(); -#endif + APIEntryShim entryShim(vm.get(), false); + vm->makeUsableFromMultipleThreads(); if (!globalObjectClass) { - JSGlobalObject* globalObject = new (globalData.get()) JSGlobalObject; + JSGlobalObject* globalObject = JSGlobalObject::create(*vm, JSGlobalObject::createStructure(*vm, jsNull())); return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec())); } - JSGlobalObject* globalObject = new (globalData.get()) JSCallbackObject(globalObjectClass); + JSGlobalObject* globalObject = JSCallbackObject::create(*vm, globalObjectClass, JSCallbackObject::createStructure(*vm, 0, jsNull())); ExecState* exec = globalObject->globalExec(); - JSValuePtr prototype = globalObjectClass->prototype(exec); + JSValue prototype = globalObjectClass->prototype(exec); if (!prototype) prototype = jsNull(); - globalObject->resetPrototype(prototype); + globalObject->resetPrototype(*vm, prototype); return JSGlobalContextRetain(toGlobalRef(exec)); } JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx) { ExecState* exec = toJS(ctx); - JSLock lock(exec); - - JSGlobalData& globalData = exec->globalData(); - - globalData.heap.registerThread(); + APIEntryShim entryShim(exec); + VM& vm = exec->vm(); gcProtect(exec->dynamicGlobalObject()); - globalData.ref(); + vm.ref(); return ctx; } void JSGlobalContextRelease(JSGlobalContextRef ctx) { + IdentifierTable* savedIdentifierTable; ExecState* exec = toJS(ctx); - JSLock lock(exec); + { + JSLockHolder lock(exec); - gcUnprotect(exec->dynamicGlobalObject()); + VM& vm = exec->vm(); + savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(vm.identifierTable); - JSGlobalData& globalData = exec->globalData(); - if (globalData.refCount() == 2) { // One reference is held by JSGlobalObject, another added by JSGlobalContextRetain(). - // The last reference was released, this is our last chance to collect. - ASSERT(!globalData.heap.protectedObjectCount()); - ASSERT(!globalData.heap.isBusy()); - globalData.heap.destroy(); - } else - globalData.heap.collect(); + bool protectCountIsZero = Heap::heap(exec->dynamicGlobalObject())->unprotect(exec->dynamicGlobalObject()); + if (protectCountIsZero) + vm.heap.reportAbandonedObjectGraph(); + vm.deref(); + } - globalData.deref(); + wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable); } JSObjectRef JSContextGetGlobalObject(JSContextRef ctx) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); // It is necessary to call toThisObject to get the wrapper object when used with WebCore. - return toRef(exec->lexicalGlobalObject()->toThisObject(exec)); + return toRef(exec->lexicalGlobalObject()->methodTable()->toThisObject(exec->lexicalGlobalObject(), exec)); } JSContextGroupRef JSContextGetGroup(JSContextRef ctx) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - return toRef(&exec->globalData()); + return toRef(&exec->vm()); } + +JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx) +{ + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + return toGlobalRef(exec->lexicalGlobalObject()->globalExec()); +} + +JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize) +{ + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + JSLockHolder lock(exec); + StringBuilder builder; + Vector stackTrace; + Interpreter::getStackTrace(&exec->vm(), stackTrace, maxStackSize); + + for (size_t i = 0; i < stackTrace.size(); i++) { + String urlString; + String functionName; + StackFrame& frame = stackTrace[i]; + JSValue function = frame.callee.get(); + if (frame.callee) + functionName = frame.friendlyFunctionName(exec); + else { + // Caller is unknown, but if frame is empty we should still add the frame, because + // something called us, and gave us arguments. + if (i) + break; + } + unsigned lineNumber; + unsigned column; + frame.computeLineAndColumn(lineNumber, column); + if (!builder.isEmpty()) + builder.append('\n'); + builder.append('#'); + builder.appendNumber(i); + builder.append(' '); + builder.append(functionName); + builder.appendLiteral("() at "); + builder.append(urlString); + if (frame.codeType != StackFrameNativeCode) { + builder.append(':'); + builder.appendNumber(lineNumber); + } + if (!function) + break; + } + return OpaqueJSString::create(builder.toString()).leakRef(); +} + +