/src/mozilla-central/toolkit/components/mozintl/MozIntlHelper.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "MozIntlHelper.h" |
7 | | #include "jsapi.h" |
8 | | #include "js/Wrapper.h" |
9 | | #include "mozilla/ModuleUtils.h" |
10 | | |
11 | | #define MOZ_MOZINTLHELPER_CID \ |
12 | | { 0xb43c96be, 0x2b3a, 0x4dc4, { 0x90, 0xe9, 0xb0, 0x6d, 0x34, 0x21, 0x9b, 0x68 } } |
13 | | |
14 | | using namespace mozilla; |
15 | | |
16 | | NS_IMPL_ISUPPORTS(MozIntlHelper, mozIMozIntlHelper) |
17 | | |
18 | 0 | MozIntlHelper::MozIntlHelper() = default; |
19 | | |
20 | 0 | MozIntlHelper::~MozIntlHelper() = default; |
21 | | |
22 | | static nsresult |
23 | | AddFunctions(JSContext* cx, JS::Handle<JS::Value> val, const JSFunctionSpec* funcs) |
24 | 0 | { |
25 | 0 | if (!val.isObject()) { |
26 | 0 | return NS_ERROR_INVALID_ARG; |
27 | 0 | } |
28 | 0 | |
29 | 0 | JS::Rooted<JSObject*> realIntlObj(cx, js::CheckedUnwrap(&val.toObject())); |
30 | 0 | if (!realIntlObj) { |
31 | 0 | return NS_ERROR_INVALID_ARG; |
32 | 0 | } |
33 | 0 | |
34 | 0 | JSAutoRealm ar(cx, realIntlObj); |
35 | 0 |
|
36 | 0 | if (!JS_DefineFunctions(cx, realIntlObj, funcs)) { |
37 | 0 | return NS_ERROR_FAILURE; |
38 | 0 | } |
39 | 0 | |
40 | 0 | return NS_OK; |
41 | 0 | } |
42 | | |
43 | | NS_IMETHODIMP |
44 | | MozIntlHelper::AddGetCalendarInfo(JS::Handle<JS::Value> val, JSContext* cx) |
45 | 0 | { |
46 | 0 | static const JSFunctionSpec funcs[] = { |
47 | 0 | JS_SELF_HOSTED_FN("getCalendarInfo", "Intl_getCalendarInfo", 1, 0), |
48 | 0 | JS_FS_END |
49 | 0 | }; |
50 | 0 |
|
51 | 0 | return AddFunctions(cx, val, funcs); |
52 | 0 | } |
53 | | |
54 | | NS_IMETHODIMP |
55 | | MozIntlHelper::AddGetDisplayNames(JS::Handle<JS::Value> val, JSContext* cx) |
56 | 0 | { |
57 | 0 | static const JSFunctionSpec funcs[] = { |
58 | 0 | JS_SELF_HOSTED_FN("getDisplayNames", "Intl_getDisplayNames", 2, 0), |
59 | 0 | JS_FS_END |
60 | 0 | }; |
61 | 0 |
|
62 | 0 | return AddFunctions(cx, val, funcs); |
63 | 0 | } |
64 | | |
65 | | NS_IMETHODIMP |
66 | | MozIntlHelper::AddDateTimeFormatConstructor(JS::Handle<JS::Value> val, JSContext* cx) |
67 | 0 | { |
68 | 0 | if (!val.isObject()) { |
69 | 0 | return NS_ERROR_INVALID_ARG; |
70 | 0 | } |
71 | 0 | |
72 | 0 | JS::Rooted<JSObject*> realIntlObj(cx, js::CheckedUnwrap(&val.toObject())); |
73 | 0 | if (!realIntlObj) { |
74 | 0 | return NS_ERROR_INVALID_ARG; |
75 | 0 | } |
76 | 0 | |
77 | 0 | JSAutoRealm ar(cx, realIntlObj); |
78 | 0 |
|
79 | 0 | if (!js::AddMozDateTimeFormatConstructor(cx, realIntlObj)) { |
80 | 0 | return NS_ERROR_FAILURE; |
81 | 0 | } |
82 | 0 | |
83 | 0 | return NS_OK; |
84 | 0 | } |
85 | | |
86 | | NS_IMETHODIMP |
87 | | MozIntlHelper::AddRelativeTimeFormatConstructor(JS::Handle<JS::Value> val, JSContext* cx) |
88 | 0 | { |
89 | 0 | if (!val.isObject()) { |
90 | 0 | return NS_ERROR_INVALID_ARG; |
91 | 0 | } |
92 | 0 | |
93 | 0 | JS::Rooted<JSObject*> realIntlObj(cx, js::CheckedUnwrap(&val.toObject())); |
94 | 0 | if (!realIntlObj) { |
95 | 0 | return NS_ERROR_INVALID_ARG; |
96 | 0 | } |
97 | 0 | |
98 | 0 | JSAutoRealm ar(cx, realIntlObj); |
99 | 0 |
|
100 | 0 | if (!js::AddRelativeTimeFormatConstructor(cx, realIntlObj)) { |
101 | 0 | return NS_ERROR_FAILURE; |
102 | 0 | } |
103 | 0 | |
104 | 0 | return NS_OK; |
105 | 0 | } |
106 | | |
107 | | NS_IMETHODIMP |
108 | | MozIntlHelper::AddGetLocaleInfo(JS::Handle<JS::Value> val, JSContext* cx) |
109 | 0 | { |
110 | 0 | static const JSFunctionSpec funcs[] = { |
111 | 0 | JS_SELF_HOSTED_FN("getLocaleInfo", "Intl_getLocaleInfo", 1, 0), |
112 | 0 | JS_FS_END |
113 | 0 | }; |
114 | 0 |
|
115 | 0 | return AddFunctions(cx, val, funcs); |
116 | 0 | } |
117 | | |
118 | | NS_GENERIC_FACTORY_CONSTRUCTOR(MozIntlHelper) |
119 | | NS_DEFINE_NAMED_CID(MOZ_MOZINTLHELPER_CID); |
120 | | |
121 | | static const Module::CIDEntry kMozIntlHelperCIDs[] = { |
122 | | { &kMOZ_MOZINTLHELPER_CID, false, nullptr, MozIntlHelperConstructor }, |
123 | | { nullptr } |
124 | | }; |
125 | | |
126 | | static const mozilla::Module::ContractIDEntry kMozIntlHelperContracts[] = { |
127 | | { "@mozilla.org/mozintlhelper;1", &kMOZ_MOZINTLHELPER_CID }, |
128 | | { nullptr } |
129 | | }; |
130 | | |
131 | | static const mozilla::Module kMozIntlHelperModule = { |
132 | | mozilla::Module::kVersion, |
133 | | kMozIntlHelperCIDs, |
134 | | kMozIntlHelperContracts, |
135 | | nullptr, |
136 | | nullptr, |
137 | | nullptr, |
138 | | nullptr |
139 | | }; |
140 | | |
141 | | NSMODULE_DEFN(mozMozIntlHelperModule) = &kMozIntlHelperModule; |