Skip to content

Commit

Permalink
[OV JS] Use type_validation for model.clone
Browse files Browse the repository at this point in the history
Make use of the allowed_signatures pattern
provided in the type_validation.hpp file to
validate the model.clone API arguments

Clang format the files.

Resolves: openvinotoolkit#25402

Signed-off-by: Nashez Zubair <[email protected]>
  • Loading branch information
nashez committed Aug 5, 2024
1 parent 802bd73 commit d2a0463
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/bindings/js/node/src/model_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,15 @@ Napi::Value ModelWrap::get_output_element_type(const Napi::CallbackInfo& info) {
}

Napi::Value ModelWrap::clone(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() > 0) {
reportError(env, "clone() does not accept any arguments.");
return env.Undefined();
std::vector<std::string> allowed_signatures;
try {
if (ov::js::validate(info, allowed_signatures)) {
return cpp_to_js(info.Env(), _model->clone());
} else {
OPENVINO_THROW("'clone'", ov::js::get_parameters_error_msg(info, allowed_signatures));
}
} catch (const std::exception& e) {
reportError(info.Env(), e.what());
return info.Env().Undefined();
}
return cpp_to_js(env, _model->clone());
}
7 changes: 4 additions & 3 deletions src/bindings/js/node/tests/unit/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ describe('Model.clone()', () => {
});

it('should not accept any arguments', () => {
assert.throws(() => {
model.clone('unexpected argument');
}, /^Error: clone\(\) does not accept any arguments\.$/, 'Expected clone to throw an error when called with arguments');
assert.throws(
() => model.clone("Unexpected argument").then(),
/'clone' method called with incorrect parameters./
);
});
});

0 comments on commit d2a0463

Please sign in to comment.