diff options
author | meatloaf <ktulip2@protonmail.com> | 2021-02-03 20:53:17 -0600 |
---|---|---|
committer | meatloaf <ktulip2@protonmail.com> | 2021-02-04 12:59:54 -0600 |
commit | 24c40b583687e6f67ba57e6c8927515ceb5399ce (patch) | |
tree | 8b2a41127db4e432e31609f354ed62b56a5057e0 /js/src/vm | |
parent | c5ad76a2875ca5c06c5bbff7b2f2e3ff7b3599c3 (diff) | |
download | uxp-24c40b583687e6f67ba57e6c8927515ceb5399ce.tar.gz |
Issue #1726 - Implement String.replaceAll()
This also implements IsRegExp, as this demands it.
Ported from https://bugzilla.mozilla.org/show_bug.cgi?id=1540021
Diffstat (limited to 'js/src/vm')
-rw-r--r-- | js/src/vm/SelfHosting.cpp | 66 |
1 files changed, 44 insertions, 22 deletions
diff --git a/js/src/vm/SelfHosting.cpp b/js/src/vm/SelfHosting.cpp index 9c20bbe9df..38785a822c 100644 --- a/js/src/vm/SelfHosting.cpp +++ b/js/src/vm/SelfHosting.cpp @@ -191,6 +191,28 @@ intrinsic_IsInstanceOfBuiltin(JSContext* cx, unsigned argc, Value* vp) template<typename T> static bool +intrinsic_IsPossiblyWrappedBuiltin(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + MOZ_ASSERT(args.length() == 1); + + bool isTypeT = false; + if (args[0].isObject()) { + JSObject* obj = CheckedUnwrap(&args[0].toObject()); + if (!obj) { + JS_ReportErrorASCII(cx, "Permission denied to access object"); + return false; + } + + isTypeT = obj->is<T>(); + } + + args.rval().setBoolean(isTypeT); + return true; +} + +template<typename T> +static bool intrinsic_GuardToBuiltin(JSContext* cx, unsigned argc, Value* vp) { CallArgs args = CallArgsFromVp(argc, vp); @@ -1173,27 +1195,6 @@ intrinsic_IsFloat32TypedArray(JSContext* cx, unsigned argc, Value* vp) } static bool -intrinsic_IsPossiblyWrappedTypedArray(JSContext* cx, unsigned argc, Value* vp) -{ - CallArgs args = CallArgsFromVp(argc, vp); - MOZ_ASSERT(args.length() == 1); - - bool isTypedArray = false; - if (args[0].isObject()) { - JSObject* obj = CheckedUnwrap(&args[0].toObject()); - if (!obj) { - JS_ReportErrorASCII(cx, "Permission denied to access object"); - return false; - } - - isTypedArray = obj->is<TypedArrayObject>(); - } - - args.rval().setBoolean(isTypedArray); - return true; -} - -static bool intrinsic_TypedArrayBuffer(JSContext* cx, unsigned argc, Value* vp) { CallArgs args = CallArgsFromVp(argc, vp); @@ -1756,6 +1757,24 @@ intrinsic_StringReplaceString(JSContext* cx, unsigned argc, Value* vp) return true; } +static bool +intrinsic_StringReplaceAllString(JSContext * cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + MOZ_ASSERT(args.length() == 3); + + RootedString string(cx, args[0].toString()); + RootedString pattern(cx, args[1].toString()); + RootedString replacement(cx, args[2].toString()); + JSString* result = str_replaceAll_string_raw(cx, string, pattern, replacement); + if (!result) { + return false; + } + + args.rval().setString(result); + return true; +} + bool js::intrinsic_StringSplitString(JSContext* cx, unsigned argc, Value* vp) { @@ -2401,7 +2420,7 @@ static const JSFunctionSpec intrinsic_functions[] = { JS_INLINABLE_FN("IsTypedArray", intrinsic_IsInstanceOfBuiltin<TypedArrayObject>, 1,0, IntrinsicIsTypedArray), - JS_INLINABLE_FN("IsPossiblyWrappedTypedArray",intrinsic_IsPossiblyWrappedTypedArray,1,0, + JS_INLINABLE_FN("IsPossiblyWrappedTypedArray",intrinsic_IsPossiblyWrappedBuiltin<TypedArrayObject>,1,0, IntrinsicIsPossiblyWrappedTypedArray), JS_FN("TypedArrayBuffer", intrinsic_TypedArrayBuffer, 1,0), @@ -2513,6 +2532,8 @@ static const JSFunctionSpec intrinsic_functions[] = { JS_INLINABLE_FN("IsRegExpObject", intrinsic_IsInstanceOfBuiltin<RegExpObject>, 1,0, IsRegExpObject), + JS_INLINABLE_FN("IsPossiblyWrappedRegExpObject", intrinsic_IsPossiblyWrappedBuiltin<RegExpObject>,1,0, + IntrinsicIsPossiblyWrappedRegExpObject), JS_FN("CallRegExpMethodIfWrapped", CallNonGenericSelfhostedMethod<Is<RegExpObject>>, 2,0), JS_INLINABLE_FN("RegExpMatcher", RegExpMatcher, 4,0, @@ -2536,6 +2557,7 @@ static const JSFunctionSpec intrinsic_functions[] = { JS_FN("FlatStringSearch", FlatStringSearch, 2,0), JS_INLINABLE_FN("StringReplaceString", intrinsic_StringReplaceString, 3, 0, IntrinsicStringReplaceString), + JS_FN("StringReplaceAllString", intrinsic_StringReplaceAllString, 3, 0), JS_INLINABLE_FN("StringSplitString", intrinsic_StringSplitString, 2, 0, IntrinsicStringSplitString), JS_FN("StringSplitStringLimit", intrinsic_StringSplitStringLimit, 3, 0), |