JSAPI.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "JSAPI.h"
  5. #include "JSVM.h"
  6. /*
  7. // script can new an instance
  8. // script can get an engine ptr back (which may be to a previously script new'd instance)
  9. // script should always get the same JS Object back which means a lookup table (strong ref)
  10. // we can look directly at the tval (exposed from duktape internals) to see if the reference count is 1
  11. // script owned
  12. // new'd script side, script holds a RefCounted reference
  13. // native owned
  14. // native script classes not derived from refcounted
  15. // if not inherited from RefCounted can script access?
  16. // if so, script side will always need to delete?
  17. */
  18. namespace Atomic
  19. {
  20. void js_class_get_prototype(duk_context* ctx, const char* package, const char *classname)
  21. {
  22. duk_get_global_string(ctx, package);
  23. duk_get_prop_string(ctx, -1, classname);
  24. duk_get_prop_string(ctx, -1, "prototype");
  25. duk_remove(ctx, -2); // remove class object
  26. duk_remove(ctx, -2); // remove Atomic object
  27. }
  28. void js_constructor_basecall(duk_context* ctx, const char* package, const char* baseclass)
  29. {
  30. int top = duk_get_top(ctx);
  31. duk_get_global_string(ctx, package);
  32. duk_get_prop_string(ctx, -1, baseclass);
  33. assert(duk_is_function(ctx, -1));
  34. duk_push_this(ctx);
  35. duk_call_method(ctx, 0);
  36. duk_pop_n(ctx, 2);
  37. assert (top == duk_get_top(ctx));
  38. }
  39. void js_class_declare_internal(JSVM* vm, void* uniqueClassID, const char* package, const char* classname, duk_c_function constructor)
  40. {
  41. duk_context* ctx = vm->GetJSContext();
  42. // stash a lookup from the uniqueID to the package name
  43. // (NULL) == non-object, so core "Atomic" package
  44. if (uniqueClassID)
  45. {
  46. duk_push_heap_stash(ctx);
  47. duk_push_pointer(ctx, uniqueClassID);
  48. duk_push_string(ctx, package);
  49. duk_put_prop(ctx, -3);
  50. duk_pop(ctx);
  51. }
  52. else
  53. {
  54. assert(String("Atomic") == package );
  55. }
  56. duk_get_global_string(ctx, package);
  57. duk_push_c_function(ctx, constructor, DUK_VARARGS);
  58. duk_put_prop_string(ctx, -2, classname);
  59. duk_pop(ctx);
  60. }
  61. void js_class_push_propertyobject(JSVM* vm, const char* package, const char* classname)
  62. {
  63. duk_context* ctx = vm->GetJSContext();
  64. String pname;
  65. pname.AppendWithFormat("__%s__Properties", classname);
  66. duk_get_global_string(ctx, package);
  67. duk_push_object(ctx);
  68. duk_dup(ctx, -1);
  69. duk_put_prop_string(ctx, -3, pname.CString());
  70. duk_remove(ctx, -2); // remove Atomic object
  71. }
  72. void js_setup_prototype(JSVM* vm, const char* package, const char* classname, const char* basePackage, const char* basename, bool hasProperties)
  73. {
  74. duk_context* ctx = vm->GetJSContext();
  75. String pname;
  76. pname.AppendWithFormat("__%s__Properties", classname);
  77. int top = duk_get_top(ctx);
  78. duk_get_global_string(ctx,package);
  79. duk_get_prop_string(ctx, -1, classname);
  80. assert(duk_is_c_function(ctx, -1));
  81. if (!strlen(basename))
  82. {
  83. // prototype
  84. duk_push_object(ctx);
  85. duk_dup(ctx, -2); // AObject constructor function
  86. duk_put_prop_string(ctx, -2, "constructor");
  87. duk_put_prop_string(ctx, -2, "prototype");
  88. duk_pop_n(ctx, 2);
  89. assert (top == duk_get_top(ctx));
  90. return;
  91. }
  92. // prototype
  93. duk_get_global_string(ctx, "Object");
  94. duk_get_prop_string(ctx, -1, "create");
  95. assert(duk_is_function(ctx, -1));
  96. duk_remove(ctx, -2); // remove Object
  97. duk_get_global_string(ctx, basePackage);
  98. duk_get_prop_string(ctx, -1, basename);
  99. assert(duk_is_function(ctx, -1));
  100. duk_get_prop_string(ctx, -1, "prototype");
  101. assert(duk_is_object(ctx, -1));
  102. duk_remove(ctx, -2); // remove basename
  103. int numargs = 1;
  104. if (hasProperties)
  105. {
  106. duk_get_global_string(ctx, package);
  107. duk_get_prop_string(ctx, -1, pname.CString());
  108. assert(duk_is_object(ctx, -1));
  109. duk_remove(ctx, -2);
  110. duk_remove(ctx, -3); // remove package
  111. numargs++;
  112. }
  113. else
  114. duk_remove(ctx, -2); // remove package
  115. duk_call(ctx, numargs);
  116. assert(duk_is_object(ctx, -1));
  117. duk_dup(ctx, -2);
  118. duk_put_prop_string(ctx, -2, "constructor");
  119. //duk_dup(ctx, -1);
  120. duk_put_prop_string(ctx, -2, "prototype");
  121. // pop the classname object
  122. duk_pop(ctx);
  123. // pop the Atomic Object
  124. duk_pop(ctx);
  125. assert (top == duk_get_top(ctx));
  126. }
  127. void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
  128. {
  129. v.Clear();
  130. duk_enum(ctx, objIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
  131. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  132. /* [ ... enum key ] */
  133. const char* key = duk_to_string(ctx, -2);
  134. if (duk_is_number(ctx, -1)) {
  135. v[key] = (float) duk_to_number(ctx, -1);
  136. } else if (duk_is_boolean(ctx, -1)) {
  137. v[key] = duk_to_boolean(ctx, -1) ? true : false;
  138. }
  139. else if (duk_is_string(ctx, -1)) {
  140. v[key] = duk_to_string(ctx, -1);
  141. } else if (duk_get_heapptr(ctx, -1)) {
  142. v[key] = js_to_class_instance<Object>(ctx, -1, 0);
  143. }
  144. duk_pop_2(ctx); /* pop_key & value*/
  145. }
  146. duk_pop(ctx); /* pop enum object */
  147. }
  148. void js_push_variant(duk_context *ctx, const Variant& v)
  149. {
  150. VariantType type = v.GetType();
  151. RefCounted* ref;
  152. Object* object;
  153. Vector2& vector2 = (Vector2&) Vector2::ZERO;
  154. Vector3& vector3 = (Vector3&) Vector3::ZERO;
  155. void* uniqueClassID = NULL;
  156. const char* package = NULL;
  157. switch (type)
  158. {
  159. case VAR_VOIDPTR:
  160. duk_push_null(ctx);
  161. break;
  162. case VAR_PTR:
  163. ref = v.GetPtr();
  164. if (!ref || !ref->IsObject())
  165. {
  166. duk_push_null(ctx);
  167. break;
  168. }
  169. object = (Object*) ref;
  170. // check that class is supported
  171. uniqueClassID = (void *) object->GetTypeName().CString();
  172. duk_push_heap_stash(ctx);
  173. duk_push_pointer(ctx, uniqueClassID);
  174. duk_get_prop(ctx, -2);
  175. package = duk_to_string(ctx, -1);
  176. duk_pop_2(ctx);
  177. // check that class is supported
  178. duk_get_global_string(ctx, package);
  179. // will not handle renamed classes!
  180. duk_get_prop_string(ctx, -1, object->GetTypeName().CString());
  181. if (!duk_is_function(ctx, -1))
  182. object = NULL;
  183. duk_pop_n(ctx, 2);
  184. if (object)
  185. js_push_class_object_instance(ctx, object);
  186. else
  187. duk_push_undefined(ctx);
  188. break;
  189. case VAR_BOOL:
  190. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  191. break;
  192. case VAR_INT:
  193. duk_push_number(ctx, v.GetInt());
  194. break;
  195. case VAR_FLOAT:
  196. duk_push_number(ctx, v.GetFloat());
  197. break;
  198. case VAR_STRING:
  199. duk_push_string(ctx, v.GetString().CString());
  200. break;
  201. case VAR_VECTOR2:
  202. vector2 = v.GetVector2();
  203. duk_push_array(ctx);
  204. duk_push_number(ctx, vector2.x_);
  205. duk_put_prop_index(ctx, -1, 0);
  206. duk_push_number(ctx, vector2.y_);
  207. duk_put_prop_index(ctx, -1, 1);
  208. break;
  209. case VAR_VECTOR3:
  210. vector3 = v.GetVector3();
  211. duk_push_array(ctx);
  212. duk_push_number(ctx, vector3.x_);
  213. duk_put_prop_index(ctx, -1, 0);
  214. duk_push_number(ctx, vector3.y_);
  215. duk_put_prop_index(ctx, -1, 1);
  216. duk_push_number(ctx, vector3.z_);
  217. duk_put_prop_index(ctx, -1, 1);
  218. break;
  219. default:
  220. duk_push_undefined(ctx);
  221. break;
  222. }
  223. }
  224. }