Bug 1243001 part 8. Tell SpiderMonkey to put its promise jobs into the CycleCollectedJSRuntime job queue. r=peterv

This will run the SpiderMonkey promise jobs more or less the same way that we
run Promise jobs right now, including using a Web IDL callback for the actual
invocation.
This commit is contained in:
Boris Zbarsky 2016-02-09 17:40:31 -05:00
parent 564e7c635a
commit da1ed669ae
4 changed files with 50 additions and 0 deletions

View File

@ -9,6 +9,7 @@
interface DummyInterface {
void lifecycleCallbacks(optional LifecycleCallbacks arg);
void promiseJobCallback(PromiseJobCallback arg);
};
interface DummyInterfaceWorkers {

View File

@ -12,6 +12,8 @@
// function"; for now, we just use "object".
callback PromiseInit = void (object resolve, object reject);
callback PromiseJobCallback = void();
[TreatNonCallableAsNull]
callback AnyCallback = any (any value);

View File

@ -68,6 +68,7 @@
#include "mozilla/dom/DOMJSClass.h"
#include "mozilla/dom/ProfileTimelineMarkerBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseBinding.h"
#include "mozilla/dom/ScriptSettings.h"
#include "jsprf.h"
#include "js/Debug.h"
@ -468,6 +469,10 @@ CycleCollectedJSRuntime::CycleCollectedJSRuntime(JSRuntime* aParentRuntime,
};
SetDOMCallbacks(mJSRuntime, &DOMcallbacks);
#ifdef SPIDERMONKEY_PROMISE
JS::SetEnqueuePromiseJobCallback(mJSRuntime, EnqueuePromiseJobCallback, this);
#endif // SPIDERMONKEY_PROMISE
JS::dbg::SetDebuggerMallocSizeOf(mJSRuntime, moz_malloc_size_of);
nsCycleCollector_registerJSRuntime(this);
@ -864,6 +869,45 @@ CycleCollectedJSRuntime::ContextCallback(JSContext* aContext,
return self->CustomContextCallback(aContext, aOperation);
}
class PromiseJobRunnable final : public nsRunnable
{
public:
PromiseJobRunnable(JSContext* aCx, JS::HandleObject aCallback)
: mCallback(new PromiseJobCallback(aCx, aCallback, nullptr))
{
}
virtual ~PromiseJobRunnable()
{
}
protected:
NS_IMETHOD
Run() override
{
mCallback->Call();
return NS_OK;
}
private:
RefPtr<PromiseJobCallback> mCallback;
};
/* static */
bool
CycleCollectedJSRuntime::EnqueuePromiseJobCallback(JSContext* aCx,
JS::HandleObject aJob,
void* aData)
{
CycleCollectedJSRuntime* self = static_cast<CycleCollectedJSRuntime*>(aData);
MOZ_ASSERT(JS_GetRuntime(aCx) == self->Runtime());
MOZ_ASSERT(Get() == self);
nsCOMPtr<nsIRunnable> runnable = new PromiseJobRunnable(aCx, aJob);
self->GetPromiseMicroTaskQueue().push(runnable);
return true;
}
struct JsGcTracer : public TraceCallbacks
{
virtual void Trace(JS::Heap<JS::Value>* aPtr, const char* aName,

View File

@ -210,6 +210,9 @@ private:
static void LargeAllocationFailureCallback(void* aData);
static bool ContextCallback(JSContext* aCx, unsigned aOperation,
void* aData);
static bool EnqueuePromiseJobCallback(JSContext* aCx,
JS::HandleObject aJob,
void* aData);
virtual void TraceNativeBlackRoots(JSTracer* aTracer) { };
void TraceNativeGrayRoots(JSTracer* aTracer);