summaryrefslogtreecommitdiffstats
path: root/dom/bindings/test/TestFunctions.cpp
blob: f05c92b48d50461b999d8579ee0476176ffd8ddb (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/TestFunctions.h"
#include "mozilla/dom/TestFunctionsBinding.h"
#include "nsStringBuffer.h"

namespace mozilla {
namespace dom {

/* static */ TestFunctions*
TestFunctions::Constructor(GlobalObject& aGlobal, ErrorResult& aRv)
{
  return new TestFunctions;
}

/* static */ void
TestFunctions::ThrowUncatchableException(GlobalObject& aGlobal,
                                         ErrorResult& aRv)
{
  aRv.ThrowUncatchableException();
}

/* static */ Promise*
TestFunctions::PassThroughPromise(GlobalObject& aGlobal, Promise& aPromise)
{
  return &aPromise;
}

/* static */ already_AddRefed<Promise>
TestFunctions::PassThroughCallbackPromise(GlobalObject& aGlobal,
                                          PromiseReturner& aCallback,
                                          ErrorResult& aRv)
{
  return aCallback.Call(aRv);
}

void
TestFunctions::SetStringData(const nsAString& aString)
{
  mStringData = aString;
}

void
TestFunctions::GetStringDataAsAString(nsAString& aString)
{
  aString = mStringData;
}

void
TestFunctions::GetStringDataAsAString(uint32_t aLength, nsAString& aString)
{
  MOZ_RELEASE_ASSERT(aLength <= mStringData.Length(),
                     "Bogus test passing in a too-big length");
  aString.Assign(mStringData.BeginReading(), aLength);
}

void
TestFunctions::GetStringDataAsDOMString(const Optional<uint32_t>& aLength,
                                        DOMString& aString)
{
  uint32_t length;
  if (aLength.WasPassed()) {
    length = aLength.Value();
    MOZ_RELEASE_ASSERT(length <= mStringData.Length(),
                       "Bogus test passing in a too-big length");
  } else {
    length = mStringData.Length();
  }

  nsStringBuffer* buf = nsStringBuffer::FromString(mStringData);
  if (buf) {
    aString.SetStringBuffer(buf, length);
    return;
  }

  // We better have an empty mStringData; otherwise why did we not have a string
  // buffer?
  MOZ_RELEASE_ASSERT(length == 0, "Why no stringbuffer?");
  // No need to do anything here; aString is already empty.
}

bool
TestFunctions::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
                          JS::MutableHandle<JSObject*> aWrapper)
{
  return TestFunctionsBinding::Wrap(aCx, this, aGivenProto, aWrapper);
}

}
}