Beispiel #1
0
NS_IMETHODIMP
nsJSONListener::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
                              nsresult aStatusCode)
{
  nsresult rv;

  // This can happen with short UTF-8 messages (<4 bytes)
  if (!mSniffBuffer.IsEmpty()) {
    // Just consume mSniffBuffer
    rv = ProcessBytes(nullptr, 0);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  JS::RootedValue reviver(mCx, JS::NullValue()), value(mCx);

  JS::StableCharPtr chars(reinterpret_cast<const jschar*>(mBufferedChars.Elements()),
                          mBufferedChars.Length());
  JSBool ok = JS_ParseJSONWithReviver(mCx, chars.get(),
                                      uint32_t(mBufferedChars.Length()),
                                      reviver, value.address());

  *mRootVal = value;
  mBufferedChars.TruncateLength(0);
  return ok ? NS_OK : NS_ERROR_FAILURE;
}
template<size_t N> inline bool
TryParse(JSContext *cx, const char (&input)[N], jsval filter)
{
    AutoInflatedString str(cx);
    jsval v;
    str = input;
    CHECK(JS_ParseJSONWithReviver(cx, str.chars(), str.length(), filter, &v));
    CHECK_SAME(v, JSVAL_NULL);
    return true;
}