From 4d1f93f9499e93ac4f634dfc151edd0480caad67 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Tue, 24 Sep 2024 11:41:15 +0300 Subject: [PATCH] Add hb.version() and rename hb.version to hb.version_string() hb.version() returns an object with major, minor, and micro properties as numbers, while hb.version_string() returns a string. --- hbjs.js | 21 +++++++++++++++++++-- test/index.js | 12 ++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/hbjs.js b/hbjs.js index 5d07a38..d618435 100644 --- a/hbjs.js +++ b/hbjs.js @@ -533,7 +533,23 @@ function hbjs(Module) { return trace; } - function get_version() { + function version() { + var major = exports.malloc(4); + var minor = exports.malloc(4); + var micro = exports.malloc(4); + exports.hb_version(major, minor, micro); + var version = { + major: heapu32[major / 4], + minor: heapu32[minor / 4], + micro: heapu32[micro / 4], + }; + exports.free(major); + exports.free(minor); + exports.free(micro); + return version; + } + + function version_string() { var versionPtr = exports.hb_version_string(); var version = utf8Decoder.decode(heapu8.subarray(versionPtr, heapu8.indexOf(0, versionPtr))); return version; @@ -546,7 +562,8 @@ function hbjs(Module) { createBuffer: createBuffer, shape: shape, shapeWithTrace: shapeWithTrace, - version: get_version(), + version: version, + version_string: version_string, }; } diff --git a/test/index.js b/test/index.js index 7a52dd6..48f17cb 100644 --- a/test/index.js +++ b/test/index.js @@ -1,6 +1,7 @@ const fs = require('fs'); const path = require('path'); const {expect} = require('chai'); +const exp = require('constants'); let hb; before(async function () { @@ -249,8 +250,15 @@ describe('shape', function () { }); describe('misc', function () { + it('get version', function () { + const version = hb.version(); + expect(version).to.have.property('major').that.is.a('number'); + expect(version).to.have.property('minor').that.is.a('number'); + expect(version).to.have.property('micro').that.is.a('number'); + expect(version.major).to.be.at.least(10); + }); it('get version string', function () { - const version = hb.version - expect(version).to.match(/^\d+\.\d+\.\d+$/); + const version_string = hb.version_string(); + expect(version_string).to.match(/^\d+\.\d+\.\d+$/); }); });