# HG changeset patch # User Thinker K.F. Li # Date 1280146438 -28800 # Node ID dc32c1c140ae5bf719a8f21c7333c4ec6ad5f1d2 # Parent 683889344459b7f1fd1e6d5017c287c9d621ffa0 First compilable image loader binding for Javascript diff -r 683889344459 -r dc32c1c140ae nodejs/image_ldr.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nodejs/image_ldr.cc Mon Jul 26 20:13:58 2010 +0800 @@ -0,0 +1,111 @@ +/*! \file + * This file implements Javascript binding for img_ldr_t of MadButterfly. + */ +#include + +extern "C" { +#include "mb.h" +} + +#include "mbfly_njs.h" + +using namespace v8; + +#ifndef ASSERT +#define ASSERT(x) +#endif + +static Persistent img_data_temp; + +/*! \brief load() method of img_ldr Javascript objects. + */ +static Handle +xnjsmb_img_ldr_load(const Arguments &args) { + HandleScope scope; + int argc = args.Length(); + Handle self = args.This(); + char *img_id; + mb_img_ldr_t *img_ldr; + mb_img_data_t *img_data; + Handle img_data_obj; + + if(argc != 1) + THROW("Invalid number of arguments (!= 1)"); + if(!args[0]->IsString()) + THROW("Invalid argument type"); + + String::Utf8Value img_id_utf8(args[0]->ToString()); + img_id = *img_id_utf8; + img_ldr = (mb_img_ldr_t *)UNWRAP(self); + + img_data_obj = img_data_temp->NewInstance(); + ASSERT(img_data_obj); + + img_data = MB_IMG_LDR_LOAD(img_ldr, img_id); + if(img_data == NULL) + THROW("Can not load an image"); + WRAP(img_data_obj, img_data); + + scope.Close(img_data_obj); + + return img_data_obj; +} + +/*! \brief Constructor function of img_ldr Javascript objects. + */ +static Handle +xnjsmb_img_ldr_new(const Arguments &args) { + HandleScope scope; + int argc = args.Length(); + Handle self = args.This(); + char *path; + mb_img_ldr_t *img_ldr; + + if(argc != 1) + THROW("Invalid number of arguments (!= 1)"); + if(!args[0]->IsString()) + THROW("Invalid argument type"); + + String::Utf8Value pathutf8(args[0]->ToString()); + path = *pathutf8; + + img_ldr = simple_mb_img_ldr_new(path); + if(img_ldr == NULL) + THROW("Can not create an image loader"); + WRAP(self, img_ldr); + + return Null(); +} + +/*! \brief Initialize image loader. + * + * This function is called by init() in mbfly_njs.cc when the module + * being loaded. + */ +void +xnjsmb_img_ldr_init_mb_rt_temp(Handle rt_temp) { + HandleScope scope; + Handle img_ldr_new_temp; + Handle ldr_inst_temp; + Handle ldr_proto_temp; + Handle img_ldr_load_temp; + Handle _img_data_temp; + + /* Setup object template for img_data_t object for Javascript */ + _img_data_temp = ObjectTemplate::New(); + _img_data_temp->SetInternalFieldCount(1); + img_data_temp = Persistent::New(_img_data_temp); + + /* Setup img_ldr class */ + img_ldr_new_temp = FunctionTemplate::New(xnjsmb_img_ldr_new); + img_ldr_new_temp->SetClassName(String::New("img_ldr")); + ldr_inst_temp = img_ldr_new_temp->InstanceTemplate(); + ldr_inst_temp->SetInternalFieldCount(1); + + /* Set method load() for img_ldr */ + ldr_proto_temp = img_ldr_new_temp->PrototypeTemplate(); + img_ldr_load_temp = FunctionTemplate::New(xnjsmb_img_ldr_load); + SET(ldr_proto_temp, "load", img_ldr_load_temp); + + SET(rt_temp, "img_ldr_new", img_ldr_new_temp->GetFunction()); +} diff -r 683889344459 -r dc32c1c140ae nodejs/mbfly_njs.cc --- a/nodejs/mbfly_njs.cc Mon Jul 26 09:50:44 2010 +0800 +++ b/nodejs/mbfly_njs.cc Mon Jul 26 20:13:58 2010 +0800 @@ -148,7 +148,7 @@ xnjsmb_shapes_init_mb_rt_temp(mb_rt_func); xnjsmb_paints_init_mb_rt_temp(mb_rt_func); xnjsmb_font_init_mb_rt_temp(mb_rt_func); - xnjsmb_img_ldr_init_mb_rt_temp(mb_rt_func); + xnjsmb_img_ldr_init_mb_rt_temp(target); target->Set(String::New("mb_rt"), mb_rt_func->GetFunction()); } diff -r 683889344459 -r dc32c1c140ae nodejs/mbfly_njs.h --- a/nodejs/mbfly_njs.h Mon Jul 26 09:50:44 2010 +0800 +++ b/nodejs/mbfly_njs.h Mon Jul 26 20:13:58 2010 +0800 @@ -34,6 +34,6 @@ /* From image_ldr.cc */ void -xnjsmb_img_ldr_init_mb_rt_temp(v8::Handle mb_rt_temp); +xnjsmb_img_ldr_init_mb_rt_temp(v8::Handle mb_rt_temp); #endif /* __MBFLY_NJS_H_ */