Skip to content

Commit

Permalink
Improve ThrowError cookbook example using JS::CreateError
Browse files Browse the repository at this point in the history
  • Loading branch information
hotsphink committed Jun 2, 2024
1 parent 92208a7 commit 7564c5f
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions examples/cookbook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,11 @@ static bool ThrowError(JSContext* cx, JS::HandleObject global,
JS::RootedString filenameStr(cx, JS_NewStringCopyZ(cx, filename));
if (!filenameStr) return false;

JS::RootedValueArray<3> args(cx);
args[0].setString(messageStr);
args[1].setString(filenameStr);
args[2].setInt32(lineno);
JS::RootedValue exc(cx);
// The JSAPI code here is actually simulating `throw Error(message)` without
// the new, as new is a bit harder to simulate using the JSAPI. In this case,
// unless the script has redefined Error, it amounts to the same thing.
if (!JS_CallFunctionName(cx, global, "Error", args, &exc)) return false;
JS::Rooted<JS::Value> exc(cx);
if (!JS::CreateError(cx, JSEXN_ERR, nullptr, filenameStr, lineno, 0,
nullptr, messageStr, JS::NothingHandleValue, &exc)) {
return false;
}

JS_SetPendingException(cx, exc);
return false;
Expand Down

0 comments on commit 7564c5f

Please sign in to comment.