Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use type tags #1

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"mocha": "^8.4.0",
"node-gyp": "^10.0.1",
"prebuild": "^13.0.0",
"tree-sitter-javascript": "github:segevfiner/tree-sitter-javascript#napi-2"
"tree-sitter-javascript": "github:segevfiner/tree-sitter-javascript#napi-type-tag"
},
"scripts": {
"install": "prebuild-install -r napi || node-gyp rebuild",
Expand Down
9 changes: 6 additions & 3 deletions src/language.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ using namespace Napi;

namespace node_tree_sitter::language_methods {

const napi_type_tag LANGUAGE_TYPE_TAG = {
0x95840BEBF71E4E90, 0x9DC9419B874C0271
};

const TSLanguage *UnwrapLanguage(Napi::Value value) {
Napi::Env env = value.Env();

if (value.IsObject()) {
value = value.As<Object>()["language"];
}

if (value.IsExternal()) {
auto arg = value.As<External<const TSLanguage>>();
auto arg = value.As<External<const TSLanguage>>();
if (arg.IsExternal() && arg.CheckTypeTag(&LANGUAGE_TYPE_TAG)) {
const TSLanguage *language = arg.Data();
if (language != nullptr) {
uint16_t version = ts_language_version(language);
Expand Down Expand Up @@ -93,4 +97,3 @@ void Init(Napi::Env env, Napi::Object exports) {
}

} // namespace node_tree_sitter::language_methods

5 changes: 3 additions & 2 deletions src/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void Query::Init(Napi::Env env, Napi::Object exports) {
Query::Query(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Query>(info) , query_(nullptr) {
Napi::Env env = info.Env();

Value().TypeTag(&TYPE_TAG);

const TSLanguage *language = language_methods::UnwrapLanguage(info[0]);
const char *source;
uint32_t source_len;
Expand Down Expand Up @@ -80,12 +82,11 @@ Query::~Query() {
}

Query *Query::UnwrapQuery(const Napi::Value &value) {
auto *data = value.Env().GetInstanceData<AddonData>();
if (!value.IsObject()) {
return nullptr;
}
auto js_query = value.As<Object>();
if (!js_query.InstanceOf(data->query_constructor.Value())) {
if (!js_query.CheckTypeTag(&TYPE_TAG)) {
return nullptr;
}
return Query::Unwrap(js_query);
Expand Down
4 changes: 4 additions & 0 deletions src/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace node_tree_sitter {

class Query : public Napi::ObjectWrap<Query> {
public:
static constexpr napi_type_tag TYPE_TAG = {
0x0B236F23456A4B87, 0x86BAC1E58CA6897B
};

static void Init(Napi::Env env, Napi::Object exports);
static Query *UnwrapQuery(const Napi::Value &);

Expand Down
7 changes: 4 additions & 3 deletions src/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ void Tree::Init(Napi::Env env, Napi::Object exports) {
exports["Tree"] = ctor;
}

Tree::Tree(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Tree>(info), tree_(nullptr) {}
Tree::Tree(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Tree>(info), tree_(nullptr) {
Value().TypeTag(&TYPE_TAG);
}

Tree::~Tree() {
ts_tree_delete(tree_);
Expand All @@ -49,12 +51,11 @@ Napi::Value Tree::NewInstance(Napi::Env env, TSTree *tree) {
}

const Tree *Tree::UnwrapTree(const Napi::Value &value) {
auto *data = value.Env().GetInstanceData<AddonData>();
if (!value.IsObject()) {
return nullptr;
}
auto js_tree = value.As<Object>();
if (!js_tree.InstanceOf(data->tree_constructor.Value())) {
if (!js_tree.CheckTypeTag(&TYPE_TAG)) {
return nullptr;
}
return Tree::Unwrap(js_tree);
Expand Down
4 changes: 4 additions & 0 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace node_tree_sitter {

class Tree : public Napi::ObjectWrap<Tree> {
public:
static constexpr napi_type_tag TYPE_TAG = {
0x32EA7ED51F284DAE, 0x9BBD75ACAE8DFD7D
};

static void Init(Napi::Env env, Napi::Object exports);
static Napi::Value NewInstance(Napi::Env env, TSTree *);
static const Tree *UnwrapTree(const Napi::Value &);
Expand Down