Skip to content

Commit d013044

Browse files
gabrielschulhofmhdawson
authored andcommitted
src: interject class TypeTaggable
Derive class `TypeTaggable` from `Value`, and let it serve as the base class for `Object` and `External`. That way, the type tagging is implemented for both classes. Additionally, exclude deleted .js files from linting. Signed-off-by: Gabriel Schulhof <[email protected]> Refs: #1293 PR-URL: #1298 Reviewed-By: Michael Dawson <[email protected] Reviewed-By: Chengzhong Wu <[email protected]>
1 parent d4942cc commit d013044

File tree

13 files changed

+218
-153
lines changed

13 files changed

+218
-153
lines changed

doc/external.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# External (template)
22

3-
Class `Napi::External<T>` inherits from class [`Napi::Value`][].
3+
Class `Napi::External<T>` inherits from class [`Napi::TypeTaggable`][].
44

55
The `Napi::External` template class implements the ability to create a `Napi::Value` object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data.
66

@@ -67,4 +67,4 @@ T* Napi::External::Data() const;
6767

6868
Returns a pointer to the arbitrary C++ data held by the `Napi::External` object.
6969

70-
[`Napi::Value`]: ./value.md
70+
[`Napi::TypeTaggable`]: ./type_taggable.md

doc/object.md

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Object
22

3-
Class `Napi::Object` inherits from class [`Napi::Value`][].
3+
Class `Napi::Object` inherits from class [`Napi::TypeTaggable`][].
44

55
The `Napi::Object` class corresponds to a JavaScript object. It is extended by the following node-addon-api classes that you may use when working with more specific types:
66

@@ -241,33 +241,6 @@ from being added to it and marking all existing properties as non-configurable.
241241
Values of present properties can still be changed as long as they are
242242
writable.
243243

244-
### TypeTag()
245-
246-
```cpp
247-
void Napi::Object::TypeTag(const napi_type_tag* type_tag) const;
248-
```
249-
250-
- `[in] type_tag`: The tag with which this object is to be marked.
251-
252-
The `Napi::Object::TypeTag()` method associates the value of the `type_tag`
253-
pointer with this JavaScript object. `Napi::Object::CheckTypeTag()` can then be
254-
used to compare the tag that was attached to this object with one owned by the
255-
addon to ensure that this object has the right type.
256-
257-
### CheckTypeTag()
258-
259-
```cpp
260-
bool Napi::Object::CheckTypeTag(const napi_type_tag* type_tag) const;
261-
```
262-
263-
- `[in] type_tag`: The tag with which to compare any tag found on this object.
264-
265-
The `Napi::Object::CheckTypeTag()` method compares the pointer given as
266-
`type_tag` with any that can be found on this JavaScript object. If no tag is
267-
found on this object or, if a tag is found but it does not match `type_tag`,
268-
then the return value is `false`. If a tag is found and it matches `type_tag`,
269-
then the return value is `true`.
270-
271244
### operator\[\]()
272245

273246
```cpp
@@ -434,5 +407,5 @@ void Increment(const CallbackInfo& info) {
434407
}
435408
```
436409
437-
[`Napi::Value`]: ./value.md
410+
[`Napi::TypeTaggable`]: ./type_taggable.md
438411
[`Napi::Value::From`]: ./value.md#from

doc/type_taggable.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# TypeTaggable
2+
3+
Class `Napi::TypeTaggable` inherits from class [`Napi::Value`][].
4+
5+
The `Napi::TypeTaggable` class is the base class for [`Napi::Object`][] and
6+
[`Napi::External`][]. It adds type-tagging capabilities to both. It is an
7+
abstract-only base class.
8+
9+
### TypeTag()
10+
11+
```cpp
12+
void Napi::TypeTaggable::TypeTag(const napi_type_tag* type_tag) const;
13+
```
14+
15+
- `[in] type_tag`: The tag with which this object or external is to be marked.
16+
17+
The `Napi::TypeTaggable::TypeTag()` method associates the value of the
18+
`type_tag` pointer with this JavaScript object or external.
19+
`Napi::TypeTaggable::CheckTypeTag()` can then be used to compare the tag that
20+
was attached with one owned by the add-on to ensure that this object or external
21+
has the right type.
22+
23+
### CheckTypeTag()
24+
25+
```cpp
26+
bool Napi::TypeTaggable::CheckTypeTag(const napi_type_tag* type_tag) const;
27+
```
28+
29+
- `[in] type_tag`: The tag with which to compare any tag found on this object or
30+
external.
31+
32+
The `Napi::TypeTaggable::CheckTypeTag()` method compares the pointer given as
33+
`type_tag` with any that can be found on this JavaScript object or external. If
34+
no tag is found or if a tag is found but it does not match `type_tag`, then the
35+
return value is `false`. If a tag is found and it matches `type_tag`, then the
36+
return value is `true`.
37+
38+
[`Napi::Value`]: ./value.md

napi-inl.h

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,32 @@ String String::From(napi_env env, const T& value) {
12481248
return Helper::From(env, value);
12491249
}
12501250

1251+
////////////////////////////////////////////////////////////////////////////////
1252+
// TypeTaggable class
1253+
////////////////////////////////////////////////////////////////////////////////
1254+
1255+
inline TypeTaggable::TypeTaggable() : Value() {}
1256+
1257+
inline TypeTaggable::TypeTaggable(napi_env _env, napi_value _value)
1258+
: Value(_env, _value) {}
1259+
1260+
#if NAPI_VERSION >= 8
1261+
1262+
inline void TypeTaggable::TypeTag(const napi_type_tag* type_tag) const {
1263+
napi_status status = napi_type_tag_object(_env, _value, type_tag);
1264+
NAPI_THROW_IF_FAILED_VOID(_env, status);
1265+
}
1266+
1267+
inline bool TypeTaggable::CheckTypeTag(const napi_type_tag* type_tag) const {
1268+
bool result;
1269+
napi_status status =
1270+
napi_check_object_type_tag(_env, _value, type_tag, &result);
1271+
NAPI_THROW_IF_FAILED(_env, status, false);
1272+
return result;
1273+
}
1274+
1275+
#endif // NAPI_VERSION >= 8
1276+
12511277
////////////////////////////////////////////////////////////////////////////////
12521278
// Object class
12531279
////////////////////////////////////////////////////////////////////////////////
@@ -1287,9 +1313,10 @@ inline Object Object::New(napi_env env) {
12871313
return Object(env, value);
12881314
}
12891315

1290-
inline Object::Object() : Value() {}
1316+
inline Object::Object() : TypeTaggable() {}
12911317

1292-
inline Object::Object(napi_env env, napi_value value) : Value(env, value) {}
1318+
inline Object::Object(napi_env env, napi_value value)
1319+
: TypeTaggable(env, value) {}
12931320

12941321
inline Object::PropertyLValue<std::string> Object::operator[](
12951322
const char* utf8name) {
@@ -1632,19 +1659,6 @@ inline MaybeOrValue<bool> Object::Seal() const {
16321659
napi_status status = napi_object_seal(_env, _value);
16331660
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
16341661
}
1635-
1636-
inline void Object::TypeTag(const napi_type_tag* type_tag) const {
1637-
napi_status status = napi_type_tag_object(_env, _value, type_tag);
1638-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1639-
}
1640-
1641-
inline bool Object::CheckTypeTag(const napi_type_tag* type_tag) const {
1642-
bool result;
1643-
napi_status status =
1644-
napi_check_object_type_tag(_env, _value, type_tag, &result);
1645-
NAPI_THROW_IF_FAILED(_env, status, false);
1646-
return result;
1647-
}
16481662
#endif // NAPI_VERSION >= 8
16491663

16501664
////////////////////////////////////////////////////////////////////////////////
@@ -1706,11 +1720,11 @@ inline External<T> External<T>::New(napi_env env,
17061720
}
17071721

17081722
template <typename T>
1709-
inline External<T>::External() : Value() {}
1723+
inline External<T>::External() : TypeTaggable() {}
17101724

17111725
template <typename T>
17121726
inline External<T>::External(napi_env env, napi_value value)
1713-
: Value(env, value) {}
1727+
: TypeTaggable(env, value) {}
17141728

17151729
template <typename T>
17161730
inline T* External<T>::Data() const {

napi.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,19 @@ class Symbol : public Name {
714714
napi_value value); ///< Wraps a Node-API value primitive.
715715
};
716716

717+
class TypeTaggable : public Value {
718+
public:
719+
#if NAPI_VERSION >= 8
720+
void TypeTag(const napi_type_tag* type_tag) const;
721+
bool CheckTypeTag(const napi_type_tag* type_tag) const;
722+
#endif // NAPI_VERSION >= 8
723+
protected:
724+
TypeTaggable();
725+
TypeTaggable(napi_env env, napi_value value);
726+
};
727+
717728
/// A JavaScript object value.
718-
class Object : public Value {
729+
class Object : public TypeTaggable {
719730
public:
720731
/// Enables property and element assignments using indexing syntax.
721732
///
@@ -991,14 +1002,11 @@ class Object : public Value {
9911002
/// See
9921003
/// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
9931004
MaybeOrValue<bool> Seal() const;
994-
995-
void TypeTag(const napi_type_tag* type_tag) const;
996-
bool CheckTypeTag(const napi_type_tag* type_tag) const;
9971005
#endif // NAPI_VERSION >= 8
9981006
};
9991007

10001008
template <typename T>
1001-
class External : public Value {
1009+
class External : public TypeTaggable {
10021010
public:
10031011
static External New(napi_env env, T* data);
10041012

test/binding.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Object InitVersionManagement(Env env);
7676
Object InitThunkingManual(Env env);
7777
#if (NAPI_VERSION > 7)
7878
Object InitObjectFreezeSeal(Env env);
79-
Object InitObjectTypeTag(Env env);
79+
Object InitTypeTaggable(Env env);
8080
#endif
8181

8282
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
@@ -169,7 +169,7 @@ Object Init(Env env, Object exports) {
169169
exports.Set("thunking_manual", InitThunkingManual(env));
170170
#if (NAPI_VERSION > 7)
171171
exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
172-
exports.Set("object_type_tag", InitObjectTypeTag(env));
172+
exports.Set("type_taggable", InitTypeTaggable(env));
173173
#endif
174174

175175
#if defined(NODE_ADDON_API_ENABLE_MAYBE)

test/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
'object/has_property.cc',
4949
'object/object.cc',
5050
'object/object_freeze_seal.cc',
51-
'object/object_type_tag.cc',
5251
'object/set_property.cc',
5352
'object/subscript_operator.cc',
5453
'promise.cc',
@@ -60,6 +59,7 @@
6059
'threadsafe_function/threadsafe_function_sum.cc',
6160
'threadsafe_function/threadsafe_function_unref.cc',
6261
'threadsafe_function/threadsafe_function.cc',
62+
'type_taggable.cc',
6363
'typed_threadsafe_function/typed_threadsafe_function_ctx.cc',
6464
'typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc',
6565
'typed_threadsafe_function/typed_threadsafe_function_ptr.cc',

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ if (majorNodeVersion < 12 && !filterConditionsProvided) {
134134

135135
if (napiVersion < 8 && !filterConditionsProvided) {
136136
testModules.splice(testModules.indexOf('object/object_freeze_seal'), 1);
137-
testModules.splice(testModules.indexOf('object/object_type_tag'), 1);
137+
testModules.splice(testModules.indexOf('type_taggable'), 1);
138138
}
139139

140140
(async function () {

test/object/object_type_tag.cc

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/object/object_type_tag.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)