Skip to content
Browse files

Update templates

  • Loading branch information...
1 parent 385f4cc commit 39caad6039a0c653502c35e71a7980df5fac44c1 @Benvie committed Apr 11, 2012
Showing with 47 additions and 18 deletions.
  1. +35 −12 src/template.cc
  2. +12 −6 src/template.h
View
47 src/template.cc
@@ -16,15 +16,15 @@
// names of contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ARTUR ADIB BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BUILDING_NODE_EXTENSION
@@ -46,14 +46,17 @@ __Template__Wrap::~__Template__Wrap() {
}
void __Template__Wrap::Initialize(Handle<Object> target) {
- // Prepare constructor template
+ // Constructor
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
- tpl->SetClassName(String::NewSymbol("__Template__"));
- tpl->InstanceTemplate()->SetInternalFieldCount(1);
+ tpl->SetClassName(String::NewSymbol("__Template__"))
// Prototype
- tpl->PrototypeTemplate()->Set(String::NewSymbol("example"),
- FunctionTemplate::New(Example)->GetFunction());
+ node::SetPrototypeMethod(tpl, "exampleMethod", ExampleMethod);
+
+ // Instance
+ Local<ObjectTemplate> inst = tpl->InstanceTemplate();
+ inst->SetInternalFieldCount(1);
+ inst->SetAccessor(String::NewSymbol("example"), GetExample, SetExample);
constructor = Persistent<Function>::New(tpl->GetFunction());
target->Set(String::NewSymbol("__Template__"), constructor);
@@ -70,15 +73,35 @@ Handle<Value> __Template__Wrap::New(const Arguments& args) {
Handle<Value> __Template__Wrap::NewInstance(__Template__ q) {
HandleScope scope;
-
+
Local<Object> instance = constructor->NewInstance(0, NULL);
__Template__Wrap* w = node::ObjectWrap::Unwrap<__Template__Wrap>(instance);
w->SetWrapped(q);
return scope.Close(instance);
}
-Handle<Value> __Template__Wrap::Example(const Arguments& args) {
+
+Handle<Value> __Template__Wrap::GetExample(Local<String> name, const AccessorInfo& info) {
+ HandleScope scope;
+
+ __Template__Wrap* w = node::ObjectWrap::Unwrap<__Template__Wrap>(info.Holder());
+ __Template__* q = w->GetWrapped();
+
+ return scope.Close(Integer::New(q->getExample()));
+}
+
+void __Template__Wrap::SetExample(Local<String> property, Local<Value> value, const AccessorInfo &info) {
+ HandleScope scope;
+
+ __Template__Wrap* w = node::ObjectWrap::Unwrap<__Template__Wrap>(info.Holder());
+ __Template__* q = w->GetWrapped();
+
+ q->setExample(value->IntegerValue());
+}
+
+
+Handle<Value> __Template__Wrap::ExampleMethod(const Arguments& args) {
HandleScope scope;
__Template__Wrap* w = ObjectWrap::Unwrap<__Template__Wrap>(args.This());
@@ -87,4 +110,4 @@ Handle<Value> __Template__Wrap::Example(const Arguments& args) {
// q->...?
return scope.Close(Undefined());
-}
+}
View
18 src/template.h
@@ -34,24 +34,30 @@
#include <node.h>
#include <__Template__>
+using namespace v8;
+
class __Template__Wrap : public node::ObjectWrap {
public:
- static void Initialize(v8::Handle<v8::Object> target);
+ static void Initialize(Handle<Object> target);
__Template__* GetWrapped() const { return q_; };
void SetWrapped(__Template__ q) {
if (q_) delete q_;
q_ = new __Template__(q);
};
- static v8::Handle<v8::Value> NewInstance(__Template__ q);
+ static Handle<Value> NewInstance(__Template__ q);
private:
- __Template__Wrap(const v8::Arguments& args);
+ __Template__Wrap(const Arguments& args);
~__Template__Wrap();
- static v8::Persistent<v8::Function> constructor;
- static v8::Handle<v8::Value> New(const v8::Arguments& args);
+ static Persistent<Function> constructor;
+ static Handle<Value> New(const Arguments& args);
+
+ // Wrapped properties
+ static Handle<Value> GetExample(Local<String> name, const AccessorInfo& info);
+ static void SetExample(Local<String> name, Local<Value> value, const AccessorInfo& info);
// Wrapped methods
- static v8::Handle<v8::Value> Example(const v8::Arguments& args);
+ static Handle<Value> ExampleMethod(const Arguments& args);
// Wrapped object
__Template__* q_;

0 comments on commit 39caad6

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