Skip to content

Commit

Permalink
Bug 1682121 - Add a ToJSValue overload for nsACString. r=hsivonen
Browse files Browse the repository at this point in the history
I wrote this for bug 1682003, but I split it off it because I don't
really _need_ it (I need it on one place where I know I have valid
utf-8).

I think this is a sensible thing to do though. This throws on invalid
utf-8 via:

  https://searchfox.org/mozilla-central/rev/0bcf81557b89e7757c44e25bb4bc7f4cb8619dc9/js/src/vm/CharacterEncoding.cpp#432

This would allow doing stuff like resolving promises using nsCStrings
slightly more conveniently / efficiently than using NS_ConvertUTF*.

Differential Revision: https://phabricator.services.mozilla.com/D99589
  • Loading branch information
emilio committed Dec 14, 2020
1 parent 5e3c951 commit fcbf80b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dom/bindings/ToJSValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ bool ToJSValue(JSContext* aCx, const nsAString& aArgument,
return true;
}

bool ToJSValue(JSContext* aCx, const nsACString& aArgument,
JS::MutableHandle<JS::Value> aValue) {
return UTF8StringToJsval(aCx, aArgument, aValue);
}

bool ToJSValue(JSContext* aCx, nsresult aArgument,
JS::MutableHandle<JS::Value> aValue) {
RefPtr<Exception> exception = CreateException(aArgument);
Expand Down
4 changes: 4 additions & 0 deletions dom/bindings/ToJSValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class TypedArrayCreator;
MOZ_MUST_USE bool ToJSValue(JSContext* aCx, const nsAString& aArgument,
JS::MutableHandle<JS::Value> aValue);

// Treats the input as UTF-8, and throws otherwise.
MOZ_MUST_USE bool ToJSValue(JSContext* aCx, const nsACString& aArgument,
JS::MutableHandle<JS::Value> aValue);

// Accept booleans. But be careful here: if we just have a function that takes
// a boolean argument, then any pointer that doesn't match one of our other
// signatures/templates will get treated as a boolean, which is clearly not
Expand Down

0 comments on commit fcbf80b

Please sign in to comment.