library_godot_runtime.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* library_godot_runtime.js */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. const GodotRuntime = {
  31. $GodotRuntime: {
  32. /*
  33. * Functions
  34. */
  35. get_func: function (ptr) {
  36. return wasmTable.get(ptr); // eslint-disable-line no-undef
  37. },
  38. /*
  39. * Prints
  40. */
  41. error: function () {
  42. err.apply(null, Array.from(arguments)); // eslint-disable-line no-undef
  43. },
  44. print: function () {
  45. out.apply(null, Array.from(arguments)); // eslint-disable-line no-undef
  46. },
  47. /*
  48. * Memory
  49. */
  50. malloc: function (p_size) {
  51. return _malloc(p_size); // eslint-disable-line no-undef
  52. },
  53. free: function (p_ptr) {
  54. _free(p_ptr); // eslint-disable-line no-undef
  55. },
  56. getHeapValue: function (p_ptr, p_type) {
  57. return getValue(p_ptr, p_type); // eslint-disable-line no-undef
  58. },
  59. setHeapValue: function (p_ptr, p_value, p_type) {
  60. setValue(p_ptr, p_value, p_type); // eslint-disable-line no-undef
  61. },
  62. heapSub: function (p_heap, p_ptr, p_len) {
  63. const bytes = p_heap.BYTES_PER_ELEMENT;
  64. return p_heap.subarray(p_ptr / bytes, p_ptr / bytes + p_len);
  65. },
  66. heapSlice: function (p_heap, p_ptr, p_len) {
  67. const bytes = p_heap.BYTES_PER_ELEMENT;
  68. return p_heap.slice(p_ptr / bytes, p_ptr / bytes + p_len);
  69. },
  70. heapCopy: function (p_dst, p_src, p_ptr) {
  71. const bytes = p_src.BYTES_PER_ELEMENT;
  72. return p_dst.set(p_src, p_ptr / bytes);
  73. },
  74. /*
  75. * Strings
  76. */
  77. parseString: function (p_ptr) {
  78. return UTF8ToString(p_ptr); // eslint-disable-line no-undef
  79. },
  80. parseStringArray: function (p_ptr, p_size) {
  81. const strings = [];
  82. const ptrs = GodotRuntime.heapSub(HEAP32, p_ptr, p_size); // TODO wasm64
  83. ptrs.forEach(function (ptr) {
  84. strings.push(GodotRuntime.parseString(ptr));
  85. });
  86. return strings;
  87. },
  88. strlen: function (p_str) {
  89. return lengthBytesUTF8(p_str); // eslint-disable-line no-undef
  90. },
  91. allocString: function (p_str) {
  92. const length = GodotRuntime.strlen(p_str) + 1;
  93. const c_str = GodotRuntime.malloc(length);
  94. stringToUTF8(p_str, c_str, length); // eslint-disable-line no-undef
  95. return c_str;
  96. },
  97. allocStringArray: function (p_strings) {
  98. const size = p_strings.length;
  99. const c_ptr = GodotRuntime.malloc(size * 4);
  100. for (let i = 0; i < size; i++) {
  101. HEAP32[(c_ptr >> 2) + i] = GodotRuntime.allocString(p_strings[i]);
  102. }
  103. return c_ptr;
  104. },
  105. freeStringArray: function (p_ptr, p_len) {
  106. for (let i = 0; i < p_len; i++) {
  107. GodotRuntime.free(HEAP32[(p_ptr >> 2) + i]);
  108. }
  109. GodotRuntime.free(p_ptr);
  110. },
  111. stringToHeap: function (p_str, p_ptr, p_len) {
  112. return stringToUTF8Array(p_str, HEAP8, p_ptr, p_len); // eslint-disable-line no-undef
  113. },
  114. },
  115. };
  116. autoAddDeps(GodotRuntime, '$GodotRuntime');
  117. mergeInto(LibraryManager.library, GodotRuntime);