Skip to content

Commit e87cf7e

Browse files
segevfinerverhovskyamaanqObserverOfTime
authored
refactor(bindings)!: convert node bindings to NAPI
Co-authored-by: Boris Verkhovskiy <[email protected]> Co-authored-by: Amaan Qureshi <[email protected]> Co-authored-by: ObserverOfTime <[email protected]>
1 parent 3d65ffa commit e87cf7e

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

cli/src/generate/grammar_files.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,30 @@ pub fn generate_grammar_files(
193193
generate_file(path, INDEX_JS_TEMPLATE, language_name)
194194
})?;
195195

196-
missing_path(path.join("binding.cc"), |path| {
197-
generate_file(path, JS_BINDING_CC_TEMPLATE, language_name)
198-
})?;
196+
missing_path_else(
197+
path.join("binding.cc"),
198+
|path| generate_file(path, JS_BINDING_CC_TEMPLATE, language_name),
199+
|path| {
200+
let binding_cc =
201+
fs::read_to_string(path).with_context(|| "Failed to read binding.cc")?;
202+
if binding_cc.contains("NAN_METHOD(New) {}") {
203+
eprintln!("Replacing binding.cc with new binding API");
204+
write_file(path, JS_BINDING_CC_TEMPLATE)?;
205+
}
206+
Ok(())
207+
},
208+
)?;
199209

200-
// Create binding.gyp, or update it with new binding path.
210+
// Create binding.gyp, or update it with new binding API.
201211
missing_path_else(
202212
repo_path.join("binding.gyp"),
203213
|path| generate_file(path, BINDING_GYP_TEMPLATE, language_name),
204214
|path| {
205215
let binding_gyp =
206216
fs::read_to_string(path).with_context(|| "Failed to read binding.gyp")?;
207-
let old_path = "\"src/binding.cc\"";
208-
if binding_gyp.contains(old_path) {
209-
eprintln!("Updating binding.gyp with new binding path");
210-
let binding_gyp =
211-
binding_gyp.replace(old_path, "\"bindings/node/binding.cc\"");
212-
write_file(path, binding_gyp)?;
217+
if binding_gyp.contains("require('nan')") {
218+
eprintln!("Replacing binding.gyp with new binding API");
219+
write_file(path, BINDING_GYP_TEMPLATE)?;
213220
}
214221
Ok(())
215222
},

cli/src/generate/templates/binding.gyp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"targets": [
33
{
44
"target_name": "tree_sitter_PARSER_NAME_binding",
5+
"dependencies": ["<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except"],
56
"include_dirs": [
6-
"<!(node -e \"require('nan')\")",
77
"src",
88
],
99
"sources": [
@@ -16,7 +16,15 @@
1616
],
1717
"cflags_cc": [
1818
"-Wno-cast-function-type",
19-
]
19+
],
20+
"conditions": [
21+
["OS=='mac'", {
22+
"cflags+": ["-fvisibility=hidden"],
23+
"xcode_settings": {
24+
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES", # -fvisibility=hidden
25+
}
26+
}]
27+
],
2028
}
2129
]
2230
}
Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
#include "nan.h"
2-
#include <node.h>
3-
4-
using namespace v8;
1+
#include <napi.h>
52

63
typedef struct TSLanguage TSLanguage;
74

8-
extern "C" const TSLanguage *tree_sitter_PARSER_NAME(void);
9-
10-
namespace {
11-
12-
NAN_METHOD(New) {}
5+
extern "C" TSLanguage *tree_sitter_PARSER_NAME();
136

14-
void Init(Local<Object> exports, Local<Object> module) {
15-
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
16-
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
17-
tpl->InstanceTemplate()->SetInternalFieldCount(1);
18-
19-
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
20-
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
21-
Nan::SetInternalFieldPointer(instance, 0, (void *)tree_sitter_PARSER_NAME());
22-
23-
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("PARSER_NAME").ToLocalChecked());
24-
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
7+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
8+
exports["name"] = Napi::String::New(env, "PARSER_NAME");
9+
exports["language"] = Napi::External<TSLanguage>::New(env, tree_sitter_PARSER_NAME());
10+
return exports;
2511
}
2612

27-
NODE_MODULE_CONTEXT_AWARE(tree_sitter_PARSER_NAME_binding, Init)
28-
29-
} // namespace
13+
NODE_API_MODULE(tree_sitter_PARSER_NAME_binding, Init)

cli/src/generate/templates/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"LOWER_PARSER_NAME"
2121
],
2222
"dependencies": {
23-
"nan": "^2.18.0"
23+
"node-addon-api": "^7.1.0"
2424
},
2525
"devDependencies": {
2626
"tree-sitter-cli": "^CLI_VERSION"

0 commit comments

Comments
 (0)