L 记事本

node调用dylib方法

   #流水账 #工具 

用cmake编译了一个dylib,提供接口给node调用。


var path = './libticketenc.dylib'
var libm = ffi.Library(path, {
  t_encode: ["int", ["byte *", "int", "byte *", "int *"]],
  t_decode: ["int", ["byte *", "int", "byte *", "int *"]],
});


 encode(source: string): string {
    var bfSource = Buffer.from(source, "utf-8");
    var lenSource = bfSource.length;
    var bfOut = Buffer.alloc(lenSource + 1024);
    var bfLen = Buffer.alloc(4);
    bfLen.type = ref.types.int;
    libm.t_encode(bfSource, lenSource, bfOut, bfLen as any);
    var len = bfLen.readInt32LE()
    var bfResult = bfOut.subarray(0,len)
    return bfResult.toString("base64");
  }