-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsbindings.cpp
148 lines (135 loc) · 3.72 KB
/
jsbindings.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "jsbindings.hpp"
#include "js_array.hpp"
#include <quickjs.h>
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
template<class T> class jsallocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template<class U> struct rebind { typedef jsallocator<U> other; };
pointer
address(reference value) const {
return &value;
}
const_pointer
address(const_reference value) const {
return &value;
}
jsallocator() throw() {}
jsallocator(const jsallocator&) throw() {}
template<class U> jsallocator(const jsallocator<U>&) throw() {}
~jsallocator() throw() {}
size_type
max_size() const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
pointer
allocate(size_type num, const void* = 0) {
pointer ret;
std::cerr << "allocate " << num << " element(s)"
<< " of size " << sizeof(T) << std::endl;
std::cerr << " allocated at: " << (void*)ret << std::endl;
return ret;
}
void
construct(pointer p, const T& value) {
p->T(value);
}
void
destroy(pointer p) {
p->~T();
}
void
deallocate(pointer p, size_type num) {
std::cerr << "deallocate " << num << " element(s)"
<< " of size " << sizeof(T) << " at: " << (void*)p << std::endl;
js_free(p);
}
};
int
js_color_read(JSContext* ctx, JSValueConst color, JSColorData<double>* out) {
int ret = 1;
std::array<double, 4> c = {0, 0, 0, 255};
if(JS_IsObject(color)) {
JSValue v[4];
if(js_is_array(ctx, color)) {
v[0] = JS_GetPropertyUint32(ctx, color, 0);
v[1] = JS_GetPropertyUint32(ctx, color, 1);
v[2] = JS_GetPropertyUint32(ctx, color, 2);
v[3] = JS_GetPropertyUint32(ctx, color, 3);
} else {
v[0] = JS_GetPropertyStr(ctx, color, "r");
v[1] = JS_GetPropertyStr(ctx, color, "g");
v[2] = JS_GetPropertyStr(ctx, color, "b");
v[3] = JS_GetPropertyStr(ctx, color, "a");
}
if(!(JS_IsNumber(v[0]) && JS_IsNumber(v[1]) && JS_IsNumber(v[2]) /* && JS_IsNumber(v[3])*/)) {
ret = 0;
} else {
JS_ToFloat64(ctx, &c[0], v[0]);
JS_ToFloat64(ctx, &c[1], v[1]);
JS_ToFloat64(ctx, &c[2], v[2]);
if(JS_IsNumber(v[3]))
JS_ToFloat64(ctx, &c[3], v[3]);
}
JS_FreeValue(ctx, v[0]);
JS_FreeValue(ctx, v[1]);
JS_FreeValue(ctx, v[2]);
JS_FreeValue(ctx, v[3]);
} else if(JS_IsNumber(color)) {
uint32_t value;
JS_ToUint32(ctx, &value, color);
c[0] = value & 0xff;
c[1] = (value >> 8) & 0xff;
c[2] = (value >> 16) & 0xff;
c[3] = (value >> 24) & 0xff;
} else {
ret = 0;
}
std::copy(c.cbegin(), c.cend(), out->arr.begin());
return ret;
}
int
js_color_read(JSContext* ctx, JSValueConst value, JSColorData<uint8_t>* out) {
JSColorData<double> color;
if(js_is_array(ctx, value)) {
std::array<uint8_t, 4> a;
if(js_array_to(ctx, value, a) >= 3) {
out->arr[0] = a[0];
out->arr[1] = a[1];
out->arr[2] = a[2];
out->arr[3] = a[3];
return 1;
}
}
if(js_color_read(ctx, value, &color)) {
out->arr[0] = color.arr[0];
out->arr[1] = color.arr[1];
out->arr[2] = color.arr[2];
out->arr[3] = color.arr[3];
return 1;
}
return 0;
}
int
js_ref(JSContext* ctx, const char* name, JSValueConst arg, JSValue value) {
if(JS_IsFunction(ctx, arg)) {
JSValueConst v = value;
JS_Call(ctx, arg, JS_UNDEFINED, 1, &v);
} else if(js_is_array(ctx, arg)) {
JS_SetPropertyUint32(ctx, arg, 0, value);
} else if(JS_IsObject(arg)) {
JS_SetPropertyStr(ctx, arg, name, value);
} else {
return 0;
}
return 1;
}