Bug 1455217 - Part 2: Add support for promises to XPCConvert, r=mccr8

This commit is contained in:
Nika Layzell 2018-04-30 15:54:52 -04:00
parent c9d0fe3dd2
commit 9c090cb316
2 changed files with 45 additions and 0 deletions

View File

@ -355,6 +355,22 @@ XPCConvert::NativeData2JS(MutableHandleValue d, const void* s,
return type.GetDOMObjectInfo().Wrap(cx, ptr, d);
}
case nsXPTType::T_PROMISE:
{
Promise* promise = *static_cast<Promise* const*>(s);
if (!promise) {
d.setNull();
return true;
}
RootedObject jsobj(cx, promise->PromiseObj());
if (!JS_WrapObject(cx, &jsobj)) {
return false;
}
d.setObject(*jsobj);
return true;
}
default:
NS_ERROR("bad type");
return false;
@ -736,6 +752,29 @@ XPCConvert::JSData2Native(void* d, HandleValue s,
return NS_SUCCEEDED(err);
}
case nsXPTType::T_PROMISE:
{
nsIGlobalObject* glob = NativeGlobal(CurrentGlobalOrNull(cx));
if (!glob) {
if (pErr) {
*pErr = NS_ERROR_UNEXPECTED;
}
return false;
}
// Call Promise::Resolve to create a Promise object. This allows us to
// support returning non-promise values from Promise-returning functions
// in JS.
IgnoredErrorResult err;
*(Promise**)d = Promise::Resolve(glob, cx, s, err).take();
bool ok = !err.Failed();
if (pErr) {
*pErr = err.StealNSResult();
}
return ok;
}
default:
NS_ERROR("bad type");
return false;
@ -796,6 +835,7 @@ XPCConvert::NativeInterface2JSObject(MutableHandleValue d,
return true;
}
// NOTE(nika): Remove if Promise becomes non-nsISupports
if (iid->Equals(NS_GET_IID(nsISupports))) {
// Check for a Promise being returned via nsISupports. In that
// situation, we want to dig out its underlying JS object and return
@ -937,6 +977,7 @@ XPCConvert::JSObject2NativeInterface(void** dest, HandleObject src,
return false;
}
// NOTE(nika): Remove if Promise becomes non-nsISupports
// Deal with Promises being passed as nsISupports. In that situation we
// want to create a dom::Promise and use that.
if (iid->Equals(NS_GET_IID(nsISupports))) {
@ -1293,6 +1334,7 @@ XPCConvert::NativeArray2JS(MutableHandleValue d, const void** s,
case nsXPTType::T_INTERFACE : POPULATE(nsISupports*); break;
case nsXPTType::T_INTERFACE_IS : POPULATE(nsISupports*); break;
case nsXPTType::T_DOMOBJECT : POPULATE(void*); break;
case nsXPTType::T_PROMISE : POPULATE(Promise*); break;
case nsXPTType::T_UTF8STRING : NS_ERROR("bad type"); return false;
case nsXPTType::T_CSTRING : NS_ERROR("bad type"); return false;
case nsXPTType::T_ASTRING : NS_ERROR("bad type"); return false;
@ -1557,6 +1599,7 @@ XPCConvert::JSArray2Native(void** d, HandleValue s,
} \
PR_END_MACRO
// NOTE(nika): Add a cleanup mode if Promise becomes non-nsISupports.
// No Action, FRee memory, RElease object, CLeanup object
enum CleanupMode {na, fr, re, cl};
@ -1591,6 +1634,7 @@ XPCConvert::JSArray2Native(void** d, HandleValue s,
case nsXPTType::T_INTERFACE : POPULATE(re, nsISupports*); break;
case nsXPTType::T_INTERFACE_IS : POPULATE(re, nsISupports*); break;
case nsXPTType::T_DOMOBJECT : POPULATE(cl, void*); break;
case nsXPTType::T_PROMISE : POPULATE(re, Promise*); break;
case nsXPTType::T_UTF8STRING : NS_ERROR("bad type"); goto failure;
case nsXPTType::T_CSTRING : NS_ERROR("bad type"); goto failure;
case nsXPTType::T_ASTRING : NS_ERROR("bad type"); goto failure;

View File

@ -1898,6 +1898,7 @@ CallMethodHelper::CleanupParam(nsXPTCMiniVariant& param, nsXPTType& type)
case nsXPTType::T_JSVAL:
js::RemoveRawValueRoot(mCallContext, &param.val.j);
break;
case nsXPTType::T_PROMISE: // NOTE(nika): Change if Promise becomes non-nsISupports
case nsXPTType::T_INTERFACE:
case nsXPTType::T_INTERFACE_IS:
((nsISupports*)param.val.p)->Release();