JSClassWriter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/FileSystem.h>
  23. #include "../JSBind.h"
  24. #include "../JSBModule.h"
  25. #include "../JSBPackage.h"
  26. #include "../JSBEnum.h"
  27. #include "../JSBClass.h"
  28. #include "../JSBFunction.h"
  29. #include "JSClassWriter.h"
  30. #include "JSFunctionWriter.h"
  31. namespace ToolCore
  32. {
  33. JSClassWriter::JSClassWriter(JSBClass *klass) : JSBClassWriter(klass)
  34. {
  35. }
  36. void JSClassWriter::WriteFunctions(String& source)
  37. {
  38. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  39. {
  40. JSBFunction* function = klass_->functions_.At(i);
  41. if (function->Skip())
  42. continue;
  43. if (function->IsDestructor())
  44. continue;
  45. JSFunctionWriter writer(function);
  46. writer.GenerateSource(source);
  47. }
  48. }
  49. void JSClassWriter::GenerateSource(String& sourceOut)
  50. {
  51. String source = "";
  52. if (klass_->IsNumberArray())
  53. return;
  54. WriteFunctions(source);
  55. String packageName = klass_->GetModule()->GetPackage()->GetName();
  56. source.AppendWithFormat("static void jsb_class_define_%s(JSVM* vm)\n{\n", klass_->GetName().CString());
  57. source.Append("duk_context* ctx = vm->GetJSContext();\n");
  58. GenerateStaticFunctionsSource(source, packageName);
  59. GenerateNonStaticFunctionsSource(source, packageName);
  60. source.Append("}\n");
  61. sourceOut += source;
  62. }
  63. void JSClassWriter::GenerateStaticFunctionsSource(String& source, String& packageName)
  64. {
  65. // Store function on both the constructor and prototype
  66. // so can access static functions from both the class constructor and an instance
  67. source.AppendWithFormat("js_class_get_constructor(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());
  68. source.AppendWithFormat("js_class_get_prototype(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());
  69. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  70. {
  71. JSBFunction* function = klass_->functions_.At(i);
  72. if (function->Skip())
  73. continue;
  74. if (function->IsConstructor() || function->IsDestructor())
  75. continue;
  76. if (!function->IsStatic())
  77. continue;
  78. if (function->FirstDefaultParameter() != -1)
  79. {
  80. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, DUK_VARARGS);\n", klass_->GetName().CString(), function->GetName().CString());
  81. }
  82. else
  83. {
  84. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, %i);\n", klass_->GetName().CString(), function->GetName().CString(), (int)function->GetParameters().Size());
  85. }
  86. String scriptName = function->GetName();
  87. scriptName[0] = tolower(scriptName[0]);
  88. source.Append("duk_dup(ctx, -1);\n");
  89. source.AppendWithFormat("duk_put_prop_string(ctx, -3, \"%s\");\n", scriptName.CString());
  90. source.AppendWithFormat("duk_put_prop_string(ctx, -3, \"%s\");\n", scriptName.CString());
  91. }
  92. source.Append("duk_pop_2(ctx);\n");
  93. }
  94. void JSClassWriter::GenerateNonStaticFunctionsSource(String& source, String& packageName)
  95. {
  96. source.AppendWithFormat("js_class_get_prototype(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());
  97. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  98. {
  99. JSBFunction* function = klass_->functions_.At(i);
  100. if (function->Skip())
  101. continue;
  102. if (function->IsConstructor() || function->IsDestructor())
  103. continue;
  104. if (function->IsStatic())
  105. continue;
  106. if (function->FirstDefaultParameter() != -1)
  107. {
  108. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, DUK_VARARGS);\n", klass_->GetName().CString(), function->GetName().CString());
  109. }
  110. else
  111. {
  112. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, %i);\n", klass_->GetName().CString(), function->GetName().CString(), (int)function->GetParameters().Size());
  113. }
  114. String scriptName = function->GetName();
  115. scriptName[0] = tolower(scriptName[0]);
  116. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", scriptName.CString());
  117. }
  118. source.Append("duk_pop(ctx);\n");
  119. }
  120. }