From fe629461e1ccce6fe064c005db7412b7a9ba1559 Mon Sep 17 00:00:00 2001 From: Vishniakov Nikolai Date: Mon, 15 Apr 2024 15:05:24 +0200 Subject: [PATCH] [OV JS] Support add_extension method (#23848) ### Tickets: - 137673 --- src/bindings/js/node/include/core_wrap.hpp | 1 + src/bindings/js/node/lib/addon.ts | 3 ++- src/bindings/js/node/src/core_wrap.cpp | 15 ++++++++++++- src/bindings/js/node/tests/core.test.js | 26 +++++++++++++++++----- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/bindings/js/node/include/core_wrap.hpp b/src/bindings/js/node/include/core_wrap.hpp index 387dcfe1720..169812234f9 100644 --- a/src/bindings/js/node/include/core_wrap.hpp +++ b/src/bindings/js/node/include/core_wrap.hpp @@ -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, diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts index 565839a57b0..1909a15042e 100644 --- a/src/bindings/js/node/lib/addon.ts +++ b/src/bindings/js/node/lib/addon.ts @@ -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; diff --git a/src/bindings/js/node/src/core_wrap.cpp b/src/bindings/js/node/src/core_wrap.cpp index 442d9c3c79b..cbcf49281e2 100644 --- a/src/bindings/js/node/src/core_wrap.cpp +++ b/src/bindings/js/node/src/core_wrap.cpp @@ -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()); + } +} diff --git a/src/bindings/js/node/tests/core.test.js b/src/bindings/js/node/tests/core.test.js index ca45ae4dff9..e0fda391257 100644 --- a/src/bindings/js/node/tests/core.test.js +++ b/src/bindings/js/node/tests/core.test.js @@ -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'/ + ); +});