[JS API] Simplify infer_dispatch() method (#24266)

### Details:
 - Simplify infer_dispatch() method

Co-authored-by: Michal Lukaszewski <michal.lukaszewski@intel.com>
This commit is contained in:
Alicja Miloszewska 2024-05-06 10:35:56 +02:00 committed by GitHub
parent ad471ea3f3
commit 78b3336b59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 20 deletions

View File

@ -152,29 +152,23 @@ Napi::Value InferRequestWrap::get_output_tensors(const Napi::CallbackInfo& info)
} }
Napi::Value InferRequestWrap::infer_dispatch(const Napi::CallbackInfo& info) { Napi::Value InferRequestWrap::infer_dispatch(const Napi::CallbackInfo& info) {
if (info.Length() == 0) try {
_infer_request.infer(); if (info.Length() == 0)
else if (info.Length() == 1 && info[0].IsTypedArray()) { _infer_request.infer();
reportError(info.Env(), "TypedArray cannot be passed directly into infer() method."); else if (info.Length() == 1 && info[0].IsTypedArray()) {
return info.Env().Null(); OPENVINO_THROW("TypedArray cannot be passed directly into infer() method.");
} else if (info.Length() == 1 && info[0].IsArray()) { } else if (info.Length() == 1 && info[0].IsArray()) {
try {
infer(info[0].As<Napi::Array>()); infer(info[0].As<Napi::Array>());
} catch (std::exception& e) { } else if (info.Length() == 1 && info[0].IsObject()) {
reportError(info.Env(), e.what());
return info.Env().Null();
}
} else if (info.Length() == 1 && info[0].IsObject()) {
try {
infer(info[0].As<Napi::Object>()); infer(info[0].As<Napi::Object>());
} catch (std::exception& e) { } else {
reportError(info.Env(), e.what()); OPENVINO_THROW("Infer method takes as an argument an array or an object.");
return info.Env().Null();
} }
} else { return get_output_tensors(info);
reportError(info.Env(), "Infer method takes as an argument an array or an object."); } catch (std::exception& e) {
reportError(info.Env(), e.what());
return info.Env().Undefined();
} }
return get_output_tensors(info);
} }
void InferRequestWrap::infer(const Napi::Array& inputs) { void InferRequestWrap::infer(const Napi::Array& inputs) {

View File

@ -55,7 +55,7 @@ describe('InferRequest', () => {
it('Test infer(TypedArray) throws', () => { it('Test infer(TypedArray) throws', () => {
assert.throws( assert.throws(
() => inferRequest.infer(tensorData), () => inferRequest.infer(tensorData),
{message: 'TypedArray cannot be passed directly into infer() method.'}); {message: /TypedArray cannot be passed directly into infer\(\) method./});
}); });
const buffer = new ArrayBuffer(tensorData.length); const buffer = new ArrayBuffer(tensorData.length);