-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
27 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This is a trivial extension, mostly for learning. | ||
|
||
Feel free to absorb as you see fit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |