This repository was archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathobject.cpp
More file actions
281 lines (255 loc) · 9.45 KB
/
Copy pathobject.cpp
File metadata and controls
281 lines (255 loc) · 9.45 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Nexus.js - The next-gen JavaScript platform
* Copyright (C) 2016 Abdullah A. Hassan <abdullah@webtomizer.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "nexus.h"
#include <stdexcept>
#include <utility>
#include <wtf/FastMalloc.h>
#include <WTF/wtf/StackTrace.h>
#include "object.h"
#include "value.h"
#include "exception.h"
#include "context.h"
#include "scoped_string.h"
NX::Object::Object (JSContextRef context, JSClassRef cls, void * data): myContext(context), myObject(nullptr)
{
myObject = JSObjectMake(context, cls, data);
attach();
}
NX::Object::Object (JSContextRef context, JSObjectRef obj): myContext(context), myObject(obj)
{
attach();
}
NX::Object::Object (JSContextRef context, JSValueRef val): myContext(context), myObject(nullptr)
{
JSValueRef exception = nullptr;
myObject = JSValueToObject(myContext, val, &exception);
if (exception) {
throw NX::Exception(myContext, exception);
}
attach();
}
NX::Object::Object (const NX::Object & other): myContext(other.myContext), myObject(other.myObject)
{
attach();
}
NX::Object::Object (JSContextRef context, time_t val): myContext(context), myObject(nullptr)
{
JSValueRef args[] {
NX::Value(myContext, val * 1000).value()
};
JSValueRef exception = nullptr;
myObject = JSObjectMakeDate(myContext, 1, args, &exception);
if (exception) {
throw NX::Exception(myContext, exception);
}
attach();
}
NX::Object::Object (JSContextRef context, const std::exception & e): myContext(context), myObject(nullptr)
{
WTF::StringPrintStream ss;
ss.printf("%s\n", e.what());
if (auto * nxp = dynamic_cast<const NX::Exception*>(&e)) {
nxp->trace()->dump(ss, "\t");
} else {
auto trace = WTF::StackTrace::captureStackTrace(10);
trace->dump(ss, "\t");
}
ss.printf("\n");
NX::Value message(myContext, ss.toString().utf8().data());
JSValueRef args[] { message.value() };
myObject = JSObjectMakeError(myContext, 1, args, nullptr);
attach();
}
NX::Object::Object (JSContextRef context, const boost::system::error_code & e): myContext(context), myObject(nullptr)
{
NX::Value message(myContext, e.message());
JSValueRef args[] { message.value() };
myObject = JSObjectMakeError(myContext, 1, args, nullptr);
attach();
}
NX::Object::~Object()
{
clear();
}
std::shared_ptr<NX::Value> NX::Object::operator[] (const char * name)
{
if (!myObject) return std::shared_ptr<NX::Value>(nullptr);
NX::ScopedString nameRef(name);
JSValueRef exception = nullptr;
JSValueRef val = JSObjectGetProperty(myContext, myObject, nameRef, &exception);
if (exception) {
NX::Value except(myContext, exception);
throw NX::Exception(except.toString());
}
return std::make_shared<NX::Value>(myContext, val);
}
std::shared_ptr<NX::Value> NX::Object::operator[] (unsigned int index)
{
if (!myObject) return std::shared_ptr<NX::Value>(nullptr);
JSValueRef exception;
JSValueRef val = JSObjectGetPropertyAtIndex(myContext, myObject, index, &exception);
if (exception) {
NX::Value except(myContext, exception);
throw NX::Exception(except.toString());
}
return std::make_shared<NX::Value>(myContext, val);
}
std::shared_ptr<NX::Value> NX::Object::operator[] (const char * name) const
{
if (!myObject) return std::shared_ptr<NX::Value>(nullptr);
NX::ScopedString nameRef(name);
JSValueRef exception = nullptr;
JSValueRef val = JSObjectGetProperty(myContext, myObject, nameRef, &exception);
if (exception) {
NX::Value except(myContext, exception);
throw NX::Exception(except.toString());
}
return std::make_shared<NX::Value>(myContext, val);
}
std::shared_ptr<NX::Value> NX::Object::operator[] (unsigned int index) const
{
if (!myObject) return std::shared_ptr<NX::Value>(nullptr);
JSValueRef exception;
JSValueRef val = JSObjectGetPropertyAtIndex(myContext, myObject, index, &exception);
if (exception) {
NX::Value except(myContext, exception);
throw NX::Exception(except.toString());
}
return std::make_shared<NX::Value>(myContext, val);
}
std::string NX::Object::toString()
{
NX::Value val(myContext, myObject);
return val.toString();
}
NX::Object NX::Object::then(NX::Object::PromiseCallback onResolve, NX::Object::PromiseCallback onReject)
{
if (!myObject)
throw NX::Exception("not a promise");
struct PromiseData {
PromiseCallback onResolve;
PromiseCallback onReject;
};
auto promiseData = new PromiseData { onResolve, onReject };
JSObjectRef dataCarrier = JSObjectMake(myContext, NX::Context::FromJsContext(myContext)->nexus()->genericClass(), promiseData);
JSObjectSetPrivate(dataCarrier, promiseData);
JSValueRef exp = nullptr;
JSValueRef resolve = onResolve ? JSBindFunction(myContext, JSObjectMakeFunctionWithCallback(myContext, ScopedString("onResolve"),
[](JSContextRef ctx, JSObjectRef function,
JSObjectRef thisObject, size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception) -> JSValueRef
{
auto promiseData = reinterpret_cast<PromiseData*>(JSObjectGetPrivate(thisObject));
if (promiseData) {
JSValueRef val = promiseData->onResolve(ctx, arguments[0], exception);
delete promiseData;
return val;
} else
return *exception = NX::Exception("empty promise data").toError(ctx);
}), dataCarrier, 0, nullptr, &exp) : JSValueMakeUndefined(myContext);
if (exp)
{
NX::Nexus::ReportException(myContext, exp);
}
JSValueRef reject = onReject ? JSBindFunction(myContext, JSObjectMakeFunctionWithCallback(myContext, ScopedString("onReject"),
[](JSContextRef ctx, JSObjectRef function,
JSObjectRef thisObject, size_t argumentCount,
const JSValueRef arguments[],
JSValueRef* exception) -> JSValueRef
{
auto promiseData = reinterpret_cast<PromiseData*>(JSObjectGetPrivate(thisObject));
if (promiseData) {
JSValueRef val = promiseData->onReject(ctx, arguments[0], exception);
delete promiseData;
return val;
} else
return *exception = NX::Exception("empty promise data").toError(ctx);
}), dataCarrier, 0, nullptr, &exp) : JSValueMakeUndefined(myContext);
if (exp)
{
NX::Nexus::ReportException(myContext, exp);
}
return NX::Object(myContext, operator[]("then")->toObject()->call(myObject, std::vector<JSValueRef> { resolve, reject }, nullptr));
}
void NX::Object::push(JSValueRef value, JSValueRef *exception) {
this->operator[]("push")->toObject()->call(this->value(), { value }, exception);
}
JSValueRef NX::Object::await() {
auto context = NX::Context::FromJsContext(myContext);
auto scheduler = context->nexus()->scheduler();
JSValueRef result = nullptr, exception = nullptr;
this->then([&](JSContextRef ctx, JSValueRef res, JSValueRef*) {
result = res;
JSValueProtect(context->toJSContext(), result);
return JSValueMakeUndefined(ctx);
}, [&](JSContextRef ctx, JSValueRef exp, JSValueRef*) {
exception = exp;
JSValueProtect(context->toJSContext(), exp);
return JSValueMakeUndefined(ctx);
});
while(!result && !exception)
scheduler->yield();
if (exception) {
JSValueUnprotect(context->toJSContext(), exception);
}
if (result) {
JSValueUnprotect(context->toJSContext(), result);
}
if (exception)
throw NX::Exception(context->toJSContext(), exception);
return result;
}
NX::Object::Object(JSContextRef context, const std::vector<JSValueRef> &values): myContext(context), myObject(nullptr)
{
JSValueRef exp = nullptr;
if (!values.empty())
myObject = JSObjectMakeArray(context, values.size(), values.begin().operator->(), &exp);
else
myObject = JSObjectMakeArray(context, 0, nullptr, &exp);
if (exp) {
NX::Value except(myContext, exp);
throw NX::Exception(except.toString());
}
attach();
}
bool NX::Object::toBoolean() {
if (myObject)
return JSValueToBoolean(myContext, myObject);
else
return false;
}
NX::Object::Object(NX::Object &&other) noexcept:
myContext(other.myContext), myObject(other.myObject)
{
other.myContext = nullptr;
other.myObject = nullptr;
attach();
}
void NX::Object::clear() {
if (myContext && myObject) {
JSValueUnprotect(myContext, myObject);
}
myContext = nullptr;
myObject = nullptr;
}
void NX::Object::attach() {
if (myContext && myObject)
JSValueProtect(myContext, myObject);
}