Mercurial > MadButterfly
comparison nodejs/image_ldr.cc @ 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 | |
children | 9016e7e70f7d |
comparison
equal
deleted
inserted
replaced
638:683889344459 | 639:dc32c1c140ae |
---|---|
1 /*! \file | |
2 * This file implements Javascript binding for img_ldr_t of MadButterfly. | |
3 */ | |
4 #include <v8.h> | |
5 | |
6 extern "C" { | |
7 #include "mb.h" | |
8 } | |
9 | |
10 #include "mbfly_njs.h" | |
11 | |
12 using namespace v8; | |
13 | |
14 #ifndef ASSERT | |
15 #define ASSERT(x) | |
16 #endif | |
17 | |
18 static Persistent<ObjectTemplate> img_data_temp; | |
19 | |
20 /*! \brief load() method of img_ldr Javascript objects. | |
21 */ | |
22 static Handle<Value> | |
23 xnjsmb_img_ldr_load(const Arguments &args) { | |
24 HandleScope scope; | |
25 int argc = args.Length(); | |
26 Handle<Object> self = args.This(); | |
27 char *img_id; | |
28 mb_img_ldr_t *img_ldr; | |
29 mb_img_data_t *img_data; | |
30 Handle<Object> img_data_obj; | |
31 | |
32 if(argc != 1) | |
33 THROW("Invalid number of arguments (!= 1)"); | |
34 if(!args[0]->IsString()) | |
35 THROW("Invalid argument type"); | |
36 | |
37 String::Utf8Value img_id_utf8(args[0]->ToString()); | |
38 img_id = *img_id_utf8; | |
39 img_ldr = (mb_img_ldr_t *)UNWRAP(self); | |
40 | |
41 img_data_obj = img_data_temp->NewInstance(); | |
42 ASSERT(img_data_obj); | |
43 | |
44 img_data = MB_IMG_LDR_LOAD(img_ldr, img_id); | |
45 if(img_data == NULL) | |
46 THROW("Can not load an image"); | |
47 WRAP(img_data_obj, img_data); | |
48 | |
49 scope.Close(img_data_obj); | |
50 | |
51 return img_data_obj; | |
52 } | |
53 | |
54 /*! \brief Constructor function of img_ldr Javascript objects. | |
55 */ | |
56 static Handle<Value> | |
57 xnjsmb_img_ldr_new(const Arguments &args) { | |
58 HandleScope scope; | |
59 int argc = args.Length(); | |
60 Handle<Object> self = args.This(); | |
61 char *path; | |
62 mb_img_ldr_t *img_ldr; | |
63 | |
64 if(argc != 1) | |
65 THROW("Invalid number of arguments (!= 1)"); | |
66 if(!args[0]->IsString()) | |
67 THROW("Invalid argument type"); | |
68 | |
69 String::Utf8Value pathutf8(args[0]->ToString()); | |
70 path = *pathutf8; | |
71 | |
72 img_ldr = simple_mb_img_ldr_new(path); | |
73 if(img_ldr == NULL) | |
74 THROW("Can not create an image loader"); | |
75 WRAP(self, img_ldr); | |
76 | |
77 return Null(); | |
78 } | |
79 | |
80 /*! \brief Initialize image loader. | |
81 * | |
82 * This function is called by init() in mbfly_njs.cc when the module | |
83 * being loaded. | |
84 */ | |
85 void | |
86 xnjsmb_img_ldr_init_mb_rt_temp(Handle<Object> rt_temp) { | |
87 HandleScope scope; | |
88 Handle<FunctionTemplate> img_ldr_new_temp; | |
89 Handle<ObjectTemplate> ldr_inst_temp; | |
90 Handle<ObjectTemplate> ldr_proto_temp; | |
91 Handle<FunctionTemplate> img_ldr_load_temp; | |
92 Handle<ObjectTemplate> _img_data_temp; | |
93 | |
94 /* Setup object template for img_data_t object for Javascript */ | |
95 _img_data_temp = ObjectTemplate::New(); | |
96 _img_data_temp->SetInternalFieldCount(1); | |
97 img_data_temp = Persistent<ObjectTemplate>::New(_img_data_temp); | |
98 | |
99 /* Setup img_ldr class */ | |
100 img_ldr_new_temp = FunctionTemplate::New(xnjsmb_img_ldr_new); | |
101 img_ldr_new_temp->SetClassName(String::New("img_ldr")); | |
102 ldr_inst_temp = img_ldr_new_temp->InstanceTemplate(); | |
103 ldr_inst_temp->SetInternalFieldCount(1); | |
104 | |
105 /* Set method load() for img_ldr */ | |
106 ldr_proto_temp = img_ldr_new_temp->PrototypeTemplate(); | |
107 img_ldr_load_temp = FunctionTemplate::New(xnjsmb_img_ldr_load); | |
108 SET(ldr_proto_temp, "load", img_ldr_load_temp); | |
109 | |
110 SET(rt_temp, "img_ldr_new", img_ldr_new_temp->GetFunction()); | |
111 } |