src,doc: add SyntaxError napi support · nodejs/node@df574f3 · GitHub
Skip to content

Commit df574f3

Browse files
Idan Attiasdanielleadams
authored andcommitted
src,doc: add SyntaxError napi support
Add `napi_create_syntax_error` and `napi_throw_syntax_error`. Fixes: nodejs/node-addon-api#1099 PR-URL: #40736 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent eb0106d commit df574f3

5 files changed

Lines changed: 157 additions & 4 deletions

File tree

doc/api/n-api.md

Lines changed: 48 additions & 4 deletions

src/js_native_api.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ NAPI_EXTERN napi_status napi_create_range_error(napi_env env,
111111
napi_value code,
112112
napi_value msg,
113113
napi_value* result);
114+
#ifdef NAPI_EXPERIMENTAL
115+
NAPI_EXTERN napi_status node_api_create_syntax_error(napi_env env,
116+
napi_value code,
117+
napi_value msg,
118+
napi_value* result);
119+
#endif // NAPI_EXPERIMENTAL
114120

115121
// Methods to get the native napi_value from Primitive type
116122
NAPI_EXTERN napi_status napi_typeof(napi_env env,
@@ -370,6 +376,11 @@ NAPI_EXTERN napi_status napi_throw_type_error(napi_env env,
370376
NAPI_EXTERN napi_status napi_throw_range_error(napi_env env,
371377
const char* code,
372378
const char* msg);
379+
#ifdef NAPI_EXPERIMENTAL
380+
NAPI_EXTERN napi_status node_api_throw_syntax_error(napi_env env,
381+
const char* code,
382+
const char* msg);
383+
#endif // NAPI_EXPERIMENTAL
373384
NAPI_EXTERN napi_status napi_is_error(napi_env env,
374385
napi_value value,
375386
bool* result);

src/js_native_api_v8.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,26 @@ napi_status napi_create_range_error(napi_env env,
17471747
return napi_clear_last_error(env);
17481748
}
17491749

1750+
napi_status node_api_create_syntax_error(napi_env env,
1751+
napi_value code,
1752+
napi_value msg,
1753+
napi_value* result) {
1754+
CHECK_ENV(env);
1755+
CHECK_ARG(env, msg);
1756+
CHECK_ARG(env, result);
1757+
1758+
v8::Local<v8::Value> message_value = v8impl::V8LocalValueFromJsValue(msg);
1759+
RETURN_STATUS_IF_FALSE(env, message_value->IsString(), napi_string_expected);
1760+
1761+
v8::Local<v8::Value> error_obj =
1762+
v8::Exception::SyntaxError(message_value.As<v8::String>());
1763+
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));
1764+
1765+
*result = v8impl::JsValueFromV8LocalValue(error_obj);
1766+
1767+
return napi_clear_last_error(env);
1768+
}
1769+
17501770
napi_status napi_typeof(napi_env env,
17511771
napi_value value,
17521772
napi_valuetype* result) {
@@ -1964,6 +1984,24 @@ napi_status napi_throw_range_error(napi_env env,
19641984
return napi_clear_last_error(env);
19651985
}
19661986

1987+
napi_status node_api_throw_syntax_error(napi_env env,
1988+
const char* code,
1989+
const char* msg) {
1990+
NAPI_PREAMBLE(env);
1991+
1992+
v8::Isolate* isolate = env->isolate;
1993+
v8::Local<v8::String> str;
1994+
CHECK_NEW_FROM_UTF8(env, str, msg);
1995+
1996+
v8::Local<v8::Value> error_obj = v8::Exception::SyntaxError(str);
1997+
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));
1998+
1999+
isolate->ThrowException(error_obj);
2000+
// any VM calls after this point and before returning
2001+
// to the javascript invoker will fail
2002+
return napi_clear_last_error(env);
2003+
}
2004+
19672005
napi_status napi_is_error(napi_env env, napi_value value, bool* result) {
19682006
// Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot
19692007
// throw JS exceptions.

test/js-native-api/test_error/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ assert.throws(() => {
6060
test_error.throwTypeError();
6161
}, /^TypeError: type error$/);
6262

63+
assert.throws(() => {
64+
test_error.throwSyntaxError();
65+
}, /^SyntaxError: syntax error$/);
66+
6367
[42, {}, [], Symbol('xyzzy'), true, 'ball', undefined, null, NaN]
6468
.forEach((value) => assert.throws(
6569
() => test_error.throwArbitrary(value),
@@ -90,6 +94,13 @@ assert.throws(
9094
message: 'TypeError [type error]'
9195
});
9296

97+
assert.throws(
98+
() => test_error.throwSyntaxErrorCode(),
99+
{
100+
code: 'ERR_TEST_CODE',
101+
message: 'SyntaxError [syntax error]'
102+
});
103+
93104
let error = test_error.createError();
94105
assert.ok(error instanceof Error, 'expected error to be an instance of Error');
95106
assert.strictEqual(error.message, 'error');
@@ -104,6 +115,11 @@ assert.ok(error instanceof TypeError,
104115
'expected error to be an instance of TypeError');
105116
assert.strictEqual(error.message, 'type error');
106117

118+
error = test_error.createSyntaxError();
119+
assert.ok(error instanceof SyntaxError,
120+
'expected error to be an instance of SyntaxError');
121+
assert.strictEqual(error.message, 'syntax error');
122+
107123
error = test_error.createErrorCode();
108124
assert.ok(error instanceof Error, 'expected error to be an instance of Error');
109125
assert.strictEqual(error.code, 'ERR_TEST_CODE');
@@ -123,3 +139,10 @@ assert.ok(error instanceof TypeError,
123139
assert.strictEqual(error.message, 'TypeError [type error]');
124140
assert.strictEqual(error.code, 'ERR_TEST_CODE');
125141
assert.strictEqual(error.name, 'TypeError');
142+
143+
error = test_error.createSyntaxErrorCode();
144+
assert.ok(error instanceof SyntaxError,
145+
'expected error to be an instance of SyntaxError');
146+
assert.strictEqual(error.message, 'SyntaxError [syntax error]');
147+
assert.strictEqual(error.code, 'ERR_TEST_CODE');
148+
assert.strictEqual(error.name, 'SyntaxError');

test/js-native-api/test_error/test_error.c

Lines changed: 37 additions & 0 deletions

0 commit comments

Comments
 (0)