-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.cpp
104 lines (95 loc) · 3.4 KB
/
util.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "util.hpp"
static size_t heap_base = 0;
bool
str_end(const std::string& str, const std::string& suffix) {
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
bool
str_end(const char* str, const char* suffix) {
return str_end(std::string(str), std::string(suffix));
}
void*
get_heap_base() {
if(!heap_base) {
std::ifstream infile("/proc/self/maps");
infile >> std::hex;
for(std::string line; std::getline(infile, line);) {
if(line.find("[heap]") != std::string::npos) {
size_t start = line.find("-");
heap_base = strtoull(line.c_str() + start + 1, nullptr, 16);
}
}
infile >> heap_base;
// std::cerr << "heap_base: " << heap_base << std::endl;
}
return reinterpret_cast<void*>(heap_base);
}
std::string
js_prop_flags(int flags) {
std::vector<const char*> names;
if(flags & JS_PROP_CONFIGURABLE)
names.push_back("CONFIGURABLE");
if(flags & JS_PROP_WRITABLE)
names.push_back("WRITABLE");
if(flags & JS_PROP_ENUMERABLE)
names.push_back("ENUMERABLE");
if(flags & JS_PROP_NORMAL)
names.push_back("NORMAL");
if(flags & JS_PROP_GETSET)
names.push_back("GETSET");
if(flags & JS_PROP_VARREF)
names.push_back("VARREF");
if(flags & JS_PROP_AUTOINIT)
names.push_back("AUTOINIT");
return join(names.cbegin(), names.cend(), "|");
}
std::ostream&
operator<<(std::ostream& s, const JSCFunctionListEntry& entry) {
std::string name(entry.name);
s << name << std::setw(30 - name.size()) << ' ';
s << "type = "
<< (std::vector<const char*>{
"CFUNC", "CGETSET", "CGETSET_MAGIC", "PROP_STRING", "PROP_INT32", "PROP_INT64", "PROP_DOUBLE", "PROP_UNDEFINED", "OBJECT", "ALIAS"})[entry.def_type]
<< ", ";
switch(entry.def_type) {
case JS_DEF_CGETSET_MAGIC: s << "magic = " << (unsigned int)entry.magic << ", "; break;
case JS_DEF_PROP_INT32: s << "value = " << std::setw(9) << entry.u.i32 << ", "; break;
case JS_DEF_PROP_INT64: s << "value = " << std::setw(9) << entry.u.i64 << ", "; break;
case JS_DEF_PROP_DOUBLE: s << "value = " << std::setw(9) << entry.u.f64 << ", "; break;
case JS_DEF_PROP_UNDEFINED:
s << "value = " << std::setw(9) << "undefined"
<< ", ";
break;
case JS_DEF_PROP_STRING: s << "value = " << std::setw(9) << entry.u.str << ", "; break;
}
s << "flags = " << js_prop_flags(entry.prop_flags) << std::endl;
return s;
}
JSMatDimensions
mat_dimensions(const cv::Mat& mat) {
JSMatDimensions ret;
ret.rows = mat.rows;
ret.cols = mat.cols;
return ret;
}
JSMatDimensions
mat_dimensions(const cv::UMat& mat) {
JSMatDimensions ret;
ret.rows = mat.rows;
ret.cols = mat.cols;
return ret;
}
/* clang-format off */
int mat_depth(const cv::Mat& mat) { return mattype_depth(mat.type()); }
int mat_channels(const cv::Mat& mat) { return mattype_channels(mat.type()); }
bool mat_signed(const cv::Mat& mat) { return mattype_signed(mat.type()); }
bool mat_floating(const cv::Mat& mat) { return mattype_floating(mat.type()); }
int mat_depth(const cv::UMat& mat) { return mattype_depth(mat.type()); }
int mat_channels(const cv::UMat& mat) { return mattype_channels(mat.type()); }
bool mat_signed(const cv::UMat& mat) { return mattype_signed(mat.type()); }
bool mat_floating(const cv::UMat& mat) { return mattype_floating(mat.type()); }
/* clang-format on */