Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "JsiMain.h"
#include "InferenceSessionHostObject.h"
#include "EpContextDataReadCallback.h"
#include "JsiHelper.h"
#include "SessionUtils.h"
#include <memory>
 
using namespace facebook::jsi;
 
namespace onnxruntimejsi {
 
std::shared_ptr<Env>
install(Runtime& runtime,
        std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
  auto env = std::make_shared<Env>(jsInvoker);
  try {
    auto ortApi = Object(runtime);
 
    auto initOrtOnceMethod = Function::createFromHostFunction(
        runtime, PropNameID::forAscii(runtime, "initOrtOnce"), 2,
        [env](Runtime& runtime, const Value& thisValue, const Value* arguments,
              size_t count) -> Value {
          try {
            OrtLoggingLevel logLevel = ORT_LOGGING_LEVEL_WARNING;
            if (count > 0 && arguments[0].isNumber()) {
              int level = static_cast<int>(arguments[0].asNumber());
              switch (level) {
                case 0:
                  logLevel = ORT_LOGGING_LEVEL_VERBOSE;
                  break;
                case 1:
                  logLevel = ORT_LOGGING_LEVEL_INFO;
                  break;
                case 2:
                  logLevel = ORT_LOGGING_LEVEL_WARNING;
                  break;
                case 3:
                  logLevel = ORT_LOGGING_LEVEL_ERROR;
                  break;
                case 4:
                  logLevel = ORT_LOGGING_LEVEL_FATAL;
                  break;
                default:
                  logLevel = ORT_LOGGING_LEVEL_WARNING;
                  break;
              }
            }
            env->setTensorConstructor(runtime, arguments[1].asObject(runtime));
            env->initOrtEnv(logLevel, "onnxruntime-react-native-jsi");
            return Value::undefined();
          } catch (const std::exception& e) {
            throw JSError(runtime, "Failed to initialize ONNX Runtime: " +
                                       std::string(e.what()));
          }
        });
 
    ortApi.setProperty(runtime, "initOrtOnce", initOrtOnceMethod);
 
    auto createInferenceSessionMethod = Function::createFromHostFunction(
        runtime, PropNameID::forAscii(runtime, "createInferenceSession"), 0,
        std::bind(InferenceSessionHostObject::constructor, env,
                  std::placeholders::_1, std::placeholders::_2,
                  std::placeholders::_3, std::placeholders::_4));
    ortApi.setProperty(runtime, "createInferenceSession",
                       createInferenceSessionMethod);
 
    auto testEpContextDataReadCallback = Function::createFromHostFunction(
        runtime,
        PropNameID::forAscii(runtime, "__testEpContextDataReadCallback"), 3,
        [env](Runtime& runtime, const Value&, const Value* arguments,
              size_t count) -> Value {
          return EpContextDataReadCallback::testCallbackBridge(
              runtime, arguments, count, env);
        });
    ortApi.setProperty(runtime, "__testEpContextDataReadCallback",
                       testEpContextDataReadCallback);
 
    auto listSupportedBackendsMethod = Function::createFromHostFunction(
        runtime, PropNameID::forAscii(runtime, "listSupportedBackends"), 0,
        [](Runtime& runtime, const Value& thisValue, const Value* arguments,
           size_t count) -> Value {
          auto backends = Array(runtime, supportedBackends.size());
          for (size_t i = 0; i < supportedBackends.size(); i++) {
            auto backend = Object(runtime);
            backend.setProperty(
                runtime, "name",
                String::createFromUtf8(runtime, supportedBackends[i]));
            backends.setValueAtIndex(runtime, i, Value(runtime, backend));
          }
          return Value(runtime, backends);
        });
 
    ortApi.setProperty(runtime, "listSupportedBackends",
                       listSupportedBackendsMethod);
 
    ortApi.setProperty(
        runtime, "version",
        String::createFromUtf8(runtime, OrtGetApiBase()->GetVersionString()));
 
    runtime.global().setProperty(runtime, "OrtApi", ortApi);
  } catch (const std::exception& e) {
    throw JSError(runtime, "Failed to install ONNX Runtime JSI bindings: " +
                               std::string(e.what()));
  }
 
  return env;
}
 
}  // namespace onnxruntimejsi