[OV JS] Support add_extension method (#23848)

### Tickets:
 - 137673
This commit is contained in:
Vishniakov Nikolai 2024-04-15 15:05:24 +02:00 committed by GitHub
parent 9b13074624
commit fe629461e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 7 deletions

View File

@ -68,6 +68,7 @@ public:
Napi::Value set_property(const Napi::CallbackInfo& info);
Napi::Value get_property(const Napi::CallbackInfo& info);
void add_extension(const Napi::CallbackInfo& info);
protected:
Napi::Value compile_model_sync(const Napi::CallbackInfo& info,
const Napi::Object& model,

View File

@ -53,7 +53,8 @@ interface Core {
getProperty(
deviceName: string,
propertyName: string,
): string | number | boolean,
): string | number | boolean;
addExtension(libraryPath: string): void;
}
interface CoreConstructor {
new(): Core;

View File

@ -63,7 +63,8 @@ Napi::Function CoreWrap::get_class(Napi::Env env) {
InstanceMethod("getAvailableDevices", &CoreWrap::get_available_devices),
InstanceMethod("getVersions", &CoreWrap::get_versions),
InstanceMethod("setProperty", &CoreWrap::set_property),
InstanceMethod("getProperty", &CoreWrap::get_property)});
InstanceMethod("getProperty", &CoreWrap::get_property),
InstanceMethod("addExtension", &CoreWrap::add_extension)});
}
Napi::Value CoreWrap::read_model_sync(const Napi::CallbackInfo& info) {
@ -359,3 +360,15 @@ Napi::Value CoreWrap::get_property(const Napi::CallbackInfo& info) {
return any_to_js(info, value);
}
void CoreWrap::add_extension(const Napi::CallbackInfo& info) {
try {
if (!info[0].IsString())
OPENVINO_THROW("addExtension method applies one argument of string type");
std::string library_path = info[0].ToString();
_core.add_extension(library_path);
} catch (std::runtime_error& err) {
reportError(info.Env(), err.what());
}
}

View File

@ -8,7 +8,7 @@ const { describe, it } = require('node:test');
const core = new ov.Core();
it('Core.setProperty()', () => {
it('Core.setProperty()', () => {
const tmpDir = '/tmp';
core.setProperty({ 'CACHE_DIR': tmpDir });
@ -18,7 +18,7 @@ it('Core.setProperty()', () => {
assert.equal(cacheDir, tmpDir);
});
it('Core.setProperty(\'CPU\')', () => {
it('Core.setProperty(\'CPU\')', () => {
const tmpDir = '/tmp';
core.setProperty('CPU', { 'CACHE_DIR': tmpDir });
@ -28,13 +28,13 @@ it('Core.setProperty(\'CPU\')', () => {
assert.equal(cacheDir, tmpDir);
});
it('Core.getProperty(\'CPU\', \'SUPPORTED_PROPERTIES\') is Array', () => {
it('Core.getProperty(\'CPU\', \'SUPPORTED_PROPERTIES\') is Array', () => {
const supportedPropertiesArray = core.getProperty('CPU', 'SUPPORTED_PROPERTIES');
assert.ok(Array.isArray(supportedPropertiesArray));
});
it('Core.setProperty(\'CPU\', { \'NUM_STREAMS\': 5 })', () => {
it('Core.setProperty(\'CPU\', { \'NUM_STREAMS\': 5 })', () => {
const streams = 5;
core.setProperty('CPU', { 'NUM_STREAMS': streams });
@ -43,7 +43,7 @@ it('Core.setProperty(\'CPU\', { \'NUM_STREAMS\': 5 })', () => {
assert.equal(result, streams);
});
it('Core.setProperty(\'CPU\', { \'INFERENCE_NUM_THREADS\': 3 })', () => {
it('Core.setProperty(\'CPU\', { \'INFERENCE_NUM_THREADS\': 3 })', () => {
const threads = 3;
core.setProperty('CPU', { 'INFERENCE_NUM_THREADS': threads });
@ -51,3 +51,19 @@ it('Core.setProperty(\'CPU\', { \'INFERENCE_NUM_THREADS\': 3 })', () => {
assert.equal(result, threads);
});
it('Core.addExtension() with empty parameters', () => {
assert.throws(
() => core.addExtension(),
/addExtension method applies one argument of string type/
);
});
it('Core.addExtension(\'not_exists\') with non-existed library', () => {
const notExistsExt = 'not_exists';
assert.throws(
() => core.addExtension(notExistsExt),
/Cannot load library 'not_exists'/
);
});