feat(web)!: use the WASM module in the bindings, and not the other way around
Parser is no longer the default export, but you *must* call `Parser.init()` before doing anything still
This commit is contained in:
parent
b1e39d2dba
commit
be7716dfa7
29 changed files with 613 additions and 662 deletions
14
lib/binding_web/src/bindings.ts
Normal file
14
lib/binding_web/src/bindings.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import createModule, { type MainModule } from '../lib/tree-sitter';
|
||||
|
||||
export let Module: MainModule | null = null;
|
||||
|
||||
export async function initializeBinding(moduleOptions: EmscriptenModule): Promise<MainModule> {
|
||||
if (!Module) {
|
||||
Module = await createModule(moduleOptions);
|
||||
}
|
||||
return Module;
|
||||
}
|
||||
|
||||
export function checkModule(): boolean {
|
||||
return !!Module;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { CaptureQuantifier } from './query';
|
||||
import { type MainModule } from '../lib/tree-sitter';
|
||||
|
||||
export interface Point {
|
||||
row: number;
|
||||
|
|
@ -56,183 +56,8 @@ export function isPoint(point?: Point): point is Point {
|
|||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
export const C: EmscriptenModule & {
|
||||
// Global
|
||||
_ts_init(): number;
|
||||
export function setModule(module: MainModule) {
|
||||
C = module;
|
||||
}
|
||||
|
||||
// Libc
|
||||
_malloc(size: number): number;
|
||||
_calloc(count: number, size: number): number;
|
||||
_free(ptr: number): void;
|
||||
|
||||
// Parser
|
||||
_ts_parser_new_wasm(): void;
|
||||
_ts_parser_delete(address: number): void;
|
||||
_ts_parser_set_language(parserAddress: number, languageAddress: number): void;
|
||||
_ts_parser_enable_logger_wasm(address: number, enabled: number): void;
|
||||
_ts_parser_parse_wasm(
|
||||
address: number,
|
||||
payload: number,
|
||||
oldTreeAddress: number,
|
||||
rangeAddress: number,
|
||||
rangeCount: number
|
||||
): number;
|
||||
_ts_parser_reset(address: number): void;
|
||||
_ts_parser_timeout_micros(address: number): number;
|
||||
_ts_parser_set_timeout_micros(address: number, timeout: number): void;
|
||||
_ts_parser_included_ranges_wasm(address: number): void;
|
||||
|
||||
// Language
|
||||
_ts_language_symbol_count(address: number): number;
|
||||
_ts_language_symbol_name(address: number, index: number): number;
|
||||
_ts_language_symbol_type(address: number, index: number): number;
|
||||
_ts_language_field_count(address: number): number;
|
||||
_ts_language_field_name_for_id(address: number, id: number): number;
|
||||
_ts_language_name(address: number): number;
|
||||
_ts_language_version(address: number): number;
|
||||
_ts_language_state_count(address: number): number;
|
||||
_ts_language_symbol_for_name(address: number, typeAddress: number, typeLength: number, named: boolean): number;
|
||||
_ts_language_type_is_named_wasm(address: number, typeId: number): number;
|
||||
_ts_language_type_is_visible_wasm(address: number, typeId: number): number;
|
||||
_ts_language_next_state(address: number, stateId: number, typeId: number): number;
|
||||
_ts_language_supertypes_wasm(address: number): void;
|
||||
_ts_language_subtypes_wasm(address: number, supertype: number): void;
|
||||
|
||||
// Tree
|
||||
_ts_tree_copy(tree: number): number;
|
||||
_ts_tree_delete(tree: number): void;
|
||||
_ts_tree_edit_wasm(tree: number): void;
|
||||
_ts_tree_root_node_wasm(tree: number): void;
|
||||
_ts_tree_root_node_with_offset_wasm(tree: number): void;
|
||||
_ts_tree_get_changed_ranges_wasm(self: number, other: number): void;
|
||||
_ts_tree_included_ranges_wasm(self: number): void;
|
||||
|
||||
// Node
|
||||
_ts_node_symbol_wasm(tree: number): number;
|
||||
_ts_node_grammar_symbol_wasm(tree: number): number;
|
||||
_ts_node_end_point_wasm(tree: number): void;
|
||||
_ts_node_end_index_wasm(tree: number): number;
|
||||
_ts_node_parse_state_wasm(tree: number): number;
|
||||
_ts_node_next_parse_state_wasm(tree: number): number;
|
||||
_ts_node_is_named_wasm(tree: number): number;
|
||||
_ts_node_has_error_wasm(tree: number): number;
|
||||
_ts_node_has_changes_wasm(tree: number): number;
|
||||
_ts_node_is_error_wasm(tree: number): number;
|
||||
_ts_node_is_missing_wasm(tree: number): number;
|
||||
_ts_node_is_extra_wasm(tree: number): number;
|
||||
_ts_node_child_wasm(tree: number, index: number): void;
|
||||
_ts_node_named_child_wasm(tree: number, index: number): void;
|
||||
_ts_node_child_by_field_id_wasm(tree: number, fieldId: number): void;
|
||||
_ts_node_field_name_for_child_wasm(tree: number, index: number): number;
|
||||
_ts_node_field_name_for_named_child_wasm(tree: number, index: number): number;
|
||||
_ts_node_children_by_field_id_wasm(tree: number, fieldId: number): void;
|
||||
_ts_node_first_child_for_byte_wasm(tree: number): void;
|
||||
_ts_node_first_named_child_for_byte_wasm(tree: number): void;
|
||||
_ts_node_child_count_wasm(tree: number): number;
|
||||
_ts_node_named_child_count_wasm(tree: number): number;
|
||||
_ts_node_children_wasm(tree: number): void;
|
||||
_ts_node_named_children_wasm(tree: number): void;
|
||||
_ts_node_descendants_of_type_wasm(
|
||||
tree: number,
|
||||
symbolsAddress: number,
|
||||
symbolCount: number,
|
||||
startRow: number,
|
||||
startColumn: number,
|
||||
endRow: number,
|
||||
endColumn: number
|
||||
): void;
|
||||
_ts_node_next_sibling_wasm(tree: number): void;
|
||||
_ts_node_prev_sibling_wasm(tree: number): void;
|
||||
_ts_node_next_named_sibling_wasm(tree: number): void;
|
||||
_ts_node_prev_named_sibling_wasm(tree: number): void;
|
||||
_ts_node_descendant_count_wasm(tree: number): number;
|
||||
_ts_node_parent_wasm(tree: number): void;
|
||||
_ts_node_descendant_for_index_wasm(tree: number): void;
|
||||
_ts_node_named_descendant_for_index_wasm(tree: number): void;
|
||||
_ts_node_descendant_for_position_wasm(tree: number): void;
|
||||
_ts_node_named_descendant_for_position_wasm(tree: number): void;
|
||||
_ts_tree_cursor_new_wasm(tree: number): void;
|
||||
_ts_node_to_string_wasm(tree: number): number;
|
||||
|
||||
// TreeCursor
|
||||
_ts_tree_cursor_copy_wasm(cursor: number): void;
|
||||
_ts_tree_cursor_delete_wasm(cursor: number): void;
|
||||
_ts_tree_cursor_reset_wasm(cursor: number): void;
|
||||
_ts_tree_cursor_reset_to_wasm(cursor: number, other: number): void;
|
||||
_ts_tree_cursor_current_node_type_id_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_current_node_state_id_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_current_node_id_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_current_node_is_named_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_current_node_is_missing_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_start_index_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_end_index_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_start_position_wasm(cursor: number): void;
|
||||
_ts_tree_cursor_end_position_wasm(cursor: number): void;
|
||||
_ts_tree_cursor_current_node_wasm(cursor: number): void;
|
||||
_ts_tree_cursor_current_field_id_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_current_depth_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_current_descendant_index_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_first_child_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_last_child_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_first_child_for_index_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_first_child_for_position_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_next_sibling_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_previous_sibling_wasm(cursor: number): number;
|
||||
_ts_tree_cursor_goto_descendant_wasm(cursor: number, index: number): void;
|
||||
_ts_tree_cursor_goto_parent_wasm(cursor: number): number;
|
||||
|
||||
// Query
|
||||
_ts_query_new(languageAddress: number, sourceAddress: number, sourceLength: number, errorOffset: number, errorType: number): number;
|
||||
_ts_query_string_count(address: number): number;
|
||||
_ts_query_capture_count(address: number): number;
|
||||
_ts_query_pattern_count(address: number): number;
|
||||
_ts_query_capture_name_for_id(address: number, id: number, buffer: number): number;
|
||||
_ts_query_capture_quantifier_for_id(address: number, patternId: number, captureId: number): CaptureQuantifier;
|
||||
_ts_query_string_value_for_id(address: number, id: number, buffer: number): number;
|
||||
_ts_query_predicates_for_pattern(address: number, patternId: number, buffer: number): number;
|
||||
_ts_query_delete(address: number): void;
|
||||
_ts_query_matches_wasm(
|
||||
address: number,
|
||||
treeAddress: number,
|
||||
startRow: number,
|
||||
startColumn: number,
|
||||
endRow: number,
|
||||
endColumn: number,
|
||||
startIndex: number,
|
||||
endIndex: number,
|
||||
matchLimit: number,
|
||||
maxStartDepth: number,
|
||||
timeoutMicros: number
|
||||
): void;
|
||||
_ts_query_captures_wasm(
|
||||
address: number,
|
||||
treeAddress: number,
|
||||
startRow: number,
|
||||
startColumn: number,
|
||||
endRow: number,
|
||||
endColumn: number,
|
||||
startIndex: number,
|
||||
endIndex: number,
|
||||
matchLimit: number,
|
||||
maxStartDepth: number,
|
||||
timeoutMicros: number
|
||||
): void;
|
||||
_ts_query_disable_capture(address: number, nameAddress: number, nameLength: number): void;
|
||||
_ts_query_disable_pattern(address: number, patternIndex: number): void;
|
||||
_ts_query_start_byte_for_pattern(address: number, patternIndex: number): number;
|
||||
_ts_query_end_byte_for_pattern(address: number, patternIndex: number): number;
|
||||
_ts_query_is_pattern_non_local(address: number, patternIndex: number): number;
|
||||
_ts_query_is_pattern_rooted(address: number, patternIndex: number): number;
|
||||
_ts_query_is_pattern_guaranteed_at_step(address: number, patternIndex: number, stepIndex: number): number;
|
||||
|
||||
// LookaheadIterator
|
||||
_ts_lookahead_iterator_new(address: number, stateId: number): number;
|
||||
_ts_lookahead_iterator_current_symbol(address: number): number;
|
||||
_ts_lookahead_iterator_delete(address: number): void;
|
||||
_ts_lookahead_iterator_reset_state(address: number, stateId: number): boolean;
|
||||
_ts_lookahead_iterator_reset(address: number, languageAddress: number, stateId: number): boolean;
|
||||
_ts_lookahead_iterator_next(address: number): boolean;
|
||||
|
||||
// @ts-expect-error Module is defined after compilation
|
||||
} = Module;
|
||||
export let C: MainModule;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import { INTERNAL, Internal, assertInternal, SIZE_OF_INT, SIZE_OF_SHORT, C } from './constants';
|
||||
import { C, INTERNAL, Internal, assertInternal, SIZE_OF_INT, SIZE_OF_SHORT } from './constants';
|
||||
import { LookaheadIterator } from './lookahead_iterator';
|
||||
import { Node } from './node';
|
||||
import { TRANSFER_BUFFER } from './parser';
|
||||
import { CaptureQuantifier, Predicate, PredicateStep, Properties, Query, TextPredicate } from './query';
|
||||
|
||||
declare const loadWebAssemblyModule: (bytes: Uint8Array, options: { loadAsync: boolean }) => Promise<Record<string, () => number>>;
|
||||
|
||||
const PREDICATE_STEP_TYPE_CAPTURE = 1;
|
||||
const PREDICATE_STEP_TYPE_STRING = 2;
|
||||
|
||||
|
|
@ -23,14 +21,14 @@ export class Language {
|
|||
this.types = new Array<string>(C._ts_language_symbol_count(this[0]));
|
||||
for (let i = 0, n = this.types.length; i < n; i++) {
|
||||
if (C._ts_language_symbol_type(this[0], i) < 2) {
|
||||
this.types[i] = UTF8ToString(C._ts_language_symbol_name(this[0], i));
|
||||
this.types[i] = C.UTF8ToString(C._ts_language_symbol_name(this[0], i));
|
||||
}
|
||||
}
|
||||
this.fields = new Array<string>(C._ts_language_field_count(this[0]) + 1);
|
||||
for (let i = 0, n = this.fields.length; i < n; i++) {
|
||||
const fieldName = C._ts_language_field_name_for_id(this[0], i);
|
||||
if (fieldName !== 0) {
|
||||
this.fields[i] = UTF8ToString(fieldName);
|
||||
this.fields[i] = C.UTF8ToString(fieldName);
|
||||
} else {
|
||||
this.fields[i] = null;
|
||||
}
|
||||
|
|
@ -40,7 +38,7 @@ export class Language {
|
|||
get name(): string | null {
|
||||
const ptr = C._ts_language_name(this[0]);
|
||||
if (ptr === 0) return null;
|
||||
return UTF8ToString(ptr);
|
||||
return C.UTF8ToString(ptr);
|
||||
}
|
||||
|
||||
get version(): number {
|
||||
|
|
@ -65,10 +63,10 @@ export class Language {
|
|||
}
|
||||
|
||||
idForNodeType(type: string, named: boolean): number | null {
|
||||
const typeLength = lengthBytesUTF8(type);
|
||||
const typeLength = C.lengthBytesUTF8(type);
|
||||
const typeAddress = C._malloc(typeLength + 1);
|
||||
stringToUTF8(type, typeAddress, typeLength + 1);
|
||||
const result = C._ts_language_symbol_for_name(this[0], typeAddress, typeLength, named);
|
||||
C.stringToUTF8(type, typeAddress, typeLength + 1);
|
||||
const result = C._ts_language_symbol_for_name(this[0], typeAddress, typeLength, named ? 1 : 0);
|
||||
C._free(typeAddress);
|
||||
return result || null;
|
||||
}
|
||||
|
|
@ -79,7 +77,7 @@ export class Language {
|
|||
|
||||
nodeTypeForId(typeId: number): string | null {
|
||||
const name = C._ts_language_symbol_name(this[0], typeId);
|
||||
return name ? UTF8ToString(name) : null;
|
||||
return name ? C.UTF8ToString(name) : null;
|
||||
}
|
||||
|
||||
nodeTypeIsNamed(typeId: number): boolean {
|
||||
|
|
@ -92,14 +90,14 @@ export class Language {
|
|||
|
||||
get supertypes(): number[] {
|
||||
C._ts_language_supertypes_wasm(this[0]);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<number>(count);
|
||||
|
||||
if (count > 0) {
|
||||
let address = buffer;
|
||||
for (let i = 0; i < count; i++) {
|
||||
result[i] = getValue(address, 'i16');
|
||||
result[i] = C.getValue(address, 'i16');
|
||||
address += SIZE_OF_SHORT;
|
||||
}
|
||||
}
|
||||
|
|
@ -109,14 +107,14 @@ export class Language {
|
|||
|
||||
subtypes(supertype: number): number[] {
|
||||
C._ts_language_subtypes_wasm(this[0], supertype);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<number>(count);
|
||||
|
||||
if (count > 0) {
|
||||
let address = buffer;
|
||||
for (let i = 0; i < count; i++) {
|
||||
result[i] = getValue(address, 'i16');
|
||||
result[i] = C.getValue(address, 'i16');
|
||||
address += SIZE_OF_SHORT;
|
||||
}
|
||||
}
|
||||
|
|
@ -135,9 +133,9 @@ export class Language {
|
|||
}
|
||||
|
||||
query(source: string): Query {
|
||||
const sourceLength = lengthBytesUTF8(source);
|
||||
const sourceLength = C.lengthBytesUTF8(source);
|
||||
const sourceAddress = C._malloc(sourceLength + 1);
|
||||
stringToUTF8(source, sourceAddress, sourceLength + 1);
|
||||
C.stringToUTF8(source, sourceAddress, sourceLength + 1);
|
||||
const address = C._ts_query_new(
|
||||
this[0],
|
||||
sourceAddress,
|
||||
|
|
@ -147,9 +145,9 @@ export class Language {
|
|||
);
|
||||
|
||||
if (!address) {
|
||||
const errorId = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const errorByte = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const errorIndex = UTF8ToString(sourceAddress, errorByte).length;
|
||||
const errorId = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const errorByte = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const errorIndex = C.UTF8ToString(sourceAddress, errorByte).length;
|
||||
const suffix = source.slice(errorIndex, errorIndex + 100).split('\n')[0];
|
||||
let word = suffix.match(QUERY_WORD_REGEX)?.[0] ?? '';
|
||||
let error: Error;
|
||||
|
|
@ -195,15 +193,15 @@ export class Language {
|
|||
i,
|
||||
TRANSFER_BUFFER
|
||||
);
|
||||
const nameLength = getValue(TRANSFER_BUFFER, 'i32');
|
||||
captureNames[i] = UTF8ToString(nameAddress, nameLength);
|
||||
const nameLength = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
captureNames[i] = C.UTF8ToString(nameAddress, nameLength);
|
||||
}
|
||||
|
||||
for (let i = 0; i < patternCount; i++) {
|
||||
const captureQuantifiersArray = new Array<CaptureQuantifier>(captureCount);
|
||||
for (let j = 0; j < captureCount; j++) {
|
||||
const quantifier = C._ts_query_capture_quantifier_for_id(address, i, j);
|
||||
captureQuantifiersArray[j] = quantifier;
|
||||
captureQuantifiersArray[j] = quantifier as CaptureQuantifier;
|
||||
}
|
||||
captureQuantifiers[i] = captureQuantifiersArray;
|
||||
}
|
||||
|
|
@ -214,8 +212,8 @@ export class Language {
|
|||
i,
|
||||
TRANSFER_BUFFER
|
||||
);
|
||||
const nameLength = getValue(TRANSFER_BUFFER, 'i32');
|
||||
stringValues[i] = UTF8ToString(valueAddress, nameLength);
|
||||
const nameLength = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
stringValues[i] = C.UTF8ToString(valueAddress, nameLength);
|
||||
}
|
||||
|
||||
const setProperties = new Array<Properties>(patternCount);
|
||||
|
|
@ -230,7 +228,7 @@ export class Language {
|
|||
i,
|
||||
TRANSFER_BUFFER
|
||||
);
|
||||
const stepCount = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const stepCount = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
|
||||
predicates[i] = [];
|
||||
textPredicates[i] = [];
|
||||
|
|
@ -238,9 +236,9 @@ export class Language {
|
|||
const steps: PredicateStep[] = [];
|
||||
let stepAddress = predicatesAddress;
|
||||
for (let j = 0; j < stepCount; j++) {
|
||||
const stepType = getValue(stepAddress, 'i32');
|
||||
const stepType = C.getValue(stepAddress, 'i32');
|
||||
stepAddress += SIZE_OF_INT;
|
||||
const stepValueId: number = getValue(stepAddress, 'i32');
|
||||
const stepValueId: number = C.getValue(stepAddress, 'i32');
|
||||
stepAddress += SIZE_OF_INT;
|
||||
|
||||
if (stepType === PREDICATE_STEP_TYPE_CAPTURE) {
|
||||
|
|
@ -469,7 +467,7 @@ export class Language {
|
|||
}
|
||||
}
|
||||
|
||||
const mod = await loadWebAssemblyModule(await bytes, { loadAsync: true });
|
||||
const mod = await C.loadWebAssemblyModule(await bytes, { loadAsync: true });
|
||||
const symbolNames = Object.keys(mod);
|
||||
const functionName = symbolNames.find((key) => LANGUAGE_FUNCTION_REGEX.test(key) &&
|
||||
!key.includes('external_scanner_'));
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Edit, INTERNAL, Point, Range, SIZE_OF_INT, SIZE_OF_NODE, SIZE_OF_POINT } from "./constants";
|
||||
import { Edit, INTERNAL, Point, Range, SIZE_OF_INT, SIZE_OF_NODE, SIZE_OF_POINT, C } from "./constants";
|
||||
import { Node } from "./node";
|
||||
import { Tree } from "./tree";
|
||||
import { Query } from "./query";
|
||||
|
|
@ -7,7 +7,7 @@ import { TRANSFER_BUFFER } from "./parser";
|
|||
|
||||
export function unmarshalCaptures(query: Query, tree: Tree, address: number, result: {name: string, node: Node}[]) {
|
||||
for (let i = 0, n = result.length; i < n; i++) {
|
||||
const captureIndex = getValue(address, 'i32');
|
||||
const captureIndex = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const node = unmarshalNode(tree, address)!;
|
||||
address += SIZE_OF_NODE;
|
||||
|
|
@ -18,29 +18,29 @@ export function unmarshalCaptures(query: Query, tree: Tree, address: number, res
|
|||
|
||||
export function marshalNode(node: Node) {
|
||||
let address = TRANSFER_BUFFER;
|
||||
setValue(address, node.id, 'i32');
|
||||
C.setValue(address, node.id, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
setValue(address, node.startIndex, 'i32');
|
||||
C.setValue(address, node.startIndex, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
setValue(address, node.startPosition.row, 'i32');
|
||||
C.setValue(address, node.startPosition.row, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
setValue(address, node.startPosition.column, 'i32');
|
||||
C.setValue(address, node.startPosition.column, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
setValue(address, node[0], 'i32');
|
||||
C.setValue(address, node[0], 'i32');
|
||||
}
|
||||
|
||||
export function unmarshalNode(tree: Tree, address = TRANSFER_BUFFER): Node | null {
|
||||
const id = getValue(address, 'i32');
|
||||
const id = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
if (id === 0) return null;
|
||||
|
||||
const index = getValue(address, 'i32');
|
||||
const index = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const row = getValue(address, 'i32');
|
||||
const row = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const column = getValue(address, 'i32');
|
||||
const column = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const other = getValue(address, 'i32');
|
||||
const other = C.getValue(address, 'i32');
|
||||
|
||||
const result = new Node(INTERNAL, {
|
||||
id,
|
||||
|
|
@ -54,28 +54,28 @@ export function unmarshalNode(tree: Tree, address = TRANSFER_BUFFER): Node | nul
|
|||
}
|
||||
|
||||
export function marshalTreeCursor(cursor: TreeCursor, address = TRANSFER_BUFFER) {
|
||||
setValue(address + 0 * SIZE_OF_INT, cursor[0], 'i32');
|
||||
setValue(address + 1 * SIZE_OF_INT, cursor[1], 'i32');
|
||||
setValue(address + 2 * SIZE_OF_INT, cursor[2], 'i32');
|
||||
setValue(address + 3 * SIZE_OF_INT, cursor[3], 'i32');
|
||||
C.setValue(address + 0 * SIZE_OF_INT, cursor[0], 'i32');
|
||||
C.setValue(address + 1 * SIZE_OF_INT, cursor[1], 'i32');
|
||||
C.setValue(address + 2 * SIZE_OF_INT, cursor[2], 'i32');
|
||||
C.setValue(address + 3 * SIZE_OF_INT, cursor[3], 'i32');
|
||||
}
|
||||
|
||||
export function unmarshalTreeCursor(cursor: TreeCursor) {
|
||||
cursor[0] = getValue(TRANSFER_BUFFER + 0 * SIZE_OF_INT, 'i32');
|
||||
cursor[1] = getValue(TRANSFER_BUFFER + 1 * SIZE_OF_INT, 'i32');
|
||||
cursor[2] = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
|
||||
cursor[3] = getValue(TRANSFER_BUFFER + 3 * SIZE_OF_INT, 'i32');
|
||||
cursor[0] = C.getValue(TRANSFER_BUFFER + 0 * SIZE_OF_INT, 'i32');
|
||||
cursor[1] = C.getValue(TRANSFER_BUFFER + 1 * SIZE_OF_INT, 'i32');
|
||||
cursor[2] = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
|
||||
cursor[3] = C.getValue(TRANSFER_BUFFER + 3 * SIZE_OF_INT, 'i32');
|
||||
}
|
||||
|
||||
export function marshalPoint(address: number, point: Point): void {
|
||||
setValue(address, point.row, 'i32');
|
||||
setValue(address + SIZE_OF_INT, point.column, 'i32');
|
||||
C.setValue(address, point.row, 'i32');
|
||||
C.setValue(address + SIZE_OF_INT, point.column, 'i32');
|
||||
}
|
||||
|
||||
export function unmarshalPoint(address: number): Point {
|
||||
const result = {
|
||||
row: getValue(address, 'i32') >>> 0,
|
||||
column: getValue(address + SIZE_OF_INT, 'i32') >>> 0,
|
||||
row: C.getValue(address, 'i32') >>> 0,
|
||||
column: C.getValue(address + SIZE_OF_INT, 'i32') >>> 0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
|
@ -83,16 +83,16 @@ export function unmarshalPoint(address: number): Point {
|
|||
export function marshalRange(address: number, range: Range): void {
|
||||
marshalPoint(address, range.startPosition); address += SIZE_OF_POINT;
|
||||
marshalPoint(address, range.endPosition); address += SIZE_OF_POINT;
|
||||
setValue(address, range.startIndex, 'i32'); address += SIZE_OF_INT;
|
||||
setValue(address, range.endIndex, 'i32'); address += SIZE_OF_INT;
|
||||
C.setValue(address, range.startIndex, 'i32'); address += SIZE_OF_INT;
|
||||
C.setValue(address, range.endIndex, 'i32'); address += SIZE_OF_INT;
|
||||
}
|
||||
|
||||
export function unmarshalRange(address: number): Range {
|
||||
const result = {} as Range;
|
||||
result.startPosition = unmarshalPoint(address); address += SIZE_OF_POINT;
|
||||
result.endPosition = unmarshalPoint(address); address += SIZE_OF_POINT;
|
||||
result.startIndex = getValue(address, 'i32') >>> 0; address += SIZE_OF_INT;
|
||||
result.endIndex = getValue(address, 'i32') >>> 0;
|
||||
result.startIndex = C.getValue(address, 'i32') >>> 0; address += SIZE_OF_INT;
|
||||
result.endIndex = C.getValue(address, 'i32') >>> 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -100,7 +100,7 @@ export function marshalEdit(edit: Edit, address = TRANSFER_BUFFER) {
|
|||
marshalPoint(address, edit.startPosition); address += SIZE_OF_POINT;
|
||||
marshalPoint(address, edit.oldEndPosition); address += SIZE_OF_POINT;
|
||||
marshalPoint(address, edit.newEndPosition); address += SIZE_OF_POINT;
|
||||
setValue(address, edit.startIndex, 'i32'); address += SIZE_OF_INT;
|
||||
setValue(address, edit.oldEndIndex, 'i32'); address += SIZE_OF_INT;
|
||||
setValue(address, edit.newEndIndex, 'i32'); address += SIZE_OF_INT;
|
||||
C.setValue(address, edit.startIndex, 'i32'); address += SIZE_OF_INT;
|
||||
C.setValue(address, edit.oldEndIndex, 'i32'); address += SIZE_OF_INT;
|
||||
C.setValue(address, edit.newEndIndex, 'i32'); address += SIZE_OF_INT;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import { TreeCursor } from './tree_cursor';
|
|||
import { marshalNode, marshalPoint, unmarshalNode, unmarshalPoint } from './marshal';
|
||||
import { TRANSFER_BUFFER } from './parser';
|
||||
|
||||
declare const AsciiToString: (ptr: number) => string;
|
||||
|
||||
export class Node {
|
||||
private [0]: number; // Internal handle for WASM
|
||||
private _children?: (Node | null)[];
|
||||
|
|
@ -145,14 +143,14 @@ export class Node {
|
|||
marshalNode(this);
|
||||
const address = C._ts_node_field_name_for_child_wasm(this.tree[0], index);
|
||||
if (!address) return null;
|
||||
return AsciiToString(address);
|
||||
return C.AsciiToString(address);
|
||||
}
|
||||
|
||||
fieldNameForNamedChild(index: number): string | null {
|
||||
marshalNode(this);
|
||||
const address = C._ts_node_field_name_for_named_child_wasm(this.tree[0], index);
|
||||
if (!address) return null;
|
||||
return AsciiToString(address);
|
||||
return C.AsciiToString(address);
|
||||
}
|
||||
|
||||
childrenForFieldName(fieldName: string): (Node | null)[] {
|
||||
|
|
@ -164,8 +162,8 @@ export class Node {
|
|||
childrenForFieldId(fieldId: number): (Node | null)[] {
|
||||
marshalNode(this);
|
||||
C._ts_node_children_by_field_id_wasm(this.tree[0], fieldId);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<Node | null>(count);
|
||||
|
||||
if (count > 0) {
|
||||
|
|
@ -182,7 +180,7 @@ export class Node {
|
|||
firstChildForIndex(index: number): Node | null {
|
||||
marshalNode(this);
|
||||
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
|
||||
setValue(address, index, 'i32');
|
||||
C.setValue(address, index, 'i32');
|
||||
C._ts_node_first_child_for_byte_wasm(this.tree[0]);
|
||||
return unmarshalNode(this.tree);
|
||||
}
|
||||
|
|
@ -190,7 +188,7 @@ export class Node {
|
|||
firstNamedChildForIndex(index: number): Node | null {
|
||||
marshalNode(this);
|
||||
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
|
||||
setValue(address, index, 'i32');
|
||||
C.setValue(address, index, 'i32');
|
||||
C._ts_node_first_named_child_for_byte_wasm(this.tree[0]);
|
||||
return unmarshalNode(this.tree);
|
||||
}
|
||||
|
|
@ -225,8 +223,8 @@ export class Node {
|
|||
if (!this._children) {
|
||||
marshalNode(this);
|
||||
C._ts_node_children_wasm(this.tree[0]);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
this._children = new Array<Node>(count);
|
||||
if (count > 0) {
|
||||
let address = buffer;
|
||||
|
|
@ -244,8 +242,8 @@ export class Node {
|
|||
if (!this._namedChildren) {
|
||||
marshalNode(this);
|
||||
C._ts_node_named_children_wasm(this.tree[0]);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
this._namedChildren = new Array<Node>(count);
|
||||
if (count > 0) {
|
||||
let address = buffer;
|
||||
|
|
@ -278,7 +276,7 @@ export class Node {
|
|||
// Copy the array of symbols to the WASM heap
|
||||
const symbolsAddress = C._malloc(SIZE_OF_INT * symbols.length);
|
||||
for (let i = 0, n = symbols.length; i < n; i++) {
|
||||
setValue(symbolsAddress + i * SIZE_OF_INT, symbols[i], 'i32');
|
||||
C.setValue(symbolsAddress + i * SIZE_OF_INT, symbols[i], 'i32');
|
||||
}
|
||||
|
||||
// Call the C API to compute the descendants
|
||||
|
|
@ -294,8 +292,8 @@ export class Node {
|
|||
);
|
||||
|
||||
// Instantiate the nodes based on the data returned
|
||||
const descendantCount = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const descendantAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const descendantCount = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const descendantAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<Node | null>(descendantCount);
|
||||
if (descendantCount > 0) {
|
||||
let address = descendantAddress;
|
||||
|
|
@ -353,8 +351,8 @@ export class Node {
|
|||
|
||||
marshalNode(this);
|
||||
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
|
||||
setValue(address, start, 'i32');
|
||||
setValue(address + SIZE_OF_INT, end, 'i32');
|
||||
C.setValue(address, start, 'i32');
|
||||
C.setValue(address + SIZE_OF_INT, end, 'i32');
|
||||
C._ts_node_descendant_for_index_wasm(this.tree[0]);
|
||||
return unmarshalNode(this.tree);
|
||||
}
|
||||
|
|
@ -366,8 +364,8 @@ export class Node {
|
|||
|
||||
marshalNode(this);
|
||||
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
|
||||
setValue(address, start, 'i32');
|
||||
setValue(address + SIZE_OF_INT, end, 'i32');
|
||||
C.setValue(address, start, 'i32');
|
||||
C.setValue(address + SIZE_OF_INT, end, 'i32');
|
||||
C._ts_node_named_descendant_for_index_wasm(this.tree[0]);
|
||||
return unmarshalNode(this.tree);
|
||||
}
|
||||
|
|
@ -437,7 +435,7 @@ export class Node {
|
|||
toString() {
|
||||
marshalNode(this);
|
||||
const address = C._ts_node_to_string_wasm(this.tree[0]);
|
||||
const result = AsciiToString(address);
|
||||
const result = C.AsciiToString(address);
|
||||
C._free(address);
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { C, INTERNAL, Point, Range, SIZE_OF_INT, SIZE_OF_RANGE } from './constants';
|
||||
import { C, INTERNAL, Point, Range, SIZE_OF_INT, SIZE_OF_RANGE, setModule } from './constants';
|
||||
import { Language } from './language';
|
||||
import { marshalRange, unmarshalRange } from './marshal';
|
||||
import { checkModule, initializeBinding } from './bindings';
|
||||
import { Tree } from './tree';
|
||||
|
||||
interface ParseOptions {
|
||||
|
|
@ -17,29 +18,38 @@ export let TRANSFER_BUFFER: number;
|
|||
let VERSION: number;
|
||||
let MIN_COMPATIBLE_VERSION: number;
|
||||
|
||||
let currentParseCallback: ((index: number, position: Point) => string) | null = null;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
let currentLogCallback: LogCallback = null;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
let currentProgressCallback: ((percent: number) => void) | null = null;
|
||||
// declare let currentParseCallback: ((index: number, position: Point) => string) | null;
|
||||
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
// declare let currentLogCallback: LogCallback;
|
||||
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
// declare let currentProgressCallback: ((percent: number) => void) | null;
|
||||
|
||||
export class ParserImpl {
|
||||
export class Parser {
|
||||
protected [0] = 0;
|
||||
protected [1] = 0;
|
||||
protected language: Language | null = null;
|
||||
protected logCallback: LogCallback = null;
|
||||
static Language: typeof Language;
|
||||
|
||||
static init() {
|
||||
// This must always be called before creating a Parser.
|
||||
static async init(moduleOptions: EmscriptenModule) {
|
||||
setModule(await initializeBinding(moduleOptions));
|
||||
TRANSFER_BUFFER = C._ts_init();
|
||||
VERSION = getValue(TRANSFER_BUFFER, 'i32');
|
||||
MIN_COMPATIBLE_VERSION = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
VERSION = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
MIN_COMPATIBLE_VERSION = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
initialize() {
|
||||
if (!checkModule()) {
|
||||
throw new Error("cannot construct a Parser before calling `init()`");
|
||||
}
|
||||
C._ts_parser_new_wasm();
|
||||
this[0] = getValue(TRANSFER_BUFFER, 'i32');
|
||||
this[1] = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
this[0] = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
this[1] = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
}
|
||||
|
||||
delete() {
|
||||
|
|
@ -82,24 +92,24 @@ export class ParserImpl {
|
|||
options: ParseOptions = {}
|
||||
): Tree {
|
||||
if (typeof callback === 'string') {
|
||||
currentParseCallback = (index: number) => callback.slice(index);
|
||||
C.currentParseCallback = (index: number) => callback.slice(index);
|
||||
} else if (typeof callback === 'function') {
|
||||
currentParseCallback = callback;
|
||||
C.currentParseCallback = callback;
|
||||
} else {
|
||||
throw new Error('Argument must be a string or a function');
|
||||
}
|
||||
|
||||
if (options.progressCallback) {
|
||||
currentProgressCallback = options.progressCallback;
|
||||
C.currentProgressCallback = options.progressCallback;
|
||||
} else {
|
||||
currentProgressCallback = null;
|
||||
C.currentProgressCallback = null;
|
||||
}
|
||||
|
||||
if (this.logCallback) {
|
||||
currentLogCallback = this.logCallback;
|
||||
C.currentLogCallback = this.logCallback;
|
||||
C._ts_parser_enable_logger_wasm(this[0], 1);
|
||||
} else {
|
||||
currentLogCallback = null;
|
||||
C.currentLogCallback = null;
|
||||
C._ts_parser_enable_logger_wasm(this[0], 0);
|
||||
}
|
||||
|
||||
|
|
@ -124,9 +134,9 @@ export class ParserImpl {
|
|||
);
|
||||
|
||||
if (!treeAddress) {
|
||||
currentParseCallback = null;
|
||||
currentLogCallback = null;
|
||||
currentProgressCallback = null;
|
||||
C.currentParseCallback = null;
|
||||
C.currentLogCallback = null;
|
||||
C.currentProgressCallback = null;
|
||||
throw new Error('Parsing failed');
|
||||
}
|
||||
|
||||
|
|
@ -134,10 +144,10 @@ export class ParserImpl {
|
|||
throw new Error('Parser must have a language to parse');
|
||||
}
|
||||
|
||||
const result = new Tree(INTERNAL, treeAddress, this.language, currentParseCallback);
|
||||
currentParseCallback = null;
|
||||
currentLogCallback = null;
|
||||
currentProgressCallback = null;
|
||||
const result = new Tree(INTERNAL, treeAddress, this.language, C.currentParseCallback);
|
||||
C.currentParseCallback = null;
|
||||
C.currentLogCallback = null;
|
||||
C.currentProgressCallback = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -147,8 +157,8 @@ export class ParserImpl {
|
|||
|
||||
getIncludedRanges(): Range[] {
|
||||
C._ts_parser_included_ranges_wasm(this[0]);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<Range>(count);
|
||||
|
||||
if (count > 0) {
|
||||
|
|
@ -168,7 +178,7 @@ export class ParserImpl {
|
|||
}
|
||||
|
||||
setTimeoutMicros(timeout: number): void {
|
||||
C._ts_parser_set_timeout_micros(this[0], timeout);
|
||||
C._ts_parser_set_timeout_micros(this[0], 0, timeout);
|
||||
}
|
||||
|
||||
setLogger(callback: LogCallback): this {
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@ import { Node } from './node';
|
|||
import { marshalNode, unmarshalCaptures } from './marshal';
|
||||
import { TRANSFER_BUFFER } from './parser';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
let currentQueryProgressCallback: ((percent: number) => void) | null = null;
|
||||
// let currentQueryProgressCallback: ((percent: number) => void) | null = null;
|
||||
|
||||
interface QueryOptions {
|
||||
startPosition?: Point;
|
||||
|
|
@ -127,7 +126,7 @@ export class Query {
|
|||
}
|
||||
|
||||
if (progressCallback) {
|
||||
currentQueryProgressCallback = progressCallback;
|
||||
C.currentQueryProgressCallback = progressCallback;
|
||||
}
|
||||
|
||||
marshalNode(node);
|
||||
|
|
@ -146,18 +145,18 @@ export class Query {
|
|||
timeoutMicros,
|
||||
);
|
||||
|
||||
const rawCount = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const startAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const didExceedMatchLimit = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
|
||||
const rawCount = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
|
||||
const result = new Array<QueryMatch>(rawCount);
|
||||
this.exceededMatchLimit = Boolean(didExceedMatchLimit);
|
||||
|
||||
let filteredCount = 0;
|
||||
let address = startAddress;
|
||||
for (let i = 0; i < rawCount; i++) {
|
||||
const pattern = getValue(address, 'i32');
|
||||
const pattern = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const captureCount = getValue(address, 'i32');
|
||||
const captureCount = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
|
||||
const captures = new Array<Capture>(captureCount);
|
||||
|
|
@ -177,7 +176,7 @@ export class Query {
|
|||
result.length = filteredCount;
|
||||
|
||||
C._free(startAddress);
|
||||
currentQueryProgressCallback = null;
|
||||
C.currentQueryProgressCallback = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -211,7 +210,7 @@ export class Query {
|
|||
}
|
||||
|
||||
if (progressCallback) {
|
||||
currentQueryProgressCallback = progressCallback;
|
||||
C.currentQueryProgressCallback = progressCallback;
|
||||
}
|
||||
|
||||
marshalNode(node);
|
||||
|
|
@ -230,20 +229,20 @@ export class Query {
|
|||
timeoutMicros,
|
||||
);
|
||||
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const startAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const didExceedMatchLimit = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
|
||||
const result: Capture[] = [];
|
||||
this.exceededMatchLimit = Boolean(didExceedMatchLimit);
|
||||
|
||||
const captures: Capture[] = [];
|
||||
let address = startAddress;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const pattern = getValue(address, 'i32');
|
||||
const pattern = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const captureCount = getValue(address, 'i32');
|
||||
const captureCount = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const captureIndex = getValue(address, 'i32');
|
||||
const captureIndex = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
|
||||
captures.length = captureCount;
|
||||
|
|
@ -262,7 +261,7 @@ export class Query {
|
|||
}
|
||||
|
||||
C._free(startAddress);
|
||||
currentQueryProgressCallback = null;
|
||||
C.currentQueryProgressCallback = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -271,9 +270,9 @@ export class Query {
|
|||
}
|
||||
|
||||
disableCapture(captureName: string): void {
|
||||
const captureNameLength = lengthBytesUTF8(captureName);
|
||||
const captureNameLength = C.lengthBytesUTF8(captureName);
|
||||
const captureNameAddress = C._malloc(captureNameLength + 1);
|
||||
stringToUTF8(captureName, captureNameAddress, captureNameLength + 1);
|
||||
C.stringToUTF8(captureName, captureNameAddress, captureNameLength + 1);
|
||||
C._ts_query_disable_capture(this[0], captureNameAddress, captureNameLength);
|
||||
C._free(captureNameAddress);
|
||||
}
|
||||
|
|
@ -317,11 +316,7 @@ export class Query {
|
|||
return C._ts_query_is_pattern_rooted(this[0], patternIndex) === 1;
|
||||
}
|
||||
|
||||
isPatternGuaranteedAtStep(patternIndex: number, stepIndex: number): boolean {
|
||||
return C._ts_query_is_pattern_guaranteed_at_step(
|
||||
this[0],
|
||||
patternIndex,
|
||||
stepIndex
|
||||
) === 1;
|
||||
isPatternGuaranteedAtStep(byteIndex: number): boolean {
|
||||
return C._ts_query_is_pattern_guaranteed_at_step(this[0], byteIndex) === 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ export class Tree {
|
|||
|
||||
rootNodeWithOffset(offsetBytes: number, offsetExtent: Point): Node {
|
||||
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
|
||||
setValue(address, offsetBytes, 'i32');
|
||||
C.setValue(address, offsetBytes, 'i32');
|
||||
marshalPoint(address + SIZE_OF_INT, offsetExtent);
|
||||
C._ts_tree_root_node_with_offset_wasm(this[0]);
|
||||
return unmarshalNode(this)!;
|
||||
|
|
@ -81,8 +81,8 @@ export class Tree {
|
|||
}
|
||||
|
||||
C._ts_tree_get_changed_ranges_wasm(this[0], other[0]);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<Range>(count);
|
||||
|
||||
if (count > 0) {
|
||||
|
|
@ -98,8 +98,8 @@ export class Tree {
|
|||
|
||||
getIncludedRanges(): Range[] {
|
||||
C._ts_tree_included_ranges_wasm(this[0]);
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const count = C.getValue(TRANSFER_BUFFER, 'i32');
|
||||
const buffer = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array<Range>(count);
|
||||
|
||||
if (count > 0) {
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ export class TreeCursor {
|
|||
|
||||
gotoFirstChildForIndex(goalIndex: number): boolean {
|
||||
marshalTreeCursor(this);
|
||||
setValue(TRANSFER_BUFFER + SIZE_OF_CURSOR, goalIndex, 'i32');
|
||||
C.setValue(TRANSFER_BUFFER + SIZE_OF_CURSOR, goalIndex, 'i32');
|
||||
const result = C._ts_tree_cursor_goto_first_child_for_index_wasm(this.tree[0]);
|
||||
unmarshalTreeCursor(this);
|
||||
return result === 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue