Skip to content

Commit d30b028

Browse files
committed
add a local readFile for shortcutting require
1 parent 5e0eff8 commit d30b028

File tree

3 files changed

+123
-7
lines changed

3 files changed

+123
-7
lines changed

helpers/file.cc

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
extern "C" {
2-
#include <stdio.h>
3-
#include <string.h>
42
#include <sys/stat.h>
53

64
#include "postgres.h"
75
}
6+
#include <string>
7+
#include <iostream>
8+
#include <fstream>
9+
810
#include "file.h"
911

1012

@@ -27,3 +29,35 @@ struct plv8u_file_status *stat_file (const char *filename) {
2729

2830
return status;
2931
}
32+
33+
struct plv8u_file_status *read_file (const char *filename) {
34+
size_t size;
35+
char *memblock;
36+
37+
struct plv8u_file_status *stat_status = stat_file(filename);
38+
39+
if (stat_status->error) {
40+
return stat_status;
41+
}
42+
43+
struct plv8u_file_status *status = (struct plv8u_file_status *) palloc(sizeof(struct plv8u_file_status));
44+
45+
std::ifstream file (filename);
46+
47+
if (file.is_open()) {
48+
size = stat_status->stat_buf->st_size;
49+
50+
memblock = (char *) palloc(size + 1);
51+
file.seekg (0, std::ios::beg);
52+
file.read (memblock, (std::streampos) size);
53+
file.close();
54+
55+
status->error = 0;
56+
status->contents = memblock;
57+
} else {
58+
status->error = errno;
59+
}
60+
61+
pfree(stat_status);
62+
return status;
63+
}

helpers/file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
struct plv8u_file_status {
77
int error;
88
struct stat *stat_buf;
9-
unsigned char *contents;
9+
char *contents;
1010
};
1111

1212
struct plv8u_file_status *read_file (const char *);

plv8u_func.cc

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ extern "C" {
2020
#include "utils/lsyscache.h"
2121

2222
#include <errno.h>
23-
#include <stdio.h>
23+
#include <fcntl.h>
2424
} // extern "C"
2525

2626
using namespace v8;
2727

2828
extern v8::Isolate* plv8_isolate;
2929
static void plv8u_StatFile(const FunctionCallbackInfo<v8::Value>& args);
30+
static void plv8u_ReadFile(const FunctionCallbackInfo<v8::Value>& args);
3031
static void plv8_FunctionInvoker(const FunctionCallbackInfo<v8::Value>& args) throw();
3132

3233

@@ -61,11 +62,14 @@ SetupPlv8uFunctions(Handle<ObjectTemplate> plv8u)
6162
PropertyAttribute attrFull =
6263
PropertyAttribute(ReadOnly | DontEnum | DontDelete);
6364

64-
Local<ObjectTemplate> object = ObjectTemplate::New(plv8_isolate);
65+
Local<ObjectTemplate> internal = ObjectTemplate::New(plv8_isolate);
6566

66-
SetCallback(object, "stat", plv8u_StatFile, attrFull);
67+
Local<ObjectTemplate> fs = ObjectTemplate::New(plv8_isolate);
68+
SetCallback(fs, "stat", plv8u_StatFile, attrFull);
69+
SetCallback(fs, "readFile", plv8u_ReadFile, attrFull);
6770

68-
plv8u->Set(String::NewFromUtf8(plv8_isolate, "_internal"), object);
71+
internal->Set(String::NewFromUtf8(plv8_isolate, "fs"), fs);
72+
plv8u->Set(String::NewFromUtf8(plv8_isolate, "_internal"), internal);
6973

7074
plv8u->SetInternalFieldCount(PLV8_INTNL_MAX);
7175
}
@@ -192,6 +196,84 @@ static void plv8u_StatFile(const FunctionCallbackInfo<v8::Value>& args) {
192196
args.GetReturnValue().Set(object);
193197
}
194198

199+
static void plv8u_ReadFile(const FunctionCallbackInfo<v8::Value>& args) {
200+
Isolate* plv8_isolate = args.GetIsolate();
201+
if (args.Length() < 1) {
202+
// Throw an Error that is passed back to JavaScript
203+
plv8_isolate->ThrowException(Exception::TypeError(
204+
String::NewFromUtf8(plv8_isolate, "path must be a string")));
205+
return;
206+
}
207+
208+
// Check the argument types
209+
if (!args[0]->IsString()) {
210+
plv8_isolate->ThrowException(Exception::TypeError(
211+
String::NewFromUtf8(plv8_isolate, "path must be a string")));
212+
return;
213+
}
214+
215+
v8::String::Utf8Value s(args[0]);
216+
std::string str(*s);
217+
struct plv8u_file_status *status = read_file(str.c_str());
218+
219+
if (status->error) {
220+
std::stringstream ss;
221+
ss << "unable to open file or directory: '" << str.c_str() << "' " << strerror(status->error);
222+
std::string ns = ss.str();
223+
plv8_isolate->ThrowException(Exception::Error(
224+
String::NewFromUtf8(plv8_isolate, ns.c_str())
225+
));
226+
227+
return;
228+
}
229+
230+
Local<String> ret = String::NewFromUtf8(plv8_isolate, (const char *) status->contents);
231+
232+
pfree(status->contents);
233+
pfree(status);
234+
235+
args.GetReturnValue().Set(ret);
236+
}
237+
238+
#if 0
239+
// work in progress, hence defined off
195240
static void plv8u_OpenFile(const FunctionCallbackInfo<v8::Value>& args) {
241+
Isolate* plv8_isolate = args.GetIsolate();
242+
if (args.Length() < 2) {
243+
// Throw an Error that is passed back to JavaScript
244+
plv8_isolate->ThrowException(Exception::Error(
245+
String::NewFromUtf8(plv8_isolate, "Unkown file open flag: undefined")));
246+
return;
247+
}
196248

249+
// Check the argument types
250+
if (!args[0]->IsString()) {
251+
plv8_isolate->ThrowException(Exception::TypeError(
252+
String::NewFromUtf8(plv8_isolate, "path must be a string")));
253+
return;
254+
}
255+
256+
if (!args[1]->IsString()) {
257+
plv8_isolate->ThrowException(Exception::TypeError(
258+
String::NewFromUtf8(plv8_isolate, "flag must be a string")));
259+
return;
260+
}
261+
262+
// path
263+
v8::String::Utf8Value s(args[0]);
264+
std::string path(*s);
265+
266+
// flags
267+
v8::String::Utf8Value s2(args[1]);
268+
std::string flags(*s2);
269+
270+
// mode, default to 0666
271+
int mode = 0666;
272+
273+
if (args.Length() == 3) {
274+
mode = args[2]->NumberValue();
275+
}
276+
277+
int file = open(path.c_str(), )
197278
}
279+
#endif

0 commit comments

Comments
 (0)