JSAPI.cpp 8.9 KB

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