Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

4.0 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <napi.h>

#include "openvino/runtime/tensor.hpp"

class TensorWrap : public Napi::ObjectWrap<TensorWrap> {
public:
    /**
     * @brief Constructs TensorWrap class from the Napi::CallbackInfo.
     * @param info contains passed arguments. Can be empty or contain more arguments.
     * One argument is passed:
     * @param info[0] Can be:
     *        - Napi::External<ov::Tensor> - pointer to existing ov::Tensor object (for cross-addon interop)
     *        - Napi::Array - JavaScript array of strings (creates string tensor)
     * Two arguments are passed:
     * @param info[0] ov::element::Type as string or exposed to JS enumElementType.
     * @param info[1] ov::Shape as JS Array, Int32Array or Uint32Array
     * Three arguments are passed:
     * @param info[0] ov::element::Type as string or exposed to JS enumElementType.
     * @param info[1] ov::Shape as JS Array, Int32Array or Uint32Array
     * @param info[2] Tensor data as TypedArray
     * @throw Exception if params are of invalid type.
     */
    TensorWrap(const Napi::CallbackInfo& info);

    /**
     * @brief Defines a Javascript Tensor class with constructor, static and instance properties and methods.
     * @param env The environment in which to construct a JavaScript class.
     * @return Napi::Function representing the constructor function for the Javascript Tensor class.
     */
    static Napi::Function get_class(Napi::Env env);

    ov::Tensor get_tensor() const;
    void set_tensor(const ov::Tensor& tensor);
    /**
     * @brief Creates JavaScript Tensor object and wraps inside of it ov::Tensor object.
     * @param env The environment in which to construct a JavaScript object.
     * @param tensor ov::Tensor to wrap.
     * @return Javascript Tensor as Napi::Object. (Not TensorWrap object)
     */
    static Napi::Object wrap(Napi::Env env, ov::Tensor tensor);

    /**
     * @brief Helper function to access the tensor data as an attribute of JavaScript Tensor.
     * @param info Contains information about the environment in which to create the Napi::TypedArray instance.
     * @return Napi::TypedArray containing the tensor data.
     */
    Napi::Value get_data(const Napi::CallbackInfo& info);

    /**
     * @brief Setter that fills underlaying Tensor's memory by copying data from TypedArray.
     * @throw Exception if data's size from TypedArray does not match the size of the tensor's data.
     */
    void set_data(const Napi::CallbackInfo& info, const Napi::Value& value);

    /** @return Napi::Array containing a tensor shape. */
    Napi::Value get_shape(const Napi::CallbackInfo& info);
    /** @return Napi::String containing ov::element type. */
    Napi::Value get_element_type(const Napi::CallbackInfo& info);
    /** @return Napi::Number containing tensor size as total number of elements. */
    Napi::Value get_size(const Napi::CallbackInfo& info);
    /**
     * @brief Getter to check if tensor is continuous
     * @return Napi::Boolean
     */
    Napi::Value is_continuous(const Napi::CallbackInfo& info);
    /**
     * @brief Binds the C++ ov::Tensor::set_shape() method to JavaScript as Tensor.setShape().
     * @param info Callback info containing a single argument: an array of numbers
     *             representing the new tensor dimensions.
     * @return JavaScript undefined on success, or throws a JS exception on error.
     */
    Napi::Value set_shape(const Napi::CallbackInfo& info);
    void copy_to(const Napi::CallbackInfo& info);

    /**
     * @brief Returns a pointer to the underlying ov::Tensor object for cross-addon interoperability.
     * @param info Callback info (no arguments expected).
     * @return Napi::External<ov::Tensor> wrapping a pointer to an ov::Tensor copy.
     *         The External manages the lifetime of the ov::Tensor copy, while the actual
     *         tensor data is managed by ov::Tensor's internal shared_ptr.
     */
    Napi::Value get_external_tensor(const Napi::CallbackInfo& info);

private:
    ov::Tensor _tensor;
};