Skip to content

Commit

Permalink
Add TextEncoder.prototype.encoding property
Browse files Browse the repository at this point in the history
Summary: Adds the read-only `encoding` property to TextEncoder

Reviewed By: avp

Differential Revision: D53212867

fbshipit-source-id: 010875ff359aaaa8e84a185c8d716bc4689a1538
  • Loading branch information
dannysu authored and facebook-github-bot committed Feb 17, 2024
1 parent 8fb0496 commit 14790c9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/hermes/VM/InternalProperties.def
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ NAMED_PROP(IntlNativeType)
NAMED_PROP(NativeState)
NAMED_PROP(ArrayBufferExternalFinalizer)
NAMED_PROP(ExternalMemoryPressure)
NAMED_PROP(TextEncoderType)

#undef PROP
#undef NAMED_PROP
1 change: 1 addition & 0 deletions include/hermes/VM/NativeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ NATIVE_FUNCTION(symbolPrototypeToString)
NATIVE_FUNCTION(symbolPrototypeDescriptionGetter)
NATIVE_FUNCTION(symbolPrototypeValueOf)
NATIVE_FUNCTION(textEncoderConstructor)
NATIVE_FUNCTION(textEncoderPrototypeEncoding)
NATIVE_FUNCTION(throwTypeError)
NATIVE_FUNCTION(typedArrayBaseConstructor)
NATIVE_FUNCTION(typedArrayFrom)
Expand Down
2 changes: 2 additions & 0 deletions include/hermes/VM/PredefinedStrings.def
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ STR(squareSymbolReplace, "[Symbol.replace]")
STR(squareSymbolSplit, "[Symbol.split]")

STR(TextEncoder, "TextEncoder")
STR(encoding, "encoding")
STR(utf8, "utf-8")

#ifdef HERMES_ENABLE_INTL
// TODO T65916424: Consider how we can move these out of the
Expand Down
48 changes: 48 additions & 0 deletions lib/VM/JSLib/TextEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ Handle<JSObject> createTextEncoderConstructor(Runtime &runtime) {
runtime.getPredefinedStringHandle(Predefined::TextEncoder),
dpf);

// Based on
// Object.getOwnPropertyDescriptor(TextEncoder.prototype, 'encoding'), both
// Chrome and Safari have the 'encoding' property as enumerable and
// configurable. We set things up to be the same.
defineAccessor(
runtime,
textEncoderPrototype,
Predefined::getSymbolID(Predefined::encoding),
nullptr,
textEncoderPrototypeEncoding,
nullptr,
/* enumerable */ true,
/* configurable */ true);

auto cons = defineSystemConstructor<JSObject>(
runtime,
Predefined::getSymbolID(Predefined::TextEncoder),
Expand Down Expand Up @@ -52,8 +66,42 @@ textEncoderConstructor(void *, Runtime &runtime, NativeArgs args) {
}

auto selfHandle = args.vmcastThis<JSObject>();

auto valueHandle = Runtime::getUndefinedValue();
if (LLVM_UNLIKELY(
JSObject::defineNewOwnProperty(
selfHandle,
runtime,
Predefined::getSymbolID(
Predefined::InternalPropertyTextEncoderType),
PropertyFlags::defaultNewNamedPropertyFlags(),
valueHandle) == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}

return selfHandle.getHermesValue();
}

CallResult<HermesValue>
textEncoderPrototypeEncoding(void *, Runtime &runtime, NativeArgs args) {
GCScope gcScope{runtime};

auto selfHandle = args.dyncastThis<JSObject>();

NamedPropertyDescriptor desc;
bool exists = JSObject::getOwnNamedDescriptor(
selfHandle,
runtime,
Predefined::getSymbolID(Predefined::InternalPropertyTextEncoderType),
desc);
if (LLVM_UNLIKELY(!exists)) {
return runtime.raiseTypeError(
"TextEncoder.prototype.encoding called on non-TextEncoder object");
}

return HermesValue::encodeStringValue(
runtime.getPredefinedString(Predefined::utf8));
}

} // namespace vm
} // namespace hermes
9 changes: 9 additions & 0 deletions test/hermes/text-encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ print('TextEncoder');
var encoder = new TextEncoder();
print(Object.prototype.toString.call(encoder));
// CHECK-NEXT: [object TextEncoder]

const desc = Object.getOwnPropertyDescriptor(TextEncoder.prototype, 'encoding');
print(desc.enumerable);
// CHECK-NEXT: true
print(desc.configurable);
// CHECK-NEXT: true

print(encoder.encoding);
// CHECK-NEXT: utf-8

0 comments on commit 14790c9

Please sign in to comment.