JSBModule.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "../Atomic.h"
  2. #include "../IO/Log.h"
  3. #include "../Resource/JSONFile.h"
  4. #include "../Resource/ResourceCache.h"
  5. #include "../Core/ProcessUtils.h"
  6. #include "JSBind.h"
  7. #include "JSBindings.h"
  8. #include "JSBHeader.h"
  9. #include "JSBModule.h"
  10. #include "JSBFunction.h"
  11. void JSBModule::ParseHeaders()
  12. {
  13. for (unsigned i = 0; i < headerFiles_.Size(); i++)
  14. {
  15. JSBHeader* header = new JSBHeader(this, headerFiles_.At(i));
  16. headers_.Push(header);
  17. header->Parse();
  18. }
  19. }
  20. void JSBModule::PreprocessHeaders()
  21. {
  22. for (unsigned i = 0; i < headers_.Size(); i++)
  23. {
  24. headers_[i]->VisitPreprocess();
  25. }
  26. }
  27. void JSBModule::VisitHeaders()
  28. {
  29. for (unsigned i = 0; i < headers_.Size(); i++)
  30. {
  31. headers_[i]->VisitHeader();
  32. }
  33. }
  34. void JSBModule::WriteClassDeclaration(String& source)
  35. {
  36. source += "static void jsb_declare_classes(JSVM* vm)\n{\n";
  37. source += "duk_context* ctx = vm->GetJSContext();\n";
  38. for (unsigned i = 0; i < classes_.Size(); i++)
  39. {
  40. JSBClass* klass = classes_.At(i);
  41. if (klass->isNumberArray())
  42. continue;
  43. source.AppendWithFormat(" js_class_declare(vm, \"%s\", jsb_constructor_%s);\n", klass->GetName().CString(), klass->GetName().CString());
  44. if (klass->hasProperties())
  45. {
  46. source.AppendWithFormat("js_class_push_propertyobject(vm, \"%s\");\n", klass->GetName().CString());
  47. Vector<String> pnames;
  48. klass->GetPropertyNames(pnames);
  49. for (unsigned j = 0; j < pnames.Size(); j++)
  50. {
  51. JSBProperty* prop = klass->GetProperty(pnames[j]);
  52. source.Append("duk_push_object(ctx);\n");
  53. if (prop->getter_ && !prop->getter_->Skip())
  54. {
  55. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 0);\n",
  56. klass->GetName().CString(), prop->getter_->name_.CString());
  57. source.Append("duk_put_prop_string(ctx, -2, \"get\");\n");
  58. }
  59. if (prop->setter_ && !prop->setter_->Skip())
  60. {
  61. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 1);\n",
  62. klass->GetName().CString(), prop->setter_->name_.CString());
  63. source.Append("duk_put_prop_string(ctx, -2, \"set\");\n");
  64. }
  65. pnames[j][0] = tolower(pnames[j][0]);
  66. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", pnames[j].CString());
  67. }
  68. source.Append("duk_pop(ctx);\n");
  69. }
  70. }
  71. source += "\n}\n\n";
  72. }
  73. void JSBModule::WriteClassDefine(String& source)
  74. {
  75. source += "static void jsb_init_classes(JSVM* vm)\n{\n";
  76. for (unsigned i = 0; i < classes_.Size(); i++)
  77. {
  78. JSBClass* klass = classes_.At(i);
  79. if (klass->isNumberArray())
  80. continue;
  81. source.AppendWithFormat(" jsb_class_define_%s(vm);\n", klass->GetName().CString());
  82. }
  83. source += "\n}\n\n";
  84. }
  85. void JSBModule::WriteModulePreInit(String& source)
  86. {
  87. source.AppendWithFormat("\nvoid jsb_preinit_%s (JSVM* vm)\n{\n\njsb_declare_classes(vm);\n", name_.ToLower().CString());
  88. // register enums and constants
  89. source += "// enums and constants\n";
  90. source += "duk_context* ctx = vm->GetJSContext();\n";
  91. source += "duk_get_global_string(ctx, \"Atomic\");\n";
  92. source += "// enums\n";
  93. for (unsigned i = 0; i < enums_.Size(); i++)
  94. {
  95. JSBEnum* jenum = enums_[i];
  96. for (unsigned k = 0; k < jenum->values_.Size(); k++)
  97. {
  98. source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", jenum->values_.At(k).CString());
  99. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", jenum->values_.At(k).CString());
  100. }
  101. }
  102. source += "// constants\n";
  103. for (unsigned i = 0; i < constants_.Size(); i++)
  104. {
  105. source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", constants_.At(i).CString());
  106. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", constants_.At(i).CString());
  107. }
  108. source += "duk_pop(ctx);\n";
  109. source += "// end enums and constants\n";
  110. source += "\n}\n";
  111. }
  112. void JSBModule::WriteModuleInit(String& source)
  113. {
  114. source.AppendWithFormat("\nvoid jsb_init_%s (JSVM* vm)\n{\n\n jsb_init_classes(vm);\n\n}\n\n", name_.ToLower().CString());
  115. }
  116. void JSBModule::WriteIncludes(String& source)
  117. {
  118. Vector<JSBHeader*> allheaders;
  119. for (unsigned i = 0; i < enums_.Size(); i++)
  120. {
  121. allheaders.Push(enums_.At(i)->header_);
  122. }
  123. for (unsigned i = 0; i < classes_.Size(); i++)
  124. {
  125. allheaders.Push(classes_.At(i)->GetHeader());
  126. }
  127. Vector<JSBHeader*> included;
  128. for (unsigned i = 0; i < allheaders.Size(); i++)
  129. {
  130. JSBHeader* header = allheaders.At(i);
  131. if (included.Contains(header))
  132. continue;
  133. String headerPath = GetPath(header->filepath_);
  134. String headerfile = GetFileNameAndExtension(header->filepath_);
  135. #ifdef WIN32
  136. headerPath.Replace(JSBind::ROOT_FOLDER + "/Source/Atomic", "../..");
  137. #else
  138. headerPath.Replace(JSBind::ROOT_FOLDER + "/Source/Atomic", "../..");
  139. #endif
  140. source.AppendWithFormat("#include \"%s%s\"\n", headerPath.CString(), headerfile.CString());
  141. included.Push(header);
  142. }
  143. for (unsigned i = 0; i < includes_.Size(); i++)
  144. {
  145. source.AppendWithFormat("#include \"%s\"\n", includes_[i].CString());
  146. }
  147. }
  148. void JSBModule::EmitSource(const String& filepath)
  149. {
  150. File file(JSBind::context_);
  151. file.Open(filepath, FILE_WRITE);
  152. source_ = "// This file was autogenerated by JSBind, changes will be lost\n";
  153. source_ += "#include \"Precompiled.h\"\n";
  154. source_ += "#include <Duktape/duktape.h>\n";
  155. source_ += "#include \"../../Javascript/JSVM.h\"\n";
  156. source_ += "#include \"../../Javascript/JSAPI.h\"\n";
  157. WriteIncludes(source_);
  158. source_ += "\n\nnamespace Atomic\n{\n \n";
  159. source_ += "// Begin Class Declarations\n";
  160. for (unsigned i = 0; i < classes_.Size(); i++)
  161. {
  162. classes_[i]->WriteForwardDeclarations(source_);
  163. }
  164. source_ += "// End Class Declarations\n\n";
  165. source_ += "// Begin Classes\n";
  166. for (unsigned i = 0; i < classes_.Size(); i++)
  167. {
  168. classes_[i]->Write(source_);
  169. }
  170. source_ += "// End Classes\n\n";
  171. WriteClassDeclaration(source_);
  172. WriteClassDefine(source_);
  173. WriteModulePreInit(source_);
  174. WriteModuleInit(source_);
  175. // end Atomic namespace
  176. source_ += "\n}\n";
  177. file.Write(source_.CString(), source_.Length());
  178. file.Close();
  179. }
  180. void JSBModule::Load(const String &moduleJSONFilename)
  181. {
  182. ResourceCache* cache = JSBind::context_->GetSubsystem<ResourceCache>();
  183. JSONFile* moduleJSONFile = cache->GetResource<JSONFile>(moduleJSONFilename);
  184. if (!moduleJSONFile)
  185. {
  186. LOGERRORF("Couldn't load module json: %s", moduleJSONFilename.CString());
  187. ErrorExit("Couldn't load module json");
  188. }
  189. JSONValue moduleJSON = moduleJSONFile->GetRoot();
  190. JSONValue sources = moduleJSON.GetChild("sources");
  191. JSONValue classes = moduleJSON.GetChild("classes");
  192. JSONValue includes = moduleJSON.GetChild("includes");
  193. JSONValue classes_rename = moduleJSON.GetChild("classes_rename");
  194. JSONValue overloads = moduleJSON.GetChild("overloads");
  195. HashMap<String, String> rename;
  196. if (classes_rename.IsObject())
  197. {
  198. Vector<String> childNames = classes_rename.GetValueNames();
  199. for (unsigned j = 0; j < childNames.Size(); j++)
  200. {
  201. String classname = childNames.At(j);
  202. String crename = classes_rename.GetString(classname);
  203. rename[classname] = crename;
  204. }
  205. }
  206. if (includes.IsArray())
  207. {
  208. for (unsigned j = 0; j < includes.GetSize(); j++)
  209. {
  210. includes_.Push(includes.GetString(j));
  211. }
  212. }
  213. if (classes.IsArray())
  214. {
  215. for (unsigned j = 0; j < classes.GetSize(); j++)
  216. {
  217. String classname = classes.GetString(j);
  218. if (rename.Contains(classname))
  219. bindings_->RegisterClass(classname, rename[classname]);
  220. else
  221. bindings_->RegisterClass(classname);
  222. }
  223. }
  224. if (overloads.IsObject())
  225. {
  226. Vector<String> childNames = overloads.GetChildNames();
  227. for (unsigned j = 0; j < childNames.Size(); j++)
  228. {
  229. String classname = childNames.At(j);
  230. JSBClass* klass = bindings_->GetClass(classname);
  231. if (!klass)
  232. {
  233. ErrorExit("Bad overload klass");
  234. }
  235. JSONValue classoverloads = overloads.GetChild(classname);
  236. Vector<String> functionNames = classoverloads.GetChildNames();
  237. for (unsigned k = 0; k < functionNames.Size(); k++)
  238. {
  239. JSONValue sig = classoverloads.GetChild(functionNames[k]);
  240. if (!sig.IsArray())
  241. {
  242. ErrorExit("Bad overload defintion");
  243. }
  244. Vector<String> values;
  245. for (unsigned x = 0; x < sig.GetSize(); x++)
  246. {
  247. values.Push(sig.GetString(x));
  248. }
  249. JSBFunctionOverride* fo = new JSBFunctionOverride(functionNames[k], values);
  250. klass->AddFunctionOverride(fo);
  251. }
  252. }
  253. }
  254. this->name_ = moduleJSON.GetString("name");
  255. for (unsigned j = 0; j < sources.GetSize(); j++)
  256. {
  257. String sourceFolder = sources.GetString(j);
  258. Vector<String> fileNames;
  259. JSBind::fileSystem_->ScanDir(fileNames, JSBind::ROOT_FOLDER + "/Source/Atomic/" + sourceFolder, "*.h", SCAN_FILES, false);
  260. for (unsigned k = 0; k < fileNames.Size(); k++)
  261. {
  262. // TODO: filter
  263. String filepath = JSBind::ROOT_FOLDER + "/Source/Atomic/" + sourceFolder + "/" + fileNames[k];
  264. this->headerFiles_.Push(filepath);
  265. }
  266. }
  267. }