Mercurial > MadButterfly
changeset 639:dc32c1c140ae
First compilable image loader binding for Javascript
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Mon, 26 Jul 2010 20:13:58 +0800 |
parents | 683889344459 |
children | 9016e7e70f7d |
files | nodejs/image_ldr.cc nodejs/mbfly_njs.cc nodejs/mbfly_njs.h |
diffstat | 3 files changed, 113 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /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 <v8.h> + +extern "C" { +#include "mb.h" +} + +#include "mbfly_njs.h" + +using namespace v8; + +#ifndef ASSERT +#define ASSERT(x) +#endif + +static Persistent<ObjectTemplate> img_data_temp; + +/*! \brief load() method of img_ldr Javascript objects. + */ +static Handle<Value> +xnjsmb_img_ldr_load(const Arguments &args) { + HandleScope scope; + int argc = args.Length(); + Handle<Object> self = args.This(); + char *img_id; + mb_img_ldr_t *img_ldr; + mb_img_data_t *img_data; + Handle<Object> 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<Value> +xnjsmb_img_ldr_new(const Arguments &args) { + HandleScope scope; + int argc = args.Length(); + Handle<Object> 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<Object> rt_temp) { + HandleScope scope; + Handle<FunctionTemplate> img_ldr_new_temp; + Handle<ObjectTemplate> ldr_inst_temp; + Handle<ObjectTemplate> ldr_proto_temp; + Handle<FunctionTemplate> img_ldr_load_temp; + Handle<ObjectTemplate> _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<ObjectTemplate>::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()); +}
--- 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()); }
--- 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<v8::FunctionTemplate> mb_rt_temp); +xnjsmb_img_ldr_init_mb_rt_temp(v8::Handle<v8::Object> mb_rt_temp); #endif /* __MBFLY_NJS_H_ */