Skip to content
Browse files

Works!

  • Loading branch information...
1 parent 27ff552 commit 123555ce2c6f17deb364614b8ad3821b11b55e63 @aredridel committed Sep 12, 2010
Showing with 27 additions and 39 deletions.
  1. +3 −0 README.md
  2. +24 −39 ext/ioctl.cc
View
3 README.md
@@ -0,0 +1,3 @@
+This is a trivial extension, mostly for learning.
+
+Feel free to absorb as you see fit.
View
63 ext/ioctl.cc
@@ -1,59 +1,44 @@
#include <v8.h>
+#include <node.h>
#include <sys/ioctl.h>
using namespace v8;
static Persistent<String> WINSZ_SYMBOL;
-class Ioctl : public Object {
- public:
- static void Initialize (Handle<Object> target) {
- HandleScope scope;
- Local<FunctionTemplate> t = FunctionTemplate::New(New);
+static Handle<Value> getwinsz (const Arguments &args) {
+ if (args.Length() < 1) return Undefined();
- t->Inherit(Object::constructor_template);
- t->InstanceTemplate()->SetInternalFieldCount(1);
-
- t->PrototypeTemplate()->SetAccessor(WINSZ_SYMBOL, WinSZGetter);
-
- }
-
- static Handle<Value> WinSZGetter (Local<String> property, const AccessorInfo& info) {
- int fd = 1;
+ int fd = args[0]->ToInteger()->Value();
#ifdef TIOCGSIZE
- struct ttysize win;
+ struct ttysize win;
#elif defined(TIOCGWINSZ)
- struct winsize win;
+ struct winsize win;
#endif
- HandleScope scope;
- Local<Array> winsz = Array::New(4);
+ HandleScope scope;
+ Local<Array> winsz = Array::New(4);
#ifdef TIOCGSIZE
- if (ioctl (fd, TIOCGSIZE, &win))
- return scope.Close(Integer::New(0));
- winsz->Set(Integer::New(win.ts_lines), 0);
- winsz->Set(Integer::New(win.ts_cols), 1);
- winsz->Set(Integer::New(win.ts_xxx), 2);
- winsz->Set(Integer::New(win.ts_yyy), 3);
+ if (ioctl (fd, TIOCGSIZE, &win))
+ return scope.Close(Number::New(0));
+ winsz->Set(Number::New(0), Integer::New(win.ts_lines));
+ winsz->Set(Number::New(1), Integer::New(win.ts_cols));
+ winsz->Set(Number::New(2), Integer::New(win.ts_xxx));
+ winsz->Set(Number::New(3), Integer::New(win.ts_yyy));
#else
- if (ioctl (fd, TIOCGWINSZ, &win))
- return scope.Close(Integer::New(0));
- winsz->Set(Integer::New(win.ws_row), 0);
- winsz->Set(Integer::New(win.ws_col), 1);
- winsz->Set(Integer::New(win.ws_xpixel), 2);
- winsz->Set(Integer::New(win.ws_ypixel), 3);
+ if (ioctl (fd, TIOCGWINSZ, &win))
+ return scope.Close(Integer::New(0));
+ winsz->Set(Number::New(0), Integer::New(win.ws_row));
+ winsz->Set(Number::New(1), Integer::New(win.ws_col));
+ winsz->Set(Number::New(2), Integer::New(win.ws_xpixel));
+ winsz->Set(Number::New(3), Integer::New(win.ws_ypixel));
#endif
- return scope.Close(winsz);
- }
-
-
+ return scope.Close(winsz);
}
extern "C" void
-init (Handle<Object> target)
-{
- HandleScope scope;
- Ioctl.Initialize(target);
- target->Set(String::New("hello"), String::New("World"));
+init (Handle<Object> target) {
+ HandleScope scope;
+ NODE_SET_METHOD(target, "getwinsz", getwinsz);
}

0 comments on commit 123555c

Please sign in to comment.
Something went wrong with that request. Please try again.