diff --git a/binding/exported-functions.txt b/binding/exported-functions.txt index 0caaf32..deff335 100644 --- a/binding/exported-functions.txt +++ b/binding/exported-functions.txt @@ -1,5 +1,6 @@ _free _malloc +_xmlCleanupInputCallbacks _xmlCtxtReadFile _xmlCtxtReadMemory _xmlCtxtSetErrorHandler @@ -14,6 +15,7 @@ _xmlNewDocComment _xmlNewDocText _xmlNewParserCtxt _xmlNodeGetContent +_xmlRegisterInputCallbacks _xmlRelaxNGFree _xmlRelaxNGFreeParserCtxt _xmlRelaxNGFreeValidCtxt diff --git a/src/libxml2.mts b/src/libxml2.mts index 2eee271..268592e 100644 --- a/src/libxml2.mts +++ b/src/libxml2.mts @@ -1,3 +1,5 @@ +import { assert } from 'console'; +import { openSync, readSync, closeSync } from 'fs'; import type { CString, Pointer, @@ -293,6 +295,33 @@ export class XmlErrorStruct { static col = getValueFunc(40, 'i32'); } +const matchFunc = (filename: CString) => { console.log(`match called for ${filename}`); return 1; }; +const openFunc = (filename: CString) => { + console.log(`Opening ${filename}`); + return openSync(moveUtf8ToString(filename), 'r'); +}; +const readFunc = (ctx: Pointer, buf: Pointer, len: number) => { + console.log(`reading from ${ctx} with ${buf}`); + return readSync(ctx, buf as unknown as Uint8Array, 0, len, null); +}; +const closeFunc = (ctx: Pointer) => { + console.log(`closing ${ctx}`); + closeSync(ctx); + return 0; +}; + +function registerInputCallbacks(): void { + console.log('Registering callbacks'); + const regResult = libxml2._xmlRegisterInputCallbacks( + matchFunc, + openFunc, + readFunc, + closeFunc, + ); + assert(regResult !== -1, 'registerInputCallbacks failed'); + console.log(`Registered callbacks ${regResult}`); +} + export const xmlCtxtSetErrorHandler = libxml2._xmlCtxtSetErrorHandler; export const xmlDocGetRootElement = libxml2._xmlDocGetRootElement; export const xmlFreeDoc = libxml2._xmlFreeDoc; @@ -325,3 +354,6 @@ export const xmlXPathFreeContext = libxml2._xmlXPathFreeContext; export const xmlXPathFreeObject = libxml2._xmlXPathFreeObject; export const xmlXPathNewContext = libxml2._xmlXPathNewContext; export const xmlXPathSetContextNode = libxml2._xmlXPathSetContextNode; + +registerInputCallbacks(); +registerInputCallbacks(); diff --git a/src/libxml2raw.d.ts b/src/libxml2raw.d.ts index 3a934f2..8e6b879 100644 --- a/src/libxml2raw.d.ts +++ b/src/libxml2raw.d.ts @@ -25,6 +25,7 @@ export class LibXml2 { _free(memblock: Pointer): void; _malloc(size: number): Pointer; + _xmlCleanupInputCallbacks(): void; _xmlCtxtReadFile( ctxt: XmlParserCtxtPtr, filename: CString, @@ -53,6 +54,12 @@ export class LibXml2 { _xmlNewDoc(): XmlDocPtr; _xmlNewParserCtxt(): XmlParserCtxtPtr; _xmlNodeGetContent(node: XmlNodePtr): CString; + _xmlRegisterInputCallbacks( + xmlInputMatchCallback: (filename: CString) => number, + xmlInputOpenCallback: (filename: CString) => Pointer, + xmlInputReadCallback: (ctx: Pointer, buffer: Pointer, length: number) => number, + xmlInputCloseCallback: (ctx: Pointer) => number, + ): number; _xmlRelaxNGFree(schema: XmlRelaxNGPtr): void; _xmlRelaxNGFreeParserCtxt(ctxt: XmlRelaxNGParserCtxtPtr): void; _xmlRelaxNGFreeValidCtxt(ctxt: XmlRelaxNGValidCtxtPtr): void;