Path: blob/master/platform/web/js/libs/library_godot_runtime.js
10279 views
/**************************************************************************/1/* library_godot_runtime.js */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930const GodotRuntime = {31$GodotRuntime: {32/*33* Functions34*/35get_func: function (ptr) {36return wasmTable.get(ptr);37},3839/*40* Prints41*/42error: function () {43err.apply(null, Array.from(arguments)); // eslint-disable-line no-undef44},4546print: function () {47out.apply(null, Array.from(arguments)); // eslint-disable-line no-undef48},4950/*51* Memory52*/53malloc: function (p_size) {54return _malloc(p_size);55},5657free: function (p_ptr) {58_free(p_ptr);59},6061getHeapValue: function (p_ptr, p_type) {62return getValue(p_ptr, p_type);63},6465setHeapValue: function (p_ptr, p_value, p_type) {66setValue(p_ptr, p_value, p_type);67},6869heapSub: function (p_heap, p_ptr, p_len) {70const bytes = p_heap.BYTES_PER_ELEMENT;71return p_heap.subarray(p_ptr / bytes, p_ptr / bytes + p_len);72},7374heapSlice: function (p_heap, p_ptr, p_len) {75const bytes = p_heap.BYTES_PER_ELEMENT;76return p_heap.slice(p_ptr / bytes, p_ptr / bytes + p_len);77},7879heapCopy: function (p_dst, p_src, p_ptr) {80const bytes = p_src.BYTES_PER_ELEMENT;81return p_dst.set(p_src, p_ptr / bytes);82},8384/*85* Strings86*/87parseString: function (p_ptr) {88return UTF8ToString(p_ptr);89},9091parseStringArray: function (p_ptr, p_size) {92const strings = [];93const ptrs = GodotRuntime.heapSub(HEAP32, p_ptr, p_size); // TODO wasm6494ptrs.forEach(function (ptr) {95strings.push(GodotRuntime.parseString(ptr));96});97return strings;98},99100strlen: function (p_str) {101return lengthBytesUTF8(p_str);102},103104allocString: function (p_str) {105const length = GodotRuntime.strlen(p_str) + 1;106const c_str = GodotRuntime.malloc(length);107stringToUTF8(p_str, c_str, length);108return c_str;109},110111allocStringArray: function (p_strings) {112const size = p_strings.length;113const c_ptr = GodotRuntime.malloc(size * 4);114for (let i = 0; i < size; i++) {115HEAP32[(c_ptr >> 2) + i] = GodotRuntime.allocString(p_strings[i]);116}117return c_ptr;118},119120freeStringArray: function (p_ptr, p_len) {121for (let i = 0; i < p_len; i++) {122GodotRuntime.free(HEAP32[(p_ptr >> 2) + i]);123}124GodotRuntime.free(p_ptr);125},126127stringToHeap: function (p_str, p_ptr, p_len) {128return stringToUTF8Array(p_str, HEAP8, p_ptr, p_len);129},130},131};132autoAddDeps(GodotRuntime, '$GodotRuntime');133mergeInto(LibraryManager.library, GodotRuntime);134135136