Skip to content

Commit

Permalink
fix: generate bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ryall committed Jul 20, 2024
1 parent e2984c6 commit 384c55e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 44 deletions.
24 changes: 17 additions & 7 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
"targets": [
{
"target_name": "tree_sitter_beancount_binding",
"dependencies": [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
"src",
],
"sources": [
"bindings/node/binding.cc",
# If your language uses an external scanner, add it here.
"src/parser.c",
# "src/scanner.c",
# NOTE: if your language has an external scanner, add it here.
],
"conditions": [
["OS!='win'", {
"cflags_c": [
"-std=c11",
],
}, { # OS == "win"
"cflags_c": [
"/std:c11",
"/utf-8",
],
}],
],
"cflags_c": [
"-std=c99",
]
}
]
}
36 changes: 14 additions & 22 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
#include <napi.h>

using namespace v8;
typedef struct TSLanguage TSLanguage;

extern "C" TSLanguage * tree_sitter_beancount();
extern "C" TSLanguage *tree_sitter_beancount();

namespace {
// "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};

NAN_METHOD(New) {}

void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_beancount());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("beancount").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["name"] = Napi::String::New(env, "beancount");
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_beancount());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}

NODE_MODULE(tree_sitter_beancount_binding, Init)

} // namespace
NODE_API_MODULE(tree_sitter_beancount_binding, Init)
28 changes: 28 additions & 0 deletions bindings/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type BaseNode = {
type: string;
named: boolean;
};

type ChildNode = {
multiple: boolean;
required: boolean;
types: BaseNode[];
};

type NodeInfo =
| (BaseNode & {
subtypes: BaseNode[];
})
| (BaseNode & {
fields: { [name: string]: ChildNode };
children: ChildNode[];
});

type Language = {
name: string;
language: unknown;
nodeTypeInfo: NodeInfo[];
};

declare const language: Language;
export = language;
18 changes: 3 additions & 15 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
try {
module.exports = require("../../build/Release/tree_sitter_beancount_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_beancount_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
const root = require("path").join(__dirname, "..", "..");

module.exports = require("node-gyp-build")(root);

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
Expand Down
3 changes: 3 additions & 0 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ fn main() {
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs")
.flag_if_supported("-O");
#[cfg(target_env = "msvc")]
c_config.flag("-utf-8");

let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);

Expand Down

0 comments on commit 384c55e

Please sign in to comment.