JSAPI.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_to_variant(duk_context* ctx, int variantIdx, Variant &v)
  149. {
  150. v.Clear();
  151. if (duk_is_boolean(ctx, variantIdx))
  152. {
  153. v = duk_to_boolean(ctx, variantIdx) ? true : false;
  154. return;
  155. }
  156. if (duk_is_string(ctx, variantIdx))
  157. {
  158. v = duk_to_string(ctx, variantIdx);
  159. return;
  160. }
  161. if (duk_is_number(ctx, variantIdx))
  162. {
  163. v = (float) duk_to_number(ctx, variantIdx);
  164. return;
  165. }
  166. if (duk_is_pointer(ctx, variantIdx))
  167. {
  168. v = (RefCounted*) duk_get_pointer(ctx, variantIdx);
  169. return;
  170. }
  171. }
  172. void js_push_variant(duk_context *ctx, const Variant& v)
  173. {
  174. VariantType type = v.GetType();
  175. RefCounted* ref;
  176. Object* object;
  177. Vector2& vector2 = (Vector2&) Vector2::ZERO;
  178. Vector3& vector3 = (Vector3&) Vector3::ZERO;
  179. Vector4& vector4 = (Vector4&) Vector3::ZERO;
  180. void* uniqueClassID = NULL;
  181. const char* package = NULL;
  182. switch (type)
  183. {
  184. case VAR_VOIDPTR:
  185. duk_push_null(ctx);
  186. break;
  187. case VAR_PTR:
  188. ref = v.GetPtr();
  189. if (!ref || !ref->IsObject())
  190. {
  191. duk_push_null(ctx);
  192. break;
  193. }
  194. object = (Object*) ref;
  195. // check that class is supported
  196. uniqueClassID = (void *) object->GetTypeName().CString();
  197. duk_push_heap_stash(ctx);
  198. duk_push_pointer(ctx, uniqueClassID);
  199. duk_get_prop(ctx, -2);
  200. package = duk_to_string(ctx, -1);
  201. duk_pop_2(ctx);
  202. // check that class is supported
  203. duk_get_global_string(ctx, package);
  204. // will not handle renamed classes!
  205. duk_get_prop_string(ctx, -1, object->GetTypeName().CString());
  206. if (!duk_is_function(ctx, -1))
  207. object = NULL;
  208. duk_pop_n(ctx, 2);
  209. if (object)
  210. js_push_class_object_instance(ctx, object);
  211. else
  212. duk_push_undefined(ctx);
  213. break;
  214. case VAR_BOOL:
  215. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  216. break;
  217. case VAR_INT:
  218. duk_push_number(ctx, v.GetInt());
  219. break;
  220. case VAR_FLOAT:
  221. duk_push_number(ctx, v.GetFloat());
  222. break;
  223. case VAR_STRING:
  224. duk_push_string(ctx, v.GetString().CString());
  225. break;
  226. case VAR_VECTOR2:
  227. vector2 = v.GetVector2();
  228. duk_push_array(ctx);
  229. duk_push_number(ctx, vector2.x_);
  230. duk_put_prop_index(ctx, -2, 0);
  231. duk_push_number(ctx, vector2.y_);
  232. duk_put_prop_index(ctx, -2, 1);
  233. break;
  234. case VAR_VECTOR3:
  235. vector3 = v.GetVector3();
  236. duk_push_array(ctx);
  237. duk_push_number(ctx, vector3.x_);
  238. duk_put_prop_index(ctx, -2, 0);
  239. duk_push_number(ctx, vector3.y_);
  240. duk_put_prop_index(ctx, -2, 1);
  241. duk_push_number(ctx, vector3.z_);
  242. duk_put_prop_index(ctx, -2, 2);
  243. break;
  244. case VAR_VECTOR4:
  245. vector4 = v.GetVector4();
  246. duk_push_array(ctx);
  247. duk_push_number(ctx, vector4.x_);
  248. duk_put_prop_index(ctx, -2, 0);
  249. duk_push_number(ctx, vector4.y_);
  250. duk_put_prop_index(ctx, -2, 1);
  251. duk_push_number(ctx, vector4.z_);
  252. duk_put_prop_index(ctx, -2, 2);
  253. duk_push_number(ctx, vector4.w_);
  254. duk_put_prop_index(ctx, -2, 3);
  255. break;
  256. default:
  257. duk_push_undefined(ctx);
  258. break;
  259. }
  260. }
  261. }