Skip to content

Commit

Permalink
Bug 1804073 - Part 4: Add isValidJSON shell testing function. r=bthrall
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-a committed Apr 20, 2023
1 parent 9d83be9 commit 5ff6a72
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions js/src/shell/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8749,6 +8749,33 @@ static bool GetWasmSmithModule(JSContext* cx, unsigned argc, Value* vp) {

#endif

static bool IsValidJSON(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
RootedObject callee(cx, &args.callee());

if (!args.get(0).isString()) {
ReportUsageErrorASCII(cx, callee, "First argument must be a String");
return false;
}

JS::Rooted<JSLinearString*> input(cx, args[0].toString()->ensureLinear(cx));
if (!input) {
return false;
}

bool result;
if (input->hasLatin1Chars()) {
JS::AutoCheckCannotGC nogc;
result = JS::IsValidJSON(input->latin1Chars(nogc), input->length());
} else {
JS::AutoCheckCannotGC nogc;
result = JS::IsValidJSON(input->twoByteChars(nogc), input->length());
}

args.rval().setBoolean(result);
return true;
}

// clang-format off
static const JSFunctionSpecWithHelp shell_functions[] = {
JS_FN_HELP("options", Options, 0, 0,
Expand Down Expand Up @@ -9408,6 +9435,10 @@ static const JSFunctionSpecWithHelp shell_functions[] = {
" Call wasm-smith to generate a random wasm module from the provided data."),
#endif

JS_FN_HELP("isValidJSON", IsValidJSON, 1, 0,
"isValidJSON(source)",
" Returns true if the given source is valid JSON."),

JS_FS_HELP_END
};
// clang-format on
Expand Down

0 comments on commit 5ff6a72

Please sign in to comment.