Bug 1673965 [wpt PR 26319] - Update interfaces/resize-observer.idl, a=testonly
[gecko.git] / storage / mozStorageStatementRow.cpp
blobf723f1bcb77160ef3538812e15beb6857bdd1429
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMemory.h"
8 #include "nsString.h"
10 #include "mozilla/dom/MozStorageStatementRowBinding.h"
11 #include "mozStorageStatementRow.h"
12 #include "mozStorageStatement.h"
14 #include "jsapi.h"
15 #include "js/Array.h" // JS::NewArrayObject
17 #include "xpc_make_class.h"
19 namespace mozilla {
20 namespace storage {
22 ////////////////////////////////////////////////////////////////////////////////
23 //// StatementRow
25 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementRow, mWindow)
27 NS_INTERFACE_TABLE_HEAD(StatementRow)
28 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
29 NS_INTERFACE_TABLE(StatementRow, nsISupports)
30 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementRow)
31 NS_INTERFACE_MAP_END
33 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementRow)
34 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementRow)
36 StatementRow::StatementRow(nsPIDOMWindowInner* aWindow, Statement* aStatement)
37 : mWindow(aWindow), mStatement(aStatement) {}
39 JSObject* StatementRow::WrapObject(JSContext* aCx,
40 JS::Handle<JSObject*> aGivenProto) {
41 return dom::MozStorageStatementRow_Binding::Wrap(aCx, this, aGivenProto);
44 void StatementRow::NamedGetter(JSContext* aCx, const nsAString& aName,
45 bool& aFound,
46 JS::MutableHandle<JS::Value> aResult,
47 mozilla::ErrorResult& aRv) {
48 if (!mStatement) {
49 aRv.Throw(NS_ERROR_NOT_INITIALIZED);
50 return;
53 nsCString name = NS_ConvertUTF16toUTF8(aName);
55 uint32_t idx;
57 nsresult rv = mStatement->GetColumnIndex(name, &idx);
58 if (NS_FAILED(rv)) {
59 // It's highly likely that the name doesn't exist, so let the JS engine
60 // check the prototype chain and throw if that doesn't have the property
61 // either.
62 aFound = false;
63 return;
67 int32_t type;
68 aRv = mStatement->GetTypeOfIndex(idx, &type);
69 if (aRv.Failed()) {
70 return;
73 switch (type) {
74 case mozIStorageValueArray::VALUE_TYPE_INTEGER:
75 case mozIStorageValueArray::VALUE_TYPE_FLOAT: {
76 double dval;
77 aRv = mStatement->GetDouble(idx, &dval);
78 if (aRv.Failed()) {
79 return;
81 aResult.set(::JS_NumberValue(dval));
82 break;
84 case mozIStorageValueArray::VALUE_TYPE_TEXT: {
85 uint32_t bytes;
86 const char16_t* sval = reinterpret_cast<const char16_t*>(
87 static_cast<mozIStorageStatement*>(mStatement)
88 ->AsSharedWString(idx, &bytes));
89 JSString* str =
90 ::JS_NewUCStringCopyN(aCx, sval, bytes / sizeof(char16_t));
91 if (!str) {
92 aRv.Throw(NS_ERROR_UNEXPECTED);
93 return;
95 aResult.setString(str);
96 break;
98 case mozIStorageValueArray::VALUE_TYPE_BLOB: {
99 uint32_t length;
100 const uint8_t* blob = static_cast<mozIStorageStatement*>(mStatement)
101 ->AsSharedBlob(idx, &length);
102 JS::Rooted<JSObject*> obj(aCx, JS::NewArrayObject(aCx, length));
103 if (!obj) {
104 aRv.Throw(NS_ERROR_UNEXPECTED);
105 return;
107 aResult.setObject(*obj);
109 // Copy the blob over to the JS array.
110 for (uint32_t i = 0; i < length; i++) {
111 if (!::JS_DefineElement(aCx, obj, i, blob[i], JSPROP_ENUMERATE)) {
112 aRv.Throw(NS_ERROR_UNEXPECTED);
113 return;
116 break;
118 case mozIStorageValueArray::VALUE_TYPE_NULL:
119 aResult.setNull();
120 break;
121 default:
122 NS_ERROR("unknown column type returned, what's going on?");
123 break;
125 aFound = true;
128 void StatementRow::GetSupportedNames(nsTArray<nsString>& aNames) {
129 if (!mStatement) {
130 return;
133 uint32_t columnCount;
134 nsresult rv = mStatement->GetColumnCount(&columnCount);
135 if (NS_WARN_IF(NS_FAILED(rv))) {
136 return;
139 for (uint32_t i = 0; i < columnCount; i++) {
140 nsAutoCString name;
141 nsresult rv = mStatement->GetColumnName(i, name);
142 if (NS_WARN_IF(NS_FAILED(rv))) {
143 return;
145 aNames.AppendElement(NS_ConvertUTF8toUTF16(name));
149 } // namespace storage
150 } // namespace mozilla