@@ -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
2626using namespace v8 ;
2727
2828extern v8::Isolate* plv8_isolate;
2929static void plv8u_StatFile (const FunctionCallbackInfo<v8::Value>& args);
30+ static void plv8u_ReadFile (const FunctionCallbackInfo<v8::Value>& args);
3031static 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
195240static 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