JSAPI.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "Precompiled.h"
  2. #include "../Javascript/JSAPI.h"
  3. #include "../Javascript/JSVM.h"
  4. /*
  5. // script can new an instance
  6. // script can get an engine ptr back (which may be to a previously script new'd instance)
  7. // script should always get the same JS Object back which means a lookup table (strong ref)
  8. // we can look directly at the tval (exposed from duktape internals) to see if the reference count is 1
  9. // script owned
  10. // new'd script side, script holds a RefCounted reference
  11. // native owned
  12. // native script classes not derived from refcounted
  13. // if not inherited from RefCounted can script access?
  14. // if so, script side will always need to delete?
  15. */
  16. namespace Atomic
  17. {
  18. void js_class_get_prototype(duk_context* ctx, const char* classname)
  19. {
  20. duk_get_global_string(ctx, "Atomic");
  21. duk_get_prop_string(ctx, -1, classname);
  22. duk_get_prop_string(ctx, -1, "prototype");
  23. duk_remove(ctx, -2); // remove class object
  24. duk_remove(ctx, -2); // remove Atomic object
  25. }
  26. void js_constructor_basecall(duk_context* ctx, const char* baseclass)
  27. {
  28. int top = duk_get_top(ctx);
  29. duk_get_global_string(ctx, "Atomic");
  30. duk_get_prop_string(ctx, -1, baseclass);
  31. assert(duk_is_function(ctx, -1));
  32. duk_push_this(ctx);
  33. duk_call_method(ctx, 0);
  34. duk_pop_n(ctx, 2);
  35. assert (top == duk_get_top(ctx));
  36. }
  37. void js_class_declare(JSVM* vm, const char* classname, duk_c_function constructor)
  38. {
  39. duk_context* ctx = vm->GetJSContext();
  40. duk_get_global_string(ctx, "Atomic");
  41. duk_push_c_function(ctx, constructor, DUK_VARARGS);
  42. duk_put_prop_string(ctx, -2, classname);
  43. duk_pop(ctx);
  44. }
  45. void js_class_push_propertyobject(JSVM* vm, const char* classname)
  46. {
  47. duk_context* ctx = vm->GetJSContext();
  48. String pname;
  49. pname.AppendWithFormat("__%s__Properties", classname);
  50. duk_get_global_string(ctx, "Atomic");
  51. duk_push_object(ctx);
  52. duk_dup(ctx, -1);
  53. duk_put_prop_string(ctx, -3, pname.CString());
  54. duk_remove(ctx, -2); // remove Atomic object
  55. }
  56. void js_setup_prototype(JSVM* vm, const char* classname, const char* basename, bool hasProperties)
  57. {
  58. duk_context* ctx = vm->GetJSContext();
  59. String pname;
  60. pname.AppendWithFormat("__%s__Properties", classname);
  61. int top = duk_get_top(ctx);
  62. duk_get_global_string(ctx, "Atomic");
  63. duk_get_prop_string(ctx, -1, classname);
  64. assert(duk_is_c_function(ctx, -1));
  65. if (!strlen(basename))
  66. {
  67. // prototype
  68. duk_push_object(ctx);
  69. duk_dup(ctx, -2); // AObject constructor function
  70. duk_put_prop_string(ctx, -2, "constructor");
  71. duk_put_prop_string(ctx, -2, "prototype");
  72. duk_pop_n(ctx, 2);
  73. assert (top == duk_get_top(ctx));
  74. return;
  75. }
  76. // prototype
  77. duk_get_global_string(ctx, "Object");
  78. duk_get_prop_string(ctx, -1, "create");
  79. assert(duk_is_function(ctx, -1));
  80. duk_remove(ctx, -2); // remove Object
  81. duk_get_global_string(ctx, "Atomic");
  82. duk_get_prop_string(ctx, -1, basename);
  83. assert(duk_is_function(ctx, -1));
  84. duk_get_prop_string(ctx, -1, "prototype");
  85. assert(duk_is_object(ctx, -1));
  86. duk_remove(ctx, -2); // remove basename
  87. int numargs = 1;
  88. if (hasProperties)
  89. {
  90. duk_get_prop_string(ctx, -2, pname.CString());
  91. assert(duk_is_object(ctx, -1));
  92. duk_remove(ctx, -3); // remove Atomic
  93. numargs++;
  94. }
  95. else
  96. duk_remove(ctx, -2); // remove Atomic
  97. duk_call(ctx, numargs);
  98. assert(duk_is_object(ctx, -1));
  99. duk_dup(ctx, -2);
  100. duk_put_prop_string(ctx, -2, "constructor");
  101. //duk_dup(ctx, -1);
  102. duk_put_prop_string(ctx, -2, "prototype");
  103. // pop the classname object
  104. duk_pop(ctx);
  105. // pop the Atomic Object
  106. duk_pop(ctx);
  107. assert (top == duk_get_top(ctx));
  108. }
  109. }