onnxruntime/nodejs/src/common.h
Yulong Wang 5dfc91db51
Node.js binding for ONNX Runtime (#3613)
* initial commit for Node.js binding

* add c++ code

* add inference session impl

* e2e working

* add settings.json

* add test data

* adjust binding declaration

* refine tensor constructor declaration

* update tests

* enable onnx tests

* simply refine readme

* refine cpp impl

* refine tests

* formatting

* add linting

* move bin folder

* fix linux build

* manually update test filter list

* update C++ API headers: fix crash in release build

* make (manually) prebuild work

* add test into prepack script

* specify prebuild runtime type (N-API)

* build.ts: update rebuild and include regex

* fix lazy load on electron.js

* update dev version, git link and binary host

* support session options and run options

* bump dev version

* update README

* add 1 example

* move folder

* adjust path

* update document for examples

* rename example 01

* add example 02

* add session option: log severity level

* add example 04

* resolve comments

* fix typo

* remove double guard in header files

* add copyright banner

* move BUILD outside from README

* consume test filter list from onnxruntime
2020-05-05 11:45:12 -07:00

50 lines
2.3 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <napi.h>
#include <sstream>
#include <vector>
inline void MakeStringInternal(std::ostringstream & /*ss*/) noexcept {}
template <typename T> inline void MakeStringInternal(std::ostringstream &ss, const T &t) noexcept { ss << t; }
template <typename T, typename... Args>
inline void MakeStringInternal(std::ostringstream &ss, const T &t, const Args &... args) noexcept {
::MakeStringInternal(ss, t);
::MakeStringInternal(ss, args...);
}
template <typename... Args> std::string MakeString(const Args &... args) {
std::ostringstream ss;
::MakeStringInternal(ss, args...);
return std::string(ss.str());
}
#define ORT_NAPI_THROW(ERROR, ENV, ...) \
do { \
throw Napi::ERROR::New((ENV), MakeString(__VA_ARGS__)); \
} while (false)
#define ORT_NAPI_THROW_ERROR(ENV, ...) ORT_NAPI_THROW(Error, ENV, __VA_ARGS__)
#define ORT_NAPI_THROW_TYPEERROR(ENV, ...) ORT_NAPI_THROW(TypeError, ENV, __VA_ARGS__)
#define ORT_NAPI_THROW_RANGEERROR(ENV, ...) ORT_NAPI_THROW(RangeError, ENV, __VA_ARGS__)
#define ORT_NAPI_THROW_IF(COND, ERROR, ENV, ...) \
if (COND) { \
ORT_NAPI_THROW(ERROR, ENV, __VA_ARGS__); \
}
#define ORT_NAPI_THROW_ERROR_IF(COND, ENV, ...) ORT_NAPI_THROW_IF(COND, Error, ENV, __VA_ARGS__)
#define ORT_NAPI_THROW_TYPEERROR_IF(COND, ENV, ...) ORT_NAPI_THROW_IF(COND, TypeError, ENV, __VA_ARGS__)
#define ORT_NAPI_THROW_RANGEERROR_IF(COND, ENV, ...) ORT_NAPI_THROW_IF(COND, RangeError, ENV, __VA_ARGS__)
template <typename T> Napi::Value CreateNapiArrayFrom(napi_env env, const std::vector<T> &vec) {
Napi::EscapableHandleScope scope(env);
auto array = Napi::Array::New(env, vec.size());
for (uint32_t i = 0; i < vec.size(); i++) {
array.Set(i, Napi::Value::From(env, vec[i]));
}
return scope.Escape(array);
}