| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
const GodotRuntime = { |
| 32 |
$GodotRuntime: { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
get_func: function (ptr) { |
| 37 |
return wasmTable.get(ptr); |
| 38 |
}, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
error: function () { |
| 44 |
err.apply(null, Array.from(arguments)); |
| 45 |
}, |
| 46 |
|
| 47 |
print: function () { |
| 48 |
out.apply(null, Array.from(arguments)); |
| 49 |
}, |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
malloc: function (p_size) { |
| 55 |
return _malloc(p_size); |
| 56 |
}, |
| 57 |
|
| 58 |
free: function (p_ptr) { |
| 59 |
_free(p_ptr); |
| 60 |
}, |
| 61 |
|
| 62 |
getHeapValue: function (p_ptr, p_type) { |
| 63 |
return getValue(p_ptr, p_type); |
| 64 |
}, |
| 65 |
|
| 66 |
setHeapValue: function (p_ptr, p_value, p_type) { |
| 67 |
setValue(p_ptr, p_value, p_type); |
| 68 |
}, |
| 69 |
|
| 70 |
heapSub: function (p_heap, p_ptr, p_len) { |
| 71 |
const bytes = p_heap.BYTES_PER_ELEMENT; |
| 72 |
return p_heap.subarray(p_ptr / bytes, p_ptr / bytes + p_len); |
| 73 |
}, |
| 74 |
|
| 75 |
heapSlice: function (p_heap, p_ptr, p_len) { |
| 76 |
const bytes = p_heap.BYTES_PER_ELEMENT; |
| 77 |
return p_heap.slice(p_ptr / bytes, p_ptr / bytes + p_len); |
| 78 |
}, |
| 79 |
|
| 80 |
heapCopy: function (p_dst, p_src, p_ptr) { |
| 81 |
const bytes = p_src.BYTES_PER_ELEMENT; |
| 82 |
return p_dst.set(p_src, p_ptr / bytes); |
| 83 |
}, |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
parseString: function (p_ptr) { |
| 89 |
return UTF8ToString(p_ptr); |
| 90 |
}, |
| 91 |
|
| 92 |
parseStringArray: function (p_ptr, p_size) { |
| 93 |
const strings = []; |
| 94 |
const ptrs = GodotRuntime.heapSub(HEAP32, p_ptr, p_size); |
| 95 |
ptrs.forEach(function (ptr) { |
| 96 |
strings.push(GodotRuntime.parseString(ptr)); |
| 97 |
}); |
| 98 |
return strings; |
| 99 |
}, |
| 100 |
|
| 101 |
strlen: function (p_str) { |
| 102 |
return lengthBytesUTF8(p_str); |
| 103 |
}, |
| 104 |
|
| 105 |
allocString: function (p_str) { |
| 106 |
const length = GodotRuntime.strlen(p_str) + 1; |
| 107 |
const c_str = GodotRuntime.malloc(length); |
| 108 |
stringToUTF8(p_str, c_str, length); |
| 109 |
return c_str; |
| 110 |
}, |
| 111 |
|
| 112 |
allocStringArray: function (p_strings) { |
| 113 |
const size = p_strings.length; |
| 114 |
const c_ptr = GodotRuntime.malloc(size * 4); |
| 115 |
for (let i = 0; i < size; i++) { |
| 116 |
HEAP32[(c_ptr >> 2) + i] = GodotRuntime.allocString(p_strings[i]); |
| 117 |
} |
| 118 |
return c_ptr; |
| 119 |
}, |
| 120 |
|
| 121 |
freeStringArray: function (p_ptr, p_len) { |
| 122 |
for (let i = 0; i < p_len; i++) { |
| 123 |
GodotRuntime.free(HEAP32[(p_ptr >> 2) + i]); |
| 124 |
} |
| 125 |
GodotRuntime.free(p_ptr); |
| 126 |
}, |
| 127 |
|
| 128 |
stringToHeap: function (p_str, p_ptr, p_len) { |
| 129 |
return stringToUTF8Array(p_str, HEAP8, p_ptr, p_len); |
| 130 |
}, |
| 131 |
}, |
| 132 |
}; |
| 133 |
autoAddDeps(GodotRuntime, '$GodotRuntime'); |
| 134 |
mergeInto(LibraryManager.library, GodotRuntime); |