-
Notifications
You must be signed in to change notification settings - Fork 4
/
plv8u_func.cc
303 lines (255 loc) · 11.4 KB
/
plv8u_func.cc
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*-------------------------------------------------------------------------
*
* plv8_func.cc : PL/v8 built-in functions.
*
* Copyright (c) 2009-2012, the PLV8JS Development Group.
*-------------------------------------------------------------------------
*/
#include "plv8.h"
#include "plv8_param.h"
#include "helpers/file.h"
#include <string>
#include <sstream>
extern "C" {
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include <errno.h>
#include <fcntl.h>
} // extern "C"
using namespace v8;
extern v8::Isolate* plv8_isolate;
static void plv8u_StatFile(const FunctionCallbackInfo<v8::Value>& args);
static void plv8u_ReadFile(const FunctionCallbackInfo<v8::Value>& args);
static void plv8_FunctionInvoker(const FunctionCallbackInfo<v8::Value>& args) throw();
static inline Local<v8::Value>
WrapCallback(FunctionCallback func)
{
return External::New(plv8_isolate,
reinterpret_cast<void *>(
reinterpret_cast<uintptr_t>(func)));
}
static inline FunctionCallback
UnwrapCallback(Handle<v8::Value> value)
{
return reinterpret_cast<FunctionCallback>(
reinterpret_cast<uintptr_t>(External::Cast(*value)->Value()));
}
static inline void
SetCallback(Handle<ObjectTemplate> obj, const char *name,
FunctionCallback func, PropertyAttribute attr = None)
{
obj->Set(String::NewFromUtf8(plv8_isolate, name, String::kInternalizedString),
FunctionTemplate::New(plv8_isolate, plv8_FunctionInvoker,
WrapCallback(func)), attr);
}
void
SetupPlv8uFunctions(Handle<ObjectTemplate> plv8u)
{
PropertyAttribute attrFull =
PropertyAttribute(ReadOnly | DontEnum | DontDelete);
Local<ObjectTemplate> internal = ObjectTemplate::New(plv8_isolate);
Local<ObjectTemplate> fs = ObjectTemplate::New(plv8_isolate);
SetCallback(fs, "stat", plv8u_StatFile, attrFull);
SetCallback(fs, "readFile", plv8u_ReadFile, attrFull);
internal->Set(String::NewFromUtf8(plv8_isolate, "fs"), fs);
plv8u->Set(String::NewFromUtf8(plv8_isolate, "_internal"), internal);
plv8u->SetInternalFieldCount(PLV8_INTNL_MAX);
}
/*
* v8 is not exception-safe! We cannot throw C++ exceptions over v8 functions.
* So, we catch C++ exceptions and convert them to JavaScript ones.
*/
static void
plv8_FunctionInvoker(const FunctionCallbackInfo<v8::Value> &args) throw()
{
HandleScope handle_scope(plv8_isolate);
MemoryContext ctx = CurrentMemoryContext;
FunctionCallback fn = UnwrapCallback(args.Data());
try
{
return fn(args);
}
catch (js_error& e)
{
args.GetReturnValue().Set(plv8_isolate->ThrowException(e.error_object()));
}
catch (pg_error& e)
{
MemoryContextSwitchTo(ctx);
ErrorData *edata = CopyErrorData();
Handle<String> message = ToString(edata->message);
Handle<String> sqlerrcode = ToString(unpack_sql_state(edata->sqlerrcode));
#if PG_VERSION_NUM >= 90300
Handle<Primitive> schema_name = edata->schema_name ?
Handle<Primitive>(ToString(edata->schema_name)) : Null(plv8_isolate);
Handle<Primitive> table_name = edata->table_name ?
Handle<Primitive>(ToString(edata->table_name)) : Null(plv8_isolate);
Handle<Primitive> column_name = edata->column_name ?
Handle<Primitive>(ToString(edata->column_name)) : Null(plv8_isolate);
Handle<Primitive> datatype_name = edata->datatype_name ?
Handle<Primitive>(ToString(edata->datatype_name)) : Null(plv8_isolate);
Handle<Primitive> constraint_name = edata->constraint_name ?
Handle<Primitive>(ToString(edata->constraint_name)) : Null(plv8_isolate);
Handle<Primitive> detail = edata->detail ?
Handle<Primitive>(ToString(edata->detail)) : Null(plv8_isolate);
Handle<Primitive> hint = edata->hint ?
Handle<Primitive>(ToString(edata->hint)) : Null(plv8_isolate);
Handle<Primitive> context = edata->context ?
Handle<Primitive>(ToString(edata->context)) : Null(plv8_isolate);
Handle<Primitive> internalquery = edata->internalquery ?
Handle<Primitive>(ToString(edata->internalquery)) : Null(plv8_isolate);
Handle<Integer> code = Uint32::New(plv8_isolate, edata->sqlerrcode);
#endif
FlushErrorState();
FreeErrorData(edata);
Handle<v8::Object> err = Exception::Error(message)->ToObject();
err->Set(String::NewFromUtf8(plv8_isolate, "sqlerrcode"), sqlerrcode);
#if PG_VERSION_NUM >= 90300
err->Set(String::NewFromUtf8(plv8_isolate, "schema_name"), schema_name);
err->Set(String::NewFromUtf8(plv8_isolate, "table_name"), table_name);
err->Set(String::NewFromUtf8(plv8_isolate, "column_name"), column_name);
err->Set(String::NewFromUtf8(plv8_isolate, "datatype_name"), datatype_name);
err->Set(String::NewFromUtf8(plv8_isolate, "constraint_name"), constraint_name);
err->Set(String::NewFromUtf8(plv8_isolate, "detail"), detail);
err->Set(String::NewFromUtf8(plv8_isolate, "hint"), hint);
err->Set(String::NewFromUtf8(plv8_isolate, "context"), context);
err->Set(String::NewFromUtf8(plv8_isolate, "internalquery"), internalquery);
err->Set(String::NewFromUtf8(plv8_isolate, "code"), code);
#endif
args.GetReturnValue().Set(plv8_isolate->ThrowException(err));
}
}
static void plv8u_StatFile(const FunctionCallbackInfo<v8::Value>& args) {
Isolate* plv8_isolate = args.GetIsolate();
if (args.Length() < 1) {
// Throw an Error that is passed back to JavaScript
plv8_isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(plv8_isolate, "path must be a string")));
return;
}
// Check the argument types
if (!args[0]->IsString()) {
plv8_isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(plv8_isolate, "path must be a string")));
return;
}
v8::String::Utf8Value s(args[0]);
std::string str(*s);
struct plv8u_file_status *status = stat_file(str.c_str());
if (status->error) {
std::stringstream ss;
ss << "unable to open file or directory: '" << str.c_str() << "' " << strerror(status->error);
std::string ns = ss.str();
plv8_isolate->ThrowException(Exception::Error(
String::NewFromUtf8(plv8_isolate, ns.c_str())
));
return;
}
Local<Object> object = Object::New(plv8_isolate);
object->Set(String::NewFromUtf8(plv8_isolate, "dev"), Number::New(plv8_isolate, status->stat_buf->st_dev));
object->Set(String::NewFromUtf8(plv8_isolate, "ino"), Number::New(plv8_isolate, status->stat_buf->st_ino));
object->Set(String::NewFromUtf8(plv8_isolate, "mode"), Number::New(plv8_isolate, status->stat_buf->st_mode));
object->Set(String::NewFromUtf8(plv8_isolate, "nlink"), Number::New(plv8_isolate, status->stat_buf->st_nlink));
object->Set(String::NewFromUtf8(plv8_isolate, "uid"), Number::New(plv8_isolate, status->stat_buf->st_uid));
object->Set(String::NewFromUtf8(plv8_isolate, "gid"), Number::New(plv8_isolate, status->stat_buf->st_gid));
object->Set(String::NewFromUtf8(plv8_isolate, "size"), Number::New(plv8_isolate, status->stat_buf->st_size));
object->Set(String::NewFromUtf8(plv8_isolate, "blocks"), Number::New(plv8_isolate, status->stat_buf->st_blocks));
object->Set(String::NewFromUtf8(plv8_isolate, "blksize"), Number::New(plv8_isolate, status->stat_buf->st_blksize));
#ifdef __APPLE__
object->Set(String::NewFromUtf8(plv8_isolate, "atimeMs"), Number::New(plv8_isolate, status->stat_buf->st_atimespec.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "mtimeMs"), Number::New(plv8_isolate, status->stat_buf->st_mtimespec.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "ctimeMs"), Number::New(plv8_isolate, status->stat_buf->st_ctimespec.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "atime"), Date::New(plv8_isolate, status->stat_buf->st_atimespec.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "mtime"), Date::New(plv8_isolate, status->stat_buf->st_mtimespec.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "ctime"), Date::New(plv8_isolate, status->stat_buf->st_ctimespec.tv_sec * 1000));
#ifdef _DARWIN_FEATURE_64_BIT_INODE
object->Set(String::NewFromUtf8(plv8_isolate, "birthtimeMs"), Number::New(plv8_isolate, status->stat_buf->st_birthtimespec.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "birthtime"), Date::New(plv8_isolate, status->stat_buf->st_birthtimespec.tv_sec * 1000));
#else
object->Set(String::NewFromUtf8(plv8_isolate, "birthtimeMs"), Number::New(plv8_isolate, 0));
object->Set(String::NewFromUtf8(plv8_isolate, "birthtime"), Date::New(plv8_isolate, 0));
#endif
#endif
#ifdef __linux__
object->Set(String::NewFromUtf8(plv8_isolate, "atimeMs"), Number::New(plv8_isolate, status->stat_buf->st_atim.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "mtimeMs"), Number::New(plv8_isolate, status->stat_buf->st_mtim.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "ctimeMs"), Number::New(plv8_isolate, status->stat_buf->st_ctim.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "atimeMs"), Number::New(plv8_isolate, status->stat_buf->st_atim.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "mtimeMs"), Number::New(plv8_isolate, status->stat_buf->st_mtim.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "ctimeMs"), Number::New(plv8_isolate, status->stat_buf->st_ctim.tv_sec * 1000));
object->Set(String::NewFromUtf8(plv8_isolate, "birthtimeMs"), Number::New(plv8_isolate, 0));
object->Set(String::NewFromUtf8(plv8_isolate, "birthtime"), Date::New(plv8_isolate, 0));
#endif
pfree(status);
args.GetReturnValue().Set(object);
}
static void plv8u_ReadFile(const FunctionCallbackInfo<v8::Value>& args) {
Isolate* plv8_isolate = args.GetIsolate();
if (args.Length() < 1) {
// Throw an Error that is passed back to JavaScript
plv8_isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(plv8_isolate, "path must be a string")));
return;
}
// Check the argument types
if (!args[0]->IsString()) {
plv8_isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(plv8_isolate, "path must be a string")));
return;
}
v8::String::Utf8Value s(args[0]);
std::string str(*s);
struct plv8u_file_status *status = read_file(str.c_str());
if (status->error) {
std::stringstream ss;
ss << "unable to open file or directory: '" << str.c_str() << "' " << strerror(status->error);
std::string ns = ss.str();
plv8_isolate->ThrowException(Exception::Error(
String::NewFromUtf8(plv8_isolate, ns.c_str())
));
return;
}
Local<String> ret = String::NewFromUtf8(plv8_isolate, (const char *) status->contents);
pfree(status->contents);
pfree(status);
args.GetReturnValue().Set(ret);
}
#if 0
// work in progress, hence defined off
static void plv8u_OpenFile(const FunctionCallbackInfo<v8::Value>& args) {
Isolate* plv8_isolate = args.GetIsolate();
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
plv8_isolate->ThrowException(Exception::Error(
String::NewFromUtf8(plv8_isolate, "Unkown file open flag: undefined")));
return;
}
// Check the argument types
if (!args[0]->IsString()) {
plv8_isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(plv8_isolate, "path must be a string")));
return;
}
if (!args[1]->IsString()) {
plv8_isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(plv8_isolate, "flag must be a string")));
return;
}
// path
v8::String::Utf8Value s(args[0]);
std::string path(*s);
// flags
v8::String::Utf8Value s2(args[1]);
std::string flags(*s2);
// mode, default to 0666
int mode = 0666;
if (args.Length() == 3) {
mode = args[2]->NumberValue();
}
int file = open(path.c_str(), )
}
#endif