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