diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c3ae870 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +// Copyright 2010, devKnowledge.com Ltd. All rights reserved. +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..7dc53e7 --- /dev/null +++ b/README @@ -0,0 +1,21 @@ +node-gc +============= + +A very small wrapper around v8 Garbage collection. + +To build: + +node-waf configure +node-waf build + +you should then have a gc.node binary which you will need to make available to any +scripts that want to use it. + +To use in a script: + +var gc = require("./gc"); + +var GC = new gc.GC(); +gc.collect(); + +run node.js for a full example. \ No newline at end of file diff --git a/gc.cc b/gc.cc new file mode 100644 index 0000000..484da59 --- /dev/null +++ b/gc.cc @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +using namespace v8; +using namespace node; + +class GC : public EventEmitter { + public: + static void + Initialize (v8::Handle target) + { + HandleScope scope; + + Local t = FunctionTemplate::New(New); + + t->Inherit(EventEmitter::constructor_template); + t->InstanceTemplate()->SetInternalFieldCount(1); + + NODE_SET_PROTOTYPE_METHOD(t, "collect", Collect); + + target->Set(String::NewSymbol("GC"), t->GetFunction()); + } + + void Collect() { + while(!V8::IdleNotification()){}; + } + + protected: + + static Handle + New (const Arguments& args) + { + HandleScope scope; + + GC *gc = new GC(); + gc->Wrap(args.This()); + + return args.This(); + } + + static Handle + Collect (const Arguments& args) + { + GC *gc = ObjectWrap::Unwrap(args.This()); + + HandleScope scope; + + gc->Collect(); + + return v8::Null(); + } + + GC () : EventEmitter () + { + } + + ~GC () + { + } +}; + +extern "C" void +init (Handle target) +{ + HandleScope scope; + GC::Initialize(target); +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..32099e3 --- /dev/null +++ b/test.js @@ -0,0 +1,6 @@ +var sys = require("sys"); +var gc = require("./gc"); + +var GC = new gc.GC(); +sys.puts(sys.inspect(GC, true, 10)); +GC.collect(); diff --git a/wscript b/wscript new file mode 100644 index 0000000..710ade2 --- /dev/null +++ b/wscript @@ -0,0 +1,30 @@ +import Options +from os import unlink, symlink, popen +from os.path import exists + +srcdir = "." +blddir = "build" +VERSION = "0.0.1" + +def set_options(opt): + opt.tool_options("compiler_cxx") + opt.tool_options("compiler_cc") + +def configure(conf): + conf.check_tool("compiler_cxx") + conf.check_tool("compiler_cc") + conf.check_tool("node_addon") + +def build(bld): + obj = bld.new_task_gen("cxx", "shlib", "node_addon") + obj.target = "gc" + obj.source = "gc.cc" + +def shutdown(): + # HACK to get compress.node out of build directory. + # better way to do this? + if Options.commands['clean']: + if exists('gc.node'): unlink('gc.node') + else: + if exists('build/default/gc.node') and not exists('gc.node'): + symlink('build/default/gc.node', 'gc.node')