Skip to content

Commit

Permalink
Merge pull request #190 from segevfiner/napi-2
Browse files Browse the repository at this point in the history
refactor!: use NAPI instead of Nan
  • Loading branch information
amaanq authored Mar 8, 2024
2 parents 9ccc10b + b413646 commit 9d8c5fd
Show file tree
Hide file tree
Showing 39 changed files with 4,345 additions and 1,847 deletions.
31 changes: 11 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ on:
- released

env:
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 }}
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 }}
PREBUILD_CMD: npx prebuild -r napi --all --strip -u ${{ secrets.GH_TOKEN }}

jobs:

Expand All @@ -22,27 +21,22 @@ jobs:
matrix:
os:
- windows-2019
- macos-latest
- macos-14
- ubuntu-20.04
node:
- 14
- 16
- 18
- 20
fail-fast: false
name: Testing Node ${{ matrix.node }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- run: npm install
- run: npm test
Expand All @@ -53,11 +47,11 @@ jobs:
runs-on: ubuntu-20.04
needs: test
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org
Expand All @@ -71,24 +65,21 @@ jobs:
matrix:
os:
- windows-2019
- macos-latest
- macos-14
- ubuntu-20.04
fail-fast: false
name: Prebuild for ${{ matrix.os }}
runs-on: ${{ matrix.os }}
needs: publish
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install
- run: ${{ env.PREBUILD_CMD }}
- if: runner.os == 'macOS'
run: ${{ env.NODE_PREBUILD_CMD }} --arch arm64
- if: runner.os == 'macOS'
run: ${{ env.ELECTRON_PREBUILD_CMD }} --arch arm64
- run: ${{ env.NODE_PREBUILD_CMD }}
- run: ${{ env.ELECTRON_PREBUILD_CMD }}
run: ${{ env.PREBUILD_CMD }} --arch arm64
22 changes: 9 additions & 13 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
"targets": [
{
"target_name": "tree_sitter_runtime_binding",
"dependencies": ["tree_sitter"],
"dependencies": ["tree_sitter", "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except"],
"sources": [
"src/binding.cc",
"src/conversions.cc",
"src/language.cc",
"src/logger.cc",
"src/lookaheaditerator.cc",
"src/node.cc",
"src/parser.cc",
"src/query.cc",
"src/tree.cc",
"src/tree_cursor.cc",
"src/util.cc",
],
"include_dirs": [
"vendor/tree-sitter/lib/include",
"<!(node -e \"require('nan')\")",
],
"defines": [
"NAPI_VERSION=<(napi_build_version)",
],
'cflags': [
'-std=c++17'
Expand All @@ -28,9 +30,9 @@
'conditions': [
['OS=="mac"', {
'xcode_settings': {
'MACOSX_DEPLOYMENT_TARGET': '10.9',
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
'CLANG_CXX_LANGUAGE_STANDARD': 'c++17',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.9',
},
}],
['OS=="win"', {
Expand All @@ -48,10 +50,7 @@
'-Wno-cast-function-type'
]
}],
['runtime=="electron"', {
'defines': ['NODE_RUNTIME_ELECTRON=1']
}],
],
]
},
{
"target_name": "tree_sitter",
Expand All @@ -73,8 +72,5 @@
'openssl_fips': '',
'v8_enable_pointer_compression%': 0,
'v8_enable_31bit_smis_on_64bit_arch%': 0,
},
'conditions': [
['runtime=="electron"', { 'defines': ['NODE_RUNTIME_ELECTRON=1'] }],
]
}
}
110 changes: 96 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ try {
}

const util = require('util')
const {Query, Parser, NodeMethods, Tree, TreeCursor} = binding;
const {Query, Parser, NodeMethods, Tree, TreeCursor, LookaheadIterator} = binding;

/*
* Tree
*/

const {rootNode, edit} = Tree.prototype;
const {rootNode, rootNodeWithOffset, edit} = Tree.prototype;

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

Tree.prototype.rootNodeWithOffset = function(offset_bytes, offset_extent) {
return unmarshalNode(rootNodeWithOffset.call(this, offset_bytes, offset_extent.row, offset_extent.column), this);
}

Tree.prototype.edit = function(arg) {
if (this instanceof Tree && edit) {
edit.call(
Expand Down Expand Up @@ -75,21 +79,61 @@ class SyntaxNode {
'}'
}

get type() {
get id() {
marshalNode(this);
return NodeMethods.type(this.tree);
return NodeMethods.id(this.tree);
}

get typeId() {
marshalNode(this);
return NodeMethods.typeId(this.tree);
}

get grammarId() {
marshalNode(this);
return NodeMethods.grammarId(this.tree);
}

get type() {
marshalNode(this);
return NodeMethods.type(this.tree);
}

get grammarName() {
marshalNode(this);
return NodeMethods.grammarName(this.tree);
}

get isExtra() {
marshalNode(this);
return NodeMethods.isExtra(this.tree);
}

get isNamed() {
marshalNode(this);
return NodeMethods.isNamed(this.tree);
}

get isMissing() {
marshalNode(this);
return NodeMethods.isMissing(this.tree);
}

get hasChanges() {
marshalNode(this);
return NodeMethods.hasChanges(this.tree);
}

get hasError() {
marshalNode(this);
return NodeMethods.hasError(this.tree);
}

get isError() {
marshalNode(this);
return NodeMethods.isError(this.tree);
}

get text() {
return this.tree.getText(this);
}
Expand Down Expand Up @@ -181,19 +225,19 @@ class SyntaxNode {
return unmarshalNode(NodeMethods.previousNamedSibling(this.tree), this.tree);
}

hasChanges() {
get parseState() {
marshalNode(this);
return NodeMethods.hasChanges(this.tree);
return NodeMethods.parseState(this.tree);
}

hasError() {
get nextParseState() {
marshalNode(this);
return NodeMethods.hasError(this.tree);
return NodeMethods.nextParseState(this.tree);
}

isMissing() {
get descendantCount() {
marshalNode(this);
return NodeMethods.isMissing(this.tree);
return NodeMethods.descendantCount(this.tree);
}

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

childForFieldName(fieldName) {
marshalNode(this);
return unmarshalNode(NodeMethods.childForFieldName(this.tree, fieldName), this.tree);
}

childForFieldId(fieldId) {
marshalNode(this);
return unmarshalNode(NodeMethods.childForFieldId(this.tree, fieldId), this.tree);
}

fieldNameForChild(childIndex) {
marshalNode(this);
return NodeMethods.fieldNameForChild(this.tree, childIndex);
}

childrenForFieldName(fieldName, cursor) {
marshalNode(this);
return unmarshalNodes(NodeMethods.childrenForFieldName(this.tree, fieldName, cursor), this.tree);
}

childrenForFieldId(fieldId) {
marshalNode(this);
return unmarshalNodes(NodeMethods.childrenForFieldId(this.tree, fieldId), this.tree);
}

firstChildForIndex(index) {
marshalNode(this);
return unmarshalNode(NodeMethods.firstChildForIndex(this.tree, index), this.tree);
Expand Down Expand Up @@ -302,7 +371,8 @@ Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={
input,
oldTree,
bufferSize,
includedRanges)
includedRanges,
)
: undefined;

if (tree) {
Expand Down Expand Up @@ -556,11 +626,22 @@ Query.prototype._init = function() {
this.refutedProperties = Object.freeze(refutedProperties);
}

Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
Query.prototype.matches = function(
rootNode,
{
startPosition = ZERO_POINT,
endPosition = ZERO_POINT,
startIndex = 0,
endIndex = 0,
matchLimit = 0xFFFFFFFF,
maxStartDepth = 0xFFFFFFFF
} = {}
) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
startPosition.row, startPosition.column,
endPosition.row, endPosition.column
endPosition.row, endPosition.column,
startIndex, endIndex, matchLimit, maxStartDepth
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const results = [];
Expand Down Expand Up @@ -798,3 +879,4 @@ module.exports.Query = Query;
module.exports.Tree = Tree;
module.exports.SyntaxNode = SyntaxNode;
module.exports.TreeCursor = TreeCursor;
module.exports.LookaheadIterator = LookaheadIterator;
10 changes: 0 additions & 10 deletions jest-tests/constants.js

This file was deleted.

9 changes: 0 additions & 9 deletions jest-tests/parse_input.js

This file was deleted.

9 changes: 0 additions & 9 deletions jest-tests/runit.js

This file was deleted.

Loading

0 comments on commit 9d8c5fd

Please sign in to comment.