Skip to content

Commit 9d8c5fd

Browse files
authored
Merge pull request #190 from segevfiner/napi-2
refactor!: use NAPI instead of Nan
2 parents 9ccc10b + b413646 commit 9d8c5fd

39 files changed

+4345
-1847
lines changed

.github/workflows/build.yml

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ on:
1212
- released
1313

1414
env:
15-
NODE_PREBUILD_CMD: npx prebuild -t 14.0.0 -t 16.0.0 -t 18.0.0 -t 20.0.0 --strip -u ${{ secrets.GH_TOKEN }}
16-
ELECTRON_PREBUILD_CMD: npx prebuild -r electron -t 12.0.0 -t 13.0.0 -t 14.0.0 -t 15.0.0 -t 16.0.0 -t 17.0.0 -t 18.0.0 -t 19.0.0 -t 20.0.0 -t 21.0.0 -t 22.0.0 -t 23.0.0 -t 24.0.0 -t 25.0.0 --strip -u ${{ secrets.GH_TOKEN }}
15+
PREBUILD_CMD: npx prebuild -r napi --all --strip -u ${{ secrets.GH_TOKEN }}
1716

1817
jobs:
1918

@@ -22,27 +21,22 @@ jobs:
2221
matrix:
2322
os:
2423
- windows-2019
25-
- macos-latest
24+
- macos-14
2625
- ubuntu-20.04
2726
node:
28-
- 14
29-
- 16
3027
- 18
3128
- 20
3229
fail-fast: false
3330
name: Testing Node ${{ matrix.node }} on ${{ matrix.os }}
3431
runs-on: ${{ matrix.os }}
3532
steps:
36-
- uses: actions/checkout@v3
33+
- uses: actions/checkout@v4
3734
with:
3835
submodules: true
3936
fetch-depth: 0
40-
- uses: actions/setup-node@v3
37+
- uses: actions/setup-node@v4
4138
with:
4239
node-version: ${{ matrix.node }}
43-
- uses: actions/setup-python@v4
44-
with:
45-
python-version: '3.10'
4640

4741
- run: npm install
4842
- run: npm test
@@ -53,11 +47,11 @@ jobs:
5347
runs-on: ubuntu-20.04
5448
needs: test
5549
steps:
56-
- uses: actions/checkout@v3
50+
- uses: actions/checkout@v4
5751
with:
5852
submodules: true
5953
fetch-depth: 0
60-
- uses: actions/setup-node@v3
54+
- uses: actions/setup-node@v4
6155
with:
6256
node-version: 20
6357
registry-url: https://registry.npmjs.org
@@ -71,24 +65,21 @@ jobs:
7165
matrix:
7266
os:
7367
- windows-2019
74-
- macos-latest
68+
- macos-14
7569
- ubuntu-20.04
7670
fail-fast: false
7771
name: Prebuild for ${{ matrix.os }}
7872
runs-on: ${{ matrix.os }}
7973
needs: publish
8074
steps:
81-
- uses: actions/checkout@v3
75+
- uses: actions/checkout@v4
8276
with:
8377
submodules: true
8478
fetch-depth: 0
85-
- uses: actions/setup-node@v3
79+
- uses: actions/setup-node@v4
8680
with:
8781
node-version: 20
8882
- run: npm install
83+
- run: ${{ env.PREBUILD_CMD }}
8984
- if: runner.os == 'macOS'
90-
run: ${{ env.NODE_PREBUILD_CMD }} --arch arm64
91-
- if: runner.os == 'macOS'
92-
run: ${{ env.ELECTRON_PREBUILD_CMD }} --arch arm64
93-
- run: ${{ env.NODE_PREBUILD_CMD }}
94-
- run: ${{ env.ELECTRON_PREBUILD_CMD }}
85+
run: ${{ env.PREBUILD_CMD }} --arch arm64

binding.gyp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
"targets": [
33
{
44
"target_name": "tree_sitter_runtime_binding",
5-
"dependencies": ["tree_sitter"],
5+
"dependencies": ["tree_sitter", "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except"],
66
"sources": [
77
"src/binding.cc",
88
"src/conversions.cc",
99
"src/language.cc",
1010
"src/logger.cc",
11+
"src/lookaheaditerator.cc",
1112
"src/node.cc",
1213
"src/parser.cc",
1314
"src/query.cc",
1415
"src/tree.cc",
1516
"src/tree_cursor.cc",
16-
"src/util.cc",
1717
],
1818
"include_dirs": [
1919
"vendor/tree-sitter/lib/include",
20-
"<!(node -e \"require('nan')\")",
20+
],
21+
"defines": [
22+
"NAPI_VERSION=<(napi_build_version)",
2123
],
2224
'cflags': [
2325
'-std=c++17'
@@ -28,9 +30,9 @@
2830
'conditions': [
2931
['OS=="mac"', {
3032
'xcode_settings': {
31-
'MACOSX_DEPLOYMENT_TARGET': '10.9',
33+
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
3234
'CLANG_CXX_LANGUAGE_STANDARD': 'c++17',
33-
'CLANG_CXX_LIBRARY': 'libc++',
35+
'MACOSX_DEPLOYMENT_TARGET': '10.9',
3436
},
3537
}],
3638
['OS=="win"', {
@@ -48,10 +50,7 @@
4850
'-Wno-cast-function-type'
4951
]
5052
}],
51-
['runtime=="electron"', {
52-
'defines': ['NODE_RUNTIME_ELECTRON=1']
53-
}],
54-
],
53+
]
5554
},
5655
{
5756
"target_name": "tree_sitter",
@@ -73,8 +72,5 @@
7372
'openssl_fips': '',
7473
'v8_enable_pointer_compression%': 0,
7574
'v8_enable_31bit_smis_on_64bit_arch%': 0,
76-
},
77-
'conditions': [
78-
['runtime=="electron"', { 'defines': ['NODE_RUNTIME_ELECTRON=1'] }],
79-
]
75+
}
8076
}

index.js

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ try {
1010
}
1111

1212
const util = require('util')
13-
const {Query, Parser, NodeMethods, Tree, TreeCursor} = binding;
13+
const {Query, Parser, NodeMethods, Tree, TreeCursor, LookaheadIterator} = binding;
1414

1515
/*
1616
* Tree
1717
*/
1818

19-
const {rootNode, edit} = Tree.prototype;
19+
const {rootNode, rootNodeWithOffset, edit} = Tree.prototype;
2020

2121
Object.defineProperty(Tree.prototype, 'rootNode', {
2222
get() {
@@ -36,9 +36,13 @@ Object.defineProperty(Tree.prototype, 'rootNode', {
3636
},
3737
// Jest worker pool may attempt to override property due to race condition,
3838
// we don't want to error on this
39-
configurable: true
39+
configurable: true
4040
});
4141

42+
Tree.prototype.rootNodeWithOffset = function(offset_bytes, offset_extent) {
43+
return unmarshalNode(rootNodeWithOffset.call(this, offset_bytes, offset_extent.row, offset_extent.column), this);
44+
}
45+
4246
Tree.prototype.edit = function(arg) {
4347
if (this instanceof Tree && edit) {
4448
edit.call(
@@ -75,21 +79,61 @@ class SyntaxNode {
7579
'}'
7680
}
7781

78-
get type() {
82+
get id() {
7983
marshalNode(this);
80-
return NodeMethods.type(this.tree);
84+
return NodeMethods.id(this.tree);
8185
}
8286

8387
get typeId() {
8488
marshalNode(this);
8589
return NodeMethods.typeId(this.tree);
8690
}
8791

92+
get grammarId() {
93+
marshalNode(this);
94+
return NodeMethods.grammarId(this.tree);
95+
}
96+
97+
get type() {
98+
marshalNode(this);
99+
return NodeMethods.type(this.tree);
100+
}
101+
102+
get grammarName() {
103+
marshalNode(this);
104+
return NodeMethods.grammarName(this.tree);
105+
}
106+
107+
get isExtra() {
108+
marshalNode(this);
109+
return NodeMethods.isExtra(this.tree);
110+
}
111+
88112
get isNamed() {
89113
marshalNode(this);
90114
return NodeMethods.isNamed(this.tree);
91115
}
92116

117+
get isMissing() {
118+
marshalNode(this);
119+
return NodeMethods.isMissing(this.tree);
120+
}
121+
122+
get hasChanges() {
123+
marshalNode(this);
124+
return NodeMethods.hasChanges(this.tree);
125+
}
126+
127+
get hasError() {
128+
marshalNode(this);
129+
return NodeMethods.hasError(this.tree);
130+
}
131+
132+
get isError() {
133+
marshalNode(this);
134+
return NodeMethods.isError(this.tree);
135+
}
136+
93137
get text() {
94138
return this.tree.getText(this);
95139
}
@@ -181,19 +225,19 @@ class SyntaxNode {
181225
return unmarshalNode(NodeMethods.previousNamedSibling(this.tree), this.tree);
182226
}
183227

184-
hasChanges() {
228+
get parseState() {
185229
marshalNode(this);
186-
return NodeMethods.hasChanges(this.tree);
230+
return NodeMethods.parseState(this.tree);
187231
}
188232

189-
hasError() {
233+
get nextParseState() {
190234
marshalNode(this);
191-
return NodeMethods.hasError(this.tree);
235+
return NodeMethods.nextParseState(this.tree);
192236
}
193237

194-
isMissing() {
238+
get descendantCount() {
195239
marshalNode(this);
196-
return NodeMethods.isMissing(this.tree);
240+
return NodeMethods.descendantCount(this.tree);
197241
}
198242

199243
toString() {
@@ -211,6 +255,31 @@ class SyntaxNode {
211255
return unmarshalNode(NodeMethods.namedChild(this.tree, index), this.tree);
212256
}
213257

258+
childForFieldName(fieldName) {
259+
marshalNode(this);
260+
return unmarshalNode(NodeMethods.childForFieldName(this.tree, fieldName), this.tree);
261+
}
262+
263+
childForFieldId(fieldId) {
264+
marshalNode(this);
265+
return unmarshalNode(NodeMethods.childForFieldId(this.tree, fieldId), this.tree);
266+
}
267+
268+
fieldNameForChild(childIndex) {
269+
marshalNode(this);
270+
return NodeMethods.fieldNameForChild(this.tree, childIndex);
271+
}
272+
273+
childrenForFieldName(fieldName, cursor) {
274+
marshalNode(this);
275+
return unmarshalNodes(NodeMethods.childrenForFieldName(this.tree, fieldName, cursor), this.tree);
276+
}
277+
278+
childrenForFieldId(fieldId) {
279+
marshalNode(this);
280+
return unmarshalNodes(NodeMethods.childrenForFieldId(this.tree, fieldId), this.tree);
281+
}
282+
214283
firstChildForIndex(index) {
215284
marshalNode(this);
216285
return unmarshalNode(NodeMethods.firstChildForIndex(this.tree, index), this.tree);
@@ -302,7 +371,8 @@ Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={
302371
input,
303372
oldTree,
304373
bufferSize,
305-
includedRanges)
374+
includedRanges,
375+
)
306376
: undefined;
307377

308378
if (tree) {
@@ -556,11 +626,22 @@ Query.prototype._init = function() {
556626
this.refutedProperties = Object.freeze(refutedProperties);
557627
}
558628

559-
Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
629+
Query.prototype.matches = function(
630+
rootNode,
631+
{
632+
startPosition = ZERO_POINT,
633+
endPosition = ZERO_POINT,
634+
startIndex = 0,
635+
endIndex = 0,
636+
matchLimit = 0xFFFFFFFF,
637+
maxStartDepth = 0xFFFFFFFF
638+
} = {}
639+
) {
560640
marshalNode(rootNode);
561641
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
562642
startPosition.row, startPosition.column,
563-
endPosition.row, endPosition.column
643+
endPosition.row, endPosition.column,
644+
startIndex, endIndex, matchLimit, maxStartDepth
564645
);
565646
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
566647
const results = [];
@@ -798,3 +879,4 @@ module.exports.Query = Query;
798879
module.exports.Tree = Tree;
799880
module.exports.SyntaxNode = SyntaxNode;
800881
module.exports.TreeCursor = TreeCursor;
882+
module.exports.LookaheadIterator = LookaheadIterator;

jest-tests/constants.js

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

jest-tests/parse_input.js

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

jest-tests/runit.js

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

0 commit comments

Comments
 (0)