Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/web/js/libs/library_godot_runtime.js
10279 views
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
31
const GodotRuntime = {
32
$GodotRuntime: {
33
/*
34
* Functions
35
*/
36
get_func: function (ptr) {
37
return wasmTable.get(ptr);
38
},
39
40
/*
41
* Prints
42
*/
43
error: function () {
44
err.apply(null, Array.from(arguments)); // eslint-disable-line no-undef
45
},
46
47
print: function () {
48
out.apply(null, Array.from(arguments)); // eslint-disable-line no-undef
49
},
50
51
/*
52
* Memory
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
* Strings
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); // TODO wasm64
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);
135
136