}
// this.icon
static v8::Handle<Value> GetIcon(v8::Local<v8::String> property, const v8::AccessorInfo& info) {
// Extract the C++ request object from the JavaScript wrapper.
Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(info.Holder());
return v8::String::New(gtknotify_instance->icon.c_str());
}
// this.icon=
static void SetIcon(Local<String> property, Local<Value> value, const AccessorInfo& info) {
Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(info.Holder());
v8::String::Utf8Value v8str(value);
gtknotify_instance->icon = *v8str;
}
原型方法: Send()
首先我们抽取 C++ 对象的 this 引用,然后使用对象的属性来构建通知并显示。
// this.send()
static v8::Handle<Value> Send(const Arguments& args) {
v8::HandleScope scope;
// Extract C++ object reference from "this"
Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(args.This()); // Convert first argument to V8 String
v8::String::Utf8Value v8str(args[0]);
// For more info on the Notify library: http://library.gnome.org/devel/libnotify/0.7/NotifyNotification.html
Notify::init("Basic");
// Arguments: title, content, icon
Notify::Notification n(gtknotify_instance->title.c_str(), *v8str, gtknotify_instance->icon.c_str()); // *v8str points to the C string it wraps
// Display the notification
n.show();
// Return value
return v8::Boolean::New(true);
}
编译扩展
node-waf 是一个构建工具,用来编译 Node 的扩展,这是 waf 的基本封装。构建过程可通过名为 wscript 的文件进行配置。
def set_options(opt):
opt.tool_options("compiler_cxx")def configure(conf):
conf.check_tool("compiler_cxx")
conf.check_tool("node_addon")
# This will tell the compiler to link our extension with the gtkmm and libnotifymm libraries.
conf.check_cfg(package='gtkmm-2.4', args='--cflags --libs', uselib_store='LIBGTKMM')
conf.check_cfg(package='libnotifymm-1.0', args='--cflags --libs', uselib_store='LIBNOTIFYMM')
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"] # This is the name of our extension.
obj.target = "gtknotify"
obj.source = "src/node_gtknotify.cpp"
obj.uselib = ['LIBGTKMM', 'LIBNOTIFYMM']
现在我们已经准备好要开始构建了,在顶级目录下运行如下命令:
node-waf configure && node-waf build
如果一切正常,我们将得到编译过的扩展,位于:./build/default/gtknotify.node ,来试试:









