Avoid wasm calls in Node.startIndex and Node.startPosition

This commit is contained in:
Max Brunsfeld 2019-09-11 14:44:49 -07:00
parent 0d913dec65
commit 307a1a6c11
2 changed files with 29 additions and 30 deletions

View file

@ -204,10 +204,6 @@ class Node {
this.tree = tree;
}
get id() {
return this[0];
}
get typeId() {
marshalNode(this);
return C._ts_node_symbol_wasm(this.tree);
@ -217,23 +213,12 @@ class Node {
return this.tree.language.types[this.typeId] || 'ERROR';
}
get startPosition() {
marshalNode(this);
C._ts_node_start_point_wasm(this.tree[0]);
return unmarshalPoint(TRANSFER_BUFFER);
}
get endPosition() {
marshalNode(this);
C._ts_node_end_point_wasm(this.tree[0]);
return unmarshalPoint(TRANSFER_BUFFER);
}
get startIndex() {
marshalNode(this);
return C._ts_node_start_index_wasm(this.tree[0]);
}
get endIndex() {
marshalNode(this);
return C._ts_node_end_index_wasm(this.tree[0]);
@ -843,22 +828,36 @@ function isPoint(point) {
function marshalNode(node) {
let address = TRANSFER_BUFFER;
for (let i = 0; i < 5; i++) {
setValue(address, node[i], 'i32');
address += SIZE_OF_INT;
}
setValue(address, node.id, 'i32');
address += SIZE_OF_INT;
setValue(address, node.startIndex, 'i32');
address += SIZE_OF_INT;
setValue(address, node.startPosition.row, 'i32');
address += SIZE_OF_INT;
setValue(address, node.startPosition.column, 'i32');
address += SIZE_OF_INT;
setValue(address, node[0], 'i32');
}
function unmarshalNode(tree, address = TRANSFER_BUFFER) {
const id = getValue(address, 'i32');
if (id === 0) return null;
const result = new Node(INTERNAL, tree);
result[0] = id;
address += SIZE_OF_INT;
for (let i = 1; i < 5; i++) {
result[i] = getValue(address, 'i32');
address += SIZE_OF_INT;
}
if (id === 0) return null;
const index = getValue(address, 'i32');
address += SIZE_OF_INT;
const row = getValue(address, 'i32');
address += SIZE_OF_INT;
const column = getValue(address, 'i32');
address += SIZE_OF_INT;
const other = getValue(address, 'i32');
const result = new Node(INTERNAL, tree);
result.id = id;
result.startIndex = index;
result.startPosition = {row, column};
result[0] = other;
return result;
}