JSAPI.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_class_get_constructor(duk_context* ctx, const char* package, const char *classname)
  29. {
  30. duk_get_global_string(ctx, package);
  31. duk_get_prop_string(ctx, -1, classname);
  32. duk_remove(ctx, -2); // remove package
  33. }
  34. void js_constructor_basecall(duk_context* ctx, const char* package, const char* baseclass)
  35. {
  36. int top = duk_get_top(ctx);
  37. duk_get_global_string(ctx, package);
  38. duk_get_prop_string(ctx, -1, baseclass);
  39. assert(duk_is_function(ctx, -1));
  40. duk_push_this(ctx);
  41. duk_call_method(ctx, 0);
  42. duk_pop_n(ctx, 2);
  43. assert (top == duk_get_top(ctx));
  44. }
  45. void js_class_declare_internal(JSVM* vm, void* uniqueClassID, const char* package, const char* classname, duk_c_function constructor)
  46. {
  47. duk_context* ctx = vm->GetJSContext();
  48. // stash a lookup from the uniqueID to the package name
  49. // (NULL) == non-object, so core "Atomic" package
  50. if (uniqueClassID)
  51. {
  52. duk_push_heap_stash(ctx);
  53. duk_push_pointer(ctx, uniqueClassID);
  54. duk_push_string(ctx, package);
  55. duk_put_prop(ctx, -3);
  56. duk_pop(ctx);
  57. }
  58. else
  59. {
  60. assert(String("Atomic") == package );
  61. }
  62. duk_get_global_string(ctx, package);
  63. duk_push_c_function(ctx, constructor, DUK_VARARGS);
  64. duk_put_prop_string(ctx, -2, classname);
  65. duk_pop(ctx);
  66. }
  67. void js_class_push_propertyobject(JSVM* vm, const char* package, const char* classname)
  68. {
  69. duk_context* ctx = vm->GetJSContext();
  70. String pname;
  71. pname.AppendWithFormat("__%s__Properties", classname);
  72. duk_get_global_string(ctx, package);
  73. duk_push_object(ctx);
  74. duk_dup(ctx, -1);
  75. duk_put_prop_string(ctx, -3, pname.CString());
  76. duk_remove(ctx, -2); // remove Atomic object
  77. }
  78. void js_setup_prototype(JSVM* vm, const char* package, const char* classname, const char* basePackage, const char* basename, bool hasProperties)
  79. {
  80. duk_context* ctx = vm->GetJSContext();
  81. String pname;
  82. pname.AppendWithFormat("__%s__Properties", classname);
  83. int top = duk_get_top(ctx);
  84. duk_get_global_string(ctx,package);
  85. duk_get_prop_string(ctx, -1, classname);
  86. assert(duk_is_c_function(ctx, -1));
  87. if (!strlen(basename))
  88. {
  89. // prototype
  90. duk_push_object(ctx);
  91. duk_dup(ctx, -2); // AObject constructor function
  92. duk_put_prop_string(ctx, -2, "constructor");
  93. duk_put_prop_string(ctx, -2, "prototype");
  94. duk_pop_n(ctx, 2);
  95. assert (top == duk_get_top(ctx));
  96. return;
  97. }
  98. // prototype
  99. duk_get_global_string(ctx, "Object");
  100. duk_get_prop_string(ctx, -1, "create");
  101. assert(duk_is_function(ctx, -1));
  102. duk_remove(ctx, -2); // remove Object
  103. duk_get_global_string(ctx, basePackage);
  104. duk_get_prop_string(ctx, -1, basename);
  105. assert(duk_is_function(ctx, -1));
  106. duk_get_prop_string(ctx, -1, "prototype");
  107. assert(duk_is_object(ctx, -1));
  108. duk_remove(ctx, -2); // remove basename
  109. int numargs = 1;
  110. if (hasProperties)
  111. {
  112. duk_get_global_string(ctx, package);
  113. duk_get_prop_string(ctx, -1, pname.CString());
  114. assert(duk_is_object(ctx, -1));
  115. duk_remove(ctx, -2);
  116. duk_remove(ctx, -3); // remove package
  117. numargs++;
  118. }
  119. else
  120. duk_remove(ctx, -2); // remove package
  121. duk_call(ctx, numargs);
  122. assert(duk_is_object(ctx, -1));
  123. duk_dup(ctx, -2);
  124. duk_put_prop_string(ctx, -2, "constructor");
  125. //duk_dup(ctx, -1);
  126. duk_put_prop_string(ctx, -2, "prototype");
  127. // pop the classname object
  128. duk_pop(ctx);
  129. // pop the Atomic Object
  130. duk_pop(ctx);
  131. assert (top == duk_get_top(ctx));
  132. }
  133. void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
  134. {
  135. v.Clear();
  136. duk_enum(ctx, objIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
  137. while (duk_next(ctx, -1 /*enum_index*/, 1 /*get_value*/)) {
  138. /* [ ... enum key ] */
  139. const char* key = duk_to_string(ctx, -2);
  140. if (duk_is_number(ctx, -1)) {
  141. v[key] = (float) duk_to_number(ctx, -1);
  142. } else if (duk_is_boolean(ctx, -1)) {
  143. v[key] = duk_to_boolean(ctx, -1) ? true : false;
  144. }
  145. else if (duk_is_string(ctx, -1)) {
  146. v[key] = duk_to_string(ctx, -1);
  147. } else if (duk_get_heapptr(ctx, -1)) {
  148. v[key] = js_to_class_instance<Object>(ctx, -1, 0);
  149. }
  150. duk_pop_2(ctx); /* pop_key & value*/
  151. }
  152. duk_pop(ctx); /* pop enum object */
  153. }
  154. void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
  155. {
  156. v.Clear();
  157. if (duk_is_boolean(ctx, variantIdx))
  158. {
  159. v = duk_to_boolean(ctx, variantIdx) ? true : false;
  160. return;
  161. }
  162. if (duk_is_string(ctx, variantIdx))
  163. {
  164. v = duk_to_string(ctx, variantIdx);
  165. return;
  166. }
  167. if (duk_is_number(ctx, variantIdx))
  168. {
  169. v = (float) duk_to_number(ctx, variantIdx);
  170. return;
  171. }
  172. if (duk_is_pointer(ctx, variantIdx))
  173. {
  174. v = (RefCounted*) duk_get_pointer(ctx, variantIdx);
  175. return;
  176. }
  177. }
  178. void js_push_variant(duk_context *ctx, const Variant& v)
  179. {
  180. VariantType type = v.GetType();
  181. RefCounted* ref;
  182. Object* object;
  183. Vector2& vector2 = (Vector2&) Vector2::ZERO;
  184. Vector3& vector3 = (Vector3&) Vector3::ZERO;
  185. Vector4& vector4 = (Vector4&) Vector3::ZERO;
  186. void* uniqueClassID = NULL;
  187. const char* package = NULL;
  188. switch (type)
  189. {
  190. case VAR_VOIDPTR:
  191. duk_push_null(ctx);
  192. break;
  193. case VAR_PTR:
  194. ref = v.GetPtr();
  195. if (!ref || !ref->IsObject())
  196. {
  197. duk_push_null(ctx);
  198. break;
  199. }
  200. object = (Object*) ref;
  201. // check that class is supported
  202. uniqueClassID = (void *) object->GetTypeName().CString();
  203. duk_push_heap_stash(ctx);
  204. duk_push_pointer(ctx, uniqueClassID);
  205. duk_get_prop(ctx, -2);
  206. package = duk_to_string(ctx, -1);
  207. duk_pop_2(ctx);
  208. // check that class is supported
  209. duk_get_global_string(ctx, package);
  210. // will not handle renamed classes!
  211. duk_get_prop_string(ctx, -1, object->GetTypeName().CString());
  212. if (!duk_is_function(ctx, -1))
  213. object = NULL;
  214. duk_pop_n(ctx, 2);
  215. if (object)
  216. js_push_class_object_instance(ctx, object);
  217. else
  218. duk_push_undefined(ctx);
  219. break;
  220. case VAR_BOOL:
  221. duk_push_boolean(ctx, v.GetBool() ? 1 : 0);
  222. break;
  223. case VAR_INT:
  224. duk_push_number(ctx, v.GetInt());
  225. break;
  226. case VAR_FLOAT:
  227. duk_push_number(ctx, v.GetFloat());
  228. break;
  229. case VAR_STRING:
  230. duk_push_string(ctx, v.GetString().CString());
  231. break;
  232. case VAR_VECTOR2:
  233. vector2 = v.GetVector2();
  234. duk_push_array(ctx);
  235. duk_push_number(ctx, vector2.x_);
  236. duk_put_prop_index(ctx, -2, 0);
  237. duk_push_number(ctx, vector2.y_);
  238. duk_put_prop_index(ctx, -2, 1);
  239. break;
  240. case VAR_VECTOR3:
  241. vector3 = v.GetVector3();
  242. duk_push_array(ctx);
  243. duk_push_number(ctx, vector3.x_);
  244. duk_put_prop_index(ctx, -2, 0);
  245. duk_push_number(ctx, vector3.y_);
  246. duk_put_prop_index(ctx, -2, 1);
  247. duk_push_number(ctx, vector3.z_);
  248. duk_put_prop_index(ctx, -2, 2);
  249. break;
  250. case VAR_VECTOR4:
  251. vector4 = v.GetVector4();
  252. duk_push_array(ctx);
  253. duk_push_number(ctx, vector4.x_);
  254. duk_put_prop_index(ctx, -2, 0);
  255. duk_push_number(ctx, vector4.y_);
  256. duk_put_prop_index(ctx, -2, 1);
  257. duk_push_number(ctx, vector4.z_);
  258. duk_put_prop_index(ctx, -2, 2);
  259. duk_push_number(ctx, vector4.w_);
  260. duk_put_prop_index(ctx, -2, 3);
  261. break;
  262. default:
  263. duk_push_undefined(ctx);
  264. break;
  265. }
  266. }
  267. }