annotate nodejs/image_ldr.cc @ 640:9016e7e70f7d

Group binding for image loader with Doxygen syntax
author Thinker K.F. Li <thinker@branda.to>
date Mon, 26 Jul 2010 20:59:49 +0800
parents dc32c1c140ae
children a65720721c60
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
640
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
18 /*! \defgroup img_ldr_js Javascript binding for image loader.
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
19 *
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
20 * @{
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
21 */
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
22
639
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 static Persistent<ObjectTemplate> img_data_temp;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 /*! \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
26 */
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 static Handle<Value>
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 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
29 HandleScope scope;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 int argc = args.Length();
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 Handle<Object> self = args.This();
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 char *img_id;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 mb_img_ldr_t *img_ldr;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 mb_img_data_t *img_data;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 Handle<Object> img_data_obj;
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 if(argc != 1)
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 THROW("Invalid number of arguments (!= 1)");
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 if(!args[0]->IsString())
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 THROW("Invalid argument type");
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 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
43 img_id = *img_id_utf8;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 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
45
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 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
47 ASSERT(img_data_obj);
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 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
50 if(img_data == NULL)
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 THROW("Can not load an image");
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 WRAP(img_data_obj, img_data);
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 scope.Close(img_data_obj);
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 return img_data_obj;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 }
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 /*! \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
60 */
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 static Handle<Value>
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 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
63 HandleScope scope;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 int argc = args.Length();
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 Handle<Object> self = args.This();
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 char *path;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 mb_img_ldr_t *img_ldr;
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 if(argc != 1)
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 THROW("Invalid number of arguments (!= 1)");
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 if(!args[0]->IsString())
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 THROW("Invalid argument type");
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 String::Utf8Value pathutf8(args[0]->ToString());
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 path = *pathutf8;
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 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
78 if(img_ldr == NULL)
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 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
80 WRAP(self, img_ldr);
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 return Null();
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 }
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84
640
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
85 /* @} */
9016e7e70f7d Group binding for image loader with Doxygen syntax
Thinker K.F. Li <thinker@branda.to>
parents: 639
diff changeset
86
639
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 /*! \brief Initialize image loader.
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 *
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 * 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
90 * being loaded.
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 */
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 void
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 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
94 HandleScope scope;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 Handle<FunctionTemplate> img_ldr_new_temp;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 Handle<ObjectTemplate> ldr_inst_temp;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 Handle<ObjectTemplate> ldr_proto_temp;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 Handle<FunctionTemplate> img_ldr_load_temp;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 Handle<ObjectTemplate> _img_data_temp;
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 /* 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
102 _img_data_temp = ObjectTemplate::New();
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 _img_data_temp->SetInternalFieldCount(1);
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 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
105
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106 /* Setup img_ldr class */
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 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
108 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
109 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
110 ldr_inst_temp->SetInternalFieldCount(1);
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112 /* Set method load() for img_ldr */
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 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
114 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
115 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
116
dc32c1c140ae First compilable image loader binding for Javascript
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 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
118 }