Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1.  
  2. #include <v8.h>
  3. #include <node.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9.  
  10. using namespace std;
  11. using namespace node;
  12. using namespace v8;
  13.  
  14. extern "C" void init (Handle<Object>);
  15.  
  16. static Handle<Value> OpendirAsync (const Arguments&);
  17. static int Opendir (eio_req *);
  18. static int Opendir_After (eio_req *);
  19. /*
  20. static Handle<Value> DoSomething2Async (const Arguments&);
  21. static int DoSomething2 (eio_req *);
  22. static int DoSomething2_After (eio_req *);
  23. *?
  24. /*
  25. static Handle<Value> ReadDir(const Arguments& args) {
  26. HandleScope scope;
  27.  
  28. if (args.Length() < 1 || !args[0]->IsString()) {
  29. return THROW_BAD_ARGS;
  30. }
  31.  
  32. String::Utf8Value path(args[0]->ToString());
  33.  
  34. if (args[1]->IsFunction()) {
  35. ASYNC_CALL(readdir, args[1], *path, 0 ) // 0= flags
  36. } else {
  37. DIR *dir = opendir(*path);
  38. if (!dir) return ThrowException(ErrnoException(errno, NULL, "", *path));
  39.  
  40. struct dirent *ent;
  41.  
  42. Local<Array> files = Array::New();
  43. char *name;
  44. int i = 0;
  45.  
  46. while (ent = readdir(dir)) {
  47. name = ent->d_name;
  48.  
  49. if (name[0] != '.' || (name[1] && (name[1] != '.' || name[2]))) {
  50. files->Set(Integer::New(i), String::New(name));
  51. i++;
  52. }
  53. }
  54.  
  55. closedir(dir);
  56.  
  57. return scope.Close(files);
  58. }
  59. }
  60. */
  61.  
  62. struct opendir_simple_request {
  63. //int y;
  64. DIR *dir;
  65. Persistent<Function> cb;
  66. Persistent<String> path;
  67.  
  68. // maybe it matters to put the char[] last? not sure.
  69. //char name[1];
  70. };
  71.  
  72. static Handle<Value> OpendirAsync (const Arguments& args) {
  73. HandleScope scope;
  74. const char *usage = "usage: Opendir(path, cb)";
  75. if (args.Length() != 2) {
  76. return ThrowException(Exception::Error(String::New(usage)));
  77. }
  78. Local<Function> cb = Local<Function>::Cast(args[1]);
  79. Local<String> path = Local<String>::Cast(args[0]);
  80.  
  81. opendir_simple_request *sr = (opendir_simple_request *)
  82. malloc(sizeof(struct opendir_simple_request) + 1);
  83.  
  84. sr->cb = Persistent<Function>::New(cb);
  85. sr->path = Persistent<String>::New(path);
  86. eio_custom(Opendir, EIO_PRI_DEFAULT, Opendir_After, sr);
  87. ev_ref(EV_DEFAULT_UC);
  88. return Undefined();
  89. }
  90.  
  91. // this function happens on the thread pool
  92. static int Opendir (eio_req *req) {
  93. struct opendir_simple_request * sr = (struct opendir_simple_request *)req->data;
  94. //sleep(2); // just to make it less pointless to be async.
  95. String::Utf8Value path(sr->path->ToString());
  96. DIR *dir = opendir(*path);
  97. sr->dir=dir;
  98. //req->result = sr->x + sr->y;
  99. return 0;
  100. }
  101.  
  102. static int Opendir_After (eio_req *req) {
  103. HandleScope scope;
  104. ev_unref(EV_DEFAULT_UC);
  105. struct opendir_simple_request * sr = (struct opendir_simple_request *)req->data;
  106. Local<Value> argv[1];
  107. //argv[1] = Integer::New(req->result);
  108. External <Handle> ex((void **)sr->dir);
  109. argv[0] = ex;
  110.  
  111. argv[0] = Local<Value>::New(Null());
  112. //argv[0] = Local<Value>::New(Null());
  113. //argv[0] = Local<Value>::New(Null());
  114. //v8::Handle<v8::External> class_ptr
  115. //v8::Handle<v8::External> class_ptr = v8::External::New(foo);
  116. //argv[0]= class_ptr;
  117. //static v8::Local<T> v8::Local<T>::New(v8::Handle<T>) [with T = v8::Value]
  118. //Handle<DIR> x(sr->dir);
  119. // argv[0] = Local<DIR>::New(sr->dir);
  120. //argv[0] = Persistent<Value>::New((sr->dir));
  121. //argv[0] = Handle::New(sr->dir);
  122. //argv[2] = String::New(sr->name);
  123. if (!sr->dir)
  124. {
  125. //int errno=1;
  126. ThrowException(Exception::Error(String::New("Opendir error...")));
  127. //return ThrowException(ErrnoException(errno, NULL, "", *path));
  128. }
  129.  
  130. TryCatch try_catch;
  131. sr->cb->Call(Context::GetCurrent()->Global(), 1, argv);
  132. if (try_catch.HasCaught()) {
  133. FatalException(try_catch);
  134. }
  135. closedir(sr->dir);
  136.  
  137. sr->cb.Dispose();
  138. sr->path.Dispose();
  139. free(sr);
  140. return 0;
  141. }
  142. /*
  143. struct simple2_request {
  144. int x;
  145. int y;
  146. Persistent<Function> cb;
  147. Local<Value> js_result;
  148. };
  149.  
  150. static Handle<Value> DoSomething2Async (const Arguments& args) {
  151. HandleScope scope;
  152. const char *usage = "usage: doSomething2(x, y, cb)";
  153. if (args.Length() != 3) {
  154. return ThrowException(Exception::Error(String::New(usage)));
  155. }
  156. int x = args[0]->Int32Value();
  157. int y = args[1]->Int32Value();
  158. Local<Function> cb = Local<Function>::Cast(args[2]);
  159.  
  160. simple2_request *sr = (simple2_request *)
  161. malloc(sizeof(struct simple2_request));
  162.  
  163. sr->cb = Persistent<Function>::New(cb);
  164. sr->x = x;
  165. sr->y = y;
  166.  
  167. eio_custom(DoSomething2, EIO_PRI_DEFAULT, DoSomething2_After, sr);
  168. ev_ref(EV_DEFAULT_UC);
  169. return Undefined();
  170. }
  171.  
  172. // this function happens on the thread pool
  173. static int DoSomething2 (eio_req *req) {
  174. HandleScope scope;
  175. struct simple2_request *sr = (struct simple2_request *)req->data;
  176. sr->js_result = scope.Close(Integer::New(sr->x + sr->y));
  177. return 0;
  178. }
  179.  
  180. static int DoSomething2_After (eio_req *req) {
  181. HandleScope scope;
  182. ev_unref(EV_DEFAULT_UC);
  183. struct simple2_request * sr = (struct simple2_request *)req->data;
  184. Local<Value> argv[3];
  185. argv[0] = Local<Value>::New(Null());
  186. argv[1] = sr->js_result;
  187.  
  188. TryCatch try_catch;
  189. sr->cb->Call(Context::GetCurrent()->Global(), 2, argv);
  190. if (try_catch.HasCaught()) {
  191. FatalException(try_catch);
  192. }
  193.  
  194. sr->cb.Dispose();
  195. free(sr);
  196. return 0;
  197. }
  198. */
  199. extern "C" void init (Handle<Object> target) {
  200. HandleScope scope;
  201. NODE_SET_METHOD(target, "Opendir", OpendirAsync);
  202. //NODE_SET_METHOD(target, "doSomething2", DoSomething2Async);
  203. }
Add Comment
Please, Sign In to add comment