Skip to content

Commit c6a649c

Browse files
committed
napi
1 parent 652433f commit c6a649c

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

binding.gyp

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
"targets": [
33
{
44
"target_name": "tree_sitter_c_binding",
5+
"dependencies": [
6+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
7+
],
58
"include_dirs": [
6-
"<!(node -e \"require('nan')\")",
7-
"src"
9+
"src",
810
],
911
"sources": [
12+
"bindings/node/binding.cc",
1013
"src/parser.c",
11-
"bindings/node/binding.cc"
1214
],
1315
"cflags_c": [
14-
"-std=c99",
15-
]
16+
"-std=c11",
17+
],
1618
}
1719
]
1820
}

bindings/node/binding.cc

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
#include "nan.h"
2-
#include "tree_sitter/parser.h"
3-
#include <node.h>
1+
#include <napi.h>
42

5-
using namespace v8;
3+
typedef struct TSLanguage TSLanguage;
64

75
extern "C" TSLanguage *tree_sitter_c();
86

9-
namespace {
10-
11-
NAN_METHOD(New) {}
12-
13-
void Init(Local<Object> exports, Local<Object> module) {
14-
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
15-
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
16-
tpl->InstanceTemplate()->SetInternalFieldCount(1);
17-
18-
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
19-
Local<Object> instance =
20-
constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
21-
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_c());
22-
23-
Nan::Set(instance, Nan::New("name").ToLocalChecked(),
24-
Nan::New("c").ToLocalChecked());
25-
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
7+
// "tree-sitter", "language" hashed with BLAKE2
8+
const napi_type_tag LANGUAGE_TYPE_TAG = {
9+
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
10+
};
11+
12+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
13+
exports["name"] = Napi::String::New(env, "c");
14+
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_c());
15+
language.TypeTag(&LANGUAGE_TYPE_TAG);
16+
exports["language"] = language;
17+
return exports;
2618
}
2719

28-
NODE_MODULE(tree_sitter_c_binding, Init)
29-
30-
} // namespace
20+
NODE_API_MODULE(tree_sitter_c_binding, Init)

bindings/node/index.d.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
type BaseNode = {
2+
type: string;
3+
named: boolean;
4+
};
5+
6+
type ChildNode = {
7+
multiple: boolean;
8+
required: boolean;
9+
types: BaseNode[];
10+
};
11+
12+
type NodeInfo =
13+
| (BaseNode & {
14+
subtypes: BaseNode[];
15+
})
16+
| (BaseNode & {
17+
fields: { [name: string]: ChildNode };
18+
children: ChildNode[];
19+
});
20+
21+
type Language = {
22+
name: string;
23+
language: unknown;
24+
nodeTypeInfo: NodeInfo[];
25+
};
26+
27+
declare const language: Language;
28+
export = language;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.20.8",
44
"description": "C grammar for node-tree-sitter",
55
"main": "bindings/node",
6+
"types": "bindings/node",
67
"keywords": [
78
"parser",
89
"lexer",
@@ -18,7 +19,7 @@
1819
],
1920
"license": "MIT",
2021
"dependencies": {
21-
"nan": "^2.18.0"
22+
"node-addon-api": "^7.1.0"
2223
},
2324
"devDependencies": {
2425
"eslint": "^8.56.0",

0 commit comments

Comments
 (0)