utilities for v8 development
Using a C package manager like cpm you can install v8stdio into the deps/
directory of your project. It also installs to /usr/local/include
and /usr/local/lib
by default.
$ cpm install jwerle/v8stdio
Just link in your compilation
$ g++ -I/usr/local/include/v8 -lv8 -lv8stdio program.cc -o program
Change to v8stdio
root directory
$ cd v8stdio/
Install libv8stdio
with make
. requires g++
You can set the PREFIX
variable to be whatever you would like. Defaults to /usr/local/
$ make install
You can now link v8stdio
to your project
$ g++ -I/usr/local/include/v8 -lv8 -lv8stdio program.cc -o program
You can include v8stdio
in your node.js addon by simply including it in the "sources" array in your binding.gyp
file
{
"targets": [
{
"target_name": "myaddon",
"sources": [
"deps/v8stdio.h", "deps/v8stdio.cc",
"src/myaddon.cc"
]
}
]
}
To expose the stdout
and stderr
objects to your JavaScript environment in v8 you must attached it to an object like global object.
Attaching to the global object
v8stdio::AttachTo(v8stdio::Init(), v8::Context::GetCurrent()->Global());
Attaching to a target object
v8::Handle<v8::Object> target = v8::Object::New();
v8stdio::AttachTo(v8stdio::Init(), target);
stdout in javascript
stdio.stdout.write("hello world\n");
stderr in javascript
stdio.stderr.write("Error!\n");
Initializes the stdout
and stderr
objects
example
v8::Handle<v8::Object> stdiov = v8stdio::Init();
Attaches stdio
object to a given object
example
v8stdio::AttachTo(stdiov, global);
Initializes the StdOut
object
example
v8::Local<v8::Object> stdout_ = v8stdio::StdOut::Init(v8::Object::New());
Callback for JavaScript bindings to perform a write to stdout
example*
target->Set(
v8::String::New("write"),
v8::FunctionTemplate::New((v8::FunctionCallback) v8stdio::StdOut::v8Write)->GetFunction()
);
Initializes the StdErr
object
example
v8::Local<v8::Object> stdout_ = v8stdio::StdErr::Init(v8::Object::New());
Callback for JavaScript bindings to perform a write to stderr
example*
target->Set(
v8::String::New("write"),
v8::FunctionTemplate::New((v8::FunctionCallback) v8stdio::StdErr::v8Write)->GetFunction()
);
MIT