#pragma once #include "Env.h" #include "EpContextDataReadCallback.h" #include "JsiHelper.h" #include #include #include "onnxruntime_cxx_api.h" #include using namespace facebook::jsi; namespace onnxruntimejsi { class InferenceSessionHostObject : public HostObjectHelper, public std::enable_shared_from_this { public: InferenceSessionHostObject(std::shared_ptr env) : HostObjectHelper({ METHOD_INFO(InferenceSessionHostObject, loadModel, 2), METHOD_INFO(InferenceSessionHostObject, run, 2), METHOD_INFO(InferenceSessionHostObject, dispose, 0), METHOD_INFO(InferenceSessionHostObject, endProfiling, 0), }, { GETTER_INFO(InferenceSessionHostObject, inputMetadata), GETTER_INFO(InferenceSessionHostObject, outputMetadata), }), env_(env) {} static inline facebook::jsi::Value constructor(std::shared_ptr env, facebook::jsi::Runtime& runtime, const facebook::jsi::Value& thisValue, const facebook::jsi::Value* arguments, size_t count) { return facebook::jsi::Object::createFromHostObject( runtime, std::make_shared(env)); } protected: class LoadModelAsyncWorker; class RunAsyncWorker; private: std::shared_ptr env_; // Declared before session_ so the session is destroyed first: ONNX Runtime may call back into // the callback state for as long as the session exists. std::shared_ptr epContextDataRead_; std::shared_ptr session_; DEFINE_METHOD(loadModel); DEFINE_METHOD(run); DEFINE_METHOD(dispose); DEFINE_METHOD(endProfiling); DEFINE_GETTER(inputMetadata); DEFINE_GETTER(outputMetadata); }; } // namespace onnxruntimejsi