| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 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/. */ |
| 6 |
|
| 7 |
/* Intl.DurationFormat implementation. */ |
| 8 |
|
| 9 |
#include "builtin/intl/DurationFormat.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/intl/DateTimeFormat.h" |
| 13 |
#include "mozilla/Span.h" |
| 14 |
|
| 15 |
#include "jspubtd.h" |
| 16 |
#include "NamespaceImports.h" |
| 17 |
|
| 18 |
#include "builtin/intl/CommonFunctions.h" |
| 19 |
#include "builtin/intl/FormatBuffer.h" |
| 20 |
#include "builtin/temporal/Duration.h" |
| 21 |
#include "gc/AllocKind.h" |
| 22 |
#include "gc/GCContext.h" |
| 23 |
#include "js/CallArgs.h" |
| 24 |
#include "js/PropertyDescriptor.h" |
| 25 |
#include "js/PropertySpec.h" |
| 26 |
#include "js/RootingAPI.h" |
| 27 |
#include "js/TypeDecls.h" |
| 28 |
#include "js/Value.h" |
| 29 |
#include "vm/GlobalObject.h" |
| 30 |
#include "vm/JSContext.h" |
| 31 |
#include "vm/PlainObject.h" |
| 32 |
#include "vm/SelfHosting.h" |
| 33 |
#include "vm/WellKnownAtom.h" |
| 34 |
|
| 35 |
#include "vm/JSObject-inl.h" |
| 36 |
#include "vm/NativeObject-inl.h" |
| 37 |
|
| 38 |
using namespace js; |
| 39 |
|
| 40 |
const JSClass DurationFormatObject::class_ = { |
| 41 |
"Intl.DurationFormat", |
| 42 |
JSCLASS_HAS_RESERVED_SLOTS(DurationFormatObject::SLOT_COUNT) | |
| 43 |
JSCLASS_HAS_CACHED_PROTO(JSProto_DurationFormat), |
| 44 |
JS_NULL_CLASS_OPS, |
| 45 |
&DurationFormatObject::classSpec_, |
| 46 |
}; |
| 47 |
|
| 48 |
const JSClass& DurationFormatObject::protoClass_ = PlainObject::class_; |
| 49 |
|
| 50 |
static bool durationFormat_toSource(JSContext* cx, unsigned argc, Value* vp) { |
| 51 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 52 |
args.rval().setString(cx->names().DurationFormat); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
static const JSFunctionSpec durationFormat_static_methods[] = { |
| 57 |
JS_SELF_HOSTED_FN("supportedLocalesOf", |
| 58 |
"Intl_DurationFormat_supportedLocalesOf", 1, 0), |
| 59 |
JS_FS_END, |
| 60 |
}; |
| 61 |
|
| 62 |
static const JSFunctionSpec durationFormat_methods[] = { |
| 63 |
JS_SELF_HOSTED_FN("resolvedOptions", "Intl_DurationFormat_resolvedOptions", |
| 64 |
0, 0), |
| 65 |
JS_SELF_HOSTED_FN("format", "Intl_DurationFormat_format", 1, 0), |
| 66 |
JS_SELF_HOSTED_FN("formatToParts", "Intl_DurationFormat_formatToParts", 1, |
| 67 |
0), |
| 68 |
JS_FN("toSource", durationFormat_toSource, 0, 0), |
| 69 |
JS_FS_END, |
| 70 |
}; |
| 71 |
|
| 72 |
static const JSPropertySpec durationFormat_properties[] = { |
| 73 |
JS_STRING_SYM_PS(toStringTag, "Intl.DurationFormat", JSPROP_READONLY), |
| 74 |
JS_PS_END, |
| 75 |
}; |
| 76 |
|
| 77 |
static bool DurationFormat(JSContext* cx, unsigned argc, Value* vp); |
| 78 |
|
| 79 |
const ClassSpec DurationFormatObject::classSpec_ = { |
| 80 |
GenericCreateConstructor<DurationFormat, 0, gc::AllocKind::FUNCTION>, |
| 81 |
GenericCreatePrototype<DurationFormatObject>, |
| 82 |
durationFormat_static_methods, |
| 83 |
nullptr, |
| 84 |
durationFormat_methods, |
| 85 |
durationFormat_properties, |
| 86 |
nullptr, |
| 87 |
ClassSpec::DontDefineConstructor, |
| 88 |
}; |
| 89 |
|
| 90 |
/** |
| 91 |
* Intl.DurationFormat ( [ locales [ , options ] ] ) |
| 92 |
*/ |
| 93 |
static bool DurationFormat(JSContext* cx, unsigned argc, Value* vp) { |
| 94 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 95 |
|
| 96 |
// Step 1. |
| 97 |
if (!ThrowIfNotConstructing(cx, args, "Intl.DurationFormat")) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
// Step 2 (Inlined 9.1.14, OrdinaryCreateFromConstructor). |
| 102 |
RootedObject proto(cx); |
| 103 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_DurationFormat, |
| 104 |
&proto)) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
Rooted<DurationFormatObject*> durationFormat( |
| 109 |
cx, NewObjectWithClassProto<DurationFormatObject>(cx, proto)); |
| 110 |
if (!durationFormat) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
HandleValue locales = args.get(0); |
| 115 |
HandleValue options = args.get(1); |
| 116 |
|
| 117 |
// Steps 3-28. |
| 118 |
if (!intl::InitializeObject(cx, durationFormat, |
| 119 |
cx->names().InitializeDurationFormat, locales, |
| 120 |
options)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
args.rval().setObject(*durationFormat); |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
bool js::intl_GetTimeSeparator(JSContext* cx, unsigned argc, Value* vp) { |
| 129 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 130 |
MOZ_ASSERT(args.length() == 2); |
| 131 |
MOZ_ASSERT(args[0].isString()); |
| 132 |
MOZ_ASSERT(args[1].isString()); |
| 133 |
|
| 134 |
UniqueChars locale = intl::EncodeLocale(cx, args[0].toString()); |
| 135 |
if (!locale) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
UniqueChars numberingSystem = EncodeAscii(cx, args[1].toString()); |
| 140 |
if (!numberingSystem) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
intl::FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> separator(cx); |
| 145 |
auto result = mozilla::intl::DateTimeFormat::GetTimeSeparator( |
| 146 |
mozilla::MakeStringSpan(locale.get()), |
| 147 |
mozilla::MakeStringSpan(numberingSystem.get()), separator); |
| 148 |
if (result.isErr()) { |
| 149 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
JSString* str = separator.toString(cx); |
| 154 |
if (!str) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
args.rval().setString(str); |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
bool js::TemporalDurationToLocaleString(JSContext* cx, |
| 163 |
const JS::CallArgs& args) { |
| 164 |
MOZ_ASSERT(args.thisv().isObject()); |
| 165 |
MOZ_ASSERT(args.thisv().toObject().is<temporal::DurationObject>()); |
| 166 |
|
| 167 |
Rooted<DurationFormatObject*> durationFormat( |
| 168 |
cx, NewBuiltinClassInstance<DurationFormatObject>(cx)); |
| 169 |
if (!durationFormat) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
if (!intl::InitializeObject(cx, durationFormat, |
| 174 |
cx->names().InitializeDurationFormat, args.get(0), |
| 175 |
args.get(1))) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
Rooted<Value> thisv(cx, ObjectValue(*durationFormat)); |
| 180 |
|
| 181 |
FixedInvokeArgs<1> invokeArgs(cx); |
| 182 |
invokeArgs[0].set(args.thisv()); |
| 183 |
|
| 184 |
return CallSelfHostedFunction(cx, cx->names().Intl_DurationFormat_format, |
| 185 |
thisv, invokeArgs, args.rval()); |
| 186 |
} |